Android BLE BluetoothGattDescriptor writeDescriptor问题 [英] Android BLE BluetoothGattDescriptor writeDescriptor issue

查看:843
本文介绍了Android BLE BluetoothGattDescriptor writeDescriptor问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个与BLE通信的应用程序. 我需要将int值写入BluetoothGattDescriptor. 如果我一切正常后执行此操作,但是如果我想为每个蓝牙接口写入BluetoothGattDescriptor(在单独的Feature中),则在方法mBluetoothGatt.writeDescriptor(descriptor)中会收到一个 false 值,但仅针对第二个特性,这是我第一次获得 true . 我注意到,如果我将它们之间的延迟时间设置为约1.3秒,那么两次尝试都将得到正确结果. 有人会遇到同样的问题吗?

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

setRegisterToNotification()方法:

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


    }

推荐答案

您需要先等待描述符回调,然后才能再次写入.可以在 BluetoothGattCallback#onDescriptorWrite 在您调用

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

这篇关于Android BLE BluetoothGattDescriptor writeDescriptor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆