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