Skip to main content
Version: 3.x

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.

Reference

Type

(option?: Option) => Promise<SuccessCallbackResult>

Parameters

Option

PropertyTypeRequiredDescription
complete(res: any) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: any) => voidNoThe callback function for a failed API call
success(res: Result) => voidNoThe callback function for a successful API call

SuccessCallbackResult

PropertyTypeDescription
devicesSuccessCallbackResultBlueToothDevice[]The list of connected devices corresponding to the UUIDs
errMsgstring成功:ok,错误:详细信息

SuccessCallbackResultBlueToothDevice

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.getBluetoothDevices({
success: function (res) {
console.log(res)
if (res.devices[0]) {
console.log(ab2hex(res.devices[0].advertisData))
}
}
})

API Support

APIWeChat Mini-ProgramH5React Native
Taro.getBluetoothDevices✔️