I am trying to create an connection between an Tablet and my MCU which connects as BLE and send/receive characteristics via GATT. The MCU is the GATT Server and the Tablet is my GATT Client.
What is working so far: Both devices are connecting to each other. I can send notifications with my GATT Server so my GATT Client is reading the Chosen Characteristic in the onCharacterChanged for further Tasks.
But i have some weird behavior on writing characteristics from my Gatt Client to the Server. I am triggering the send process by a onClick function:
buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] testByte = new byte[1];
testByte[0] = 3;
characteristic.setValue(testByte);
bluetoothGatt.writeCharacteristic(characteristic);
}
});
And this is working so far (i am filling the characteristic object before the onClick). Now i have another onClickListener just to Change the Value of the Characteristic and send it again.
buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] testByte = new byte[1];
testByte[0] = 7;
characteristic.setValue(testByte);
bluetoothGatt.writeCharacteristic(characteristic);
}
});
So once my characteristic on the GATT Server should have the value 3 and once 7. And this is working only once till my Connection Drops and get reopened, then i can resend the characteristic. The writeCharacteristic is from the BluetoothGatt class and looks like this:
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
service.getType(), service.getInstanceId(),
new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
new ParcelUuid(characteristic.getUuid()),
characteristic.getWriteType(), AUTHENTICATION_NONE,
characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}
I've found out that the process is stuck in
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
but i have no idea why so far. Is there any bit i should set to tell my GATT Server that i will "rewrite" that characteristic? Do i Need to use a thread or something and put it on sleep?