蓝牙开锁,相同代码苹果正常启用,安卓无法使用
/* 搜索蓝牙操作 */
searchDevice() {
let _self = this
uni.showLoading({
title: "连接中"
})
/* 1、初始化蓝牙 uni.openBluetoothAdapter(OBJECT) */
uni.openBluetoothAdapter({
success: (res) => {
/* 初始化蓝牙成功 */
/* 2、开始搜索蓝牙设备 uni.startBluetoothDevicesDiscovery(OBJECT */
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true, //是否允许同一设备多次上报,但RSSI值会有所不同
success: (res) => {
console.log("搜索成功:" + res)
_self.onBLECharacteristicValueChange()
},
fail: (res) => {
console.log("搜索失败:" + res)
}
})
},
fail: (res) => {
console.log("初始化蓝牙失败:" + res.code)
uni.showToast({
duration: 5000,
title: "请手动打开手机蓝牙",
icon: 'none',
})
}
}),
/* 搜索蓝牙设备回调 */
uni.onBluetoothDeviceFound(function(CALLBACK) {
/* 我这边是直接每次遍历指定设备,
一旦搜索到指定设备就结束搜索,
需要显示的就每次都在这边把搜索到的设备添加到数组中,然后界面去显示*/
/* 搜索过程中如果搜索到需要的设备 */
CALLBACK.devices.forEach(device => {
/* 筛选需要的deviceName */
if (device.name === _self.targetNumber) {
console.log(device, 'device')
//找到需要的设备之后,停止搜索
console.log("搜索到指定设备")
_self.DeviceID = device.deviceId
let DeviceID = device.deviceId
/* 停止搜索 */
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
uni.getBluetoothDevices({
success: (devices) => {
}
})
console.log("停止搜索成功")
},
fail: (res) => {
console.log("停止失败")
uni.hideLoading();
}
})
/* 执行连接 */
_self.connectDevice(device.deviceId);
}
})
}),
/* 监听蓝牙适配器状态变化事件 可以检测当前设备的蓝牙的断开或者连接的情况*/
uni.onBluetoothAdapterStateChange(function(CALLBACK) {
//CALLBACK.available 蓝牙适配器是否可用
//CALLBACK.discovering 蓝牙适配器是否处于搜索状态
console.log('适配器状态变化', CALLBACK)
})
},
/* 关闭蓝牙适配器*/
closeBlueAdapter() {
uni.closeBluetoothAdapter({
success() {
console.log("关闭蓝牙适配器成功")
}
})
},
/* 断开低功耗蓝牙的连接 */
disconnectBle() {
uni.closeBLEConnection({
deviceId: _self.DeviceId,
success: (res) => {
uni.showToast({
title: "断开成功"
});
console.log("断开成功" + res)
}
})
},
/* 连接低功耗蓝牙 */
connectDevice(deviceId) {
let _self = this
console.log("执行到这里了")
uni.createBLEConnection({
deviceId, //当前点击的DeviceId
timeout: 5000,
success: (res) => {
uni.hideLoading(); //需要先隐藏才行,不然不会显示弹框
console.log("连接低功耗蓝牙成功")
// _self.mDeviceId = deviceId;
/* 这边获取全部的服务,并筛选出当前需要的*/
_self.getBLEDeviceServices(deviceId)
},
fail: (error) => {
/* 连接失败 */
uni.hideLoading();
console.log("连接低功耗蓝牙失败")
console.log(error);
}
})
/* 监听蓝牙设备的连接状态 */
uni.onBLEConnectionStateChange(function(CALLBACK) {
console.log(CALLBACK.deviceId + "连接状态:" + CALLBACK.connected)
})
},
/* 获取所有的服务(目前理解来说应该是进入服务) */
getBLEDeviceServices(deviceId) {
console.log("开始获取服务")
let _self = this
//连接成功之后需要延时,继续操作才不会出问题,延时时间短一点都没关系,我这边用了1秒
setTimeout(() => {
uni.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success: (res) => {
console.log('device services:', res)
res.services.forEach((item) => {
let serviceId = item.uuid;
// console.log(UUID_SERVICE)
if (serviceId.indexOf('FFF0') != -1) {
console.log('serverId:', serviceId)
_self.serviceId = serviceId
/* 进入特征值 */
_self.getBLEDeviceCharacteristics(deviceId, serviceId);
}
})
},
fail: (error) => {
console.log("获取服务失败")
console.log(error)
uni.hideLoading();
}
})
}, 1000)
},
/* 获取所有特征值 */
getBLEDeviceCharacteristics(deviceId, serviceId) {
let _self = this
console.log("开始获取特征", deviceId, serviceId)
setTimeout(() => {
uni.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
success: (res) => {
console.log(res)
//let characteristics = res.characteristics
res.characteristics.forEach((item) => {
console.log(item.uuid)
let characterId = item.uuid;
/* 只要用到的唤醒即可 */
if (characterId.indexOf('FFF2') != -1) {
console.log(item.properties.write)
console.log('characteristicId:', characterId)
_self.characterId = characterId
_self.notifyBLECharacteristicValueChange(deviceId,
serviceId, characterId)
} else if (characterId.indexOf('FFF1') != -1) {
_self.notifyCharaId = characterId;
console.log(item.properties.notify)
console.log(_self.notifyCharaId)
}
})
},
fail: (res) => {
uni.hideLoading();
console.log(res)
}
})
}, 1000)
},
notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId) {
let _self = this;
console.log("开始唤醒", deviceId, serviceId, characteristicId)
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
type: 'notification',
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId: _self.notifyCharaId,
success: (res) => {
console.log('唤醒成功')
console.log(deviceId, serviceId, _self.notifyCharaId, )
setTimeout(() => {
// setInterval(function () {
_self.writeMeter(); //发送指令方法
// }, 1000);
}, 300)
/* 连接成功 */
uni.showToast({
duration: 3000,
title: "蓝牙连接成功",
icon: 'success'
});
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
},
fail: (res) => {
uni.hideLoading();
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
})
},
onBLECharacteristicValueChange() {
/* 测试发现如果不做分包发送的话,还是会有问题,所以这边还是需要做分包发送 */
/* 这边对特征值2进行数据的监听 */
let resultAll;
uni.onBLECharacteristicValueChange(function(CALLBACK) {
console.log(CALLBACK, new Uint8Array(CALLBACK.value))
let result = blueTool.ab2hex(CALLBACK.value);
console.log(blueTool.ab2hex(CALLBACK.value))
resultAll += result;
console.log("将两次收到的数据值拼合一下", resultAll)
})
},
/* 连接成功以后,执行设置的操作,并且打开返回值的监听开关并监听 */
writeMeter() {
let _self = this;
/* 获取所有的服务 */
/* 每次设置钱都将notify打开(设备回复开关) */
let key = this.key;
let arrayBuffer = blueTool.string2buffer('BABEC0DE' + key)
console.log(key, arrayBuffer, 'key')
console.log(new Uint8Array(arrayBuffer))
_self.onBLECharacteristicValueChange()
_self.writeDevice(arrayBuffer);
},
/* 执行演示分包操作 */
writeDevice(_Buffer) {
let _self = this;
let Num = 0;
let ByteLength = _Buffer.byteLength;
console.log(ByteLength, 'ByteLength')
let i = 1;
while (ByteLength > 0) {
i++;
let TmpBuffer;
// console.log("TmpBuffer", TmpBuffer)
console.log("Num", Num)
console.log("ByteLength", ByteLength)
console.log(_self.DeviceID, _self.serviceId, _self.characterId)
if (ByteLength > 20) {
TmpBuffer = _Buffer.slice(Num, Num + 20);
Num += 20;
ByteLength -= 20;
let _self = this;
uTool.delayed(250).then(() => {
console.log('执行常规循环')
// setTimeout(() => {
uni.writeBLECharacteristicValue({
deviceId: _self.DeviceID,
serviceId: _self.serviceId,
characteristicId: _self.characterId,
value: TmpBuffer,
success: (res) => {
/* 发送成功 */
console.log('前面的循环成功', res)
console.log(_self.DeviceID,_self.serviceId,_self.characterId,TmpBuffer)
},
fail: (error) => {
/* 发送失败 */
console.log('前面的循环失败', error)
console.log(_self.DeviceID,_self.serviceId,_self.characterId,TmpBuffer)
// setInterval(function() {
// }, 300)
// _self.sendAlwaydata()
}
});
// }, 200)
})
} else {
TmpBuffer = _Buffer.slice(Num, Num + ByteLength)
Num += ByteLength
ByteLength -= ByteLength
/* 当长度不满20的时候执行最后一次发送即可 */
uTool.delayed(250).then(() => {
console.log('执行最后一次')
// _self.sendAlwaydata()
// setTimeout(() => {
uni.writeBLECharacteristicValue({
deviceId: _self.DeviceID,
serviceId: _self.serviceId,
characteristicId: _self.characterId,
value: TmpBuffer,
success: (res) => {
/* 发送成功 */
console.log('最后一次写入成功', res)
console.log(_self.DeviceID,_self.serviceId,_self.characterId,TmpBuffer)
},
fail: (error) => {
/* 发送失败 */
console.log('最后一次写入失败', error)
console.log(_self.DeviceID,_self.serviceId,_self.characterId,TmpBuffer)
// _self.sendAlwaydata()
}
})
// }, 200)
})
}
}
},