Discovering Services and Characteristics

Last modified by Microchip on 2023/11/09 08:53

Before starting access operations, the GATT Client must perform Services Discovery to discover all the services and characteristics hosted by the GATT Server on the remote device. The Services Discovery is initiated by the discoverServices() method of the BluetoothGatt class.

The discovery of services is an asynchronous operation. Once service discovery is completed, the onServicesDiscovered() callback in the BluetoothGattCallback class is triggered. If the discovery is successful, the remote services list can be retrieved using the getService() method.

The members in the services list are instances of the BluetoothGattService class. The getUuid() method can be used to retrieve the UUID of the service.

For each service discovered in the GATT Server, the list of characteristics can be retrieved using the getCharacteristics() method.

The members in the characteristics list are instances of the BluetoothGattCharacteristic class. The getUuid() method can be used to retrieve the UUID of the characteristic.

In the MCHP Transparent UART Android app, the onConnectionStateChange() callback method of the BluetoothGattCallback class provides information on when the Central GATT Client has connected/disconnected to/from the remote GATT Server on the peripheral device. On the BluetoothProfile.STATE_CONNECTED callback event indicating a successful connection with the peripheral, the BLEService automatically attempts to discover the services hosted by the peripheral as shown in the accompanying screenshot.

BLEService automatically attempts to discover the services hosted by the peripheral

The onServicesDiscovered() callback method is called when the peripheral services are discovered successfully. The discovered services can be accessed using the BluetoothGatt.getServices() method through BLEService.getSupportedGattServices().

Discovered services can be accessed using BluetoothGatt.getServices() method

The characteristics available in a specific service can be accessed using the BluetoothGattService.getCharacteristics() method through BLEService.getSupportedGattServices().

Characteristics available in an specific service can be accessed using the BluetoothGattService.getCharacteristics() method