Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BLE: cannot write a Characteristic (no PROPERTY_WRITE)

I have an Android (Glass) application that acts as a BLE central and connects to a BLE peripheral (which is an iOS device using Core Bluetooth). I am trying to read from and write to the peripheral.

Reading works fine (and receiving notifications works fine too).

However I didn't manage to write a characteristic. Here's my code:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  if (status == BluetoothGatt.GATT_SUCCESS) {
    BluetoothGattService bse = gatt.getService(TRANSFER_SERVICE_UUID);
    BluetoothGattCharacteristic bgc = bse.getCharacteristic(TRANSFER_CHARACTERISTIC_UUID);
    bgc.setValue("Hello");
    boolean writeOk = gatt.writeCharacteristic(bgc);
  }
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
  // never called
}

writeOk is always false. I debugged it and found out that the reason is the properties. bgc.getProperties() always returns 50, no matter what properties are set on the iOS side. 50 is PROPERTY_READ, PROPERTY_NOTIFY and PROPERTY_INDICATE, but its missing PROPERTY_WRITE, so BluetoothGatt.writeCharacteristic() immediately exits:

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
        && (characteristic.getProperties() &
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
...
}

It seems to me that the properties are not transmitted correctly from the iOS peripheral to the Android central. When connecting to the iOS peripheral with an iOS central, the properties are transmitted correctly and writing works.

I have tried:

  • pairing the devices
  • using reliable write (but I don't really know what that is anyway)

So - am I doing anything wrong on the Android side? If not: Is this a bug? Or does iOS only wants to get writes from iOS devices?

I am using Android 4.4.2 (Glass XE18.11).

like image 675
Manuel M Avatar asked Oct 20 '22 06:10

Manuel M


2 Answers

Seems that this was a bug in the Android BLE API. After the latest update (Glass XE18.3) this behaviour is gone and transmitting the Properties works as expected.

like image 134
Manuel M Avatar answered Oct 23 '22 00:10

Manuel M


You can maybe find the answer at this link:

https://devzone.nordicsemi.com/question/1423/writing-to-a-characteristic-using-the-android-43-api/

Regards

like image 41
kovach Avatar answered Oct 23 '22 00:10

kovach