【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )
文章目錄
- 一、問(wèn)題描述
- 二、問(wèn)題分析
- 三、完整設(shè)置代碼
一、問(wèn)題描述
Android 應(yīng)用連接 BLE 硬件設(shè)備后 , 出現(xiàn)如下情況 :
發(fā)送數(shù)據(jù)成功 : Android 應(yīng)用 向 BLE 硬件設(shè)備發(fā)送數(shù)據(jù) , 成功 ;
接收數(shù)據(jù)失敗 : Android 應(yīng)用 無(wú)法接收到 BLE 硬件設(shè)備發(fā)送給手機(jī)的數(shù)據(jù) ;
二、問(wèn)題分析
舉個(gè)栗子 :
這是在 Google 官方的 BLE 藍(lán)牙示例程序 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 ) 中的數(shù)據(jù) , 就將特性傳入上述 setCharacteristicNotification 方法 參數(shù) ;
但是上述設(shè)置 , 僅設(shè)置了一半內(nèi)容 , 還需要為 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 作進(jìn)一步設(shè)置 ;
在上面的基礎(chǔ)上 , 還需要為 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 集合中的所有元素設(shè)置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 然后寫(xiě)出該 BluetoothGattDescriptor , 此時(shí)設(shè)置讀取該 BluetoothGattCharacteristic 特性值才能生效 , 否則無(wú)法讀取其中的數(shù)據(jù) ;
BluetoothGattCharacteristic 中維護(hù)了下面的變量 , BluetoothGattDescriptor 隊(duì)列 , 通過(guò)調(diào)用下面的 getDescriptors 方法 , 獲取該隊(duì)列 ;
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;} }調(diào)用 BluetoothGattDescriptor 的 setValue 方法 , 為其設(shè)置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并寫(xiě)出該值 , 即可將讀取該特性的設(shè)置發(fā)送給 BLE 藍(lán)牙模塊 ;
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;} }三、完整設(shè)置代碼
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 隊(duì)列List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();// 遍歷設(shè)置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并寫(xiě)出for(BluetoothGattDescriptor descriptor : descriptors) {descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);mBluetoothGatt.writeDescriptor(descriptor);}}
進(jìn)行上述修改后 , 便可接收 BLE 藍(lán)牙設(shè)備的數(shù)據(jù) ;
總結(jié)
以上是生活随笔為你收集整理的【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【计算机网络】网络安全 : 入侵检测系统
- 下一篇: 【Android 异步操作】线程池 (