gpt4 book ai didi

java - BluetoothGatt.writeCharacteristic 一半时间返回 false

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:01 26 4
gpt4 key购买 nike

实际上我是通过蓝牙进行更新的。第一次我删除存储体,然后在上面写入一个十六进制文件。

但是有一半的时间更新无法正常工作,对于我传输的每个数据,第一个 writeCharacteristic 将返回 false。它有一半的时间发生在整个更新中。

我在 Debug模式下尝试,但在这种情况下该方法永远不会返回 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");
}
}

这是 native 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() return sometimes false .

关于java - BluetoothGatt.writeCharacteristic 一半时间返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com