# tap.onBluetoothDeviceFound(function listener)
# 功能描述
搜索到新设备的事件监听
# 参数
# function listener
搜索到新设备的事件的监听函数
# 参数
# object res
| 属性 | 类型 | 说明 | |||||||||||||||||||||||||||||||||||||
| devices | Array.<Object> | 新搜索到的设备列表 | |||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||
# 注意
- 若在 tap.onBluetoothDeviceFound 回调了某个设备,则此设备会添加到 tap.getBluetoothDevices 接口获取到的数组中。
# 示例代码
// 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('');
}
tap.onBluetoothDeviceFound(function(res) {
var devices = res.devices;
console.log('new device list has founded')
console.dir(devices)
console.log(ab2hex(devices[0].advertisData))
})
# 注意
- 蓝牙设备在被搜索到时,系统返回的
name字段一般为广播包中的LocalName字段中的设备名称,而如果与蓝牙设备建立连接,系统返回的name字段会改为从蓝牙设备上获取到的GattName。若需要动态改变设备名称并展示,建议使用localName字段。 - 安卓下部分机型需要有位置权限才能搜索到设备,需留意是否开启了位置权限
