2***@qq.com
2***@qq.com
  • 发布:2020-12-11 13:44
  • 更新:2023-01-12 22:05
  • 阅读:2302

【报Bug】createBLEConnection 在调用24次左右后,即使无法连接设备,也会触发success回调函数。

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

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

HBuilderX类型: 正式

HBuilderX版本号: 2.9.8

手机系统: Android

手机系统版本号: Android 10

手机厂商: 小米

手机机型: MI8

页面类型: vue

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

printer.js


var printerData = {  
    connected:false,  
    connectedName:'weihuang',  
    deviceInfo:{},  
    deviceId:'EC:11:27:88:F3:D8',  
    deviceName:'weihuang',  
    serviceId:'000018F0-0000-1000-8000-00805F9B34FB',  
    characteristId:'00002AF1-0000-1000-8000-00805F9B34FB',  

}  

var trying = false;  

function openBluetoothAdapter()  
{  
    // device.checkOpenGPSService();  
    uni.openBluetoothAdapter({  
          success: (res) => {  
            console.log('openBluetoothAdapter success', res);  
            createBLEConnection();  
            // this.startBluetoothDevicesDiscovery()  
          },  
          fail: (res) => {  
            // if (res.errCode === 10001) {  
            //   uni.onBluetoothAdapterStateChange(function (res) {  
            //     console.log('onBluetoothAdapterStateChange', res)  
            //     if (res.available) {  
            //       this.startBluetoothDevicesDiscovery()  
            //     }  
            //   })  
            // }  
            console.log('openBluetoothAdapter fail', res);  
          }  
    })  
}  
function onBLEConnectionStateChange()  
{  
    console.log('onBLEConnectionStateChange');  
    uni.onBLEConnectionStateChange(res => {  
        // 该方法回调中可以用于处理连接意外断开等异常情况  
        console.log(`蓝牙连接状态 -------------------------->`);  
        console.log(JSON.stringify(res));  
        if (res.deviceId==='EC:11:27:88:F3:D8'&&!res.connected) {  
            // if (this.isStop) return;  
            // console.log('断开低功耗蓝牙成功:');  
            if(trying)  
            {  
                return;  
            }  
            printerData.connected=false;  
            uni.showToast({  
                icon: "none",  
                title: "蓝牙已经断开!",  
                mask: false,  
                duration: 3000  
            });  
            closeBLEConnection();  
            setTimeout(function(){  
                trying = true;  
                return createBLEConnection()    
            },1000)  
            // createBLEConnection();  

            // 要用闭包,不然会有空指针错误  
            // setTimeout(createBLEConnection,1000);  
            // setTimeout(function(){    
            //     return createBLEConnection()    
            // },3000)  

            //在这里尝试重连  
            // createBLEConnection(callback,buffer);  
            //关闭连接  
            // this.closeBluetoothAdapter();  
        }  
    });  
    // setInterval(onBLEConnectionStateChange,1000);  
}  
function closeBLEConnection(){  
    uni.closeBLEConnection({  
      deviceId:printerData.deviceId,  
      success:res=> {  
          // this.isLink.splice(index,1,4)  
        console.log('closeBLEConnection',res)  
      }  
    })  
}  

function closeBluetoothAdapter(){  
    uni.closeBluetoothAdapter({  
      success: res => {  
          // this.appendMSG("closeBluetoothAdapter success",res);  
        console.log('关闭蓝牙适配器');  
      }  
    });  
}  
// 可以不用发现直接连接  
function startBluetoothDevicesDiscovery() { //开始扫描  
    uni.startBluetoothDevicesDiscovery({  
      allowDuplicatesKey: true,  
      success: (res) => {  
        console.log('startBluetoothDevicesDiscovery success', res)  

        // this.onBluetoothDeviceFound()  
      },  
    })  
}  

function createBLEConnection(){  
    console.log("createBLEConnection")  
    uni.createBLEConnection({  
          deviceId:printerData.deviceId,  
          timeout: 4000,  
          success: (res) => {  
            uni.showToast({  
              title: '连接蓝牙打印机成功!',  
              icon: 'success',  
              duration: 1000,  
              mask: true  
            })  
            printerData.connected=true;  
            console.log("createBLEConnection success",JSON.stringify(res))  
            trying = false;  
          },  
          fail(err) {  

            console.log("连接失败,错误=", err)  

            // 要用闭包,不然会有空指针错误  
            // setTimeout(createBLEConnection,1000);  
            setTimeout(function(){    
                return createBLEConnection()    
            },1000)  
          },  
        })  
}  

function notifyBLECharacteristicValueChange()  
{  
    let deviceId = printerData.deviceId;  
    let serviceId = printerData.serviceId;  
    let characteristicId = printerData.characteristId;  

    uni.notifyBLECharacteristicValueChange({  
        state: true, // 启用 notify 功能  
        deviceId,  
        serviceId,  
        characteristicId,  
        success: res => {  
            uni.onBLECharacteristicValueChange((res) => {  
                console.log('特征值变化:', res)  
            });  
        },  
        fail: res => {  
            console.log('notifyBLECharacteristicValueChange 失败:' + res.errMsg);  
        }  
    });  
}  

function SendArray(buffer){  
    console.log('sendarray',buffer.byteLength);  
    if (printerData.connected == false) {  
        uni.showToast({  
          title: '蓝牙未连接,取消操作',  
          duration: 1000,  
          mask: true  
        })  
      console.log("SendArray connected 的值为false,取消发送")  
      return  
    }  

    // 1.并行调用多次会存在写失败的可能性  
    // 2.建议每次写入不超过20字节  
    // 分包处理,延时调用   
    const maxChunk = 20;  
    const delay = 20;  
    for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {  
        let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);  
        setTimeout(writeData, j * delay, subPackage);  
    }  
}  

function writeData(tmpBuffer) { //发送数据,蓝牙设备特征值对应的二进制值  
    // console.log('writeData',tmpBuffer);  
    uni.writeBLECharacteristicValue({  
      deviceId: printerData.deviceId,  
      serviceId: printerData.serviceId,  
      characteristicId: printerData.characteristId,  
      value: tmpBuffer,  
      success: (res) => {  

         // console.log("发送成功:"+charjs.buffer2String(tmpBuffer),tmpBuffer.byteLength)  
      },  
      fail: (res) => {  
        console.log("发送失败:",res)  
        uni.showToast({  
          title: "发送失败" + res.errMsg,  
          icon: '../icon/fail',  
          duration: 1000  
        })  

      },  

    })  

}  
module.exports = {  

  openBluetoothAdapter: openBluetoothAdapter,  
  closeBluetoothAdapter:closeBluetoothAdapter,  
  onBLEConnectionStateChange:onBLEConnectionStateChange,  

}

App.vue

<script>  
    import printer from 'common/printer.js'  
    export default {  
        onLaunch: function() {  
            console.log('App Launch')  
        },  
        onShow: function() {  
            console.log('App Show')  
            printer.openBluetoothAdapter();  
            printer.onBLEConnectionStateChange();  
        },  
        onHide: function() {  
            console.log('App Hide')  
        }  
    }  
</script>  

<style>  
    /*每个页面公共css */  
</style>  

操作步骤:

连接真机,启动调试即可

预期结果:

一直重复输出如下调试信息
13:28:23.494 createBLEConnection at common/printer.js:110
13:28:27.538 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer.js:127

实际结果:

11:43:21.664 openBluetoothAdapter success, [Object] {"errMsg":"openBluetoothAdapter:ok"} at common/printer/printer.js:25
11:43:21.681 createBLEConnection at common/printer/printer.js:115
11:43:21.701 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:21.723 createBLEConnection at common/printer/printer.js:115
11:43:21.744 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:21.764 createBLEConnection at common/printer/printer.js:115
11:43:24.134 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:25.139 createBLEConnection at common/printer/printer.js:115
11:43:29.159 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:30.169 createBLEConnection at common/printer/printer.js:115
11:43:34.171 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:35.184 createBLEConnection at common/printer/printer.js:115
11:43:39.202 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:40.214 createBLEConnection at common/printer/printer.js:115
11:43:44.213 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:45.217 createBLEConnection at common/printer/printer.js:115
11:43:49.251 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:50.243 createBLEConnection at common/printer/printer.js:115
11:43:54.261 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:43:55.270 createBLEConnection at common/printer/printer.js:115
11:43:59.286 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:00.294 createBLEConnection at common/printer/printer.js:115
11:44:04.310 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:05.319 createBLEConnection at common/printer/printer.js:115
11:44:09.337 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:10.342 createBLEConnection at common/printer/printer.js:115
11:44:14.360 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:15.366 createBLEConnection at common/printer/printer.js:115
11:44:19.386 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:20.374 createBLEConnection at common/printer/printer.js:115
11:44:24.404 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:25.412 createBLEConnection at common/printer/printer.js:115
11:44:29.437 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:30.426 createBLEConnection at common/printer/printer.js:115
11:44:34.468 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:35.451 createBLEConnection at common/printer/printer.js:115
11:44:39.493 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:40.474 createBLEConnection at common/printer/printer.js:115
11:44:44.509 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:45.501 createBLEConnection at common/printer/printer.js:115
11:44:49.539 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:50.527 createBLEConnection at common/printer/printer.js:115
11:44:54.556 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:44:55.545 createBLEConnection at common/printer/printer.js:115
11:44:59.586 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:45:00.587 createBLEConnection at common/printer/printer.js:115
11:45:04.593 连接失败,错误=, [Object] {"errMsg":"createBLEConnection:fail operate time out","errCode":10012} at common/printer/printer.js:132
11:45:05.604 createBLEConnection at common/printer/printer.js:115
11:45:05.625 createBLEConnection success, {"errMsg":"createBLEConnection:ok"} at common/printer/printer.js:127
11:45:05.645 蓝牙连接状态 --------------------------> at common/printer/printer.js:47
11:45:05.666 {"deviceId":"EC:11:27:88:F3:D8","connected":false} at common/printer/printer.js:48
11:45:05.687 closeBLEConnection, [Object] {"errMsg":"closeBLEConnection:ok"} at common/printer/printer.js:89

bug描述:

想要实现蓝牙连接后台自动连接功能,所以在项目加载时启用蓝牙并注册连接状态变更事件。
之后不断尝试创建蓝牙连接。
经过测试,在尝试创建连接24次左右,即使没有打开蓝牙设备,也就是说根本无法连接成功的情况下,创建连接方法的回调函数会触发success,并返回连接成功信息。但这是通过状态回调函数返回的连接状态是false。在这之后,感觉连接管理这块出现错乱,导致即使真的启动蓝牙设备,也无法连接成功。

附件是单独摘出来的演示代码,可以直接运行。

2020-12-11 13:44 负责人:无 分享
已邀请:

最佳回复

DCloud

DCloud

HBuilderX 3.6.17.20230112 已修复。

s***@dhysmz.com

s***@dhysmz.com

我也发现了这个问题,竟然一年了都没人回复

  • 2***@qq.com

    太坑了,现在也是这个问题,多次连接重连后,29次后uni.createBLEConnection一直都是success了,其实根本就没连上

    2022-11-07 19:40

2***@qq.com

2***@qq.com - nblvdong

我也是发现这个问题,后面根本连不上,除非关掉app进程;这个问题太严重了,定时重连就不行了;

牛牛2021

牛牛2021

解决了吗?遇到同样问题

DCloud_Android_THB

DCloud_Android_THB

我复现一下

  • 刨地瓜

    项目实测,小米12在连接上后,关闭蓝牙设备,进行定时重连操作。第一次会走fail回调: {"errMsg":"createBLEConnection:fail operate time out","errCode":10012,"code":10012}。 后续次数执行的回调为:{"errMsg":"createBLEConnection:ok"}。 且测试在关闭设备的情况下重连次数超过40次(每次操作的间隔3秒)后, 开启设备,执行蓝牙连接也无法连接上设备。只有在重启 APP后才能正常连接

    2022-12-29 17:08

  • 刨地瓜

    回复 刨地瓜: 这个问题与手机有关系。部分手机即使重连执行了success回调:{"errMsg":"createBLEConnection:ok"}。 在蓝牙设备开启后,进行连接也是能连接上的。

    2022-12-29 17:11

DCloud_Android_zl

DCloud_Android_zl

问题已经确定,将会在下次发版修复。

要回复问题请先登录注册