|
@@ -41,6 +41,8 @@
|
41
|
41
|
deviceId: '', //蓝牙设备ID
|
42
|
42
|
serviceId: '', //服务UUID
|
43
|
43
|
characteristicId: '', //特征值
|
|
44
|
+ connected: false, //是否连接
|
|
45
|
+ isUpdateData:false,//是否是返回蓝牙匹配
|
44
|
46
|
};
|
45
|
47
|
},
|
46
|
48
|
created() {
|
|
@@ -49,6 +51,29 @@
|
49
|
51
|
// 获取蓝牙监听
|
50
|
52
|
this.stateChange()
|
51
|
53
|
},
|
|
54
|
+ onShow() {
|
|
55
|
+ let that = this
|
|
56
|
+ uni.$on('updateData', function(data) {
|
|
57
|
+ if (data) {
|
|
58
|
+ this.isUpdateData = true
|
|
59
|
+ // console.log('触发了');
|
|
60
|
+ // 断开蓝牙
|
|
61
|
+ if (that.deviceId) {
|
|
62
|
+ that.closeBLEConnection(that.deviceId)
|
|
63
|
+ }
|
|
64
|
+ that.deviceId = ''
|
|
65
|
+ that.serviceId = ''
|
|
66
|
+ that.characteristicId = ''
|
|
67
|
+ }
|
|
68
|
+ })
|
|
69
|
+ },
|
|
70
|
+ onHide() {
|
|
71
|
+ // 移除监听事件
|
|
72
|
+ uni.$on('updateData', function(data) {
|
|
73
|
+ uni.$off('updateData');
|
|
74
|
+ this.isUpdateData = false
|
|
75
|
+ })
|
|
76
|
+ },
|
52
|
77
|
methods: {
|
53
|
78
|
// 蓝牙报错提示
|
54
|
79
|
blueError(code) {
|
|
@@ -83,16 +108,16 @@
|
83
|
108
|
case 10009:
|
84
|
109
|
this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
|
85
|
110
|
break
|
86
|
|
- case 100010:
|
|
111
|
+ case 10010:
|
87
|
112
|
this.errorToast('已连接')
|
88
|
113
|
break
|
89
|
|
- case 100011:
|
|
114
|
+ case 10011:
|
90
|
115
|
this.errorToast('配对设备需要配对码')
|
91
|
116
|
break
|
92
|
|
- case 100012:
|
|
117
|
+ case 10012:
|
93
|
118
|
this.errorToast('连接超时')
|
94
|
119
|
break
|
95
|
|
- case 100013:
|
|
120
|
+ case 10013:
|
96
|
121
|
this.errorToast('连接 deviceld 为空或者是格式不正确')
|
97
|
122
|
break
|
98
|
123
|
}
|
|
@@ -171,26 +196,37 @@
|
171
|
196
|
// 找到新设备就触发该方法
|
172
|
197
|
found(res) {
|
173
|
198
|
if (res.devices[0].name.includes('BT24')) {
|
174
|
|
- this.blueDeviceList.push(res.devices[0])
|
|
199
|
+ this.blueDeviceList.push(res.devices[0])
|
|
200
|
+ }
|
|
201
|
+ // 判断是否连接
|
|
202
|
+ if (this.connected) {
|
|
203
|
+ return
|
175
|
204
|
}
|
176
|
205
|
// 检查本地是否有id
|
177
|
206
|
let deviceId = uni.getStorageSync('deviceId')
|
178
|
|
- if(deviceId){
|
179
|
|
- // 重置deviceId
|
180
|
|
- this.deviceId = ''
|
181
|
|
- let data = {
|
182
|
|
- deviceId
|
|
207
|
+ if (deviceId) {
|
|
208
|
+ let list = []
|
|
209
|
+ this.blueDeviceList.forEach(item => {
|
|
210
|
+ list.push(item.deviceId)
|
|
211
|
+ })
|
|
212
|
+ if (list.includes(deviceId)) {
|
|
213
|
+ // 重置deviceId
|
|
214
|
+ this.deviceId = ''
|
|
215
|
+ let data = {
|
|
216
|
+ deviceId
|
|
217
|
+ }
|
|
218
|
+ this.connect(data)
|
183
|
219
|
}
|
184
|
|
- this.connect(data)
|
185
|
220
|
}
|
186
|
221
|
},
|
187
|
222
|
//监听蓝牙状态
|
188
|
223
|
stateChange() {
|
189
|
224
|
uni.onBLEConnectionStateChange(function(res) {
|
190
|
225
|
// 该方法回调中可以用于处理连接意外断开等异常情况
|
|
226
|
+ let connected = res.connected
|
191
|
227
|
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`,
|
192
|
228
|
'监听到蓝牙设备变化')
|
193
|
|
- if (!res.connected) {
|
|
229
|
+ if (!res.connected&&!this.isUpdateData) {
|
194
|
230
|
console.log(111111, '断开重连操作');
|
195
|
231
|
let data = {
|
196
|
232
|
deviceId: this.deviceId
|
|
@@ -235,7 +271,7 @@
|
235
|
271
|
that.deviceId = data.deviceId
|
236
|
272
|
uni.createBLEConnection({
|
237
|
273
|
deviceId: that.deviceId,
|
238
|
|
- timeout: 30000,
|
|
274
|
+ timeout: 20000,
|
239
|
275
|
success(res) {
|
240
|
276
|
wx.hideLoading()
|
241
|
277
|
uni.showToast({
|
|
@@ -258,6 +294,7 @@
|
258
|
294
|
switch (err.errMsg) {
|
259
|
295
|
default:
|
260
|
296
|
that.blueError(err.errCode)
|
|
297
|
+ that.deviceId = ''
|
261
|
298
|
}
|
262
|
299
|
console.log('连接失败')
|
263
|
300
|
console.error(err)
|
|
@@ -377,7 +414,7 @@
|
377
|
414
|
}
|
378
|
415
|
|
379
|
416
|
.item:nth-child(2n) {
|
380
|
|
- background-color: aqua;
|
|
417
|
+ background-color: #1e98d7;
|
381
|
418
|
}
|
382
|
419
|
}
|
383
|
420
|
|
|
@@ -393,4 +430,4 @@
|
393
|
430
|
justify-content: flex-end;
|
394
|
431
|
}
|
395
|
432
|
}
|
396
|
|
-</style>
|
|
433
|
+</style>
|