HarmonyOS鸿蒙Next中api9获取不到搜索不到蓝牙设备
HarmonyOS鸿蒙Next中api9获取不到搜索不到蓝牙设备 相同的代码纯血鸿蒙可以搜到蓝牙设备,api9获取不到,收不到 bluetoothDeviceFind 的回调用 ohos.bluetoothManager 包调用startBluetoothDiscovery 方法会报错code="-4",实在不知道怎么解决
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import EntryAbility from '../entryability/EntryAbility';
import bluetooth from '@ohos.bluetooth';
import BluetoothState from '../enum/BluetoothState';
import Logger from './Logger';
export default class BluetoothUtil {
private static TAG: string = 'BluetoothUtil';
private static permissions: Permissions[] =
['ohos.permission.USE_BLUETOOTH', 'ohos.permission.DISCOVER_BLUETOOTH', 'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'];
public static startBluetooth() {
BluetoothUtil.checkPermissions();
}
private static checkPermissions() {
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(EntryAbility.getInstance()?.context, BluetoothUtil.permissions)
.then((data) => {
let allPermissionGrant: boolean = true;
let grantStatus: number[] = data.authResults;
let length: number = grantStatus.length;
for (let i = 0; i < length; i++) {
if (grantStatus[i] !== 0) {
allPermissionGrant = false;
Logger.error(BluetoothUtil.TAG, 'permission denied: ' + BluetoothUtil.permissions[i]);
}
}
if (allPermissionGrant) {
BluetoothUtil.doStartBluetooth();
}
})
.catch((err) => {
Logger.error(BluetoothUtil.TAG,
`Failed to request permissions from user: ` + JSON.stringify(err));
})
}
private static doStartBluetooth() {
bluetooth.on('stateChange', BluetoothUtil.onStateChange);
bluetooth.on('bluetoothDeviceFind', BluetoothUtil.onBluetoothDeviceFind);
if (!BluetoothUtil.isBluetoothOpen()) {
BluetoothUtil.openBluetooth();
return
}
BluetoothUtil.scanBluetooth();
}
private static isBluetoothOpen(): boolean {
let state = bluetooth.getState();
if (state == BluetoothState.ON.valueOf()) {
return true;
}
return false;
}
private static openBluetooth(): boolean {
try {
//https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V2/js-apis-bluetoothmanager-0000001493584248-V2#ZH-CN_TOPIC_0000001574088509__bluetoothstate
let state = bluetooth.getState();
if (state !== BluetoothState.TURNING_ON.valueOf() && state !== BluetoothState.ON.valueOf()) {
bluetooth.enableBluetooth()
}
return true;
} catch (e) {
Logger.error(BluetoothUtil.TAG, "openBluetooth error: " + JSON.stringify(e));
return false;
}
}
private static scanBluetooth() {
try {
bluetooth.startBluetoothDiscovery();
Logger.info(BluetoothUtil.TAG, "scanBluetooth succeed");
} catch (e) {
Logger.error(BluetoothUtil.TAG, "scanBluetooth error: " + JSON.stringify(e));
}
}
private static onStateChange(data: number) {
//https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V2/js-apis-bluetoothmanager-0000001493584248-V2#ZH-CN_TOPIC_0000001574088509__bluetoothstate
if (data != BluetoothState.ON.valueOf()) {
return
}
Logger.info(BluetoothUtil.TAG, 'onStateChange: ' + JSON.stringify(data));
BluetoothUtil.scanBluetooth();
}
private static onBluetoothDeviceFind(data: Array<string>) {
Logger.info(BluetoothUtil.TAG, 'onBluetoothDeviceFind: ' + JSON.stringify(data));
}
}
更多关于HarmonyOS鸿蒙Next中api9获取不到搜索不到蓝牙设备的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next API 9中,搜索不到蓝牙设备通常涉及权限、API调用或设备状态问题。请检查是否已正确声明并动态申请蓝牙相关权限(如ohos.permission.USE_BLUETOOTH)。确保在调用bluetooth.startBluetoothDiscovery()前,蓝牙适配器已开启且设备处于可发现模式。部分设备可能需要配对后才能被发现。
更多关于HarmonyOS鸿蒙Next中api9获取不到搜索不到蓝牙设备的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
根据你的代码和描述,问题很可能出在权限请求和API版本差异上。
在HarmonyOS Next API 9中,蓝牙权限模型有所变化。你代码中请求的'ohos.permission.DISCOVER_BLUETOOTH'权限在API 9中可能已被调整或需要不同的处理方式。
startBluetoothDiscovery返回错误码"-4"通常表示权限不足或权限被拒绝。建议检查以下几点:
-
权限配置:确保在
module.json5文件中正确声明了蓝牙相关权限:"requestPermissions": [ { "name": "ohos.permission.USE_BLUETOOTH" }, { "name": "ohos.permission.DISCOVER_BLUETOOTH" }, { "name": "ohos.permission.LOCATION" } ] -
权限请求时机:在API 9中,某些权限需要在特定的上下文中请求。确保权限请求发生在用户交互之后,而不是在应用启动时自动请求。
-
API差异:API 9可能对
startBluetoothDiscovery方法有额外的前置条件检查,比如需要先确认蓝牙适配器状态。 -
错误处理:建议在调用
startBluetoothDiscovery时添加更详细的错误日志,查看完整的错误对象信息,而不仅仅是字符串化。
可以尝试在调用扫描前添加蓝牙状态的双重检查,并确保所有权限都已明确授予。

