Browse Source

【优化】多种连接之间,判断问题打通。

程志平 4 years ago
parent
commit
fe0b1214da
2 changed files with 77 additions and 67 deletions
  1. 73 62
      pages/reshui/reshui.vue
  2. 4 5
      pages/select/select.vue

+ 73 - 62
pages/reshui/reshui.vue

@@ -184,7 +184,7 @@
 			}
 			}
 
 
 			// 获取蓝牙设备,并列表
 			// 获取蓝牙设备,并列表
-			this.openBluetoothAdapter();
+			this.openBluetoothAdapter("get_room");
 		},
 		},
 		onShow() {
 		onShow() {
 			// 充值后更新金额
 			// 充值后更新金额
@@ -200,7 +200,7 @@
 			 * 单击连接热水表
 			 * 单击连接热水表
 			 */
 			 */
 			clickRoom(e) {
 			clickRoom(e) {
-				if (this.judgeBalance() == false) {
+				if (this.judgeBalance('clickRoom') == false) {
 					return
 					return
 				}
 				}
 				// console.log(e);
 				// console.log(e);
@@ -213,10 +213,8 @@
 					}
 					}
 				}
 				}
 
 
-				this.process_control(0);
-
 				// 开始连接匹配的蓝牙设备
 				// 开始连接匹配的蓝牙设备
-				this.createBLEConnection()
+				this.openBluetoothAdapter();
 			},
 			},
 			/**
 			/**
 			 * 空中连接热水表
 			 * 空中连接热水表
@@ -543,7 +541,7 @@
 			/**
 			/**
 			 * 判断余额是否充足
 			 * 判断余额是否充足
 			 */
 			 */
-			judgeBalance() {
+			judgeBalance(param) {
 				var limit_amount = parseFloat(this.$store.state.amount)
 				var limit_amount = parseFloat(this.$store.state.amount)
 				var amount = parseFloat(this.amount);
 				var amount = parseFloat(this.amount);
 				// 启动最低金额限制
 				// 启动最低金额限制
@@ -557,42 +555,40 @@
 							if (res.confirm) {
 							if (res.confirm) {
 								// 跳转充值页面
 								// 跳转充值页面
 								this.chongzhi_yemian()
 								this.chongzhi_yemian()
-							// 	return false
-							// } else if (res.cancel) {
-							// 	return false
+								// 	return false
+								// } else if (res.cancel) {
+								// 	return false
 							}
 							}
 						}
 						}
 					})
 					})
 					return false
 					return false
-				} else {
-					return true
-				}
-			},
-			/**
-			 * 111111111111111111111111111111
-			 * 扫码连接设备
-			 */
-			scan() {
-				if (this.judgeBalance() == false) {
-					return
 				}
 				}
+
 				// 已经启动扫描连接设备中
 				// 已经启动扫描连接设备中
 				if (this.isScan) {
 				if (this.isScan) {
 					uni.showToast({
 					uni.showToast({
 						icon: 'none',
 						icon: 'none',
 						title: '正在扫描,请耐心等待',
 						title: '正在扫描,请耐心等待',
 					})
 					})
-					return
+					return false
 				}
 				}
+
 				// 已经启动设备,不启动扫描
 				// 已经启动设备,不启动扫描
 				if (this.isChecked) {
 				if (this.isChecked) {
+					let info_str = ''
+					if (param == 'scan') {
+						info_str = '设备已启动,无需重新扫码'
+					} else {
+						info_str = '设备已启动,无需重新启动'
+					}
 					uni.showToast({
 					uni.showToast({
 						icon: 'none',
 						icon: 'none',
-						title: '设备已启动,无需重新扫码',
+						title: info_str,
 					})
 					})
-					return
+					return false
 				}
 				}
 
 
+				// 设备已连接
 				if (this.deviceId) {
 				if (this.deviceId) {
 					uni.showModal({
 					uni.showModal({
 						title: '提示',
 						title: '提示',
@@ -600,18 +596,33 @@
 						success: res => {
 						success: res => {
 							if (res.confirm) {
 							if (res.confirm) {
 								this.closeBLEConnection()
 								this.closeBLEConnection()
-
-								this.scan_qr()
-							} else if (res.cancel) {
-								return
+								if (param == 'scan') {
+									this.scan_qr()
+								} else {
+									uni.showToast({
+										icon: 'none',
+										title: '请选择宿舍号重新连接',
+										duration: 3000
+									})
+								}
 							}
 							}
 						}
 						}
 					})
 					})
-				} else {
-					this.scan_qr()
+					return false
 				}
 				}
 			},
 			},
 			/**
 			/**
+			 * 111111111111111111111111111111
+			 * 扫码连接设备
+			 */
+			scan() {
+				if (this.judgeBalance('scan') == false) {
+					return
+				}
+
+				this.scan_qr()
+			},
+			/**
 			 * 启动摄像头,扫描二维码
 			 * 启动摄像头,扫描二维码
 			 */
 			 */
 			scan_qr() {
 			scan_qr() {
@@ -649,7 +660,7 @@
 			 * 22222222222222222222222222222
 			 * 22222222222222222222222222222
 			 * 初始化蓝牙
 			 * 初始化蓝牙
 			 */
 			 */
-			openBluetoothAdapter() {
+			openBluetoothAdapter(param) {
 				uni.openBluetoothAdapter({
 				uni.openBluetoothAdapter({
 					success: (res) => { // 已打开
 					success: (res) => { // 已打开
 						uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
 						uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
@@ -658,7 +669,7 @@
 								// 本机设备的蓝牙已打开
 								// 本机设备的蓝牙已打开
 								// 开始搜索蓝牙设备
 								// 开始搜索蓝牙设备
 								if (this.deviceId == '' && this.serviceId == '')
 								if (this.deviceId == '' && this.serviceId == '')
-									this.startBluetoothDeviceDiscovery()
+									this.startBluetoothDeviceDiscovery(param)
 							},
 							},
 							fail: (error) => {
 							fail: (error) => {
 								// console.log(error)
 								// console.log(error)
@@ -695,7 +706,7 @@
 			 * 333333333333333333333333333
 			 * 333333333333333333333333333
 			 * 开始搜索蓝牙设备
 			 * 开始搜索蓝牙设备
 			 */
 			 */
-			startBluetoothDeviceDiscovery() {
+			startBluetoothDeviceDiscovery(param) {
 				uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
 				uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
 					// services: ['FEE7'],   // 要搜索但蓝牙设备主 service 的 uuid 列表
 					// services: ['FEE7'],   // 要搜索但蓝牙设备主 service 的 uuid 列表
 					allowDuplicatesKey: true,
 					allowDuplicatesKey: true,
@@ -703,7 +714,7 @@
 						if (this.get_Bluetooth_Bevice == true)
 						if (this.get_Bluetooth_Bevice == true)
 							this.process_control(0)
 							this.process_control(0)
 
 
-						this.onBluetoothDeviceFound() // 监听搜索到新设备  同时进行
+						this.onBluetoothDeviceFound(param) // 监听搜索到新设备  同时进行
 
 
 						setTimeout(() => {
 						setTimeout(() => {
 							if (this.isScan) {
 							if (this.isScan) {
@@ -746,7 +757,7 @@
 				})
 				})
 			},
 			},
 
 
-			stopBluetoothDevicesDiscovery: function() { //停止搜索
+			stopBluetoothDevicesDiscovery(param) { //停止搜索
 				uni.stopBluetoothDevicesDiscovery({
 				uni.stopBluetoothDevicesDiscovery({
 					success: (res) => {
 					success: (res) => {
 						// console.log(res, '停止蓝牙搜索')
 						// console.log(res, '停止蓝牙搜索')
@@ -755,17 +766,17 @@
 						// 	duration: 2000
 						// 	duration: 2000
 						// })
 						// })
 						if (this.get_Bluetooth_Bevice == true) {
 						if (this.get_Bluetooth_Bevice == true) {
-							this.process_control(0);
-
-							// 开始连接匹配的蓝牙设备
-							this.createBLEConnection()
+							if (param != 'get_room') {
+								// 开始连接匹配的蓝牙设备
+								this.createBLEConnection()
+							}
 						} else {
 						} else {
-							this.get_Bluetooth_Bevice == true
+							this.get_Bluetooth_Bevice = true
+							this.get_room()
 						}
 						}
-						this.get_room()
 					},
 					},
-					fail: e => {
-						// console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
+					fail: err => {
+						// console.log('停止搜索蓝牙设备失败,错误码:' + err.errCode);
 						this.isScan = false
 						this.isScan = false
 						uni.showToast({
 						uni.showToast({
 							title: err.errCode,
 							title: err.errCode,
@@ -779,7 +790,7 @@
 			 * 444444444444444444444444444444444
 			 * 444444444444444444444444444444444
 			 * 发现外围设备
 			 * 发现外围设备
 			 */
 			 */
-			onBluetoothDeviceFound() {
+			onBluetoothDeviceFound(param) {
 				uni.onBluetoothDeviceFound((res) => {
 				uni.onBluetoothDeviceFound((res) => {
 					let dname = ''
 					let dname = ''
 					let i = 0
 					let i = 0
@@ -809,14 +820,13 @@
 							this.deviceId = device.deviceId
 							this.deviceId = device.deviceId
 
 
 							// 放此位置,苹果手机兼容
 							// 放此位置,苹果手机兼容
-							this.stopBluetoothDevicesDiscovery()
+							this.stopBluetoothDevicesDiscovery(param)
 
 
 							return
 							return
 						}
 						}
 
 
 						setTimeout(() => {
 						setTimeout(() => {
-							// 放此位置,苹果手机兼容
-							this.stopBluetoothDevicesDiscovery()
+							this.stopBluetoothDevicesDiscovery(param)
 						}, 2000)
 						}, 2000)
 					})
 					})
 				});
 				});
@@ -897,7 +907,7 @@
 							this.connect_BLE()
 							this.connect_BLE()
 						} else {
 						} else {
 							uni.showToast({
 							uni.showToast({
-								title: '建立连接失败',
+								title: '未知错误,请重连1次。',
 								icon: 'none',
 								icon: 'none',
 								duration: 3000,
 								duration: 3000,
 								success: (res1) => {
 								success: (res1) => {
@@ -913,8 +923,6 @@
 				if (this.isConnected === false) {
 				if (this.isConnected === false) {
 					this.isConnected = true
 					this.isConnected = true
 
 
-					this.process_control(1);
-
 					// 获取蓝牙设备所有服务(service)
 					// 获取蓝牙设备所有服务(service)
 					this.getBLEDeviceServices()
 					this.getBLEDeviceServices()
 				}
 				}
@@ -963,8 +971,6 @@
 					serviceId: this.serviceId,
 					serviceId: this.serviceId,
 					success: (res) => {
 					success: (res) => {
 
 
-						this.process_control(2);
-
 						// console.log(res)
 						// console.log(res)
 						this.characteristicIdForWrite = res.characteristics[0].uuid
 						this.characteristicIdForWrite = res.characteristics[0].uuid
 						this.characteristicIdForNotity = res.characteristics[1].uuid
 						this.characteristicIdForNotity = res.characteristics[1].uuid
@@ -997,6 +1003,13 @@
 					characteristicId: this.characteristicIdForNotity,
 					characteristicId: this.characteristicIdForNotity,
 					success: (res) => {
 					success: (res) => {
 						this.process_control(2)
 						this.process_control(2)
+						
+						uni.showToast({
+							icon:'none',
+							title:'需要启动请点【开启设备】开关',
+							duration: 3000
+						})
+						
 						this.isScan = false
 						this.isScan = false
 						// console.log('通知启用(notifyBLECharacteristicValueChange)',res);
 						// console.log('通知启用(notifyBLECharacteristicValueChange)',res);
 						this.onBLECharacteristicValueChange()
 						this.onBLECharacteristicValueChange()
@@ -1071,7 +1084,6 @@
 							// 	icon: 'none'
 							// 	icon: 'none'
 							// })
 							// })
 
 
-							this.process_control(3)
 							this.isChecked = true
 							this.isChecked = true
 
 
 							break
 							break
@@ -1113,14 +1125,15 @@
 						this.closeBluetoothAdapter()
 						this.closeBluetoothAdapter()
 					},
 					},
 					fail: () => {
 					fail: () => {
-						uni.showToast({
-							icon: 'none',
-							title: '蓝牙连接断开失败',
-							duration: 3000,
-							success: (res1) => {
-								this.setInit()
-							}
-						})
+						this.setInit()
+						// uni.showToast({
+						// 	icon: 'none',
+						// 	title: '蓝牙连接断开失败',
+						// 	duration: 3000,
+						// 	success: (res1) => {
+						// 		this.setInit()
+						// 	}
+						// })
 					}
 					}
 				})
 				})
 			},
 			},
@@ -1191,8 +1204,6 @@
 				if (val == 'start') { // 启动设备
 				if (val == 'start') { // 启动设备
 					this.isUserClose = false
 					this.isUserClose = false
 
 
-					this.process_control(3)
-					// this.createBLEConnection()
 					// 发送 17字节 启动代码(会返回 35字节 的数据)
 					// 发送 17字节 启动代码(会返回 35字节 的数据)
 					// b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
 					// b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
 					newArr = this.str2StrArray(this.start_code)
 					newArr = this.str2StrArray(this.start_code)

+ 4 - 5
pages/select/select.vue

@@ -128,7 +128,7 @@
 					this.dis_num1 = 0
 					this.dis_num1 = 0
 					uni.hideLoading()
 					uni.hideLoading()
 				}
 				}
-				
+
 				if (this.$store.state.building.roomSelect != '') {
 				if (this.$store.state.building.roomSelect != '') {
 					this.fillData()
 					this.fillData()
 				}
 				}
@@ -138,7 +138,6 @@
 			 */
 			 */
 			fillData() {
 			fillData() {
 				// 拆分之前选好的楼栋和宿舍号
 				// 拆分之前选好的楼栋和宿舍号
-
 				var str = this.$store.state.building.roomSelect;
 				var str = this.$store.state.building.roomSelect;
 				var arr = new Array();
 				var arr = new Array();
 				var i = 0;
 				var i = 0;
@@ -165,8 +164,9 @@
 					}
 					}
 					let len = arr[2].length
 					let len = arr[2].length
 					if (arr[2] != null && len > 0) {
 					if (arr[2] != null && len > 0) {
-						this.arr2 = [arr[2].substring(0, len - 3) + '栋']
-						this.arr3 = [arr[2].substring(len - 3, len - 2) + '层']
+						let a = arr[2].split('-')
+						this.arr2 = [a[0] + '栋']
+						this.arr3 = [a[1].substring(0, len - 2) + '层']
 						this.arr4 = [arr[2]]
 						this.arr4 = [arr[2]]
 					}
 					}
 
 
@@ -196,7 +196,6 @@
 				// console.log(index)
 				// console.log(index)
 				// console.log(typeof(index))
 				// console.log(typeof(index))
 				this.arr1 = this.array1;
 				this.arr1 = this.array1;
-
 				//获取选择器1的值
 				//获取选择器1的值
 				for (var i = 0; i < this.nschool; i++) {
 				for (var i = 0; i < this.nschool; i++) {
 					// this.array1.push(this.allData.data[i].school)
 					// this.array1.push(this.allData.data[i].school)