簡體   English   中英

BluetoothGatt.writeCharacteristic一半時間返回false

[英]BluetoothGatt.writeCharacteristic return false half the time

實際上,我通過藍牙進行了更新。 第一次,我擦除存儲庫,然后在其上寫入一個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;
    }

切勿使用超時嘗試解決此問題。 正確的方法是等待回調,然后執行下一個請求。 請參閱Android BLE BluetoothGatt.writeDescriptor()有時返回false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM