yuhao 1 år sedan
förälder
incheckning
f89eb8d1b3
2 ändrade filer med 63 tillägg och 23 borttagningar
  1. 11 8
      pages/edit/edit.vue
  2. 52 15
      pages/home/home.vue

+ 11 - 8
pages/edit/edit.vue

@@ -154,16 +154,16 @@
154
 					case 10009:
154
 					case 10009:
155
 						this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
155
 						this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
156
 						break
156
 						break
157
-					case 100010:
157
+					case 10010:
158
 						this.errorToast('已连接')
158
 						this.errorToast('已连接')
159
 						break
159
 						break
160
-					case 100011:
160
+					case 10011:
161
 						this.errorToast('配对设备需要配对码')
161
 						this.errorToast('配对设备需要配对码')
162
 						break
162
 						break
163
-					case 100012:
163
+					case 10012:
164
 						this.errorToast('连接超时')
164
 						this.errorToast('连接超时')
165
 						break
165
 						break
166
-					case 100013:
166
+					case 10013:
167
 						this.errorToast('连接 deviceld 为空或者是格式不正确')
167
 						this.errorToast('连接 deviceld 为空或者是格式不正确')
168
 						break
168
 						break
169
 				}
169
 				}
@@ -178,7 +178,10 @@
178
 			},
178
 			},
179
 			// -----------------返回蓝牙匹配
179
 			// -----------------返回蓝牙匹配
180
 			back() {
180
 			back() {
181
-				uni.navigateBack()
181
+				uni.$emit('updateData', true)
182
+				uni.navigateBack({
183
+					delta: 1
184
+				})
182
 			},
185
 			},
183
 			// ------------------获取设备数据列表
186
 			// ------------------获取设备数据列表
184
 			async getTypeList() {
187
 			async getTypeList() {
@@ -188,7 +191,7 @@
188
 				// console.log(JSON.parse(JSON.stringify(res.data.list)), '获取的数据返回');
191
 				// console.log(JSON.parse(JSON.stringify(res.data.list)), '获取的数据返回');
189
 				// 筛选出无创水光
192
 				// 筛选出无创水光
190
 				for (let key in res.data.list) {
193
 				for (let key in res.data.list) {
191
-					if (res.data.list[key].id != 1 && res.data.list[key].id != 15) {
194
+					if (res.data.list[key].id != 1 && res.data.list[key].id != 15 && res.data.list[key].id != 10) {
192
 						this.typeList.push(res.data.list[key])
195
 						this.typeList.push(res.data.list[key])
193
 					}
196
 					}
194
 				}
197
 				}
@@ -206,7 +209,7 @@
206
 						this.selectModeIndexA = 99
209
 						this.selectModeIndexA = 99
207
 						this.equipmentNum = '' //重置探头
210
 						this.equipmentNum = '' //重置探头
208
 						this.modeNum = '' //重置模式或强度
211
 						this.modeNum = '' //重置模式或强度
209
-						// console.log(item)
212
+						console.log(item)
210
 						//赋值哪个厂家的设备
213
 						//赋值哪个厂家的设备
211
 						if (item.type_sn.includes('ZK')) {
214
 						if (item.type_sn.includes('ZK')) {
212
 							this.TYPE_SN = 'ZK'
215
 							this.TYPE_SN = 'ZK'
@@ -221,7 +224,7 @@
221
 						this.equipmentNum = Number(item.detector_sn).toString(16) < 10 ? '0' + Number(item.detector_sn)
224
 						this.equipmentNum = Number(item.detector_sn).toString(16) < 10 ? '0' + Number(item.detector_sn)
222
 							.toString(16) : Number(item.detector_sn).toString(16) //设备探头编号(16进制)
225
 							.toString(16) : Number(item.detector_sn).toString(16) //设备探头编号(16进制)
223
 						// console.log(this.equipmentNum)
226
 						// console.log(this.equipmentNum)
224
-						// console.log(item)
227
+						console.log(item)
225
 						this.minClass = item.config.power.min //最小值
228
 						this.minClass = item.config.power.min //最小值
226
 						this.maxClass = item.config.power.max //最大值
229
 						this.maxClass = item.config.power.max //最大值
227
 						this.incrementClass = item.config.power.increment //增幅
230
 						this.incrementClass = item.config.power.increment //增幅

+ 52 - 15
pages/home/home.vue

@@ -41,6 +41,8 @@
41
 				deviceId: '', //蓝牙设备ID
41
 				deviceId: '', //蓝牙设备ID
42
 				serviceId: '', //服务UUID
42
 				serviceId: '', //服务UUID
43
 				characteristicId: '', //特征值
43
 				characteristicId: '', //特征值
44
+				connected: false, //是否连接
45
+				isUpdateData:false,//是否是返回蓝牙匹配
44
 			};
46
 			};
45
 		},
47
 		},
46
 		created() {
48
 		created() {
@@ -49,6 +51,29 @@
49
 			// 获取蓝牙监听
51
 			// 获取蓝牙监听
50
 			this.stateChange()
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
 		methods: {
77
 		methods: {
53
 			// 蓝牙报错提示
78
 			// 蓝牙报错提示
54
 			blueError(code) {
79
 			blueError(code) {
@@ -83,16 +108,16 @@
83
 					case 10009:
108
 					case 10009:
84
 						this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
109
 						this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
85
 						break
110
 						break
86
-					case 100010:
111
+					case 10010:
87
 						this.errorToast('已连接')
112
 						this.errorToast('已连接')
88
 						break
113
 						break
89
-					case 100011:
114
+					case 10011:
90
 						this.errorToast('配对设备需要配对码')
115
 						this.errorToast('配对设备需要配对码')
91
 						break
116
 						break
92
-					case 100012:
117
+					case 10012:
93
 						this.errorToast('连接超时')
118
 						this.errorToast('连接超时')
94
 						break
119
 						break
95
-					case 100013:
120
+					case 10013:
96
 						this.errorToast('连接 deviceld 为空或者是格式不正确')
121
 						this.errorToast('连接 deviceld 为空或者是格式不正确')
97
 						break
122
 						break
98
 				}
123
 				}
@@ -171,26 +196,37 @@
171
 			// 找到新设备就触发该方法
196
 			// 找到新设备就触发该方法
172
 			found(res) {
197
 			found(res) {
173
 				if (res.devices[0].name.includes('BT24')) {
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
 				// 检查本地是否有id
205
 				// 检查本地是否有id
177
 				let deviceId = uni.getStorageSync('deviceId')
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
 			stateChange() {
223
 			stateChange() {
189
 				uni.onBLEConnectionStateChange(function(res) {
224
 				uni.onBLEConnectionStateChange(function(res) {
190
 					// 该方法回调中可以用于处理连接意外断开等异常情况
225
 					// 该方法回调中可以用于处理连接意外断开等异常情况
226
+					let connected = res.connected
191
 					console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`,
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
 						console.log(111111, '断开重连操作');
230
 						console.log(111111, '断开重连操作');
195
 						let data = {
231
 						let data = {
196
 							deviceId: this.deviceId
232
 							deviceId: this.deviceId
@@ -235,7 +271,7 @@
235
 				that.deviceId = data.deviceId
271
 				that.deviceId = data.deviceId
236
 				uni.createBLEConnection({
272
 				uni.createBLEConnection({
237
 					deviceId: that.deviceId,
273
 					deviceId: that.deviceId,
238
-					timeout: 30000,
274
+					timeout: 20000,
239
 					success(res) {
275
 					success(res) {
240
 						wx.hideLoading()
276
 						wx.hideLoading()
241
 						uni.showToast({
277
 						uni.showToast({
@@ -258,6 +294,7 @@
258
 						switch (err.errMsg) {
294
 						switch (err.errMsg) {
259
 							default:
295
 							default:
260
 								that.blueError(err.errCode)
296
 								that.blueError(err.errCode)
297
+								that.deviceId = ''
261
 						}
298
 						}
262
 						console.log('连接失败')
299
 						console.log('连接失败')
263
 						console.error(err)
300
 						console.error(err)
@@ -377,7 +414,7 @@
377
 			}
414
 			}
378
 
415
 
379
 			.item:nth-child(2n) {
416
 			.item:nth-child(2n) {
380
-				background-color: aqua;
417
+				background-color: #1e98d7;
381
 			}
418
 			}
382
 		}
419
 		}
383
 
420
 
@@ -393,4 +430,4 @@
393
 			justify-content: flex-end;
430
 			justify-content: flex-end;
394
 		}
431
 		}
395
 	}
432
 	}
396
-</style>
433
+</style>