Skip to content

Commit 7cb93a0

Browse files
committed
Add error handling mechanism.
1 parent 52f724d commit 7cb93a0

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mcblekit",
3-
"version": "1.0.3",
3+
"version": "1.0.5",
44
"description": "该微信小程序开源代码库用于管理微信小程序中的蓝牙功能。支持初始化蓝牙适配器、扫描和连接蓝牙设备、获取设备服务和特征、监听特征值变化、读写特征值以及断开连接等操作。通过设置不同的监听器,可灵活处理蓝牙连接状态变化、设备发现、服务和特征发现等事件,适用于需要与蓝牙设备进行数据交互的微信小程序开发场景。This WeChat mini program open-source code library is used to manage the Bluetooth function in WeChat mini programs. Support initialization of Bluetooth adapters, scanning and connecting Bluetooth devices, obtaining device services and features, monitoring feature value changes, reading and writing feature values, and disconnecting. By setting different listeners, it is possible to flexibly handle events such as changes in Bluetooth connection status, device discovery, service and feature discovery, which is suitable for WeChat mini program development scenarios that require data interaction with Bluetooth devices. ",
55
"main": "src/MCBleKit.js",
66
"scripts": {

src/MCBleKit.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,18 @@ MCBleKit.prototype.startBluetoothDevicesDiscovery = function () {
202202
var that = this;
203203
wx.startBluetoothDevicesDiscovery({
204204
success: function (res) {
205+
console.log('🔍 开始扫描设备', res)
205206
that.onBluetoothDeviceFound();
206207
that.startDiscoverListener();
207208
},
208209
fail: function (res) {
210+
console.log('❌ 扫描设备失败', res)
211+
if (res.errCode === 10004) {
212+
wx.showToast({
213+
title: '当前设备不支持蓝牙',
214+
icon: 'none',
215+
})
216+
}
209217
console.log('startBluetoothDevicesDiscovery fail' + res.errMsg);
210218
}
211219
});
@@ -262,7 +270,7 @@ MCBleKit.prototype.connectToBluetoothDevice = function () {
262270
wx.createBLEConnection({
263271
deviceId: this.bleDevice.deviceId,
264272
success: function (res) {
265-
console.log('连接成功', res);
273+
console.log('连接设备成功', res);
266274
that.connected = true;
267275
wx.setBLEMTU({
268276
deviceId: that.bleDevice.deviceId,
@@ -278,6 +286,7 @@ MCBleKit.prototype.connectToBluetoothDevice = function () {
278286
that.getBLEDeviceServices();
279287
},
280288
fail: function (res) {
289+
console.log('❌ 连接设备失败', res)
281290
if (res.errno == 1509001 && res.errCode == 10003 && util.containsIgnoreCase(res.errMsg, 'status:133')) {
282291
wx.openBluetoothAdapter({
283292
success: function (res) {

src/MCUtil.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,42 @@ function isEqualIgnoreCase(str1, str2) {
3535
return str1.toUpperCase() === str2.toUpperCase();
3636
}
3737

38+
function handleBluetoothError(err) {
39+
const code = err.errCode
40+
let message = '发生未知错误'
41+
42+
const errorMap = {
43+
10000: '未初始化蓝牙适配器',
44+
10001: '当前蓝牙不可用',
45+
10002: '没有找到指定设备',
46+
10003: '连接失败',
47+
10004: '没有找到指定服务',
48+
10005: '没有找到指定特征值',
49+
10006: '当前连接已断开',
50+
10007: '当前特征值不支持此操作',
51+
10008: '其余所有系统上报的异常',
52+
10009: 'Android 系统特有,系统版本低于 4.3 不支持 BLE',
53+
10012: '连接超时',
54+
10013: '连接 deviceId 为空或者是格式不正确',
55+
}
56+
57+
if (errorMap[code]) {
58+
message = errorMap[code]
59+
}
60+
61+
wx.showToast({
62+
title: message,
63+
icon: 'none',
64+
})
65+
66+
console.error('蓝牙错误:', code, message)
67+
}
68+
3869
module.exports = {
3970
isEmptyStr: isEmptyStr,
4071
contains: contains,
4172
isEqualIgnoreCase: isEqualIgnoreCase,
4273
isNullObject: isNullObject,
43-
containsIgnoreCase: containsIgnoreCase
74+
containsIgnoreCase: containsIgnoreCase,
75+
handleBluetoothError: handleBluetoothError
4476
};

0 commit comments

Comments
 (0)