I want to receive characteristic updates notifications from server to client. Both server and client are my android apps. There are a question and a problem.
I have found a "solution" that I need to add this code to subscribe, but add where? On the server side or on the client side?
val CCC_DESCRIPTOR_UUID = "00002902-0000-1000-8000-00805f9b34fb" deviceGatt.setCharacteristicNotification(characteristic, true) val desc = characteristic.getDescriptor(UUID.fromString(CCC_DESCRIPTOR_UUID)) deviceGatt.writeDescriptor(desc, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
The problem is that there are no descriptors at all. When server hosts a characteristic I check and there are 0 descriptors. There should be a CCCD by default but there is no one. The same happens when client discovers a service. There are no descriptors connected to characteristic. Can I add one by myself?
Edited:
I have tried to implement according to advice. Without manual adding CCCD on the server side there are no descriptors visible on the client side. So, on the Server side we have:
gattServer!!.addService(generateGattService())
...
private fun generateGattService(): BluetoothGattService {
val service = BluetoothGattService(GAME_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY)
characteristic = BluetoothGattCharacteristic(
MESSAGE_UUID,
BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
BluetoothGattCharacteristic.PERMISSION_WRITE
)
val CCC_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
val cccd = BluetoothGattDescriptor(
CCC_DESCRIPTOR_UUID,
BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE
)
characteristic!!.addDescriptor(cccd)
service.addCharacteristic(characteristic)
return service
}
On the Client side we have:
private fun onConnected(discoveredGatt: BluetoothGatt){
val service = discoveredGatt.getService(GAME_UUID)
val messageCharacteristic = service.getCharacteristic(MESSAGE_UUID)
discoveredGatt.setCharacteristicNotification(messageCharacteristic, true)
val CCC_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
val desc = messageCharacteristic.getDescriptor(CCC_DESCRIPTOR_UUID)
//desc.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
//discoveredGatt.writeDescriptor(desc)
Log.i(TAG_CLIENT, "Connected C -> S")
}
There are 2 commented rows. Using debugger I got that this way there is a descriptor connected to characteristic. I can send messages C -> S but this descritor has null value. If we uncomment those 2 rows, there is a descriptor value but messages don't come from anyone to anyone.
P.S. Code to write C -> S:
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
characteristic.value = msg
deviceGatt.writeCharacteristic(characteristic)
Code to write S -> C:
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
characteristic.value = msg
gattServer.notifyCharacteristicChanged(deviceToNotify, characteristic, false)