lilei пре 4 година
родитељ
комит
2dfd7f362f
2 измењених фајлова са 112 додато и 26 уклоњено
  1. 48 16
      pages/index/index.vue
  2. 64 10
      pages/userAuth/userAuth.vue

+ 48 - 16
pages/index/index.vue

@@ -27,7 +27,7 @@
 		data() {
 			return {
 				deviceList:[], // 设备列表
-				deviceId: '',
+				deviceId: null,
 				connectedDeviceId: '',
 				services: [],
 				readsId:'',
@@ -41,8 +41,19 @@
 				loading: false ,// 正在连接
 			}
 		},
-		onShow() {
-			console.log('onShow')
+		onLoad() {
+			console.log('onload')
+			let _this = this
+			uni.$on('blueReConnect',function(){
+				uni.showLoading({
+					mask: true,
+					title: '正在重连中...'
+				})
+				_this.connectBlue()
+			})
+		},
+		onUnload() {
+			uni.$off('blueReConnect')
 		},
 		methods: {
 			// 去认证用户并开始称重
@@ -51,7 +62,7 @@
 				// 连接成功
 				if(this.connectStatus){
 					uni.navigateTo({
-						url: '/pages/userAuth/userAuth'
+						url: '/pages/userAuth/userAuth?deviceId=' + this.deviceId
 					})
 				}else{
 					let hasDev = this.deviceList.length
@@ -76,16 +87,22 @@
 			// 初始化蓝牙
 			toInit(){
 				let _this = this
+				console.log(this.connectStatus,this.deviceList.length,this.loading)
+				if(this.connectStatus || this.deviceList.length > 0 || this.loading){
+					return
+				}
 				uni.openBluetoothAdapter({
 				  success(res) {
 				    console.log(res, 'openBluetoothAdapter success')
 					_this.onBluetoothAdapterStateChange()
+					_this.loading = true
 					setTimeout(()=>{
 						_this.toFindDev()
-					},1000)
+					},600)
 				  },
 				  fail(err){
 					  console.log(err,'openBluetoothAdapter error')
+					  _this.loading = false
 					  uni.showModal({
 					  	title:'提示',
 					  	content:'请开启手机蓝牙',
@@ -100,12 +117,7 @@
 			},
 			toFindDev(){
 				let _this = this
-				console.log(this.connectStatus,this.deviceList.length,this.loading)
-				if(this.connectStatus || this.deviceList.length > 0 || this.loading){
-					return
-				}
-				_this.statusStr = '开始搜寻附近的电子秤'
-				_this.loading = true
+				_this.statusStr = '开始搜寻附近的电子秤,请确认设备已开启'
 				uni.getBluetoothAdapterState({
 				  success(res) {
 				    console.log(res)
@@ -160,7 +172,9 @@
 			},
 			// 连接蓝牙设备
 			connectBlue(item){
-				this.deviceId = item.deviceId
+				if(item){
+					this.deviceId = item.deviceId
+				}
 				this.statusStr = '正在连接设备...'
 				this.loading = true
 				this.createBLEConnection()
@@ -183,6 +197,14 @@
 					setTimeout(()=>{
 						_this.getBLEDeviceServices()						  
 					},1000)
+				  },
+				  fail(err) {
+					  console.log(err,'createBLEConnection error')
+					  _this.statusStr = '设备连接失败,请检查后重试'
+					  _this.connectStatus = false
+					  _this.loading = false
+					  uni.$emit('blueConnectCallback',0)
+					  uni.hideLoading()
 				  }
 				})
 			},
@@ -204,8 +226,6 @@
 					uni.stopBluetoothDevicesDiscovery({
 						success: function(res) {
 							console.log(res, '停止搜索')
-						},
-						fail(res) {
 						}
 					})
 				  }
@@ -256,13 +276,24 @@
 						that.statusStr = '连接成功,请投放称重'
 						that.connectStatus = true
 						that.loading = false
+						uni.$emit('blueConnectCallback',1)
+						uni.hideLoading()
 						/*用来监听手机蓝牙设备的数据变化*/
 						uni.onBLECharacteristicValueChange(function(res) {
-							console.log(res,'read data')
+							// console.log(res,'read data')
 							that.balanceData = that.buf2string(res.value)
 							that.$u.vuex('vuex_balanceData',that.balanceData)
 						})
-						// that.sendData()
+						// 该方法回调中可以用于处理连接意外断开等异常情况
+						uni.onBLEConnectionStateChange(function (res) {
+						  console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
+						  if(!res.connected){
+							  uni.$emit('blueConnectClose',res)
+							  that.statusStr = '设备连接已断开,请重新连接'
+							  that.connectStatus = false
+							  that.loading = false
+						  }
+						})
 					},
 					fail(res) {
 						console.log(res, '启用低功耗蓝牙设备监听失败')
@@ -337,6 +368,7 @@
 			            deviceId: that.connectedDeviceId,
 			            success: function(res) {
 							console.log(res)
+							that.deviceId = null
 							that.closeBluetoothAdapter()
 			            },
 			            fail(res) {

+ 64 - 10
pages/userAuth/userAuth.vue

@@ -68,7 +68,7 @@
 		</view>
 		
 		<!-- 提交确认弹框 -->
-		<u-modal v-model="showOk" show-cancel-button @confirm="confirm" @cancel="cancel">
+		<u-modal v-model="showOk" :duration="50" show-cancel-button @confirm="confirm" @cancel="cancel">
 			<view class="slot-content">
 				<rich-text :nodes="modelContent"></rich-text>
 			</view>
@@ -95,7 +95,9 @@
 					address: '',
 					phone: '',
 					washType: '0'
-				}
+				},
+				blueConnect: true,
+				deviceId: null
 			}
 		},
 		computed: {
@@ -136,7 +138,46 @@
 				return fh + weight
 			}
 		},
+		onLoad(options) {
+			let _this = this
+			this.deviceId = options.deviceId
+			// 连接中断
+			uni.$on('blueConnectClose',function(){
+				_this.reConnect(1)
+			})
+			// 连接成功
+			uni.$on('blueConnectCallback',function(type){
+				if(type){
+					_this.blueConnect = true
+					uni.showModal({
+						title: '提示',
+						content: '重连成功,请重新称重',
+						showCancel: false,
+					})
+				}else{
+					_this.reConnect(0)
+				}
+			})
+		},
+		onUnload() {
+			uni.$off('blueConnectClose')
+			uni.$off('blueConnectCallback')
+		},
 		methods: {
+			reConnect(type){
+				this.blueConnect = false
+				uni.showModal({
+					title: '提示',
+					content: type ? '蓝牙连接设备已断开,请重新绑定连接' : '请检测电子秤或手机蓝牙是否开启,确认后请重新连接',
+					showCancel: false,
+					confirmText: '重新连接',
+					complete(e){
+						if(e.confirm){
+							uni.$emit('blueReConnect')
+						}
+					}
+				})
+			},
 			next(){
 				if(this.formData.phone == ''){
 					uni.showToast({
@@ -159,14 +200,27 @@
 				this.current = this.current - 1
 			},
 			submit(){
-				this.showOk = true
-				this.modelContent = `
-				<div>
-					<div style="font-weight:bold;padding:10px 0;">投递重量:<span style="color:red">${this.balanceData}</span> g</div>
-					<div>提交后将为此用户计入本次投递数据,确认提交吗?</div>
-				</div>
-				`
+				let that = this
+				// 判断设备仍然连接中
+				uni.getBluetoothDevices({
+				  success(res) {
+				    console.log(res.devices,that.deviceId)
+					let has = res.devices.find(item => item.deviceId == that.deviceId)
+					if(that.blueConnect && has){
+						that.showOk = true
+						that.modelContent = `
+						<div>
+							<div style="font-weight:bold;padding:10px 0;">投递重量:<span style="color:red">${that.balanceData}</span> g</div>
+							<div>提交后将为此用户计入本次投递数据,确认提交吗?</div>
+						</div>
+						`
+					}else{
+						that.reConnect(1)
+					}
+				  }
+				})
 			},
+			// 取消提交
 			cancel(){
 				this.showOk = false
 				this.modelContent = ''
@@ -258,7 +312,7 @@
 		}
 	}
 	.slot-content{
-		padding: 50rpx;
+		padding: 25rpx 50rpx 50rpx;
 	}
 }
 </style>