/** 开锁 */
const openLock = () => {
uni.showLoading({
title: '正在开锁',
mask: true
});
uni.getBLEDeviceCharacteristics({
deviceId: deviceId.value,
serviceId: targetServiceId
});
const instruction = calculateUnlockingInstructions(deviceId.value);
const data = hex2ArrayBuffer(instruction);
if (!isCreateListener.value) {
createListener();
}
setTimeout(async () => {
await sendToDevice(deviceId.value, hex2ArrayBuffer(obtainElectricity));
await sendToDevice(deviceId.value, data);
uni.hideLoading();
}, 800);
};
/** 创建监听 */
const createListener = () => {
uni.notifyBLECharacteristicValueChange({
deviceId: deviceId.value,
serviceId: targetServiceId,
characteristicId: targetCharacteristicId_NOTIFY,
state: true,
success: () => {
if (!isCreateListener.value) {
isCreateListener.value = true;
uni.onBLECharacteristicValueChange((res) => {
let result = ab2hex(res.value as any);
if (result.indexOf('50') > -1) {
const decode = xOr(`${deviceId.value}:${result.slice(6, 8)}`);
const power = parseInt(decode, 16);
uni.showModal({
content: `电量${power}`
});
}
});
}
},
fail: (err) => {
uni.showModal({
content: `启动notify失败,原因${err.errMsg},错误码${err.code}`
});
}
});
};

- 发布:2025-07-24 14:51
- 更新:2025-07-24 14:51
- 阅读:282
产品分类: uniapp/App
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: Mac min Apple M1
HBuilderX类型: 正式
HBuilderX版本号: 4.66
手机系统: Android
手机系统版本号: Android 15
手机厂商: 小米
手机机型: REDMI K80 Pro
页面类型: vue
vue版本: vue3
打包方式: 云端
项目创建方式: HBuilderX
操作步骤:
预期结果:
创建一次监听代码每次执行都会触发监听
创建一次监听代码每次执行都会触发监听
实际结果:
但是只执行一次,如果多次创建就会一直叠加监听效果
但是只执行一次,如果多次创建就会一直叠加监听效果
bug描述:
低功耗蓝牙的监听事件无法移除,使用closeBluetoothAdapter和closeBLEConnection都没有效果,如果只创建一次那么onBLECharacteristicValueChange就执行一次,如果创建多次那么监听效果就无限叠加
0 个回复