Android BLE BluetoothGattDescriptor writeDescripto

2019-07-15 01:42发布

I'm Building an application which communicates with BLE. I need to write to BluetoothGattDescriptor an int value. If I do it once everything is working perfectly, but if I want to write to BluetoothGattDescriptor (in separate Characteristics) for each one, I receive a false value in method mBluetoothGatt.writeDescriptor(descriptor) but only for the second characteristic, for the first time I'm getting true. I notice that if I set a delay between them for about 1.3 seconds then I receive true in both tries. Does someone face the same issue?

Here I'm sending the registration for notifications with delay:

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

Here is a log for these commands:

 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

The setRegisterToNotification() method:

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


    }

1条回答
劳资没心,怎么记你
2楼-- · 2019-07-15 01:56

You need to wait for the descriptor callback before writing again. This is found in the BluetoothGattCallback#onDescriptorWrite used when you call connectGatt.

查看更多
登录 后发表回答