# wx.onBluetoothDeviceFound(function callback)
Start from base library version 1.1.0. Please remaining backward compatible.
with Promise style call: Not supported
Mini Program plugin: Support, need to Mini Program base library version no less than 2.9.1
Listen for events that search for new devices
# parameter
# function callback
A callback function that searches for events on a new device
# parameter
# Object res
attribute | type | Introductions |
---|---|---|
devices | Array.<Object> | List of newly searched devices |
res.devices Structure
attribute | type | Introductions |
---|---|---|
name | string | Bluetooth device name, which some devices may not have |
deviceId | string | Bluetooth Device id |
RSSI | number | Current Bluetooth Device Signal Strength, Unit dBm |
advertisData | ArrayBuffer | In the broadcast data segment of the current bluetooth device ManufacturerData Data segment. |
advertisServiceUUIDs | Array.<string> | In the broadcast data segment of the current bluetooth device ServiceUUIDs data segment |
localName | string | In the broadcast data segment of the current bluetooth device LocalName data segment |
serviceData | Object | In the broadcast data segment of the current bluetooth device ServiceData data segment |
# Be careful
- If in wx.onBluetoothDeviceFound Callback a device, the device is added to the wx.getBluetoothDevices Interface to the array.
# sample code
// ArrayBuffer 16 Progress String Example
function ab2hex(buffer) {
where hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
wx.onBluetoothDeviceFound(function(res) {
where devices = res.devices
console.log('new device list has founded')
Consume.dir(devices)
console.log(ab2hex(devices[0].advertisData))
})
# Be careful
- When the Bluetooth device is searched, the system returns
name
Field is typically theLocalName
If you connect to a bluetooth device, thename
Field will be changed to theGattName
. If you need to dynamically change the device name and display, you are advised to uselocalName
Field. - Android under some models need to have location permissions to search the device, pay attention to whether to open the location permissions