native-docs
native-docs copied to clipboard
onBLEConnectionStateChange怎么区分主动断开和连接异常断开
问题描述 [问题描述:尽可能简洁清晰地把问题描述清楚] onBLEConnectionStateChange监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等 1、现在要实现蓝牙断线重连功能,在onBLEConnectionStateChange里面进行重连操作,但是主动断开也会触发,怎么区分。 2、还有就是createBLEConnection连接低功耗蓝牙,设置了timeout,超时了也会走success
复现步骤 ` uni.onBLEConnectionStateChange(res => { commit('SET_CONNECTED', res.connected) // 断线重连 if(!res.connected && res.deviceId === state.deviceId){ uni.showModal({ title: '提示', content: '连接断开,是否要重连', success: function (res) { if (res.confirm) { uni.showLoading({ title: '尝试重连中...' }) dispatch('createBLEConnection', state.deviceId).then(res => { uni.showToast({ title: '重连成功', icon: 'none' }) }).catch(async(error) => { uni.hideLoading() initTypes(error.errCode) }) } else if (res.cancel) { dispatch('closeBLEConnection') } } }) } });
uni.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, timeout: 8000, success: res => { // 不管是否超时都进入这里,都设置deviceId,onBLEConnectionStateChange就没法判断了 commit('SET_DEVICE', deviceId) console.log(state.deviceId + '成功连接') resolve() }, fail: error => { commit('SET_DEVICE', '') reject(error) } }); `
预期结果 [使用简洁清晰的语言描述你希望生效的预期结果]
实际结果 [这里请贴上你的报错截图或文字]
系统信息:
- 发行平台: 5+ App
- 操作系统: Android 10.0
- HBuilderX版本 2.6.16
- 设备信息 荣耀v20