Bluetooth link in WeChat applet
Jun 22, 2018 pm 04:33 PMThis article mainly introduces the relevant information of the Bluetooth link of the WeChat applet. I hope that through this article, everyone can master the development method of the Bluetooth applet. Friends in need can refer to it
WeChat applet Bluetooth link of the program
WeChat applet Bluetooth connection 2.0 description:
1. This version distinguishes different ways of Bluetooth connection under ANDROID and IOS systems.
2. Links compatible with more situations include:
(1) The device Bluetooth is not turned on, and the connection will automatically start when Bluetooth is turned on.
(2) Automatically reinitialize the Bluetooth adapter every 3000ms after failing to initialize Bluetooth.
(3) Scanning the Bluetooth adapter on the Android side failed and will automatically restart every 3000ms.
(4) The IOS side obtains the connected Bluetooth device as empty, and automatically reacquires every 3000ms.
(5) The Android Bluetooth connection interrupts scanning after starting the connection. If the connection fails, start scanning again.
(6) After the IOS side starts to connect to the device, it will stop acquiring the connected device. If the connection fails, it will automatically restart the acquisition.
(7) After the connection is successful, turn off the system Bluetooth and reset the Bluetooth adapter.
(8) After the connection is successful, turn off the system Bluetooth, turn on Bluetooth again, and automatically restart the connection.
(9) After the connection is successful, turn off the target Bluetooth device and automatically restart scanning (acquisition).
(10) After the connection is successful, minimize the applet (the connection is not interrupted), open the applet and it will show that it is connected.
(11) After the connection is successful, kill the applet process, close the connection, and automatically restart scanning (acquisition).
3. I will update when I remember it....
4. Flowchart, whoever has time can help me draw it tomorrow or the day after tomorrow or...
My connection is made in App.js.
The onLaunch trigger in App.js is to call the init() method.
init code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Description:
1, serviceId_2~6 are the ones I know You can write only one serviceId for the Bluetooth device to be connected.
2. characterId_write is the characteristic value of the Bluetooth device I know I want to connect to write data to.
3. characterId_read is the characteristic value of the Bluetooth device I know I want to connect to read data.
(The above three are for comparison, the actual operation is based on the obtained sericeid and characterid).
4. connectedDevice is the connected device information object.
After init is completed, start calling the connection startConnect();
startConnect code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Note: There are comments in this section, so I won’t go into details, it’s relatively simple.
Call the getBluetoothAdapterState() method after successfully initializing the Bluetooth adapter state.
getBluetoothAdapterState Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Description: This method is used to obtain the current Bluetooth status.
The judegIfDiscovering method is called when it is detected that Bluetooth is available.
judegIfDiscoveringCode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Description:
1. This method is Used to determine whether scanning is in progress.
2. isAndroidPlatform is obtained through getSystemInfo of the applet to determine whether it is an Android device or an IOS device.
If it is an Android device, call startBluetoothDevicesDiscovery() to start scanning. If it is an IOS device, call getConnectedBluetoothDevices() to start acquiring paired Bluetooth devices.
startBluetoothDevicesDiscovery code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Instructions:
1. Only on Android devices Turn on scanning for nearby Bluetooth devices.
2. In the successful callback, enable event monitoring onBluetoothDeviceFound() for discovering new Bluetooth devices.
onBluetoothDeviceFound Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Description:
1. Here is the Discovered Bluetooth devices are filtered based on the name attribute.
2. When the device containing the name attribute of the device that needs to be connected is filtered out and the deviceId is obtained, the startConnectDevices() method is called to start the connection.
startConnectDevices code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Instructions:
1. After opening the connection Terminate scan (get paired) method.
2. Create a low-power Bluetooth connection based on deviceId. If the connection is successful, continue with subsequent read and write operations.
3. If the connection fails, call startBluetoothDevicesDiscovery() or getConnectedBluetoothDevices() according to the device system;
getConnectedBluetoothDevices code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Note: If the acquisition of Bluetooth paired Bluetooth devices fails, or the obtained list is empty, use failToGetConnected();
failToGetConnected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Description:
1. The devices returned after the method is successfully called is an array containing multiple Bluetooth devices that have been paired with the system.
2. If the devices list is obtained, call the loopConnect() method to start recursively calling the Bluetooth device.
loopConnect code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Note: looConnect will delete the first value of the array after the connection method of creating a connection fails, and then continue to call this method until all devices in it have been connected.
Almost missed it: call the init() method in onShow of app.js.
Special Note:
1. Different methods are recommended for Bluetooth connection on Android and IOS in the current version. The Android device directly uses the Bluetooth connection of the applet to cancel system pairing. IOS devices can be successfully connected in seconds after system pairing and opening the mini program.
2. The connection of this version still needs to be improved. The connection will not be automatically terminated (you can add it yourself if needed), and will scan and reconnect infinitely until successful.
3. Operation after the link is successful. If writing data and turning on notify need to be done at the same time, it is recommended to write first and then turn on notify. (The reason is unknown, otherwise a 10008 error will occur).
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Simple example of shopping cart in WeChat mini program
How to implement Meituan menu in WeChat mini program
About the steps for login authentication of WeChat applet
The above is the detailed content of Bluetooth link in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solve the problem of playing headphones and speakers at the same time in win11

How to turn on Bluetooth in vivo phone

Xianyu WeChat mini program officially launched

There is no Bluetooth module in win11 system device manager

How to solve the problem of Harry Potter curse swap not using Bluetooth

Does Bluetooth 5.3 require mobile phone support? For details please see

Solution to the problem of Bluetooth being unable to connect in Win11 system

Samsung brings Bluetooth Auracast audio functionality to multiple Galaxy phones and tablets
