//监听是否列表数据,有调用蓝牙信标
watch: {
list: function(value) {
this.stopSearchBeacon() //关闭蓝牙信标
if (value.length > 0) {
if (value[0].scheduleForms[0].acceptanceCriteriaCode === 3) {
this.blueToothUuids = value[0].scheduleForms[0].blueToothUuids
this.openBluetoothAdapter()
} else {
this.stopSearchBeacon()
}
} else {
this.stopSearchBeacon()
}
},
}
// 初始化蓝牙模块
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: (res) => {
console.log('第一步、蓝牙初始化成功', res)
// 开始搜索附近蓝牙
this.startBluetoothDevicesDiscovery()
},
fail: (res) => {
console.log('请打开蓝牙', res, res.state)
this.bluetoothState = res.state
that.bluetoothType = true
setTimeout(() => {
that.openBluetoothAdapter()
}, 3000)
}
})
},
// 开始搜索附近的蓝牙设备。 uuid参数为必填项有uuid才能查到相对应的蓝牙设备
startBluetoothDevicesDiscovery() {
uni.startBeaconDiscovery({ // 调用微信api搜索附近的iBeacon设备
// uuid参数 对应的ibeacon设备
uuids: ['B5B182C7-EAB1-4988-AA99-B5C1517008D9', '01000000-0000-0000-0000-000000000000', 'E2C56DB5-DFFB-48D2-B060-D0F5A71096EE'],
success: function(res) {
// 控制台打印
console.log('-----------' + JSON.stringify(res))
uni.onBeaconUpdate(function(res2) {
// 监听ibeacon设备的更新事件
clearTimeout(that.clearUuid)
console.log('获取设备', JSON.parse(JSON.stringify(res2)).beacons[0].uuid)
that.uuid = JSON.parse(JSON.stringify(res2)).beacons[0].uuid
//定时器事件10秒钟清楚相对应的uuid,使得可以知道是否超出蓝牙信标范围
that.clearUuid = setTimeout(() => {
that.uuid = ''
console.log(that.uuid, '超出距离')
}, 10000)
})
},
fail: function(res) {
}
})
},
// 关闭蓝牙信标
stopSearchBeacon() {
uni.stopBeaconDiscovery({
success: function() {
console.log('关闭蓝牙成功')
}
})
},