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 theTaro.getBluetoothDevicesAPI. - Some Android models require location permissions before they can discover devices. Be sure to grant location permissions in such cases.
Type
(callback: Callback) => void
Parameters
Callback
The callback function for the new device discovery event.
(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
// Example of an ArrayBuffer converted to a hexadecimal stringfunction 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
| API | WeChat Mini-Program | H5 | React Native |
|---|---|---|---|
| Taro.onBluetoothDeviceFound | ✔️ |
