Introduction
We encapsulate the Bluetooth capabilities of each platform and provide them to developers through a unified interface. Using the Bluetooth APIs, developers can wirelessly exchange data with other Bluetooth devices.
1. Overview of Bluetooth Capabilities
The Bluetooth technical specifications are developed by the Bluetooth Special Interest Group (SIG). Developers can obtain detailed technical documentation on its official website.
Currently, there are two most commonly used Bluetooth specifications:
- Bluetooth Basic Rate/Enhanced Data Rate (BR/EDR): Also known as Classic Bluetooth. It is commonly used in scenarios with certain requirements on data transmission bandwidth, such as Bluetooth speakers and Bluetooth headsets that need to transmit audio data;
- Bluetooth Low Energy (BLE): A protocol supported since Bluetooth 4.0. Its characteristics are extremely low power consumption and faster transmission speed. It is commonly used in various smart electronic products that have high battery-life requirements and only need to transmit small amounts of data, such as smart wearable devices, smart home appliances, and sensors, with a wide range of application scenarios.
2. Bluetooth Capabilities in Minigames
In a minigame, to use Bluetooth capabilities (except Beacon), you must first initialize the Bluetooth adapter module. Only while the Bluetooth adapter module is active can developers properly call the Bluetooth-related APIs and receive the event callbacks related to the Bluetooth module (binding listeners is not subject to this restriction).
3. Notes
Since each platform's implementation and restrictions of the Bluetooth protocol stack differ significantly, developers still need to pay attention to the following aspects when using the minigame Bluetooth capabilities:
3.1 Device ID (deviceId)
Each Bluetooth peripheral device is identified by a unique deviceId. Due to the limitations of some system implementations, for the same Bluetooth peripheral device, the deviceId obtained by scanning on different central devices may vary. Therefore, the deviceId must not be hard-coded into the code.
- The
deviceIdobtained by scanning on Android devices is the peripheral device's MAC address, which is relatively fixed; - The
deviceIdobtained by scanning on iOS devices is a UUID generated by the system based on the peripheral device's MAC address and the time the device was discovered. For previously connected devices, the UUID remains unchanged for a period of time. In addition, the UUID may also change under certain conditions (such as the system Bluetooth module restarting, a paired device being ignored, etc.), and the UUID obtained on different devices is also different.
3.2 Debugging
The Bluetooth implementations of different platforms also differ significantly. On the basis of providing a unified interface, the minigame will provide the complete system Bluetooth capabilities as much as possible, minimizing the implementation differences between platforms.
However, due to the limitations of the operating systems themselves, some capabilities cannot be guaranteed to be completely consistent. Please pay attention to the notes in the documentation and debug on real devices on all platforms. The developer tools can only simulate part of the Bluetooth API capabilities; for full functionality, please debug on a real device.
