0

I'm trying to exchange data between android device and custom board with BLE module. And i have some troubles with characteristicsWrite() method on Android devices version 5.0 and higher. In lover versions characteristicsWrite() method return true and then calls onCharacteristicsWrite() callback in BleGattCallback

write method:

private void sendMessageToDevice(String message) {

    byte nullByte = 0x00;
    byte[] temp = message.getBytes();
    byte[] tx = new byte[temp.length + 1];
    tx[tx.length - 1] = nullByte;

    System.arraycopy(temp, 0, tx, 0, temp.length);

    characteristicWrite.setValue(tx);

    boolean writeResult = mBluetoothGatt.writeCharacteristic(characteristicWrite);

    Log.d(LOG_TAG, "writeResult"); //true
}

onCharacteristicsWrite method:

@Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        Log.d(LOG_TAG, status); // status = 0 - GATT_SUCCESS
    }

But when i execute this code on android devices v 5.0 result of writeCharacteristic() method always false, and onCharacteristicWrite() callback not called.

Can anyone explain me how to properly establish connection between BLE device and Android 5.0+ ? Maybe i need some specific approach for this type of devices. Application tested on Samsung and Lenovo smartphones v5.0+.

Vlad Morzhanov
  • 1,260
  • 3
  • 14
  • 29
  • Are you sure you connect your `GATT` layer properly? – Jeeter Jul 29 '16 at 16:48
  • Application works fine with android devices version lower 5.0 so i guess layer connected properly. I'm use this tutoriall to create connection: [Android BLE](https://developer.android.com/guide/topics/connectivity/bluetooth-le.html) – Vlad Morzhanov Jul 29 '16 at 16:57

1 Answers1

-2

Problem solved. You just need to call discoverServices() again after you get "false" result of writeCharacteristic function:

private void sendMessageToDevice(String message) {

        .............

        boolean writeResult = mBluetoothGatt.writeCharacteristic(characteristicWrite);

        if(!writeResult)
           mBluetoothGatt.discoverServices();
}

After that you need to call writeCharacteristic() function again and for me result was "true".

Vlad Morzhanov
  • 1,260
  • 3
  • 14
  • 29
  • 2
    Strange. Normally, the only time it returns false is when you already have a pending gatt operation. In Android you always need to wait for the callback before you can execute a new gatt operation. – Emil Jul 29 '16 at 22:39