0

I am confused with how can I use Android app to write 20 bytes to CC2540.

I can write one byte(to one byte characteristic of 2540)easily by using the codes as bellow:

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 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;
    }
    byte[] writev = {0x0c};
    characteristic.setValue(writev);
    mBluetoothLeService.writeCharacteristic(characteristic);

}

But if I want to write 20 bytes to a 20-bytes characteristic of 2540 , It seems wrong by changing the codes as follows:

String Mysend = "11111111111111111111";//20bytes
byte[] writev = Mysend.getBytes();
characteristic.setValue(writev);
mBluetoothLeService.writeCharacteristic(characteristic);

So, how can I use Android BLE app to write 20 bytes to CC2540?

Thanks! Callon

H.Callon
  • 31
  • 4
  • Confirm which character set your String is in. If its UTF-8 or ISO-8859-1. Else there may be more bytes than you expect. See: http://stackoverflow.com/a/4385653/295004 and http://stackoverflow.com/a/18571348/295004 – Morrison Chang Nov 29 '15 at 03:01
  • Thank you for your advice.But if I just modify it by the codes as byte[] writev = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,}; it seems wrong again. – H.Callon Nov 30 '15 at 01:17

1 Answers1

0

It's hard to say what's going wrong, but some things to try:

  • Check the return values of those two calls, because they can return false.
  • Don't do more than 1 write in a row.
  • Check the gattStatus provided to BluetoothGattCallback.onCharacteristicWrite() because it may give you a clue why it's failing.
  • Try with 19 bytes, 18 bytes, etc. See what the failure threshold is. It may be that an MTU lower than 20 bytes was negotiated but I don't think that's possible. Try BluetoothGatt.requestMtu() as well.
  • Maybe the application logic on your peripheral (vs. the transport logic) is rejecting the write.
  • As quick a sanity check that your code is correct, you can try SweetBlue, which does lots of error detection/correction.
Doug Koellmer
  • 407
  • 2
  • 8