wx.readBLECharacteristicValue(Object object)

基础库 1.1.0 开始支持,低版本需做兼容处理

读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。

参数

Object object

属性类型默认值必填说明
deviceIdstring蓝牙设备 id
serviceIdstring蓝牙特征值对应服务的 uuid
characteristicIdstring蓝牙特征值的 uuid
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

错误

错误码错误信息说明
0ok正常
10000not init未初始化蓝牙适配器
10001not available当前蓝牙适配器不可用
10002no device没有找到指定设备
10003connection fail连接失败
10004no service没有找到指定服务
10005no characteristic没有找到指定特征值
10006no connection当前连接已断开
10007property not support当前特征值不支持此操作
10008system error其余所有系统上报的异常
10009system not supportAndroid 系统特有,系统版本低于 4.3 不支持 BLE
10012operate time out连接超时
10013invalid_data连接 deviceId 为空或者是格式不正确

注意

  • 并行调用多次会存在读失败的可能性。
  • 接口读取到的信息需要在 onBLECharacteristicValueChange 方法注册的回调中获取。

示例代码

在开发者工具中预览效果

  1. // 必须在这里的回调才能获取
  2. wx.onBLECharacteristicValueChange(function(characteristic) {
  3. console.log('characteristic value comed:', characteristic)
  4. })
  5. wx.readBLECharacteristicValue({
  6. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  7. deviceId,
  8. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  9. serviceId,
  10. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  11. characteristicId,
  12. success (res) {
  13. console.log('readBLECharacteristicValue:', res.errCode)
  14. }
  15. })