java - BluetoothGatt.writeCharacteristic 一半时间返回 false
问题描述
其实我是通过蓝牙进行更新的。第一次我擦除了内存库,然后我在上面写了一个 hexa 文件。
但是有一半的时间更新将无法正常工作,对于我传输的每个数据,第一个 writeCharacteristic 将返回 false。它发生在一半时间的整个更新中。
我尝试在调试模式下,但在这种情况下该方法永远不会返回 false,当然这可能是延迟问题,但我无法增加时间。
这是我发送数据的代码:
public void sendTX(final byte[] sMessage) {
BluetoothGattService service = mBluetoothGatt.getService(UUID_SERVICE_SERIAL);
if (service != null && sMessage != null) {
Log.d(TAG,"sMessage : " + sMessage);
final BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID_TX);
if (characteristic != null) {
Thread thread = new Thread() {
public void run() {
if (sMessage.length > 20) {
for (int i = 0; i < sMessage.length; i += 20) {
byte[] byteArraySplit = Arrays.copyOfRange(sMessage, i, i + 20 < sMessage.length ? i + 20 : sMessage.length);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(byteArraySplit);
while(!mBluetoothGatt.writeCharacteristic(characteristic)) {
try {
TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} else {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(sMessage);
while(!mBluetoothGatt.writeCharacteristic(characteristic)){
try {
TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
thread.start();
} else {
Log.d(TAG, "UUID TX null");
}
} else {
Log.d(TAG, "Service BLE null");
}
}
这是本机 writeCharacteristic 方法的代码:
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
characteristic.getInstanceId(), characteristic.getWriteType(),
AUTHENTICATION_NONE, characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}
解决方案
切勿使用超时来尝试解决此问题。正确的方法是等待回调,然后执行下一个请求。请参阅Android BLE BluetoothGatt.writeDescriptor() 有时返回 false。
推荐阅读
- mapbox - 无法清除 Mapbox GL 中的标记
- mongodb - PySpark 的 mongodb-spark 连接器问题(找不到类异常:com.mongodb.spark.sql.DefaultSource)
- java - Is it possible to return from a method using @Advice.OnMethodEnter?
- java - How to get URL as a string from xml file?
- linux - 终端和 SSH 会话之间的区别
- uiviewcontroller - AppDelegate 对 ViewController 子类的引用如何工作
- c - How to split a string at the first space
- python - Pandas 使用格式更新 excel 文件
- java - 从 Angular 5 应用程序中的 Spring Boot API 获取 HTML 文件时,某些属性会被删除
- excel - 线性规划 Excel 求解器