# Bluetooth ® (Bluetooth ®)

Weixin Mini Program The Bluetooth capabilities of each platform are provided to developers through a unified interface package. With the Bluetooth interface of the Mini Program, developers can exchange data wirelessly with other Bluetooth devices.

# 1. Overview of Bluetooth capabilities

Bluetooth Technical Specification by the Bluetooth Special Interest Group (SIG)Developers can access detailed technical documentation on their official website .

There are currently two specifications in which Bluetooth is most commonly used:

  • Bluetooth Basic Rate / Enhanced Data Rate (BR / EDR) : Also known as Classic Bluetooth.It is often used in scenes where there are certain requirements for data transmission bandwidth, such as Bluetooth speakers and Bluetooth headsets that need to transmit audio data;
  • Bluetooth Low Energy (Bluetooth Low Energy, BLE) :The protocol supported since Bluetooth 4.0, which is characterized by extremely low power consumption and faster transmission speed, is commonly used in various smart electronics products with high battery life requirements and only small data transfer, such as smart wearables, smart appliances, sensors, etc., and has a wide range of applications.

# 2. Bluetooth capability in Weixin Mini Program

In Weixin Mini Program, to use Bluetooth capabilities (other than Beacon) you must first call wx.openBluetoothAdapter Initial BluetoothAdapter module, whose active period is from calling wx.openBluetoothAdapter to calling [[ TAG-2-START]] wx.closeBluetoothAdapter]((wx.closeBluetoothAdapter)) or Mini Program until it is destroyed. Only when the Mini Program Bluetooth adapter module is in effect can developers properly call the Bluetooth-related Mini Program API and receive event callbacks related to the Bluetooth module (binding listening is not limited to this).

Weixin Mini Program Bluetooth support is as follows:

  • Classic Bluetooth : iOS due to system restrictions temporarily can not be provided, Android is currently in the planning.
  • ** Bluetooth Low Energy (BLE) ** :
    • Host mode: Base library 1.1.0 (WeChat Guest iOS 6.5.6, Android 6.5.7) is supported.
    • From machine mode: base library 2.10.3 Start support.
    • ** Bluetooth Beacon (Beacon) ** : Basic library 1.2.0 supports.

# 3. Note

Due to the differences in the implementation and limitations of the Bluetooth protocol stack across platforms, developers need to pay attention to the following aspects when using Weixin Mini Program Bluetooth capabilities:

# 3.1 Device ID (deviceId)

Each Bluetooth peripheral equipment is identified by a uniquedevice Id.Because of the limitations of some implementations, the device Id [[TAG-1END]] scanned on different central devices may be variable for the same Bluetooth peripheral equipment.ThereforedeviceId`cannot be hardcoded into code.

  • Android devices scanned access todeviceIdfor the MAC address of peripheral equipment, relatively fixed;
  • ` is a UUID generated by the system based on the MAC address of the peripheral equipment and when the device was discovered.For connected devices, the UUID stays the same for some time. In addition, the UUID may change under certain conditions (such as the system blue tooth module restart, the paired device is ignored, etc.), and the UUID obtained on different devices is also different.

# 3.2 debugging

There are also large differences in the Bluetooth implementation of different platforms.Weixin Mini Program will provide a complete system Bluetooth capability as much as possible on the basis of providing a unified interface, weakening the differences between different platforms.

However, due to the limitations of the operating system itself, some capabilities cannot be guaranteed to be completely consistent, so developers should take note of the notes in the documentation and debug them on each end of the real machine. Only some of the Bluetooth interface capabilities can be simulated in the developer tool, and the full functionality should be debugged using the real machine.

# sample code

Preview with Developer Tool