BluetoothGattDescriptor

BluetoothGattDescriptor

我正在构建一个与ble通信的应用程序。
我需要给BluetoothGattDescriptor写一个int值。
如果我在每件事情都正常工作后都这样做,但是如果我想为每一个写BluetoothGattDescriptor(以单独的特征),我在方法mBluetoothGatt.writeDescriptor(descriptor)中会收到一个错误值,但仅对第二个特征,这是我第一次得到真值。
我注意到如果我在它们之间设置一个大约1.3秒的延迟,那么在两次尝试中我都会收到true。
有人面临同样的问题吗?
在这里,我将延迟发送注册通知:

            Handler handler = new Handler();

    registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
            BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_DELAY_IN, SettingsProperties.DELAY_IN_REGISTER_NOTIFICATION_REQUEST);

    handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
            BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_DELAY_OUT, SettingsProperties.DELAY_OUT_REGISTER_NOTIFICATION_REQUEST), 1300);


    handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
            BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_SHORT_WASH, SettingsProperties.SHORT_FLUSH_REGISTER_NOTIFICATION_REQUEST), 2600);


    handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
            BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_LONG_WASH, SettingsProperties.LONG_FLUSH_REGISTER_NOTIFICATION_REQUEST), 3900);


    handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
            BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_SECURITY_TIME, SettingsProperties.SECURITY_TIME_REGISTER_NOTIFICATION_REQUEST), 5200);

以下是这些命令的日志:
 D/IDTEST: ............................setRegisterToNotification
 D/IDTEST: ......setRegisterToNotification ID = 53
 D/IDTEST: ............................Is descriptor registered? = true
 D/IDTEST: ............................Is Notification registered? = true
 D/IDTEST: ............................setRegisterToNotification
 D/IDTEST: ......setRegisterToNotification ID = 56
 D/IDTEST: ............................Is descriptor registered? = true
 D/IDTEST: ............................Is Notification registered? = true
 D/IDTEST: ............................setRegisterToNotification
 D/IDTEST: ......setRegisterToNotification ID = 63
 D/IDTEST: ............................Is descriptor registered? = true
 D/IDTEST: ............................Is Notification registered? = true
 D/IDTEST: ............................setRegisterToNotification
 D/IDTEST: ......setRegisterToNotification ID = 60
 D/IDTEST: ............................Is descriptor registered? = true
 D/IDTEST: ............................Is Notification registered? = true
 D/IDTEST: ............................setRegisterToNotification
 D/IDTEST: ......setRegisterToNotification ID = 66
 D/IDTEST: ............................Is descriptor registered? = true
 D/IDTEST: ............................Is Notification registered? = true

setRegisterToNotification()方法:
public void setRegisterToNotification(BLEDeviceConnectionManager.DataClass dataClass) {

        Log.d("IDTEST", "............................setRegisterToNotification");
        Log.d("IDTEST", "......setRegisterToNotification ID = " + dataClass.getRequestID());


        BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(dataClass.getServiceUUid()).getCharacteristic(dataClass.getCharacteristicsUUid());

        BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);

        if (descriptor != null) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            descriptor.setValue(BleDataParser.getInstance().intTobyteArray(dataClass.getRequestID()));
            boolean isRegistered = mBluetoothGatt.writeDescriptor(descriptor);

            Log.d("IDTEST", "............................Is descriptor registered? = " + isRegistered);

        }

        boolean isRegistered = mBluetoothGatt.setCharacteristicNotification(characteristic, dataClass.isEnableNotification());

        Log.d("IDTEST", "............................Is Notification registered? = " + isRegistered);


    }

最佳答案

在再次写入之前,需要等待描述符回调。这可以在调用BluetoothGattCallback#onDescriptorWrite时使用的connectGatt中找到。

关于android - Android BLE BluetoothGattDescriptor writeDescriptor问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52208563/

10-12 01:51