This discussion and its replies are closed

Service Change Indications

Background: Attribute Caching is a mechanism of caching attribute handles which allows clients to avoid the process of rediscovering services upon consecutive connections with paired and bonded devices. This saves power, time and reduces the packet size.

Problem: If the client caches attribute handles and the values change then it can lead to unpredictable values from those handles. This can happen when you add or remove characteristics in run time or between firmware updates. So the question arises of how do we notify the client to rediscover services upon a change in GATT server?

For more details about how Android, iOS behaves and implements this, refer this article.

 

Solution

The Generic Attribute Service includes a characteristic called Service Changed which can be used by the client to let the server know that the client wants to be indicated for any changes in GATT Server. The Service Changed Indication with UUID 0x2A05 has the range of handles which have been changed. This can help the client to rediscover that range of handles.

If the database has a Generic Attribute Service and Service Changed characteristic, the stack will monitor local database change status and manage service changed indications for a GATT client that has enabled the indication configuration of the Service Changed characteristic.

 

Implementation

Unlike other services and characteristics, the Generic Attribute Service isn’t directly visible in the GATT configurator. For any Bluetooth project in Simplicity Studio open the .isc file and click on the profile in the custom GATT view. You can enable or disable the Generic Attribute Service using the checkbox as shown in the diagram below. It is enabled by default for examples that come with the SDK.

 

For the server, import the attached gatt.xml file in GATT configurator and Generate. In the gatt header file you will find the handle for the characteristic. Additionally we have two hidden characteristics which we will enable or disable on a Button Press PB0 to make changes in the GATT database.

#ifndef __GATT_DB_H
#define __GATT_DB_H

#include "bg_gattdb_def.h"

extern const struct bg_gattdb_def bg_gattdb_data;

#define gattdb_service_changed_char             3
#define gattdb_device_name                      7
#define gattdb_ota_control                     19

typedef enum
{
    Hideable                       = 0x0001,
    hide                           = 0x0002,
    bg_gattdb_data_all_caps = 0x0003
} bg_gattdb_data_cap_t;

#endif

 

Test setup

GATT Server: A radio board running the attached firmware. Right click on the device in Simplicity Studio and Launch Console. Select Serial 1 and press Enter to get connected. Here you can monitor the events happening on the server and see the passkey used for pairing.
GATT Client: A radio board in NCP mode. In Simplicity Studio, select this board, flash the NCP empty example from the demo section and launch BGTool. Connect to the board selecting the appropriate COM port.

In the command window execute the following commands.

gecko_cmd_sm_store_bonding_configuration(8, 2)
gecko_cmd_sm_configure(1,2)
gecko_cmd_sm_set_bondable_mode(1)

Connect to the server

  1. Start Discover and connect to our GATT server device named “Empty Example”.
  2. Connection will open a new tab and ask for a passkey. Enter the passkey displayed on the console of the server. The 2 devices will now be bonded.
  3. Click discover remote database and let the client scan. Select the GATT Service Changed characteristic under Generic Attribute, select the Enable checkbox and click Apply. If you select Confirm automatically, you won’t see a pop up for indications. Your client is now subscribed to any changes in the GATT database.
    Alternatively you can also use this command by passing the appropriate variables to set characteristic notifications. Please refer BGAPI reference for more details.
gecko_cmd_gatt_set_characteristic_notification(uint8 connection, uint16 characteristic, uint8 flags);

 

4. Press PB1 and you will see an Indication window pop up as shown in the figure. Clicking confirm will send a confirmation response to the server. Clicking cancel will not send the response and will disable the indication until re-connection. You will also see the corresponding events generated in the BGTool log window.

 

 

5. To disable the indication, un-check the Indicate box and click apply.

  • Bluetooth Low Energy
  • Knowledge Base Articles