Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve Mini Program Bluetooth connection 10003

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to solve Mini Program Bluetooth connection 10003. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

10003 is a common problem encountered in Wechat Bluetooth connection. The official document given by Wechat is simply described as "connection fail" and remarked as "connection failure". However, in fact, there are many 10003 situations, such as when connecting Bluetooth according to deviceId, the Bluetooth device is not turned on or abnormal so that the connection cannot be connected; the Bluetooth device is occupied or the last Bluetooth connection is not disconnected After the error, I went to the official community of Wechat for help, and found that the problem was full of feedback on 4 pages (40 bonus items) in the community, but there was no official answer, and some of the ones that were solved were not applicable to my situation. Therefore, only my own analysis.

Problem description:

1. After starting app and successfully searching and connecting Bluetooth, the data collected for the first time is normal.

two。 Next time, if the Bluetooth of the phone is on, it can be collected normally.

3. However, if the Bluetooth of the mobile phone is turned off, click to collect data to try to connect, prompting the user to turn on the Bluetooth of the mobile phone; when the Bluetooth of the mobile phone is turned on, the Bluetooth will not be connected, and an error of 10003 will be reported.

4. Later, we will be unable to connect to Bluetooth, whether it is to restart Mini Program or turn on Bluetooth again, we will not be able to connect to the Bluetooth device properly.

Problem positioning

1. Reproduce the problem, according to the method provided by the test, first test iphone is normal, Samsung S7edge, the result is a normal connection to Bluetooth, and then test vivo x7plus and Xiaomi MI5, and finally successfully reproduce the problem

two。 Look for a solution to the 10003 error in the Wechat development community, but faced with dozens of similar problems, almost all of them have not been solved. A few of them said that calling wx.closeBLEConnection to disconnect the last time before connecting could not solve my problem after trying.

3. Since no off-the-shelf solution has been found, we have to track the code and add a log to the key method. It is found that when Bluetooth is turned off in the top column, the Bluetooth connection cannot be disconnected in Mini Program and an error is reported (10001 (not available) the current Bluetooth adapter is not available)

4. Later, by chance, I cleared the binding relationship of the Bluetooth device and re-entered the Bluetooth search page, and found that I could successfully connect to Bluetooth after doing so.

Problem repair

In the location of the above problems, (3) it can be found that it should not be caused by disconnecting the last Bluetooth connection, and (4) it is also found that reconnecting after searching Bluetooth can solve the problem.

So follow these two directions to find a solution:

1. Since the Bluetooth connection is not disconnected, disconnect Bluetooth when the phone Bluetooth is turned off; however, calling the disconnect function during Bluetooth status monitoring returns an error (10001 (not available) the current Bluetooth adapter is not available)

two。 Therefore, we can only consider the method of re-searching Bluetooth and then connecting again. After a 10003 error occurs, we search Bluetooth again and again, and connect with Bluetooth after finding the corresponding device; modify the code to test repeatedly, and according to the log, there is indeed 10003. Then enter the search module, find the specified device and connect to Bluetooth, and then connect successfully. This solution solves the problem.

Wx.createBLEConnection ({

DeviceId: deviceId

Success: function (res) {/ / connected successfully

InitnotifyCharacteristic (notifyCharacteristic); / / specify eigenvalues and exchange data

/ / connected

_ bthConnectStaus = BTH_STATUS_CONNECTED

}

Fail: function (res) {/ / failed to connect to Bluetooth

_ bthConnectStaus = BTH_STATUS_DISCONNECT

/ / callback upper layer Bluetooth connection failed

Callback (res.errCode, "connect failed")

}

Then judge whether it is 10003 according to the res.errCode in fail.

If (code = = 10003) {/ / some android phones need to be searched again to connect to Bluetooth under special circumstances. An error of 10003 is reported at this time, and Bluetooth search is carried out (ps: the reason may be that the connection status of the phone cannot be disconnected due to the Bluetooth off in the system, so that the same device cannot be connected next time)

Var timeId = setTimeout (function () {

StopSearchBluetooth ()

Callback (false, "timout")

}, 5000); / / search for up to 5s

SearchBluetooth (function (res) {

If (res.devices = undefined | | res.devices = null) {

Return

}

For (var I = 0; I < res.devices.length; iTunes +) {

If (res.devices [I] & & res.devices [I] .deviceId = = deviceId) {/ / found the device

Console.log ("searchDeviceAndReConnect:find device and re connect")

ClearTimeout (timeId)

StopSearchBluetooth () / / stop searching

Callback (true, "find device"); / / find the device and connect Bluetooth in the callback function

Break

}

}

}, function (res) {

ClearTimeout (timeId)

StopSearchBluetooth () / / stop searching

Callback (false, "searchBluetooth fail")

});

}

The above code provides a simple process, when a 10003 error occurs in the connection, search for the Bluetooth device, find it, and reconnect Bluetooth.

Considering that only some phones have a 10003 error, Bluetooth will be connected once and error 10003 will be reported before doing so, so that other phones will not slow down the speed of data collection by connecting Bluetooth due to Bluetooth search.

Of course, 10003, not only the problem was reported wrong, but also reported 10003 when the device did not turn on Bluetooth, and because of the addition of search logic, the reminder will be slower when the device does not turn on Bluetooth (the device Bluetooth does not open need to remind the user to open the device Bluetooth), but fortunately, this point does not have much impact, there is no good idea to solve, and then slowly study.

This method only solves my situation, and there are many cases in 10003, but basically we can locate it from the end of the last Bluetooth connection, and the connection after re-searching Bluetooth is also based on the fact that the last Bluetooth connection was not disconnected.

Related problems

1. WeChat Mini Programs Bluetooth connection only supports 4. 0, while BLE low-power Bluetooth devices have positioning function, so some mobile phones (such as vivo x7plus) need to turn on mobile positioning function to search for Bluetooth devices. Baidu "Android6.0 Bluetooth BLE connection, location dynamic permissions related"

two。 According to services, when you call wx.startBluetoothDevicesDiscovery to search for Bluetooth, if you can't find it, you can try to remove services. One of my test machines (Huawei P6 in my impression) can't find the device with services. In the end, you can only remove services and filter in the search results.

When 3.wx.startBluetoothDevicesDiscovery searches for Bluetooth, most people know to use wx.onBluetoothDeviceFound, and then some phones cannot get all the devices in the second search onBluetoothDeviceFound callback in a short time. At this time, you can try wx.getBluetoothDevices. This method can obtain the cached Bluetooth device. This part of the cached Bluetooth device will no longer appear in wx.onBluetoothDeviceFound in some phones.

4. After connecting to Bluetooth, if the write fails during the write operation, please check whether characteristicId is set. This eigenvalue should be taken with each write operation.

There are many Android models, and BLE low-power Bluetooth is not very stable, which leads to many strange problems in the development of Bluetooth on Android, while WeChat Mini Programs based on Android and IOS, there will also be some problems accordingly. IOS is good, the system is single in hardware and software, but there are many problems in Android. Before the development, there are some problems that need to be paid attention to. I can't remember it for the time being, and I'll add it later.

These are all the contents of this article entitled "how to solve Mini Program Bluetooth connection 10003". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report