/*
*蓝牙开门
*/
import moment from ‘@/common/moment.js’
export default {
data() {
return {
dataList: [],
customStyle: {
margin: '0 20rpx', // 注意驼峰命名,并且值必须用引号包括,因为这是对象
borderRadius: '200rpx',
backgroundColor: 'rgb(85, 170, 255)',
height: '91rpx',
},
replaceFields: {
label: 'label',
value: 'value',
time: 'orderDate',
type: 'status',
remarks: 'remarks',
startTime: 'startTime',
endTime: 'endTime',
roomNo: 'roomNo', //房间
roomName: 'roomName', //房间
bedNo: 'bedNo', //床位
bedName: 'bedName', //床位
},
info: '未初始化蓝牙适配器',
list: [],
deviceId: '', //设备id
deviceName: '', //设备名称
serviceId: '', //服务id
service: '', //蓝牙服务
characteristicId: '', //获取服务中的特征值
instructions: '',//蓝牙指令 xxxx
count: 0,连接蓝牙
count2: 0, 启用蓝牙特征值
count3:0, //输入指令失败
infosum:0,
}
},
onLoad() {
},
onReachBottom() {
},
mounted() {
},
methods: {
moment,
//初始化蓝牙模块
openBluetoothAdapter() {
var _this = this
_this.count2=1
console.log(uni.openBluetoothAdapter, 'saasas', this.info = '初始化蓝牙成功')
if (!uni.openBluetoothAdapter) {
uni.showModal({
title: '提示',
showCancel: false,
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试',
})
} else {
uni.closeBluetoothAdapter({
success: res => {
uni.openBluetoothAdapter({ // 初始化蓝牙模块
success: res => {
console.log('初始化蓝牙成功', res)
this.info = '初始化蓝牙成功'
_this.infosum = 10
_this.getBluetoothAdapterState()
},
fail: err => {
console.log(err)
uni.showModal({
title: '提示',
content: '请检查手机蓝牙是否打开',
showCancel: false,
success: function(res) {}
});
this.info = '初始化蓝牙失败'
}
})
},
})
}
},
//获取本机蓝牙适配器状态
getBluetoothAdapterState() {
var _this = this
uni.getBluetoothAdapterState({
success: res => {
if (res.available == false) {
uni.showToast({
title: '设备无法开启蓝牙连接',
icon: 'none',
duration: 2000
})
uni.closeBluetoothAdapter()
} else if (res.discovering == false) {
_this.startBluetoothDevicesDiscovery() // 开启搜索外围设备
this.infosum = 15
} else if (res.available) {
_this.startBluetoothDevicesDiscovery() // 蓝牙适配器正常,去执行搜索外围设备
this.infosum = 20
}
}
})
},
//搜索外围蓝牙设备
startBluetoothDevicesDiscovery() {
var _this = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
success: res => {
if (!res.isDiscovering) { // 是否在搜索设备
_this.getBluetoothAdapterState()
} else {
_this.onBluetoothDeviceFound() // 搜索成功后,执行监听设备的api
_this.info = '搜索成功'
this.infosum = 30
}
},
fail: err => {
console.log("蓝牙搜寻失败")
_this.info = '蓝牙搜寻失败'
uni.stopBluetoothDevicesDiscovery() // 没有搜索到设备
uni.closeBluetoothAdapter() // 关闭蓝牙模块
}
})
},
//监听寻找到新设备
onBluetoothDeviceFound() {
var _this = this
this.list = []
uni.onBluetoothDeviceFound(res => {
// this.list=res.devices
// console.log(res.devices, 'res.devices')
for (let i = 0; i < res.devices.length; i++) {
console.log(res.devices[i].name, 'res.devices', res.devices[i].name == _this.deviceName,_this.deviceName,res.devices[i].name)
if (res.devices[i].name == this.deviceName || res.devices[i].localName == _this.deviceName) {
_this.list.push(res.devices[i].name)
_this.deviceId = res.devices[i].deviceId
// this.list=res.devices
uni.stopBluetoothDevicesDiscovery() // 匹配到设备后关闭搜索
_this.count = 0 //连接蓝牙次数
_this.createBLEConnection() // 连接蓝牙
this.infosum =40
}
}
console.log(res.devices, 'res.devices')
})
},
//连接蓝牙
createBLEConnection() { // 连接低功耗蓝牙
var _this = this
console.log("连接低功耗蓝牙", _this.count, _this.deviceId)
uni.createBLEConnection({
deviceId: _this.deviceId,
success: res => {
console.log("连接蓝牙success", _this.count, _this.deviceId)
_this.info = '连接蓝牙'
_this.getBLEDeviceServices()
this.infosum =60
},
fail: err => {
console.log("连接失败")
if (_this.count < 6) {
_this.count++
_this.createBLEConnection()
} else {
uni.closeBluetoothAdapter() // 连接失败关闭蓝牙模块
}
}
})
},
//获取蓝牙设备的服务
getBLEDeviceServices() {
var _this = this
uni.getBLEDeviceServices({
deviceId: _this.deviceId,
success: res => {
_this.info = '获取服务'
// _this.serviceId= res.services[0].uuid
_this.getBLEDeviceCharacteristics()
for (let i = 0; i < res.services.length; i++) {
console.log("获取服务success", res.services[i].uuid)
// 如果提前得知可以直接判断,如果不知道可以用蓝牙工具看一下服务所需的功能
_this.serviceId = res.services[0].uuid
_this.getBLEDeviceCharacteristics(res.services[i].uuid)
if (res.services[i].uuid == _this.service) {
// _this.setData({
// serviceId: res.services[i].uuid
// })
_this.serviceId = res.services[i].uuid
_this.getBLEDeviceCharacteristics()
this.infosum =70
}
}
},
fail: err => {
console.log("获取服务失败")
uni.closeBluetoothAdapter() // 关闭蓝牙模块
}
})
},
//获取服务中的特征值
// 获取服务success xxxx
// 获取服务success xxx
getBLEDeviceCharacteristics(i) { // 获取服务中的特征值
var _this = this
console.log("获取服务中的特征值success", 1)
uni.getBLEDeviceCharacteristics({
deviceId: _this.deviceId,
serviceId: _this.serviceId,
success: res => {
console.log("获取服务中的特征值success", 2)
for (let i = 0; i < res.characteristics.length; i++) {
let model = res.characteristics[i]
// _this.characteristicId=res.characteristics[0]
// _this.notifyBLECharacteristicValueChange()
console.log("获取服务中的特征值success", 3, model, model.properties)
if ((model.properties.notify || model.properties.indicate) && (model.properties
.read && model.properties.write)) {
_this.characteristicId = model.uuid
console.log("获取服务中的特征值success", model.uuid, 4)
_this.info = '获取服务中的特征值success' + model.uuid
_this.notifyBLECharacteristicValueChange()
this.infosum =80
console.log('启用蓝牙特征值变化时的notify功能notifyBLECharacteristicValueChange')
}
}
},
fail: err => {
console.log("获取服务中的特征值失败")
uni.closeBluetoothAdapter() // 关闭蓝牙模块
}
})
},
// 启用蓝牙特征值变化时的notify功能
// 启用蓝牙特征值变化时的notify功能
// 启用蓝牙特征值变化时的notify功能
notifyBLECharacteristicValueChange() {
var _this = this
// 启用蓝牙特征值变化时的notify功能
console.log('启用蓝牙特征值变化时的notify功能', '_this.deviceId', _this.deviceId, _this.serviceId, _this
.characteristicId)
uni.notifyBLECharacteristicValueChange({
deviceId: _this.deviceId,
serviceId: _this.serviceId,
characteristicId: _this.characteristicId,
state: true,
success: res => {
console.log(res, '启用蓝牙特征值变化时的notify功能success', )
uni.onBLECharacteristicValueChange(res => {
_this.info = '启用蓝牙特征值变化时的notify功能'
console.log(res, '启用蓝牙特征值变化时的notify功能', 5)
})
_this.info = 'notify功能启用'
_this.writeBLECharacteristicValue() // 向蓝牙设备写入数据
_this.count3=1
this.infosum =90
},
fail: err => {
console.log("启用BLE蓝牙特征值变化时的notify功能错误err", 6, err)
//初始化开门第一次失败后,
if(err.errCode==1000||err.errMsg=="notifyBLECharacteristicValueChange:fail:not init"){
if (_this.count2 < 6) {
_this.count2++
_this.openBluetoothAdapter()
} else {
_this.info = 'notify功能错误'
uni.closeBluetoothAdapter() // 连接失败关闭蓝牙模块
}
}
}
})
},
//向蓝牙设备写入数据
writeBLECharacteristicValue() { // 向蓝牙设备写入数据
var _this = this
console.log('启用notify');
var typedArray = new Uint8Array(
_this.instructions.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16);
})
);
var buffer1 = typedArray.buffer;
// console.log(buffer1); // A70110112233445566778843010A0220002055FE
uni.writeBLECharacteristicValue({
deviceId: _this.deviceId,
serviceId: _this.serviceId,
characteristicId: _this.characteristicId,
value: buffer1, // 这个输入的指令,需要转换成ArrayBuffer
success: res => {
console.log("成功", buffer1)
_this.info = '开门成功'
_this.OpenDoorRegister()
uni.closeBluetoothAdapter() // 关闭蓝牙模块
this.infosum =100
},
fail: err => {
console.log("输入指令失败")
//初始化开门第一次失败后,
if(err.errCode==1000||err.errMsg=="notifyBLECharacteristicValueChange:fail:not init"){
if (_this.count3 < 6) {
_this.count3++
_this.writeBLECharacteristicValue()
} else {
_this.info = '开门失败'
uni.closeBluetoothAdapter() // 关闭蓝牙模块
}
}
}
})
},
buf2hex(buffer) {
// buffer is an ArrayBuffer
console.log(new Uint8Array(buffer).length);
return Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('');
},
//小程序主页视图显示数据需要转换
JSONObject(item) {
return JSON.parse(JSON.stringify(item))
},
}
}