uni-app HBuilderX 3.98版本 onBluetoothDeviceFound不执行

发布于 1周前 作者 sinazl 来自 Uni-App

uni-app HBuilderX 3.98版本 onBluetoothDeviceFound不执行

开发环境 版本号 项目创建方式
Windows HBuilderX
windows11

产品分类:uniapp/App

PC开发环境操作系统:Windows

手机系统:Android

手机系统版本号:Android 10

手机厂商:华为

手机机型:荣耀畅玩20

页面类型:vue

vue版本:vue2

打包方式:云端

示例代码:

let self = this;
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: res => {
console.log('startBluetoothDevicesDiscovery')
setTimeout(()=> {
uni.onBluetoothDeviceFound(devices => {
console.log('onBluetoothDeviceFound')
console.log(JSON.stringify(devices))
console.log("发现新设备: " + JSON.stringify(devices));
if (devices.devices[0].name != "") {
//不重复,就添加到devicesList中,
if (!self.devicesList.some(item => {
return item.deviceId === devices.devices[0].deviceId
})) {
self.devicesList.push(devices.devices[0])
}
}
self.devicesList.sort(function(a, b) {
if (a.deviceId === _this.connectDevice.deviceId) {
return -1;
} else if (b.deviceId === _this.connectDevice.deviceId) {
return 1;
} else {
return 0;
}
})
}),2000)
},
fail: err => {
console.log(err)
self.isOpenBle = false
self.isSearch = false
// uni.hideLoading();
// bluetooth.showToast(`搜索设备失败` + JSON.stringify(err));
}
})

操作步骤:

let self = this;
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: res => {
console.log('startBluetoothDevicesDiscovery')
setTimeout(()=> {
uni.onBluetoothDeviceFound(devices => {
console.log('onBluetoothDeviceFound')
console.log(JSON.stringify(devices))
console.log("发现新设备: " + JSON.stringify(devices));
if (devices.devices[0].name != "") {
//不重复,就添加到devicesList中,
if (!self.devicesList.some(item => {
return item.deviceId === devices.devices[0].deviceId
})) {
self.devicesList.push(devices.devices[0])
}
}
self.devicesList.sort(function(a, b) {
if (a.deviceId === _this.connectDevice.deviceId) {
return -1;
} else if (b.deviceId === _this.connectDevice.deviceId) {
return 1;
} else {
return 0;
}
})
}),2000)
},
fail: err => {
console.log(err)
self.isOpenBle = false
self.isSearch = false
// uni.hideLoading();
// bluetooth.showToast(`搜索设备失败` + JSON.stringify(err));
}
})
`

3 回复

先调用uni.openBluetoothAdapter ,再调用 uni.startBluetoothDevicesDiscovery onReady() {
uni.openBluetoothAdapter({
success(res) {
console.log(res)
}
})
},


调过了,代码没贴进去

uni-appHBuilderX 3.98 版本中,如果你发现 onBluetoothDeviceFound 事件没有执行,可能是由于以下几个原因导致的。你可以按照以下步骤进行排查和解决:

1. 检查权限

确保你的应用已经获取了蓝牙相关的权限。在 manifest.json 文件中,确保已经添加了以下权限:

{
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于蓝牙设备搜索"
    }
  }
}

在 Android 平台上,还需要在 AndroidManifest.xml 中添加以下权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2. 检查蓝牙是否开启

在调用 uni.startBluetoothDevicesDiscovery 之前,确保设备的蓝牙已经开启。你可以使用 uni.getBluetoothAdapterState 来检查蓝牙状态。

uni.getBluetoothAdapterState({
  success(res) {
    if (res.available && res.discovering) {
      console.log('蓝牙已开启');
    } else {
      console.log('蓝牙未开启');
    }
  }
});

3. 正确调用 uni.startBluetoothDevicesDiscovery

确保你已经正确调用了 uni.startBluetoothDevicesDiscovery,并且 onBluetoothDeviceFound 事件已经注册。

uni.startBluetoothDevicesDiscovery({
  success(res) {
    console.log('开始搜索蓝牙设备');
  },
  fail(err) {
    console.error('搜索蓝牙设备失败', err);
  }
});

uni.onBluetoothDeviceFound((res) => {
  console.log('发现蓝牙设备', res.devices);
});

4. 检查设备是否支持蓝牙

确保你的设备支持蓝牙功能。你可以使用 uni.getSystemInfo 来检查设备是否支持蓝牙。

uni.getSystemInfo({
  success(res) {
    if (res.bluetoothEnabled) {
      console.log('设备支持蓝牙');
    } else {
      console.log('设备不支持蓝牙');
    }
  }
});

5. 检查设备是否在范围内

确保你要搜索的蓝牙设备在范围内,并且设备处于可被发现的状态。

6. 检查 HBuilderX 版本

确保你使用的是最新的 HBuilderX 版本。如果问题依然存在,可以尝试升级到最新版本,或者回退到一个已知稳定的版本。

7. 调试和日志

在开发过程中,可以使用 console.loguni.showToast 来输出调试信息,帮助你定位问题。

uni.startBluetoothDevicesDiscovery({
  success(res) {
    console.log('开始搜索蓝牙设备');
  },
  fail(err) {
    console.error('搜索蓝牙设备失败', err);
  }
});

uni.onBluetoothDeviceFound((res) => {
  console.log('发现蓝牙设备', res.devices);
});
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!