Taro.getBluetoothDevices(option)
Obtains all Bluetooth devices discovered during the active period of the Bluetooth module, including all devices that are already connected to the mobile.
Notes
This API obtains a device list of
all Bluetooth devices discovered during the active period of the Bluetooth module
. If you do not promptly call wx.closeBluetoothAdapter to release system resources after the end of the Bluetooth module process, calling this API will return Bluetooth devices discovered in the previous Bluetooth process. These devices may no longer be near the user, and thus cannot be connected.When a Bluetooth device is discovered, the name field returned by the system is generally the device name in the LocalName field in the broadcast package. If a connection is established with the Bluetooth device, the name field returned by the system changes to the GattName obtained from the Bluetooth device. If you need to dynamically change and display the device name, we recommend using the localName field.
Type
(option?: Option) => Promise<SuccessCallbackResult>
Parameters
Option
Property | Type | Required | Description |
---|---|---|---|
complete | (res: any) => void | No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
fail | (res: any) => void | No | The callback function for a failed API call |
success | (res: Result) => void | No | The callback function for a successful API call |
SuccessCallbackResult
Property | Type | Description |
---|---|---|
devices | SuccessCallbackResultBlueToothDevice[] | The list of connected devices corresponding to the UUIDs |
errMsg | string | 成功:ok,错误:详细信息 |
SuccessCallbackResultBlueToothDevice
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 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.getBluetoothDevices({
success: function (res) {
console.log(res)
if (res.devices[0]) {
console.log(ab2hex(res.devices[0].advertisData))
}
}
})
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.getBluetoothDevices | ✔️ |