title: Taro.onBluetoothDeviceFound(callback)

sidebar_label: onBluetoothDeviceFound

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

  1. (callback: Callback) => void

Parameters

Callback

The callback function for the new device discovery event.

  1. (result: CallbackResult) => void
Property Type
result CallbackResult

CallbackResult

Property Type Description
devices CallbackResultBlueToothDevice[] The list of newly discovered devices

CallbackResultBlueToothDevice

Property Type Description
RSSI number The signal strength of the current Bluetooth device
advertisData ArrayBuffer The ManufacturerData field in the broadcast data field of the current Bluetooth device
advertisServiceUUIDs string[] The ServiceUUIDs field in the broadcast data field of the current Bluetooth device
deviceId string Device ID
localName string The LocalName field in the broadcast data field of the current Bluetooth device
name string The name of the Bluetooth device. Some devices may not have a name.
serviceData Record<string, any> The ServiceData field in the broadcast data field of the current Bluetooth device

Sample Code

  1. // Example of an ArrayBuffer converted to a hexadecimal string
  2. function ab2hex(buffer) {
  3. var hexArr = Array.prototype.map.call(
  4. new Uint8Array(buffer),
  5. function(bit) {
  6. return ('00' + bit.toString(16)).slice(-2)
  7. }
  8. )
  9. return hexArr.join('');
  10. }
  11. Taro.onBluetoothDeviceFound(function (res) {
  12. var devices = res.devices;
  13. console.log('new device list has founded')
  14. console.dir(devices)
  15. console.log(ab2hex(devices[0].advertisData))
  16. })

API Support

API WeChat Mini-Program H5 React Native
Taro.onBluetoothDeviceFound ✔️