I have one doubt with ble communication from my Android app and one peripheral. This peripheral sends data through notify on a certain characteristics "A" and I can write on it on another certain characteristics "B". For do that I do in this way:
Semaphore sem = new Semaphore(1);
void notifyActivation()
{bluetoothGatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if(descriptor != null )
{
try
{
sem.acquire();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Log.i("debug","scrittura descrittore");
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
{
super.onDescriptorWrite(gatt, descriptor, status);
Log.i("debug", "descriptor status: "+status);
sem.release();
}
public void write(final BluetoothGattCharacteristic characteristic)
{
new Thread(new Runnable() {
@Override
public void run() {
try {
//acquisisco il semaforo se è libero se no mi blocco
sem.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean res=bluetoothGatt.writeCharacteristic(characteristic);
if(!res)
{
Log.i("debug","scrittura fallita");
//res=bluetoothGatt.writeCharacteristic(characteristic);
}
sem.release();
}
}).start();
}
I used semaphore because if I do a write before that ondescriptorwrite is invoked the write fails, but I wonder if in this way I can lose some notifications..
Is The semaphore used in right way ? Or it can create some problem to me ?