我正在尝试使用BLE模块在android设备和自定义板之间交换数据。我在Android设备版本5.0及更高版本上的featuresWrite()方法遇到了一些麻烦。在情人版本中,featuresWrite()方法返回true,然后在BleGattCallback中调用onCharacteristicsWrite()回调
写入方法:
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方法:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.d(LOG_TAG, status); // status = 0 - GATT_SUCCESS
}
但是,当我在Android设备v 5.0上执行此代码时,writeCharacteristic()方法的结果始终为false,并且未调用onCharacteristicWrite()回调。
谁能解释我如何在BLE设备和Android 5.0+之间正确建立连接?对于这种类型的设备,也许我需要一些特定的方法。应用程序已在三星和联想智能手机v5.0 +上进行了测试。
问题解决了。您只需要在获得writeCharacteristic函数的“假”结果后再次调用discoverServices()即可:
private void sendMessageToDevice(String message) {
.............
boolean writeResult = mBluetoothGatt.writeCharacteristic(characteristicWrite);
if(!writeResult)
mBluetoothGatt.discoverServices();
}
之后,您需要再次调用writeCharacteristic()函数,对我而言,结果为“ true”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句