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.
boolean discoverServices()
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 was successful, the remote services list can be retrieved using the getService() method.
void onServicesDiscovered(BluetoothGatt gatt, int status)
List<BluetoothGattService> getServices()
The members in the services list are instances of 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.
List<BluetoothGattCharacteristic> getCharacteristics()
The members in the characteristics list are instances of 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 screenshot below.
The onServicesDiscovered() callback method is called when the peripheral services are discovered successfully. The discovered services can be accessed using BluetoothGatt.getServices() method through BLEService.getSupportedGattServices().
The characteristics available in an specific service can be accessed using the BluetoothGattService.getCharacteristics() method through BLEService.getSupportedGattServices().