HarmonyOS 蓝牙取消订阅扫描,仍然能收到扫描结果回调

调用了取消订阅扫描结果API后,依然能收到扫描到蓝牙的订阅回调

以UtilDemo为例。当客户端打开ble扫描,打开ble订阅扫描,以获取列表。这个时候我关闭ble订阅扫描,回调方还会一直在跑。麻烦确认下这块的逻辑

onBLEDeviceFind() {
  ble.on('BLEDeviceFind', (data: Array<ble.ScanResult>) => {
    console.info('bluetooth device find on = ' + JSON.stringify(data));
    this.bleDevices = data;
  });
  this.deviceFindSwitch = true;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
HarmonyOS
2025-01-10 09:50:41
1.0w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可能为监听关闭作用的callback不同未导致,修改后本地验证无问题可参考如下方式:

/**
 * 订阅BLE设备发现
 */
onBLEDeviceFind() {
  ble.on('BLEDeviceFind', this.onDataReceive);
  this.deviceFindSwitch = true;
}
/**
 * 取消订阅BLE设备发现
 */
offBLEDeviceFind() {
  ble.off('BLEDeviceFind',this.onDataReceive);
  this.deviceFindSwitch = false;
  console.info('test bluetooth device find deviceFindSwitch = ' + JSON.stringify(this.deviceFindSwitch))
}
//callback封装
onDataReceive = (data: Array<ble.ScanResult>) => {
  console.info('bluetooth device find on = ' + JSON.stringify(data));
  this.bleDevices = data;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
分享
微博
QQ
微信
回复
2025-01-10 12:34:46


相关问题
HarmonyOS wifi 扫描问题
987浏览 • 1回复 待解决
支付成功后没有收到
2324浏览 • 1回复 待解决
HarmonyOS customScan 如何扫描本地图片
439浏览 • 1回复 待解决
HarmonyOS 系统OCR扫描银行卡
562浏览 • 1回复 待解决
HarmonyOS代码规范扫描的方案及实现
425浏览 • 1回复 待解决
HarmonyOS ArkTS扫描UI该如何实现
558浏览 • 1回复 待解决