Home > Article > WeChat Applet > A brief discussion on the analysis and solutions of Bluetooth connection errors in small program development
When you turn on Bluetooth on your phone and try to connect to Bluetooth, you will be unable to connect and report error 10003. At this time, no matter whether you restart the applet or turn Bluetooth off again, you cannot connect to the Bluetooth device normally. . 10003 is a problem often encountered in WeChat Bluetooth connections. The official document provided by WeChat simply describes it as "connection fail", with a note of "connection failure". However, there are actually many situations where connection failure occurs. For example, the Bluetooth device is occupied or the last Bluetooth connection was not disconnected, resulting in the inability to connect; when connecting to Bluetooth based on the deviceId, the Bluetooth device is not turned on or is abnormal, resulting in the inability to connect.
Since it is caused by not disconnecting the Bluetooth connection, disconnect the Bluetooth when turning off the Bluetooth of the mobile phone; however, calling the disconnect function in the Bluetooth status monitoring returns an error (10001 (not available) The Bluetooth adapter is currently unavailable).
I didn’t find a ready-made solution, so I could only trace the code and add logs to the key methods; I found that when turning off the Bluetooth of the mobile phone in the top bar, the Bluetooth connection could not be disconnected in the applet, and an error (10001 (not available) The current Bluetooth adapter is not available); later, by accident, I cleared the binding relationship of the Bluetooth device, re-entered the Bluetooth search page, and found that I could successfully connect to Bluetooth after doing this.
In locating the above problem, it can be found that it should be caused by not disconnecting the last Bluetooth connection. Re-searching for Bluetooth and then connecting again can solve the problem; so we can only consider reconnecting. How to search for Bluetooth and then connect again? After the 10003 error occurs, search for Bluetooth again, and make a Bluetooth connection after finding the corresponding device. Modify the code and test repeatedly. According to the log, 10003 does appear. Then enter the search module and search After reaching the designated device, perform a Bluetooth connection, and then the connection is successful. This solution solves the problem.
wx.createBLEConnection({ deviceId: deviceId, success: function (res) { //连接成功 initnotifyCharacteristic(notifyCharacteristic);// 指定特征值,并进行数据交互 // 已连接 _bthConnectStaus = BTH_STATUS_CONNECTED; }, fail: function (res) {// 连接蓝牙失败 _bthConnectStaus = BTH_STATUS_DISCONNECT; // 回调上层蓝牙连接失败 callback(res.errCode, connectfailed) }
Then determine whether it is 10003 based on the res.errCode in fail.
if(code ==10003) { // 部分android手机特殊情况下需要重新搜索才能连接蓝牙,此时报错10003,进行蓝牙搜索(ps:原因可能是系统中将手机蓝牙关闭导致连接状态不能改为断开,导致下次无法连接同一个设备) var timeId = setTimeout(function () { stopSearchBluetooth(); callback(false, timout) }, 5000); // 最多搜索5s searchBluetooth(function (res) { if (res.devices === undefined ||res.devices === null) { return; } for (var i = 0; i < res.devices.length; i++) { if (res.devices[i] &&res.devices[i].deviceId == deviceId) { // 搜索到该设备 console.log(searchDeviceAndReConnect:find device and re connect); clearTimeout(timeId); stopSearchBluetooth() // 停止搜索 callback(true, finddevice);// 找到设备,在回调函数中连接蓝牙 break; } } }, function (res) { clearTimeout(timeId); stopSearchBluetooth() // 停止搜索 callback(false, searchBluetoothfail) }); }
The above code provides a simple process. When a 10003 error occurs in the connection, search for the Bluetooth device, and after finding it, reconnect to the Bluetooth.
Of course, 10003 is not the only error reported for this problem. All, 10003 is also reported when the device does not turn on Bluetooth. And because of the addition of search logic, the reminder when the device does not turn on Bluetooth will be slower ( If the device Bluetooth is not turned on, the user needs to be reminded to turn on the device Bluetooth), but fortunately this has little impact. There is currently no good idea to solve it, and we will study it slowly in the future.
Considering that only some mobile phones have the 10003 error, they will connect to Bluetooth once before reporting error 10003, so as to avoid other mobile phones from slowing down the speed of connecting to Bluetooth to collect data due to searching for Bluetooth; 10003 occurs in many cases. However, it can basically be determined by whether the last Bluetooth connection ended. Re-searching for Bluetooth and then connecting is also based on the fact that the last Bluetooth connection was not disconnected.
Recommendation: " Mini Program Development Tutorial"
The above is the detailed content of A brief discussion on the analysis and solutions of Bluetooth connection errors in small program development. For more information, please follow other related articles on the PHP Chinese website!