uniapp中,为什么跳转回tabBar页面就无法接收监听?

onBLECharacteristicValueChange这个监听的文档地址:https://uniapp.dcloud.net.cn/api/system/ble.html#onblecharact...

无论在哪个页面都能收到这个api的监听,唯独跳转回主页tabBar页面监听就没了,所以这个监听本身应该是没问题的?只是不知道返回主页的tabBar页面有什么我不知道的机制导致监听失效,

我把这个监听放在APP.VUE的onLaunch里面,但实际上不管放在哪个页面(除主页外)的效果都和上述一致。

求教大神们是哪里出问题了,用了几个跳转的api都不行↓

this.$u.route({
  url: 'pages/index/index',
  type: 'tab',
})
uni.switchTab({
  url: '/pages/index/index'
});

小弟在这里先感谢各位大神们,感激不尽,祝大神们发财祝大神们取漂亮老婆

阅读 2.8k
2 个回答

APP.VUE的onLaunch是首次打开APP时,初始化完成时触发(全局只触发一次)
tabBar页面可以放在onShow()方法里,每次打开页面都会触发

可以在main.js中调用,使用uni.$emit uni.$on来监听。

/** 监听低功耗蓝牙设备的特征值变化 */
uni?.onBLECharacteristicValueChange &&
  uni.onBLECharacteristicValueChange((res) => {
    const { value: readData, characteristicId, deviceId } = res;
    const huxStr = buf2hex(readData);
    console.log(`触发的设备id:${characteristicId} ,得到的数据 ${huxStr}`);
    uni.$emit("readData", { msg: huxStr, deviceId, bufferArr: readData });
  });

在页面中使用

    uni.$on("readData", (data) => {
      const { msg, deviceId, bufferArr } = data;
      if (deviceId !== props.deviceId) return;
      console.log(msg);
    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏