当前位置:   article > 正文

android 蓝牙串口自动断开,uni-app 蓝牙连接被自动断开问题

uni 蓝牙断开重连

遍历 快速对两个蓝牙设备(deviceId,mac地址不同)进行连接操作,时间隔为1秒,蓝牙刚进行连接,监听蓝牙连接状态,直接监听到被自动断开,而且断开两次,有知道怎么回事的不?ios ,android 有问题, 小程序没问题。

···javascript

autoConnect(){

let _this=this;

this.bleList.forEach((item,index)=>{

setTimeout(function(){

console.log(' ==== start auto connect ====')

_this.createBLEConnection(this.deviceId,true);

}.bind(item),1000);

}

});

},

/**

连接低功耗蓝牙设备。

@param {Object} deviceId 设备ID

@param {Object} isAutoConnect 是否自动连接

*/

createBLEConnection(deviceId,isAutoConnect){

uni.showLoading({title:'连接中...'});

console.log('连接中设备ID=>',deviceId);

uni.createBLEConnection({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId,

timeout:1000,

success:res=> {

uni.showToast({title:"成功",icon:'success'});

this.bleList[this.getDeviceIndex(deviceId)].isConnect=true;

this.saveConnectRecord(deviceId,true);

},

fail:error=>{

this.bleList[this.getDeviceIndex(deviceId)].isConnect=false;

this.bleList[this.getDeviceIndex(deviceId)].connectNum+=1;

if(error.errCode == 10003 && this.bleList[this.getDeviceIndex(deviceId)].connectNum<3){ //连接超时少于3次,自动重连

this.createBLEConnection(deviceId,true);

}else{

uni.showModal({

content:'连接失败,请重新连接!',

confirmText:'重新连接',

success:res=>{

res.confirm && this.createBLEConnection(deviceId,false);

}

});

}

console.log("连接失败"+ JSON.stringify(error));

},

complete:cRes=>{

uni.hideLoading();

console.log('连接 completed',cRes);

}

});

}

···

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/887295
推荐阅读
相关标签
  

闽ICP备14008679号