uni-app中uni.openBluetoothAdapter没有回调

uni-app中uni.openBluetoothAdapter没有回调

开发环境 版本号 项目创建方式
Mac 10.15.0 CLI
Android Android 10 -
小米 - -
小米 10 - -
### 示例代码:

```javascript
openBluetoothAdapter() {
    let me = this;
    console.log("openBluetoothAdapter")
    uni.openBluetoothAdapter({
        success(res){
            // 状态正常
            console.log("success",res)
        },
        fail(res) {
        },
        complete(res) {
            console.log("初始化蓝牙模块-openBluetoothAdapter", res)
        },
    })
},

操作步骤:

onLoad(options){  
    this.openBluetoothAdapter()   
},  
methods:{  
    // 打开蓝牙适配器  
    openBluetoothAdapter() {  
        let me = this;  
        console.log("openBluetoothAdapter")  
        uni.openBluetoothAdapter({  
            success(res){  
                // 状态正常  
                console.log("success",res)    
            },  
            fail(res) {  
            },  
            complete(res) {  
                console.log("初始化蓝牙模块-openBluetoothAdapter", res)  
            },  
        })  
    },  
}

预期结果:

打印成功回调结果

实际结果:

没有回调

bug描述:

项目运行在手机调试时,打开手机蓝牙后uni.openBluetoothAdapter没有任何的回调


更多关于uni-app中uni.openBluetoothAdapter没有回调的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app中uni.openBluetoothAdapter没有回调的实战教程也可以访问 https://www.itying.com/category-93-b0.html


根据你提供的信息,uni.openBluetoothAdapter 没有回调,这通常与权限配置或设备兼容性有关。以下是排查步骤:

  1. 检查 Android 权限配置
    manifest.json 中确保已添加蓝牙权限:

    "permissions": {
      "android": {
        "permissions": [
          "android.permission.BLUETOOTH",
          "android.permission.BLUETOOTH_ADMIN",
          "android.permission.ACCESS_FINE_LOCATION"
        ]
      }
    }
    

    Android 6.0+ 需要动态申请定位权限才能使用蓝牙,需在调用 openBluetoothAdapter 前通过 uni.authorize 申请 scope.location

  2. 确认系统蓝牙已开启
    代码中未检查蓝牙状态,建议先调用 uni.getSystemInfo 或监听 uni.onBluetoothAdapterStateChange 确保蓝牙可用。

  3. 真机调试日志
    fail 回调中添加 console.log 捕获错误,例如:

    fail(res) {
      console.error('openBluetoothAdapter fail:', res)
    }
回到顶部