XTiger
XTiger
  • 发布:2020-05-06 11:52
  • 更新:2024-11-01 15:25
  • 阅读:5077

【报Bug】BLE蓝牙问题,uni.onBLEConnectionStateChange等回调函数多次执行

分类:uni-app

新版本的IDE(2.6.16)使用用API连接ble蓝牙的时候,uni.onBLEConnectionStateChange,uni.onBLECharacteristicValueChange等回调函数,会随着执行的次数增多持续增加回调次数,比如:第一次执行,则回调一次,断开连接后,再执行就会回调两次,再继续就会回调3次、4次。具体可以看以下日志。
测试的手机系统是安卓9.1.1,手机是荣耀9X

第一次运行
11:10:52.966 App Hide at App.vue:43
11:38:53.360 App Show at App.vue:40
11:38:55.720 A5 at common\lockApi.js:178
11:38:55.740 openBluetooth at common\lockApi.js:178
11:38:55.760 open adapter success: {"errMsg":"openBluetoothAdapter:ok"} at common\lockApi.js:178
11:38:57.780 Device Find:BLE_LOCK,D6:49:C1:11:2C:77 at common\lockApi.js:178
11:38:57.800 connectionDelay:500 at common\lockApi.js:178
11:38:58.280 start connect at common\lockApi.js:178
11:38:58.980 create connection success: {"errMsg":"createBLEConnection:ok"} at common\lockApi.js:178
11:38:59.000 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:00.000 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:00.200 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:00.220 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:00.320 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178

11:39:00.340 res:00§00«49ª37 at common\lockApi.js:336
11:39:00.360 success at common\lockApi.js:178
11:39:00.380 close at common\lockApi.js:178
11:39:00.420 close connection success: {"errMsg":"closeBLEConnection:ok"} at common\lockApi.js:178
11:39:00.440 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:00.480 closeBluetoothAdapter success: {"errMsg":"closeBluetoothAdapter:ok"} at common\lockApi.js:178

第二次运行
11:39:08.500 A5 at common\lockApi.js:178
11:39:08.520 openBluetooth at common\lockApi.js:178
11:39:08.540 open adapter success: {"errMsg":"openBluetoothAdapter:ok"} at common\lockApi.js:178
11:39:08.800 Device Find:BLE_LOCK,D6:49:C1:11:2C:77 at common\lockApi.js:178
11:39:08.820 connectionDelay:500 at common\lockApi.js:178
11:39:09.320 start connect at common\lockApi.js:178
11:39:09.961 create connection success: {"errMsg":"createBLEConnection:ok"} at common\lockApi.js:178
11:39:09.981 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:10.001 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:11.001 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:11.021 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:11.221 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:11.241 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:11.261 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:11.381 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178
11:39:11.401 res:00§00«49ª37 at common\lockApi.js:336
11:39:11.421 success at common\lockApi.js:178
11:39:11.441 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178
11:39:11.461 close at common\lockApi.js:178
11:39:11.481 close connection success: {"errMsg":"closeBLEConnection:ok"} at common\lockApi.js:178
11:39:11.501 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:11.521 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178

11:39:11.541 closeBluetoothAdapter success: {"errMsg":"closeBluetoothAdapter:ok"} at common\lockApi.js:178

第三次运行
11:39:25.976 A5 at common\lockApi.js:178
11:39:25.996 openBluetooth at common\lockApi.js:178
11:39:26.016 open adapter success: {"errMsg":"openBluetoothAdapter:ok"} at common\lockApi.js:178
11:39:26.316 Device Find:BLE_LOCK,D6:49:C1:11:2C:77 at common\lockApi.js:178
11:39:26.336 connectionDelay:500 at common\lockApi.js:178
11:39:26.836 start connect at common\lockApi.js:178
11:39:27.517 create connection success: {"errMsg":"createBLEConnection:ok"} at common\lockApi.js:178
11:39:27.537 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:27.557 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:27.577 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:28.559 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:28.579 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:28.599 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:28.779 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:28.799 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:28.819 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:28.839 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:28.859 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:28.919 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178
11:39:28.939 res:00§00«49ª37 at common\lockApi.js:336
11:39:28.959 success at common\lockApi.js:178
11:39:28.979 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178
11:39:28.999 onBLECharacteristicValueChange:d00ea53030a73030ab3439aa3337 at common\lockApi.js:178
11:39:29.019 close at common\lockApi.js:178
11:39:29.039 close connection success: {"errMsg":"closeBLEConnection:ok"} at common\lockApi.js:178
11:39:29.059 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:29.079 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:29.099 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:29.119 closeBluetoothAdapter success: {"errMsg":"closeBluetoothAdapter:ok"} at common\lockApi.js:178

第四次运行
11:39:38.099 A5 at common\lockApi.js:178
11:39:38.119 openBluetooth at common\lockApi.js:178
11:39:38.139 open adapter success: {"errMsg":"openBluetoothAdapter:ok"} at common\lockApi.js:178
11:39:38.859 Device Find:BLE_LOCK,D6:49:C1:11:2C:77 at common\lockApi.js:178
11:39:38.879 connectionDelay:500 at common\lockApi.js:178
11:39:39.379 start connect at common\lockApi.js:178
11:39:39.999 create connection success: {"errMsg":"createBLEConnection:ok"} at common\lockApi.js:178
11:39:40.019 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:40.039 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:40.059 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:40.079 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:41.019 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:41.039 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:41.059 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:41.079 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:41.239 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:41.259 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:41.279 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:41.299 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:41.319 write characteristics failed: {"errMsg":"writeBLECharacteristicValue:fail property not support","errCode":10007} at common\lockApi.js:178
11:39:41.339 reConnection at common\lockApi.js:178
11:39:41.359 close connection success: {"errMsg":"closeBLEConnection:ok"} at common\lockApi.js:178
11:39:41.379 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:41.399 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:41.419 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:41.439 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:41.479 connectionDelay:500 at common\lockApi.js:178
11:39:41.499 connectionDelay:500 at common\lockApi.js:178
11:39:41.519 connectionDelay:500 at common\lockApi.js:178
11:39:41.539 connectionDelay:500 at common\lockApi.js:178
11:39:41.999 start connect at common\lockApi.js:178
11:39:42.039 create connection success: {"errMsg":"createBLEConnection:ok"} at common\lockApi.js:178
11:39:42.059 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:42.079 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:42.099 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:42.119 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":true} at common\lockApi.js:178
11:39:43.079 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:43.099 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:43.119 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:43.139 set notify success: {"errMsg":"notifyBLECharacteristicValueChange:ok"} at common\lockApi.js:178
11:39:43.299 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:43.319 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:43.339 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:43.359 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:43.379 write characteristics success: {"errMsg":"writeBLECharacteristicValue:ok"} at common\lockApi.js:178
11:39:43.419 onBLECharacteristicValueChange:d005a53030 at common\lockApi.js:178
11:39:43.439 res:00 at common\lockApi.js:336
11:39:43.459 success at common\lockApi.js:178
11:39:43.479 onBLECharacteristicValueChange:d005a53030 at common\lockApi.js:178
11:39:43.499 onBLECharacteristicValueChange:d005a53030 at common\lockApi.js:178
11:39:43.519 onBLECharacteristicValueChange:d005a53030 at common\lockApi.js:178
11:39:43.539 close at common\lockApi.js:178
11:39:43.559 close connection success: {"errMsg":"closeBLEConnection:ok"} at common\lockApi.js:178
11:39:43.579 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:43.599 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:43.619 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:43.639 onBLEConnectionStateChange: {"deviceId":"D6:49:C1:11:2C:77","connected":false} at common\lockApi.js:178
11:39:43.659 closeBluetoothAdapter success: {"errMsg":"closeBluetoothAdapter:ok"} at common\lockApi.js:178

2020-05-06 11:52 负责人:DCloud_Android_zl 分享
已邀请:

最佳回复

DCloud_UNI_GSQ

DCloud_UNI_GSQ

HBuilderX alpha 2.9.0+ 已修复

其他版本临时解决方案:改用 plus.bluetooth.onBluetoothDeviceFound 等接口

  • 1***@qq.com

    试了一下,暂时没发现断开连接问题

    2020-10-08 11:11

  • 1***@qq.com

    writeBLECharacteristicValue:fail property not support 华为荣耀打印时好时坏就出现这个原因,其他手机没问题

    2020-11-09 16:13

  • 两个纠缠之缘

    回复 1***@qq.com: 老哥一样啊 有时候会出现这个问题 有什么解决办法吗

    2021-09-24 14:49

  • 7***@qq.com

    HBuilderX 3.6.4版本 没解决鸭

    2022-11-10 11:28

5***@qq.com

5***@qq.com

2.5.1版本没有这个问题,2.6.1有重复执行问题

XTiger

XTiger (作者)

更新了新版本,这个问题还是没有解决

XTiger

XTiger (作者)

更新了新版本,这个问题还是没有解决

XTiger

XTiger (作者)

更新了新版本,这个问题还是没有解决

XTiger

XTiger (作者)

这问题没人管吗?

XTiger

XTiger (作者)

这问题没人管吗?

XTiger

XTiger (作者)

更新了新版本,这个问题还是没有解决

DCloud_Android_zl

DCloud_Android_zl

麻烦提供一个测试demo。

  • XTiger (作者)

    已经发现问题出在哪了,我蓝牙访问的程序封装在一个js里,每次访问蓝牙时都会从头到结束执行一遍,

    uni.onBLEConnectionStateChange等监听程序也每次都会设置一次,以前这样做是没问题的,监听程序设置多次还是只会响应一次,现在是设置多少次就会响应多少次,但是又没有取消监听的函数,所以出问题。

    2020-05-20 20:58

  • DCloud_Android_zl

    回复 XTiger: 好的,问题解决了就可以。

    2020-05-20 21:03

  • XTiger (作者)

    回复 DCloud_Android_zl: 我现在的解决方式很别扭,我是设置一个合全局变量,用来判断是否执行设置监听程序,而且由于没有办法关闭监听程序,整个APP运行期间,监听程序会一直在运行,哪怕我已经结束了蓝牙执行过程。

    感觉以前结束蓝牙执行后,会取消监听,现在不会了,又没有提供取消监听程序的API,所以这个我也没办法。

    我觉得最好是你们从底层解决比较好,要么像以前一样,结束蓝牙的API里同时取消监听,或者提供相应的API。

    2020-05-20 21:20

  • 5***@qq.com

    回复 DCloud_Android_zl: 这问题能不能从底层解决

    2020-05-26 10:22

  • 5***@qq.com

    如果在程序里面会使用到两个蓝牙设备,会出问题啊!

    2020-05-26 10:32

  • o***@lfwellness.com

    回复 DCloud_Android_zl: 问题还存在,能否从底层解决呢?

    2020-07-23 12:02

l***@163.com

l***@163.com

这个问题讨厌的地方在于:当你断开连接,然后再重新连接,这时候必须重新注册否则无法触发监听,结果必然会出现重复通知。所以还是希望能在底层把这个问题解决掉。

  • 北斗星辰

    重新注册是什么意思?

    2023-02-14 11:59

r***@21cn.com

r***@21cn.com

好像2.7也有就问题?

8***@qq.com

8***@qq.com

这个问题什么时候能解决呀

小辛一族

小辛一族

3.1.22.20210709 这个版本还是没解决@DCloud_Android_zl

侠客请接刀s

侠客请接刀s

2022年了 还没解决

牛牛2021

牛牛2021

解决了吗?惆怅

7***@qq.com

7***@qq.com - 个人开发者

3.6.4.20220922版本,未解决鸭

wjwj

wjwj

都2023年了,问题还是有啊。。。。。。。。。。。

  • 红牛8

    亲,这个问题是怎么解决的?

    2023-10-14 11:47

7***@qq.com

7***@qq.com

2023-11-10 3.9.6hbuilder 也出现

码农一个

码农一个

手机端我不知道有没有

码农一个

码农一个

uni.offBLEConnectionStateChange 手机端我不知道有没有对应的方法,我没有试过,你们可以试下,我在官方的api没有找到,但是小程序端有wx.onBLEConnectionStateChange和对应的wx.offBLEConnectionStateChange结果多次回调事件的问题

1***@qq.com

1***@qq.com

现在还是有问题

  • r***@lingxtech.com

    他喵的 我也是,并且发现只有app存在,微信小程序没问题

    2024-05-28 10:31

4***@qq.com

4***@qq.com

我这边是ios会重复触发 有时候2次,有时候3次,安卓正常

沐子鱼

沐子鱼 - 你好

2024年了 还是有这个问题
不过有解决方法
全局封装蓝牙---做一个是否已经开启的开关 只开启一次

  • 2***@qq.com

    您好,我问一下,如果按照您的方案去做,断开重连的时,notify不开启就无法监听,如果开启就会启动两个notify,这有什么解决方法吗?

    2024-07-24 13:36

  • 沐子鱼

    回复 2***@qq.com: 断开重连 还是原先那个监听 app启动 蓝牙连接一次 只要打开一次就行了

    2024-07-25 18:09

  • 9***@qq.com

    我也是这样处理的,安卓可以了。但是发现微信小程序检测不到断线状态,不知道哪里的问题。

    2024-09-25 01:26

l***@163.com

l***@163.com

uni.offBLEConnectionStateChange();

有这个?

要回复问题请先登录注册