日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

發(fā)布時(shí)間:2025/6/17 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 ) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 一、問(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


上述代碼是在遍歷完 BluetoothGattServiceBluetoothGattCharacteristic 之后 , 選擇讀取指定特性 ( 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。