BluetoothGatt.writeCharacteristic一半时间返回false

时间:2018-11-12 15:17:04

标签: java android bluetooth bluetooth-lowenergy

实际上,我是通过蓝牙进行更新的。第一次擦除存储库,然后在其上写一个hexa File。

但是有一半时间更新无法正常进行,对于我传输的每个数据,第一个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;
    }

1 个答案:

答案 0 :(得分:0)

请勿使用超时尝试解决此问题。正确的方法是等待回调,然后执行下一个请求。参见Android BLE BluetoothGatt.writeDescriptor() return sometimes false