无法获取errCode uni-app

无法获取errCode uni-app

测试过的手机:

测试环境 测试环境 测试环境

示例代码:

initBluetooth() {  
    return new Promise((resolve, reject) =>{
        uni.openBluetoothAdapter({  
            success: resolve,  
            fail(error) {  
                console.log("init bluebooth", error)  
            }  
        })  
    })  
}
init bluebooth {errMsg: "openBluetoothAdapter:fail already opened"}

操作步骤:

  1. 通过Hbuilder创建一个新的uniapp项目
  2. 添加上面的测试代码
  3. 运行到微信小程序

预期结果:

init bluebooth {errMsg: "openBluetoothAdapter:fail already opened", errCode: -1}

实际结果:

init bluebooth {errMsg: "openBluetoothAdapter:fail already opened"}

bug描述:

在调用uni.openBluetoothAdapteruni.startBluetoothDevicesDiscovery 两个API的过程中, 回调函数successfail的返回结果中仅仅包含errMsg没有errCode


更多关于无法获取errCode uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

模拟器没有errCode!!!

更多关于无法获取errCode uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html


使用微信小程序的wx.方法呢也存在这个问题?

在微信小程序环境中,uni.openBluetoothAdapter API的fail回调返回对象确实可能缺少errCode字段。这是因为uni-app底层调用了微信小程序的蓝牙API,而微信小程序本身在某些错误情况下可能不会返回完整的错误码信息。

从你提供的错误信息"openBluetoothAdapter:fail already opened"来看,这表示蓝牙适配器已经处于开启状态。这种情况下微信小程序可能没有提供标准的错误码。

建议的处理方式:

  1. 通过errMsg进行错误判断和处理
  2. 在调用前先检查蓝牙适配器状态,避免重复初始化
  3. 可以使用uni.getBluetoothAdapterState检查当前适配器状态

示例改进代码:

async initBluetooth() {
    try {
        const state = await uni.getBluetoothAdapterState()
        if (state.available) {
            console.log('蓝牙适配器已开启')
            return
        }
    } catch (e) {
        // 获取状态失败,尝试开启
    }
    
    return new Promise((resolve, reject) => {
        uni.openBluetoothAdapter({
            success: resolve,
            fail: reject
        })
    })
}
回到顶部