Skip to main content
Version: Next

Taro.onBluetoothDeviceFound(callback)

Listens on the new device discovery event.

Notes

  • If a device is called back in Taro.onBluetoothDeviceFound, this device is added to the array obtained by the Taro.getBluetoothDevices API.
  • Some Android models require location permissions before they can discover devices. Be sure to grant location permissions in such cases.

Reference

Type

(callback: Callback) => void

Parameters

Callback

The callback function for the new device discovery event.

(result: CallbackResult) => void
PropertyType
resultCallbackResult

CallbackResult

PropertyTypeDescription
devicesCallbackResultBlueToothDevice[]The list of newly discovered devices

CallbackResultBlueToothDevice

PropertyTypeDescription
RSSInumberThe signal strength of the current Bluetooth device
advertisDataArrayBufferThe ManufacturerData field in the broadcast data field of the current Bluetooth device
advertisServiceUUIDsstring[]The ServiceUUIDs field in the broadcast data field of the current Bluetooth device
deviceIdstringDevice ID
localNamestringThe LocalName field in the broadcast data field of the current Bluetooth device
namestringThe name of the Bluetooth device. Some devices may not have a name.
serviceDataRecord<string, any>The ServiceData field in the broadcast data field of the current Bluetooth device

Sample Code

// Example of an ArrayBuffer converted to a hexadecimal string
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
Taro.onBluetoothDeviceFound(function (res) {
var devices = res.devices;
console.log('new device list has founded')
console.dir(devices)
console.log(ab2hex(devices[0].advertisData))
})

API Support

APIWeChat Mini-ProgramH5React Native
Taro.onBluetoothDeviceFound✔️