tt.getBluetoothDevices
基础库 1.0.0 开始支持本方法,这是一个异步方法。
获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
| 前提条件 | 无 |
| 业务背景 | 无 |
| 使用限制 | 无 |
| 注意事项 | 无 |
| 支持沙盒 | 否 |
| 相关教程 | 无 |
语法
tt.getBluetoothDevices(options)
参数说明
options 为 object 类型,属性如下:
| 属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
|---|---|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数 | 1.0.0 | |
| fail | function | 否 | 接口调用失败的回调函数 | 1.0.0 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 1.0.0 |
回调成功
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支 持版本 |
|---|---|---|---|
| devices | array | 设备信息列表 | 3.87.0 |
| errMsg | string | "getBluetoothDevices:ok" | 3.87.0 |
| errorCode | string | 错误码信息 | 3.87.0 |
devices 类型说明
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| name | string | 蓝牙设备名称,可能为空。 | 3.87.0 |
| deviceId | string | 设备 ID。 | 3.87.0 |
| RSSI | number | 信号强度。 | 3.87.0 |
| advertisData | arrayBuffer | 当前蓝牙设备的广播数据段中的 ManufacturerData 数据段。 | 3.87.0 |
| advertisServiceUUIDs | Array<string> | 当前蓝牙设备的广播数据段中的 ServiceUUIDs 数据段。 | 3.87.0 |
| localName | string | 当前蓝牙设备的广播数据段中的 LocalName 数据段。 | 3.87.0 |
| serviceData | object | 当前蓝牙设备的广播数据段中的 ServiceData 数据段。 | 3.87.0 |
| connectable | boolean | 当前蓝牙设备是否可连接。 | 3.87.0 |
回调失败
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "getBluetoothDevices:fail" + 详细错误信息 | 3.87.0 |
| errorCode | string | 错误码信息 | 3.87.0 |
错误码
| errorCode | errMsg | errorType | 说明 | 最低支持版本 |
|---|---|---|---|---|
| 188701 | getBluetoothDevices:fail not init | D | 未初始化蓝牙适配器 | 3.87.0 |
| 188709 | getBluetoothDevices:fail system error | F | 小程序框架内部错误,有需要请创建工单咨询 | 3.87.0 |
| 188702 | getBluetoothDevices:fail not available | F | 小程序框架内部错误,有需要请创建工单咨询 | 3.87.0 |
| 188788 | getBluetoothDevices:fail system not support | F | 小程序框架内部错误,有需要请创建工单咨询 | 3.87.0 |
代码示例
// ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } tt.getBluetoothDevices({ success: function (res) { console.log(res) if (res.devices[0]) { console.log(ab2hex(res.devices[0].advertisData)) } } })
