浏览代码

bug 修复

lilei 4 年之前
父节点
当前提交
9267956bd4
共有 1 个文件被更改,包括 27 次插入23 次删除
  1. 27 23
      pages/index/index.vue

+ 27 - 23
pages/index/index.vue

@@ -22,7 +22,7 @@
 						<view class="texts">MAC:{{item.deviceId}}</view>
 					</view>
 					<view class="btns" @click="connectBlue(item)" v-if="!connectStatus">点击连接</view>
-					<view class="btns" @click="closeBlue" v-else>断开连接</view>
+					<view class="btns" @click="closeBlue('连接已断开,请重新连接设备')" v-else>断开连接</view>
 				</view>
 			</view>
 		</view>
@@ -228,23 +228,11 @@
 				  console.log(res,'适配器变化')
 				  if(!res.available){
 					// 关闭连接
-				  	_this.statusStr = '蓝牙已关闭,请开启手机蓝牙'
 					_this.loading = false
-					if(_this.connectedDeviceId){
-						uni.closeBLEConnection({
-						    deviceId: _this.connectedDeviceId,
-						    success: function(res) {
-								console.log(res)
-								_this.connectedDeviceId = null
-						    },
-						    fail(res) {
-								console.log(res)
-						    }
-						})
-					}
+					_this.closeBlue('蓝牙已关闭,请开启手机蓝牙')
 				  }else{
 					if(!res.discovering && !_this.loading){
-						_this.statusStr = '蓝牙已启用,请绑定设备'
+						_this.statusStr = _this.connectedDeviceId ? '蓝牙已启用,请连接设备':'蓝牙已启用,请绑定设备'
 					}
 				  }
 				})
@@ -288,9 +276,21 @@
 				this.createBLEConnection()
 			},
 			// 断开蓝牙设备
-			closeBlue(){
-				this.statusStr = '连接已断开,请重新绑定设备'
-				this.closeConnect()
+			closeBlue(msg){
+				let _this = this
+				if(_this.connectedDeviceId){
+					uni.closeBLEConnection({
+					    deviceId: _this.connectedDeviceId,
+					    success: function(res) {
+							console.log(res,msg)
+							_this.connectedDeviceId = null
+							_this.statusStr = msg
+					    },
+					    fail(res) {
+							console.log(res)
+					    }
+					})
+				}
 			},
 			// 开始连接指定设备
 			createBLEConnection(){
@@ -308,7 +308,7 @@
 				  },
 				  fail(err) {
 					  console.log(err,'createBLEConnection error')
-					  _this.statusStr = '设备连接失败,请检查蓝牙是否开启后重试'
+					  _this.statusStr = '连接失败,请检查蓝牙是否开启后重试'
 					  _this.connectStatus = false
 					  _this.loading = false
 					  uni.$emit('blueConnectCallback',0)
@@ -397,10 +397,10 @@
 						})
 						// 该方法回调中可以用于处理连接意外断开等异常情况
 						uni.onBLEConnectionStateChange(function (res) {
-						  console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
-						  if(!res.connected){
+						  // console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
+						  if(!res.connected&&that.connectedDeviceId){
 							  uni.$emit('blueConnectClose',res)
-							  that.statusStr = '设备连接已断开,请重新连接'
+							  that.statusStr = '连接已断开,请检查蓝牙或电子秤是否开启'
 							  that.connectStatus = false
 							  that.loading = false
 						  }
@@ -408,7 +408,7 @@
 					},
 					fail(res) {
 						console.log(res, '启用低功耗蓝牙设备监听失败')
-						that.statusStr = '设备连接失败,请点击重试'
+						that.statusStr = '连接失败,请点击重试'
 						that.closeConnect()
 					}
 				})
@@ -511,6 +511,10 @@
 		},
 		onUnload() {
 			this.closeConnect()
+		},
+		onShareAppMessage() {
+		},
+		onShareTimeline() {
 		}
 	}
 </script>