mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-09 21:45:54 +00:00
example/gatt_battery_query: register for notification updates
This commit is contained in:
parent
4ce43359e6
commit
26687d1449
@ -89,11 +89,14 @@ static hci_con_handle_t connection_handle;
|
|||||||
static uint16_t battery_service_uuid = 0x180F;
|
static uint16_t battery_service_uuid = 0x180F;
|
||||||
static uint16_t battery_level_characteristic_uuid = 0x2a19;
|
static uint16_t battery_level_characteristic_uuid = 0x2a19;
|
||||||
static gatt_client_service_t battery_service;
|
static gatt_client_service_t battery_service;
|
||||||
static gatt_client_characteristic_t config_characteristic;
|
static gatt_client_characteristic_t battery_level_characteristic;
|
||||||
|
|
||||||
static gc_state_t state = TC_IDLE;
|
static gc_state_t state = TC_IDLE;
|
||||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||||
|
|
||||||
|
static gatt_client_notification_t notification_listener;
|
||||||
|
static int listener_registered;
|
||||||
|
|
||||||
static void printUUID(uint8_t * uuid128, uint16_t uuid16){
|
static void printUUID(uint8_t * uuid128, uint16_t uuid16){
|
||||||
if (uuid16){
|
if (uuid16){
|
||||||
printf("%04x",uuid16);
|
printf("%04x",uuid16);
|
||||||
@ -181,8 +184,8 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
|||||||
case TC_W4_CHARACTERISTIC_RESULT:
|
case TC_W4_CHARACTERISTIC_RESULT:
|
||||||
switch(hci_event_packet_get_type(packet)){
|
switch(hci_event_packet_get_type(packet)){
|
||||||
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
||||||
gatt_event_characteristic_query_result_get_characteristic(packet, &config_characteristic);
|
gatt_event_characteristic_query_result_get_characteristic(packet, &battery_level_characteristic);
|
||||||
dump_characteristic(&config_characteristic);
|
dump_characteristic(&battery_level_characteristic);
|
||||||
break;
|
break;
|
||||||
case GATT_EVENT_QUERY_COMPLETE:
|
case GATT_EVENT_QUERY_COMPLETE:
|
||||||
if (packet[4] != 0){
|
if (packet[4] != 0){
|
||||||
@ -191,12 +194,17 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
|||||||
gap_disconnect(connection_handle);
|
gap_disconnect(connection_handle);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// register handler for notifications
|
||||||
|
listener_registered = 1;
|
||||||
|
gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &battery_level_characteristic);
|
||||||
|
|
||||||
state = TC_W4_BATTERY_DATA;
|
state = TC_W4_BATTERY_DATA;
|
||||||
printf("\nConfigure battery level characteristic for notify.\n");
|
printf("\nConfigure battery level characteristic for notify.\n");
|
||||||
status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &config_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
|
status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &battery_level_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
|
||||||
if (status != 0){
|
if (status != 0){
|
||||||
printf("\nNotification not supported. Query value of characteristic.\n");
|
printf("\nNotification not supported. Query value of characteristic.\n");
|
||||||
gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &config_characteristic);
|
gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &battery_level_characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -294,6 +302,12 @@ static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa
|
|||||||
gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid);
|
gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid);
|
||||||
break;
|
break;
|
||||||
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
||||||
|
// unregister listener
|
||||||
|
connection_handle = HCI_CON_HANDLE_INVALID;
|
||||||
|
if (listener_registered){
|
||||||
|
listener_registered = 0;
|
||||||
|
gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener);
|
||||||
|
}
|
||||||
|
|
||||||
if (cmdline_addr_found){
|
if (cmdline_addr_found){
|
||||||
printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr));
|
printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user