前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )

【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )

作者头像
韩曙亮
发布2023-03-28 17:41:14
发布2023-03-28 17:41:14
1.6K00
代码可运行
举报
运行总次数:0
代码可运行

文章目录

一、问题描述


Android 应用连接 BLE 硬件设备后 , 出现如下情况 :

发送数据成功 : Android 应用 向 BLE 硬件设备发送数据 , 成功 ;

接收数据失败 : Android 应用 无法接收到 BLE 硬件设备发送给手机的数据 ;

二、问题分析


举个栗子 :

这是在 Google 官方的 BLE 蓝牙示例程序 BluetoothLeGatt 中的 BLE 连接配置代码 :

代码语言:javascript
代码运行次数:0
运行
复制
    /**
     * Enables or disables notification on a give characteristic.
     *
     * @param characteristic Characteristic to act on.
     * @param enabled If true, enable notification.  False otherwise.
     */
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    }

代码文件地址 : BluetoothLeService.java

上述代码是在遍历完 BluetoothGattService 与 BluetoothGattCharacteristic 之后 , 选择读取指定特性 ( BluetoothGattCharacteristic ) 中的数据 , 就将特性传入上述 setCharacteristicNotification 方法 参数 ;

但是上述设置 , 仅设置了一半内容 , 还需要为 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 作进一步设置 ;

在上面的基础上 , 还需要为 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 集合中的所有元素设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 然后写出该 BluetoothGattDescriptor , 此时设置读取该 BluetoothGattCharacteristic 特性值才能生效 , 否则无法读取其中的数据 ;

BluetoothGattCharacteristic 中维护了下面的变量 , BluetoothGattDescriptor 队列 , 通过调用下面的 getDescriptors 方法 , 获取该队列 ;

代码语言:javascript
代码运行次数:0
运行
复制
public class BluetoothGattCharacteristic implements Parcelable {
    /**
     * List of descriptors included in this characteristic.
     */
    protected List<BluetoothGattDescriptor> mDescriptors;

    /**
     * Returns a list of descriptors for this characteristic.
     *
     * @return Descriptors for this characteristic
     */
    public List<BluetoothGattDescriptor> getDescriptors() {
        return mDescriptors;
    }
}

调用 BluetoothGattDescriptor 的 setValue 方法 , 为其设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并写出该值 , 即可将读取该特性的设置发送给 BLE 蓝牙模块 ;

代码语言:javascript
代码运行次数:0
运行
复制
public class BluetoothGattDescriptor implements Parcelable {
    /**
     * Updates the locally stored value of this descriptor.
     *
     * <p>This function modifies the locally stored cached value of this
     * descriptor. To send the value to the remote device, call
     * {@link BluetoothGatt#writeDescriptor} to send the value to the
     * remote device.
     *
     * @param value New value for this descriptor
     * @return true if the locally stored value has been set, false if the requested value could not
     * be stored locally.
     */
    public boolean setValue(byte[] value) {
        mValue = value;
        return true;
    }
}

三、完整设置代码


代码语言:javascript
代码运行次数:0
运行
复制
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

		// 获取 指定 BluetoothGattCharacteristic 中的 List<BluetoothGattDescriptor> mDescriptors 队列
        List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
        // 遍历设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并写出
        for(BluetoothGattDescriptor descriptor : descriptors) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    mBluetoothGatt.writeDescriptor(descriptor);
        }

    }

进行上述修改后 , 便可接收 BLE 蓝牙设备的数据 ;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、问题描述
  • 二、问题分析
  • 三、完整设置代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档