基础库 1.1.0 开始支持,低版本需做兼容处理
获取在小程序蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。
OBJECT参数说明:
参数 |
类型 |
必填 |
说明 |
success |
Function |
是 |
成功则返回本机蓝牙适配器状态 |
fail |
Function |
否 |
接口调用失败的回调函数 |
complete |
Function |
否 |
接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 |
类型 |
说明 |
devices |
Array |
uuid 对应的的已连接设备列表 |
errMsg |
String |
成功:ok,错误:详细信息 |
device 对象
蓝牙设备信息
参数 |
类型 |
说明 |
name |
String |
蓝牙设备名称,某些设备可能没有 |
deviceId |
String |
用于区分设备的 id |
RSSI |
Number |
当前蓝牙设备的信号强度 |
advertisData |
ArrayBuffer |
当前蓝牙设备的广播数据段中的ManufacturerData数据段 (注意:vConsole 无法打印出 ArrayBuffer 类型数据) |
advertisServiceUUIDs |
Array |
当前蓝牙设备的广播数据段中的ServiceUUIDs数据段 |
localName |
String |
当前蓝牙设备的广播数据段中的LocalName数据段 |
示例代码:
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
wx.getBluetoothDevices({
success: function (res) {
console.log(res)
if (res.devices[0]) {
console.log(ab2hex(res.devices[0].advertisData))
}
}
})