【错误记录】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 的hook技术,Android Native Hook技术(一)
- strictmode android,Android中的StrictMode
- android中适配器的作用,适配器模式 在Android中的简单理解「建议收藏」
- Android怎么查看手机中的本地数据库
- android sdk根目录,Android SDK位置
- circle button键在哪_android按钮
- 【Android 进程保活】Low Memory Killer 机制
- 【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 拷贝 lib7zr.so 动态库到 Android Studio 工程 | 配置 build.gradle 构建脚本 )
- 【错误记录】Android Studio 编译报错 ( Could not find com.android.tools.build:gradle:4.2.1. )
- 【Android 逆向】ptrace 函数 ( ptrace 函数族 | 进程附着 | 进程脱离 | 进程数据读写权限 | 进程对应的主线程寄存器读写 | 单步调试 |ptrace 函数族状态转换 )
- 【错误记录】Android 应用配置第三方 so 动态库 ( /data/app/comxxx==/base.apk/lib/arm64-v8a]couldn‘t find “libx.so“ )
- 【错误记录】Android Studio 编译报错 ( Could not install Gradle distribution from ‘https://services.gradle.or )
- 【错误记录】Android Studio 编译报错 ( Execution failed for task ‘:APP_MIDI:lintVitalRelease‘. )
- 【错误记录】Android Studio 编译报错 ( The dependency contains Java 8 bytecode. Please enable desugaring by )
- 【Android Gradle 插件】LintOptions 配置 ④ ( Lint 问题 ID | fatal 方法配置 | ignore 方法配置 | warning 方法配置 )
- 【错误记录】Android Studio 打包 apk 文件报错 ( The destination folder does not exist or is not writeable )
- 【错误记录】Android Studio 编译报错 ( The minCompileSdk (33) specified in a dependency‘s AAR metadata META-I )
- 【Android Gradle 插件】组件化中的 Gradle 构建脚本实现 ⑤ ( 优化 Gradle 构建脚本 | 构建脚本结构 | 闭包定义及用法 | 依赖配置 | android 块配置 )
- Android开发中遇到的问题(五)——Eclipse导入Android项目出现”Invalid project description overlaps the location of another project”错误的解决办法详解手机开发
- android通过蓝牙接收文件打开时无法自动选择合适的应用程序
- android输入框与文本框加滚动条scrollview示例