I try to communicate with an µ-controller through an android device via BLE. I am able to write my custom characteristic (toggle an LED on my Dev-Kit) but can't read the value of the LED status ('0x00' for off, '0x01' for on).
I want to read it when i click on the ExpandableListView when i click on an item. For the moment i implemented it within the onChildClickListener. If a characteristic-permission "PROPERTY_WRITE" > 0
then it should write the value.
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
mBluetoothLeService.readCustomCharacteristic();
mBluetoothLeService.writeCustomCharacteristic(0x01);
}
return true;
}
return false;
}
But it always fails to read the characteristic. The LED value is written and the LED switches to 'ON' but it doesn't read the value of the LED. I want to read the value for switching the LED on/off with clicking on the characteristic in my list.
I can't figure out what i'm doing wrong. It should be reading the characteristic, write it in a textfield. On my BLE-Device i have enabled reading. I can read the value with my ubuntu-terminal using gatttool -> char-read-hnd [hnd] [val]
.
This is my implementation of the readCustomCharacteristic()
in mBluetoothLeService
public void readCustomCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a2f"));
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("18192021-2223-2425-2627-282930313233"));
mBluetoothGatt.readCharacteristic(mReadCharacteristic);
if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
Log.w(TAG, "Failed to read characteristic");
}
return;
}