title: Taro.getBluetoothDevices(option)

sidebar_label: getBluetoothDevices

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.

Reference

Type

  1. (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

  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.getBluetoothDevices({
  12. success: function (res) {
  13. console.log(res)
  14. if (res.devices[0]) {
  15. console.log(ab2hex(res.devices[0].advertisData))
  16. }
  17. }
  18. })

API Support

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