【错误记录】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 异步操作】线程池 (