如何使用Android BLE应用程序向CC2540写入20个字节?

时间:2015-11-29 02:46:33

标签: android bluetooth-lowenergy

我很困惑如何使用Android应用程序向CC2540写入20个字节。

我可以使用下面的代码轻松地写一个字节(到2540的一个字节特征):

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);

}

但是如果我想将20个字节写入2540的20字节特征,那么通过如下更改代码似乎是错误的:

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

那么,我如何使用Android BLE应用程序向CC2540写入20个字节?

谢谢! 卡隆

1 个答案:

答案 0 :(得分:0)

很难说出现了什么问题,但有些事情要尝试:

  • 检查这两个调用的返回值,因为它们可以返回false。
  • 连续写入不要超过1次。
  • 检查提供给BluetoothGattCallback.onCharacteristicWrite()的gattStatus,因为它可能会告诉您它失败的原因。
  • 尝试使用19个字节,18个字节等。查看故障阈值是什么。可能是协商了低于20字节的MTU但我不认为这是可能的。也可以尝试BluetoothGatt.requestMtu()
  • 也许外围设备上的应用程序逻辑(相对于传输逻辑)拒绝写入。
  • 通过快速检查您的代码是否正确,您可以尝试SweetBlue,这会进行大量的错误检测/纠正。