heesim
heesim
  • 发布:2020-06-16 14:33
  • 更新:2023-02-07 14:19
  • 阅读:4061

【报Bug】安卓端蓝牙断开后无法再次连接到设备

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: WIN10专业版

HBuilderX类型: 正式

HBuilderX版本号: 2.6.16

手机系统: Android

手机系统版本号: Android 10

手机厂商: oneplus

手机机型: 7T PRO

页面类型: vue

打包方式: 离线

项目创建方式: HBuilderX

操作步骤:

1.连接蓝牙设备前的前序工作及正常连接至蓝牙设备
2.手动关闭手机蓝牙再打开
3.尝试与蓝牙设备建立连接
4.重启app重新执行123

预期结果:

尝试与蓝牙设备建立连接,createBLEConnection进入成功回调,onBLEConnectionStateChange监听到true状态

实际结果:

尝试与蓝牙设备建立连接,createBLEConnection进入成功回调,onBLEConnectionStateChange监听到false状态
重启app无法解决该问题,必须得与其他设备建立连接后才能再次与原有设备建立连接。

bug描述:

安卓端蓝牙连接上设备的时候,手动关闭手机蓝牙功能再次打开,导致蓝牙无法再次连接上设备。
复现demo见附件。
该情况只出现在安卓端,ios端可以正常连接。
先把代码贴一下,后续讲复现的步骤

    var that ;  
    export default {  
        data() {  
            return {  
                title: '复现demo',  
                bleAdapter:{available:false},  
                bleConnection:false,  
                list:[]  

            }  
        },  
        onLoad() {  
            that = this;  
            this.initBLEAdP();  
        },  
        methods: {  
            onAdapter(){  
                console.log('启动-适配器-监听器');  
                uni.onBluetoothAdapterStateChange(function(e){  
                    console.log('监听到适配器的状态 =》',JSON.stringify(e))  
                    that.bleAdapter = e  

                })  
            },  
            onConnection(){  
                console.log('启动-连接状态-监听器');  
                uni.onBLEConnectionStateChange(function(e){  
                    that.bleConnection = e.connected  
                    console.log('监听到蓝牙设备连接的状态 =》',JSON.stringify(e))  
                })  
            },  
            initBLEAdP(){  
                uni.openBluetoothAdapter({  
                    success(e) {  
                        console.log('蓝牙适配器初始化成功!',e);  
                        that.bleAdapter={available:true};  
                        that.onAdapter();  
                        that.onConnection();  
                    }  
                })  
            },  
            //开始搜索蓝牙设备  
            startBluetoothDevicesDiscovery() {  
                if(!this.bleAdapter.available){  
                    this.initBLEAdP();  
                    return  
                }  
                uni.startBluetoothDevicesDiscovery({  
                    success(res) {  
                        console.log("正在搜索蓝牙设备...,启动现设备监听")  
                        setTimeout(function(){  
                            that.stopBluetoothDevicesDiscovery()  
                        },30000)  
                        that.onBluetoothDeviceFound();  
                    },  
                    fail(e) {  
                        console.log('搜索蓝牙失败,错误码:' + (e.errCode || e.errMsg));  
                    }  
                })  
            },  
            //监听发现蓝牙设备  
            onBluetoothDeviceFound() {  
                    console.log("启动发现设备监听")  
                uni.onBluetoothDeviceFound(devices => {  
                    this.getBluetoothDevices();  
                });  

            },  
            //获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备  
            getBluetoothDevices() {  
                uni.getBluetoothDevices({  
                    success: res => {  
                        // console.log(JSON.stringify(res))  
                        var alldevices = [];  
                        res.devices.forEach(device => {  
                            if (device.name.indexOf('BAECTM05') != -1 || device.name.indexOf('SMS1A1TH000080') != -1) {  
                                alldevices.push(device)  
                            }  
                        })  

                        this.list = alldevices;  
                    },  
                    fail: e => {  
                        console.log('获取蓝牙设备错误,错误码:' + e.errCode);  
                        if (e.errCode !== 0) {  
                            initTypes(e.errCode);  
                        }  
                    }  
                });  
            },  
            stopBluetoothDevicesDiscovery(types) {  
                uni.stopBluetoothDevicesDiscovery({  
                    success: e => {  
                        console.log('停止搜索蓝牙设备:' + e.errMsg);  
                    },  
                    fail: e => {  
                        console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);  
                        if (e.errCode !== 0) {  
                            initTypes(e.errCode);  
                        }  
                    }  
                });  
            },  
            connectToBLE(item){  
                var deviceId = item.deviceId;  
                console.log('device==>',deviceId)  
                uni.createBLEConnection({  
                    deviceId,  
                    success(e) {  
                        console.log(JSON.stringify(e))  
                    }  
                })  

            }  
        }  
    }

1.先按正常的连接步骤进行操作
a 进入app 先初始化蓝牙模块,初始化成功后启动两个监听器-连接状态监听器和蓝牙模块监听器
b 搜索发现蓝牙设备列表(代码中过滤了部分设备,只留下笔者测试所用的设备)
c 连接上任意一个设备。
此时控制台打印内容

App:onLaunch have been invoked {"path":"pages/index/index","query":{},"scene":1001}  
null:25 App Launch  uni-app:///App.vue:4  
App:onShow have been invoked {"path":"pages/index/index","query":{},"scene":1001}  
null:25 App Show  uni-app:///App.vue:7  
pages/index/index[1]:onLoad have been invoked  
pages/index/index[1]:onShow have been invoked  
null:25 蓝牙适配器初始化成功! {code: undefined, message: undefined, errMsg: "openBluetoothAdapter:ok"}  uni-app:///pages\index\index.vue:52  
null:25 启动-适配器-监听器  uni-app:///pages\index\index.vue:35  
null:25 启动-连接状态-监听器  uni-app:///pages\index\index.vue:43  
pages/index/index[1]:onReady have been invoked  
null:25 正在搜索蓝牙设备...,启动现设备监听  uni-app:///pages\index\index.vue:67  
null:25 启动发现设备监听  uni-app:///pages\index\index.vue:80  
null:25 监听到适配器的状态 =》 {"discovering":true,"available":true}  uni-app:///pages\index\index.vue:37  
null:25 停止搜索蓝牙设备:stopBluetoothDevicesDiscovery:ok  uni-app:///pages\index\index.vue:111  
null:25 监听到适配器的状态 =》 {"discovering":false,"available":true}  uni-app:///pages\index\index.vue:37  
null:25 device==> D6:0D:2E:41:FD:7A  uni-app:///pages\index\index.vue:123  
null:25 {"errMsg":"createBLEConnection:ok"}  uni-app:///pages\index\index.vue:127  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"D6:0D:2E:41:FD:7A","connected":true}  uni-app:///pages\index\index.vue:46

2.手动关闭手机蓝牙功能,再打开(该bug核心问题),控制台打印如下

监听到适配器的状态 =》 {"discovering":false,"available":false}  uni-app:///pages\index\index.vue:37  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"D6:0D:2E:41:FD:7A","connected":false}  uni-app:///pages\index\index.vue:46  
2null:25 监听到适配器的状态 =》 {"discovering":false,"available":false}  uni-app:///pages\index\index.vue:37  
null:25 监听到适配器的状态 =》 {"discovering":false,"available":true}  uni-app:///pages\index\index.vue:37

此时手动关闭手机蓝牙的时候会断开设备的连接。再次打开蓝牙,蓝牙适配器的状态是可用的。
3.此时想与"deviceId":"D6:0D:2E:41:FD:7A"的设备建立连接
控制台打印如下

device==> F5:77:B9:CD:E4:5B  uni-app:///pages\index\index.vue:123  
null:25 {"errMsg":"createBLEConnection:ok"}  uni-app:///pages\index\index.vue:127  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"F5:77:B9:CD:E4:5B","connected":false}  uni-app:///pages\index\index.vue:46

控制台先打印(device==> F5:77:B9:CD:E4:5B),过了大概30s后,进入了createBLEConnection的成功回调!但是蓝牙连接状态的监听器缺监听到是 false的连接状态!!!!
此时我已经无法理解为什么进入连接成功的回调但是监听到的是异常断开的状态。其次也不是设备连上蓝牙闪断的原因。如果是闪断的化应该会先监听到一个 连接为true的状态再监听到false的状态。
OK,遇到不会的问题咱们就重启呗。
4.重启app,此时的手机蓝牙是打开着的。重复步骤1,此时控制台打印的一切都正常
5.连接上次异常断开的那个设备,也就是id为F5:77:B9:CD:E4:5B的设备`

device==> F5:77:B9:CD:E4:5B  uni-app:///pages\index\index.vue:123  
null:25 {"errMsg":"createBLEConnection:ok"}  uni-app:///pages\index\index.vue:127  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"F5:77:B9:CD:E4:5B","connected":false}  uni-app:///pages\index\index.vue:46

结果和步骤3一样。
重启多次之后仍是无法正常的连接上该设备。
6.尝试连接其他设备

device==> D6:0D:2E:41:FD:7A  uni-app:///pages\index\index.vue:123  
null:25 {"errMsg":"createBLEConnection:ok"}  uni-app:///pages\index\index.vue:127  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"D6:0D:2E:41:FD:7A","connected":true}  uni-app:///pages\index\index.vue:46

能够成功的建立连接
此时再回去与异常断开的设备进行连接

device==> F5:77:B9:CD:E4:5B  uni-app:///pages\index\index.vue:123  
null:25 {"errMsg":"createBLEConnection:ok"}  uni-app:///pages\index\index.vue:127  
null:25 监听到蓝牙设备连接的状态 =》 {"deviceId":"F5:77:B9:CD:E4:5B","connected":true}  uni-app:///pages\index\index.vue:46

已经能成功连接上了!

2020-06-16 14:33 负责人:DCloud_Android_zl 分享
已邀请:
heesim

heesim (作者) - zsp

HbuilderX 2.6.16版本

虽然说该流程有点不正常,但是存在这种可能性且重启无法解决。希望官方能够告知如何规避这种情况的出现。毕竟app在用户手中,用户能够操作的权限我们在app里面也处理不到这一块。

  • 自用仓库

    楼主这个问题解决了么

    2020-06-28 15:38

  • heesim (作者)

    回复 自用仓库: 你试试先执行搜索,搜到设备再进行连接。

    2020-06-28 17:03

  • 自用仓库

    回复 heesim: 每次都是重新搜索了,但是必须得重新断开蓝牙设备才能获取到数据

    2020-06-28 17:24

  • heesim (作者)

    回复 自用仓库: 没有太明白你的点,关掉手机蓝牙之后蓝牙设备与手机肯定是处于断开连接的状态吧?这时候执行断开连接的接口不会报错吗?你按我这个复现的demo去试试,写一个promise,当getdevice列表中存在你要连接的设备再resolve,用then去执行connectBLE

    2020-06-28 20:35

  • 自用仓库

    回复 heesim:谢谢,解决了

    2020-06-29 09:08

  • 牛牛2021

    回复 自用仓库: 怎么解决了?可以分享下吗?

    2022-11-10 11:29

机智发

机智发

请问怎么解决的啊

牛牛2021

牛牛2021

请问怎么解决的啊

4***@qq.com

4***@qq.com

安卓机断开连接之后调用closeBluetoothAdapter关闭适配器,亲测能够断开重连上

要回复问题请先登录注册