【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )
文章目录
- 一、问题描述
- 二、问题分析
- 三、完整设置代码
一、问题描述
Android 应用连接 BLE 硬件设备后 , 出现如下情况 :
发送数据成功 : Android 应用 向 BLE 硬件设备发送数据 , 成功 ;
接收数据失败 : Android 应用 无法接收到 BLE 硬件设备发送给手机的数据 ;
二、问题分析
举个栗子 :
这是在 Google 官方的 BLE 蓝牙示例程序 BluetoothLeGatt 中的 BLE 连接配置代码 :
/*** 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 方法 , 获取该队列 ;
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 蓝牙模块 ;
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;} }三、完整设置代码
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 蓝牙设备的数据 ;
总结
以上是生活随笔为你收集整理的【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【计算机网络】网络安全 : 入侵检测系统
- 下一篇: 【Android 异步操作】线程池 (