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

+ 52 - 15
pages/home/home.vue

@@ -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>