GATT Battery Service Client
Source Code: gatt_battery_query.c
This example demonstrates how to use the GATT Battery Service client to receive battery level information. The client supports querying of multiple battery services instances of on the remote device. The example scans for remote devices and connects to the first found device and starts the battery service client.
Main Application Setup
The Listing here shows how to setup Battery Service client. Besides calling init() method for each service, you'll also need to register HCI packet handler to handle advertisements, as well as connect and disconect events.
Handling of GATT Battery Service events will be later delegated to a sepparate packet handler, i.e. gatt_client_event_handler.
@note There are two additional files associated with this client to allow a remote device to query out GATT database:
- gatt_battary_query.gatt - contains the declaration of the provided GATT Services and Characteristics.
- gatt_battary_query.h - contains the binary representation of gatt_battary_query.gatt.
static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void gatt_client_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void battery_service_client_setup(void){
// Init L2CAP
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();
// Setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/att_server.h#L80">att_server_init</a>(profile_data, NULL, NULL);
// GATT Client setup
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt_client.h#L359">gatt_client_init</a>();
// Device Information Service Client setup
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/battery_service_client.h#L139">battery_service_client_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L125">sm_set_io_capabilities</a>(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
hci_event_callback_registration.callback = &hci_event_handler;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&hci_event_callback_registration);
}
static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
...
if (packet_type != HCI_EVENT_PACKET){
return;
}
switch (hci_event_packet_get_type(packet)) {
...
case HCI_EVENT_META_GAP:
// Wait for connection complete
if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break;
...
// Get connection handle from event
connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
// Connect to remote Battery Service.
// On succesful connection, the client tries to register for notifications. If notifications
// are not supported by remote Battery Service, the client will automatically poll the battery level - here every 2 seconds.
// If poll_interval_ms is 0, polling is disabled, and only notifications will be received (for manual polling,
// see battery_service_client.h).
// All GATT Battery Service events are handled by the gatt_client_event_handler.
(void) <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/battery_service_client.h#L162">battery_service_client_connect</a>(connection_handle, gatt_client_event_handler, 2000, &battery_service_cid);
app_state = APP_STATE_CONNECTED;
printf("Battery service connected.\n");
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
connection_handle = HCI_CON_HANDLE_INVALID;
// Disconnect battery service
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/battery_service_client.h#L179">battery_service_client_disconnect</a>(battery_service_cid);
...
printf("Disconnected %s\n", <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L271">bd_addr_to_str</a>(report.address));
printf("Restart scan.\n");
app_state = APP_STATE_W4_SCAN_RESULT;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L663">gap_start_scan</a>();
break;
default:
break;
}
}
// The gatt_client_event_handler receives following events from remote device:
// - GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED
// - GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL
//
static void gatt_client_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
...
uint8_t status;
uint8_t att_status;
if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){
return;
}
switch (hci_event_gattservice_meta_get_subevent_code(packet)){
case GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED:
status = gattservice_subevent_battery_service_connected_get_status(packet);
switch (status){
case ERROR_CODE_SUCCESS:
printf("Battery service client connected, found %d services, poll bitmap 0x%02x\n",
gattservice_subevent_battery_service_connected_get_num_instances(packet),
gattservice_subevent_battery_service_connected_get_poll_bitmap(packet));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/battery_service_client.h#L172">battery_service_client_read_battery_level</a>(battery_service_cid, 0);
break;
default:
printf("Battery service client connection failed, status 0x%02x.\n", status);
add_to_blacklist(report.address);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L386">gap_disconnect</a>(connection_handle);
break;
}
break;
case GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL:
att_status = gattservice_subevent_battery_service_level_get_att_status(packet);
if (att_status != ATT_ERROR_SUCCESS){
printf("Battery level read failed, ATT Error 0x%02x\n", att_status);
} else {
printf("Service index: %d, Battery level: %d\n",
gattservice_subevent_battery_service_level_get_service_index(packet),
gattservice_subevent_battery_service_level_get_level(packet));
}
break;
default:
break;
}
}