lilei 4 år sedan
förälder
incheckning
78028da106
1 ändrade filer med 73 tillägg och 55 borttagningar
  1. 73 55
      pages/index/index.vue

+ 73 - 55
pages/index/index.vue

@@ -4,7 +4,7 @@
 			<u-image width="100rpx" height="100rpx" src="/static/chengzhong.png"></u-image>
 			<view class="tits">投放称重</view>
 		</view>
-		<view class="bindDev" @click="toFindDev">
+		<view class="bindDev" @click="toInit">
 			{{statusStr}}
 		</view>
 		<view class="devList">
@@ -14,7 +14,7 @@
 					<view class="texts">MAC:{{item.deviceId}}</view>
 				</view>
 				<view class="btns" @click="connectBlue(item)" v-if="!connectStatus">点击连接</view>
-				<view class="btns" @click="closeConnect" v-else>断开连接</view>
+				<view class="btns" @click="closeBlue" v-else>断开连接</view>
 			</view>
 		</view>
 	</view>
@@ -36,9 +36,12 @@
 				hexstr: '',
 				statusStr: '绑定电子秤',
 				connectStatus: false ,// 设备连接状态
-				loading: false // 正在连接
+				loading: false ,// 正在连接
 			}
 		},
+		onShow() {
+			console.log('onShow')
+		},
 		methods: {
 			// 去认证用户并开始称重
 			toWork(){
@@ -58,60 +61,69 @@
 					})
 				}
 			},
-			toFindDev(){
-				if(this.connectStatus || this.deviceList.length > 0){
-					return
-				}
+			// 初始化蓝牙
+			toInit(){
 				let _this = this
-				const res = uni.getSystemInfoSync();
-				if(!res.bluetoothEnabled){
-					uni.showModal({
-						title:'提示',
-						content:'请开启手机蓝牙',
-						showCancel: false,
-						complete(e) {
-							console.log(e)
-							_this.statusStr = '请开启手机蓝牙'
-						}
-					})
-				}else{
-					_this.loading = true
-					// 初始化蓝牙模块
-					uni.openBluetoothAdapter({
-					  success(res) {
-					    console.log(res)
-						_this.statusStr = '初始化蓝牙模块成功'
-						/* 获取本机的蓝牙状态 */
-						setTimeout(() => {
-							_this.init()
-						}, 1000)
-					  }
-					})
-				}
-				
-				// 监听蓝牙适配器状态变化事件
-				uni.onBluetoothAdapterStateChange(function (res) {
-				  console.log(res,'适配器变化')
-				  if(this.connectStatus){
-					  _this.statusStr = res.available ? '蓝牙已开启,点击重连' : '蓝牙已关闭,请开启手机蓝牙'
-				  }
-				  if(!res.available){
-				  	_this.closeConnect()
+				uni.openBluetoothAdapter({
+				  success(res) {
+				    console.log(res, 'openBluetoothAdapter success')
+					_this.onBluetoothAdapterStateChange()
+					setTimeout(()=>{
+						_this.toFindDev()
+					},500)
+				  },
+				  fail(err){
+					  console.log(err,'openBluetoothAdapter error')
+					  uni.showModal({
+					  	title:'提示',
+					  	content:'请开启手机蓝牙',
+					  	showCancel: false,
+					  	complete(e) {
+					  		console.log(e)
+					  		_this.statusStr = '请开启手机蓝牙'
+					  	}
+					  })
 				  }
 				})
 			},
-			// 初始化蓝牙
-			init(){
+			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
 				uni.getBluetoothAdapterState({
 				  success(res) {
 				    console.log(res)
-					_this.statusStr = '开始搜寻附近的电子秤'
-					// 开始搜寻附近的蓝牙外围设备
-					_this.startBluetoothDevicesDiscovery()
+					if(res.available){
+						// 开始搜寻附近的蓝牙外围设备
+						_this.startBluetoothDevicesDiscovery()
+					}else{
+						_this.statusStr = '请开启手机蓝牙'
+					}
 				  },
-				  fail(res) {
-					  console.log(res)
+				  fail(err) {
+					  console.log(err)
+				  }
+				})
+			},
+			// 监听蓝牙状态
+			onBluetoothAdapterStateChange(){
+				let _this = this
+				// 监听蓝牙适配器状态变化事件
+				uni.onBluetoothAdapterStateChange(function (res) {
+				  console.log(res,'适配器变化')
+				  if(!res.available){
+					// 关闭连接
+				  	_this.statusStr = '蓝牙已关闭,请开启手机蓝牙'
+					_this.connectedDeviceId = null
+					_this.closeConnect()
+				  }else{
+					if(!res.discovering && !_this.loading){
+						_this.statusStr = '蓝牙已启用,请绑定设备'
+					}
 				  }
 				})
 			},
@@ -126,7 +138,7 @@
 					  let devices = res.devices.filter(item => item.name == 'FAYA')
 					  console.log(devices,'devices list')
 					  if(devices.length){
-						  _this.statusStr = '发现设备'
+						  _this.statusStr = '发现设备'
 						  _this.deviceList = res.devices
 						  _this.loading = false
 					  }
@@ -134,12 +146,18 @@
 				  }
 				})
 			},
+			// 连接蓝牙设备
 			connectBlue(item){
 				this.deviceId = item.deviceId
 				this.statusStr = '正在连接设备...'
 				this.loading = true
 				this.createBLEConnection()
 			},
+			// 断开蓝牙设备
+			closeBlue(){
+				this.statusStr = '连接已断开,请重新绑定设备'
+				this.closeConnect()
+			},
 			// 开始连接指定设备
 			createBLEConnection(){
 				let _this = this
@@ -306,9 +324,11 @@
 			        uni.closeBLEConnection({
 			            deviceId: that.connectedDeviceId,
 			            success: function(res) {
-			                that.closeBluetoothAdapter()
+							console.log(res)
+							that.closeBluetoothAdapter()
 			            },
 			            fail(res) {
+							console.log(res)
 			            }
 			        })
 			    } else {
@@ -321,7 +341,6 @@
 			    uni.closeBluetoothAdapter({
 			        success: function(res) {
 						console.log(res,'closeBluetoothAdapter')
-						that.statusStr = '绑定电子秤'
 						that.connectStatus = false
 						that.loading = false
 						that.balanceData = ''
@@ -344,7 +363,7 @@
 		align-items: center;
 		justify-content: center;
 		flex-direction: column;
-		background-image: radial-gradient(ellipse at bottom, #00aaff 0%, #0081c1 100%);
+		background-image: radial-gradient( #2ab4e5 0%, #0081c1 100%);
 		height: 100vh;
 		width: 100%;
 	}
@@ -352,9 +371,8 @@
 		text-align: center;
 		background: #fff;
 		padding: 50rpx 50rpx 30rpx;
-		border:10rpx solid #ef9f2e;
 		border-radius: 300rpx;
-		box-shadow: 2rpx 2rpx 5rpx #916100;
+		box-shadow: 3rpx 3rpx 10rpx rgba(0,0,0,0.4);
 		width: 250rpx;
 		height: 250rpx;
 		display: flex;
@@ -373,7 +391,7 @@
 	.bindDev{
 		margin: 30rpx 0;
 		padding: 10rpx 40rpx;
-		box-shadow: 2rpx 2rpx 5rpx rgba(0,0,0,0.2);
+		box-shadow: 2rpx 2rpx 8rpx rgba(0,0,0,0.3);
 		border-radius: 100rpx;
 		color: #fff;
 	}