# Bluetooth (Bluetooth)
Mini Programs provide the Bluetooth capability of each platform through a unified interface package to developers. Using the Mini Program's Bluetooth interface, developers can wirelessly exchange data with other Bluetooth devices.
# 1. Overview of Bluetooth Capabilities
Bluetooth specification by Bluetooth Technology Alliance (Bluetooth Special Interest Group, SIG) Formulated, the developer can be identified in itsThe official websiteAccess to detailed technical documentation.
There are two specifications that Bluetooth is most commonly used:
- Bluetooth base rate/Enhanced data rate (Bluetooth Basic Rate/Enhanced Data Rate, BR/EDR): Also known as Classic Bluetooth. Commonly used in scenes that have certain requirements for data transmission bandwidth, such as Bluetooth speakers, Bluetooth headphones, etc. that need to transmit audio data.
- Bluetooth Low Energy (Bluetooth Low Energy, BLE): From Bluetooth 4.0 The supported protocols are characterized by extremely low power consumption and faster transmission speed, and are commonly used in various intelligent electronic products that require high battery life and only need small data transmission, such as smart wearable devices, smart appliances, sensors, etc., with a wide range of application scenarios.
# 2. Bluetooth Capability in Mini Program
In the Mini Program, to use the Bluetooth capability (Beacon Except) must first be called wx.openBluetoothAdapter Initializes the Bluetooth adapter module with an effective period of calling the wx.openBluetoothAdapter To call wx.closeBluetoothAdapter Or Mini programs are destroyed. Only when the Mini Program Bluetooth adapter module is in effect, the developer can call the Bluetooth-related Mini Program normally API and receives callbacks of events related to the Blue Tooth module (binding listening is not limited by this).
Mini Program for Bluetooth support is as follows:
- Classic Bluetooth:iOS Due to system limitations, Android is currently in planning.
- Bluetooth Low Energy (BLE):
- Host Mode: Base Library 1.1.0 (WeChat Client) iOS 6.5.6,Android 6.5.7) Start support.
- Slave mode: base library 2.10.3 Start supporting.
- Bluetooth beacon (Beacon): Base Library 1.2.0 Start supporting.
# 3. Note
Due to the large differences in the implementation and limitations of the Bluetooth protocol stack on various platforms, developers need to pay attention to the following aspects when using the Bluetooth capability of the Mini Program:
# 3.1 equipment ID (deviceId)
Each Bluetooth peripheral equipment has a unique deviceId
To identify. Due to the limitation of some system implementations, for the same Bluetooth peripheral equipment, the deviceId
It may have changed. therefore deviceId
Cannot be hard-coded into the code.
- Android From the device scan.
deviceId
For peripheral equipment MAC Address, relatively fixed - iOS From the device scan.
deviceId
It's based on peripheral equipment. MAC Address and time of discovery of the device generated by the UUID。For connected devices, UUID Will remain the same for some time. In addition, UUID It may also change under certain conditions (such as system Bluetooth module reboot, pairing device ignored, etc.) UUID Is different.
# 3.2 debugging
The implementation of Bluetooth in different platforms is also quite different. The Mini Program will provide a complete system Bluetooth capability as much as possible on the basis of providing a unified interface, weakening the implementation differences of different platforms.
However, due to the limitations of the operating system itself, some of the capabilities can not be guaranteed to be completely consistent, please pay attention to the notes in the document, and debug the real machine at each end. Developer tools can only simulate part of the Bluetooth interface capabilities, complete functions please use the real machine debugging.
# sample code
{% Minicode ('OF4Y9Gme6rZ4') %}