android - samsung ble api 无法从多个 GATT 特性中获取通知

标签 android bluetooth-lowenergy samsung-mobile gatt

我正在三星 ACE3 上开发一个应用程序来连接低功耗蓝牙设备。由于三星不希望 ACE3 升级到 Android 4.3,我需要使用三星 ble api。目前,连接、读取数据、发送数据和从一个特征获取通知都可以。但是,当我为多个特征启用通知时,只有第一个启用的特征能够收到通知。有人有同样的问题吗?感谢您的帮助!

以下代码是启用连接通知

 if (mBluetoothGatt != null && device != null) {
        BluetoothGattService pucService = mBluetoothGatt.getService(device, PROFILE_UART_CONTROL_SERVICE);
        if (pucService == null) {
            showMessage("PUC service not found!");
            return;
        }

        BluetoothGattCharacteristic motion = pucService.getCharacteristic(MOTION_READ);
        if (motion == null) {
            showMessage("charateristic not found!");
            return;
        }
        enableNotification(true, motion);            

        BluetoothGattCharacteristic voltage = pucService.getCharacteristic(VOLTAGE_READ);
        if (voltage == null) {
            showMessage("charateristic not found!");
            return;
        }
        enableNotification(true, voltage);


        BluetoothGattCharacteristic pressure = pucService.getCharacteristic(PRESSURE_READ);
        if (pressure == null) {
            showMessage("charateristic not found!");
            return;
        }
        enableNotification(true, pressure);
    }

enableNotification方法如下:

    public boolean enableNotification(boolean enable, BluetoothGattCharacteristic characteristic) {
    if (mBluetoothGatt == null)
        return false;
    if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable))
        return false;

    BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
    if (clientConfig == null)
        return false;

    if (enable) {
         Log.i(TAG,"enable notification");
        clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    } else {
        Log.i(TAG,"disable notification");
        clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    }
    return mBluetoothGatt.writeDescriptor(clientConfig);
}

最佳答案

刚刚意识到这个问题在 miznick 在 this post 的第二个答案中得到了解决。 .主要是因为 Samsung BLE Api 行为同步。基本上,api 一次只能处理 1 个 Gatt 指令,例如写/读特性、w/r 描述符等。通过调用w/r GATT方法,就相当于将Gatt指令追加到等待执行的系统中。执行后系统会调用相关的回调方法,如onCharacterWrite、OnDescriptorWrite等。只有在这一点上或之后,我们才应该调用另一个 Gatt w/r 方法。代码在 miznick 的帖子中给出。

This post也有助于理解 Samsung Ble api 的行为。请务必查看三星 BLE 官方网站中的指南和提示。

关于android - samsung ble api 无法从多个 GATT 特性中获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949638/

相关文章:

android - Android 蓝牙低功耗配对方式是如何选择的

android - Samsung Galaxy S2 上的 MJPEG-Stream 比 Samsung Galaxy Tab2 上的速度更快?

android - 三星设备问题中的相机方向

android - 在 DownloadManager DOWNLOAD_COMPLETE 广播接收器上接收广播 Intent 时出错

android - 如何通过 Android Studio Gradle 构建复制或使用 native 库?

android - 具有 128 位 UUID 的 startLeScan 不适用于 native Android BLE 实现

android - 三星的多窗口支持 - 屏幕尺寸

android - 如何在android Activity 中永久隐藏导航栏?

javascript - 从 javaScript Phonegap 上传文件的方法

android - 从 android 4.3 连接到 BLE113 时正在记录 "Client registered, waiting for callback"