I'm developing two app one peripheral and one central role.
Peripheral
2 characteristic :
- one (called pippo) with write_noresponse property
- one (called paperino) with read and notify property
y
private void addServiceToGattServer() {
Service = new BluetoothGattService(
UUID.fromString(CostantUUID.xxx),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
paperino = new BluetoothGattCharacteristic(
UUID.fromString(CostantUUID.xxx),
BluetoothGattCharacteristic.PROPERTY_NOTIFY|BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
clientCharacteristiConfiguration = new BluetoothGattDescriptor(UUID.fromString(CostantUUID.clientCharacteristiConfiguration), BluetoothGattDescriptor.PERMISSION_WRITE);
paperino.addDescriptor(clientCharacteristiConfiguration);
Service.addCharacteristic(paperino);
pippo = new BluetoothGattCharacteristic(
UUID.fromString(CostantUUID.userCommands),
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE ,
BluetoothGattCharacteristic.PERMISSION_WRITE);
ebikeService.addCharacteristic(pippo);
mGattServer.addService(Service);
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
{
if(responseNeeded)
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
................
...............
paperino.setValue(payload);
mGattServer.notifyCharacteristicChanged(device,paperino, false);
}
Client side
I have abilitated the setCharacteristicNotification and wrote a descriptor for characteristic Paperino :
bluetoothGatt.setCharacteristicNotification(characteristic, true);
/**
* @author I
* I abilitate the cliatcharacteristicconfiguration descriptor
*/
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CostantUUid.clientCharacteristiConfiguration));
if(descriptor != null)
{
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
In this way all function correctly works, but I wonder: it's really necessary to add descriptor on peripherall side and write on it from client side ?
I tried to not use it and the it doesn't work. But on the internet tutorials never tell to write descriptor...
WHat is the true ?
Furthermore if I do the first notifyCharacteristicChanged(device,paperino, false); in the onConnectionStateChange callback
@Override
public void onConnectionStateChange(final BluetoothDevice device,
int status, int newState) {
super.onConnectionStateChange(device, status, newState);
Log.d(TAG, "onConnectionStateChange status=" + status + "->"
+ newState);
message=myHandler.obtainMessage();
if(newState == BluetoothProfile.STATE_CONNECTED)
{
disconnected=false;
Bundle f=new Bundle();
f.putString("connectionStatus", (String)(Utils.getStateDescription(newState)));
message.setData(f);
myHandler.sendMessage(message);
connectedDevice=device;
paperino.setValue(examplemsg);
mGattServer.notifyCharacteristicChanged(device, paperino,false);
}
on server side the notification the "examplemessage" doesn't arrive on client side. I have to call for the first time notifyCharacteristicChanged in onDescriptorWrite...
Maybe is too early to call notifycharacteristicChanged ?