Android BluetoothGatt setCharacteristicNotification仅设置第一个特征

btelman96

我在通过同一蓝牙LE设备同时使用两个不同的特性时遇到了麻烦。它仅记录第一个特征,而不记录第二个特征。如果我切换它们,它仍然会注册第一个而不是第二个。第一和第二特性都需要注册和接收。如果有帮助,我正在使用运行4.4.2的Samsung Galaxy S4。

这是调用setCharacteristicNotification()的代码部分

if (mBluetoothBeaconGatt != null && mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE) != null) {

        characteristicBracelet = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
                .getCharacteristic(UUID_CUSTOM_BRACELET);

        characteristicBeacon = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
                .getCharacteristic(UUID_CUSTOM_BEACON);

        if(characteristicBracelet != null) {
            Log.i(TAG, "Init Bracelet Characteristics");
            this.setCharacteristicNotification(characteristicBracelet,
                    true);
        }
        if(characteristicBeacon != null) {
            Log.i(TAG, "Init Beacon Characteristics");
            this.setCharacteristicNotification(characteristicBeacon, true);
        }
    }

setCharacteristicNotification

if (UUID_CUSTOM_BEACON.equals(characteristic.getUuid())) {
        if (mBluetoothBeaconGatt != null) {
            Log.i(TAG, "Enabling indication for beacon");
            mBluetoothBeaconGatt.setCharacteristicNotification(characteristic, enabled);
            BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(UUID
                            .fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor
                    .setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                            : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            mBluetoothBeaconGatt.writeDescriptor(descriptor);
        }
    } else if (UUID_CUSTOM_BRACELET.equals(characteristic.getUuid())) {
        if (mBluetoothBraceletGatt != null || mBluetoothBeaconGatt != null) {
            BluetoothGatt gatt = (mBluetoothBeaconGatt != null) ? mBluetoothBeaconGatt : mBluetoothBraceletGatt;
            gatt.setCharacteristicNotification(characteristic, enabled);
            BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(UUID
                            .fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor
                    .setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                            : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptor);
        }
    }

经过测试以查看它们是否为空的变量将验证它们是否为非空。

如果您需要其他信息,或者这是重复的,请告诉我。

感谢您抽出宝贵的时间来帮助您。

ach

您不能同时执行多个蓝牙操作。一旦获得onDescriptorWrite,就可以启用/禁用下一个特性:

private int index;
private boolean enabled;

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        index++;
        setNotification();
    }
}

public void notify(boolean enabled){
    index = 0;
    this.enabled = enabled;
    setNotification();
}

private void setNotification(){
    BluetoothGattCharacteristic characteristic;
    switch(index){
        case 0:
            characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
                        .getCharacteristic(UUID_CUSTOM_BRACELET);
            break;
        case 1:
            characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
                        .getCharacteristic(UUID_CUSTOM_BEACON);
            break;
        default:
            return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    descriptor.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                        : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(desc);
}

NewCircle的不错的教程:https ://newcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android BluetoothGatt超级方法?

来自分类Dev

Android BluetoothGatt writeCharacteristic与响应

来自分类Dev

Android BluetoothGatt超级方法?

来自分类Dev

BluetoothGatt.setCharacteristicNotification()有什么作用?

来自分类Dev

Android:ListView仅显示第一个元素

来自分类Dev

Android BluetoothGatt-状态133-注册回调

来自分类Dev

如何仅解析android中json对象中的第一个数组?

来自分类Dev

ANDROID ::自定义ListView仅显示第一个LinearLayout子级

来自分类Dev

Android仅显示我的第一个数据库搜索

来自分类Dev

Android HttpURLConnection,仅加载第一个4066符号,并中断请求

来自分类Dev

仅使用Mpchart在android中显示的第一个和最后一个标签

来自分类Dev

Android的BLE服务发现(BluetoothGatt#discoverServices())和Low Energy vs BR / EDR

来自分类Dev

Xamarin Android:如果背景设置仅可见第一个片段

来自分类Dev

Android SurfaceView仅播放第一个视频,然后屏幕冻结,最后一个视频帧,仅播放声音

来自分类Dev

Android返回NavigationNavigator的第一个片段

来自分类Dev

Android Studio第一个项目错误

来自分类Dev

Android RecyclerView选择第一个项目

来自分类Dev

第一个基本的Android应用程序?

来自分类Dev

如何在Android中创建第一个离线RecyclerView?以及如何仅更新最新数据?

来自分类Dev

BluetoothGatt:协商新的MTU成功,但是无法使用新的大小(相差3个字节)

来自分类Dev

在Android 4.3蓝牙BLE上重置蓝牙芯片后,调用BluetoothGatt.connect()时引发Catching捕获DeadObjectException

来自分类Dev

无论时间是否过去,仅在重新启动时设置第一个警报(android)

来自分类Dev

Android RecyclerView查找ItemDecoration的第一个和最后一个视图

来自分类Dev

Android群组通知显示最后一个通知而不是第一个

来自分类Dev

我的第一个Android应用程序旋转时崩溃

来自分类Dev

构建后如何启用/禁用Android Studio从跳转到第一个错误

来自分类Dev

PopBackStack,但将第一个片段保留在android中

来自分类Dev

如何在Android的Firebase令牌服务中处理第一个令牌?

来自分类Dev

我的第一个android应用程序崩溃并单击按钮

Related 相关文章

  1. 1

    Android BluetoothGatt超级方法?

  2. 2

    Android BluetoothGatt writeCharacteristic与响应

  3. 3

    Android BluetoothGatt超级方法?

  4. 4

    BluetoothGatt.setCharacteristicNotification()有什么作用?

  5. 5

    Android:ListView仅显示第一个元素

  6. 6

    Android BluetoothGatt-状态133-注册回调

  7. 7

    如何仅解析android中json对象中的第一个数组?

  8. 8

    ANDROID ::自定义ListView仅显示第一个LinearLayout子级

  9. 9

    Android仅显示我的第一个数据库搜索

  10. 10

    Android HttpURLConnection,仅加载第一个4066符号,并中断请求

  11. 11

    仅使用Mpchart在android中显示的第一个和最后一个标签

  12. 12

    Android的BLE服务发现(BluetoothGatt#discoverServices())和Low Energy vs BR / EDR

  13. 13

    Xamarin Android:如果背景设置仅可见第一个片段

  14. 14

    Android SurfaceView仅播放第一个视频,然后屏幕冻结,最后一个视频帧,仅播放声音

  15. 15

    Android返回NavigationNavigator的第一个片段

  16. 16

    Android Studio第一个项目错误

  17. 17

    Android RecyclerView选择第一个项目

  18. 18

    第一个基本的Android应用程序?

  19. 19

    如何在Android中创建第一个离线RecyclerView?以及如何仅更新最新数据?

  20. 20

    BluetoothGatt:协商新的MTU成功,但是无法使用新的大小(相差3个字节)

  21. 21

    在Android 4.3蓝牙BLE上重置蓝牙芯片后,调用BluetoothGatt.connect()时引发Catching捕获DeadObjectException

  22. 22

    无论时间是否过去,仅在重新启动时设置第一个警报(android)

  23. 23

    Android RecyclerView查找ItemDecoration的第一个和最后一个视图

  24. 24

    Android群组通知显示最后一个通知而不是第一个

  25. 25

    我的第一个Android应用程序旋转时崩溃

  26. 26

    构建后如何启用/禁用Android Studio从跳转到第一个错误

  27. 27

    PopBackStack,但将第一个片段保留在android中

  28. 28

    如何在Android的Firebase令牌服务中处理第一个令牌?

  29. 29

    我的第一个android应用程序崩溃并单击按钮

热门标签

归档