简体   繁体   English

BluetoothGatt.writeCharacteristic一半时间返回false

[英]BluetoothGatt.writeCharacteristic return false half the time

Actually I make an update through Bluetooth. 实际上,我通过蓝牙进行了更新。 In first time I erase the memory bank and then I write an hexa File on it. 第一次,我擦除存储库,然后在其上写入一个hexa File。

But half the time an update will don't work correctly, for every data I transfer the first writeCharacteristic will return false. 但是有一半的时间更新无法正常进行,对于我传输的每个数据,第一个writeCharacteristic将返回false。 It happen on an entire update half the time. 它发生在整个更新的一半时间上。

I try in debug mode but the method never return false in that case, of course it's probably a delay problem, but I can't increase the time. 我尝试在调试模式下运行,但是在那种情况下该方法永远不会返回false,这当然可能是一个延迟问题,但是我不能增加时间。

This is my code for send my data : 这是我发送数据的代码:

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

And this is the code of the native writeCharacteristic method : 这是本机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;
    }

Never use timeouts to try to workaround this issue. 切勿使用超时尝试解决此问题。 The proper way is to wait for the callback and then perform the next request. 正确的方法是等待回调,然后执行下一个请求。 See Android BLE BluetoothGatt.writeDescriptor() return sometimes false . 请参阅Android BLE BluetoothGatt.writeDescriptor()有时返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM