HarmonyOS 鸿蒙Next蓝牙权限怎么申请

发布于 1周前 作者 songsunli 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next蓝牙权限怎么申请

问题: Error name:Error Error message:BussinessError 201: Permission denied. Error code:201 SourceCode: access.enableBluetooth(); ^ Stacktrace: at enableBluetooth (entry/src/main/ets/common/bluetooth/BluetoothUtil.ets:8:5) at aboutToAppear (entry/src/main/ets/view/home/VicinityLock.ets:28:5)  

代码如下:

import { access } from ‘@kit.ConnectivityKit’;
import { AsyncCallback, BusinessError } from ‘@kit.BasicServicesKit’;

export class BluetoothUtil { // 开启蓝牙 public enableBluetooth() { access.enableBluetooth(); access.on(‘stateChange’, (data: access.BluetoothState) => { let btStateMessage = ‘’; switch (data) { case 0: btStateMessage += ‘STATE_OFF’; break; case 1: btStateMessage += ‘STATE_TURNING_ON’; break; case 2: btStateMessage += ‘STATE_ON’; break; case 3: btStateMessage += ‘STATE_TURNING_OFF’; break; case 4: btStateMessage += ‘STATE_BLE_TURNING_ON’; break; case 5: btStateMessage += ‘STATE_BLE_ON’; break; case 6: btStateMessage += ‘STATE_BLE_TURNING_OFF’; break; default: btStateMessage += ‘unknown status’; break; } if (btStateMessage == ‘STATE_ON’) { access.off(‘stateChange’); } console.info('bluetooth statues: ’ + btStateMessage); }) }

// 关闭蓝牙 public disableBluetooth() { access.disableBluetooth(); access.on(‘stateChange’, (data: access.BluetoothState) => { let btStateMessage = ‘’; switch (data) { case 0: btStateMessage += ‘STATE_OFF’; break; case 1: btStateMessage += ‘STATE_TURNING_ON’; break; case 2: btStateMessage += ‘STATE_ON’; break; case 3: btStateMessage += ‘STATE_TURNING_OFF’; break; case 4: btStateMessage += ‘STATE_BLE_TURNING_ON’; break; case 5: btStateMessage += ‘STATE_BLE_ON’; break; case 6: btStateMessage += ‘STATE_BLE_TURNING_OFF’; break; default: btStateMessage += ‘unknown status’; break; } if (btStateMessage == ‘STATE_OFF’) { access.off(‘stateChange’); } console.info("bluetooth statues: " + btStateMessage); }) } }

export default new BluetoothUtil();

2 回复
检查一下蓝牙相关权限是否配置在module.json5文件内:
"requestPermissions": [
      {
        "name": "ohos.permission.ACCESS_BLUETOOTH",
        "reason": "$string:grant_use_bluetooth",
        "usedScene": {
          "abilities": [
            "MainAbility"
          ],
          "when": "always"
        }
      },
      {
        "name": "ohos.permission.USE_BLUETOOTH",
        "reason": "$string:grant_use_bluetooth",
        "usedScene": {
          "abilities": [
            "MainAbility"
          ],
          "when": "always"
        }
      }
    ]<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

ohos.permission.ACCESS_BLUETOOTH 允许应用接入蓝牙并使用蓝牙能力,例如配对、连接外围设备等。ohos.permission.USE_BLUETOOTH 允许应用查看蓝牙的配置。

这个配置是需要申请ACL权限的,申请方法如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-signing-0000001587684945-V5#section157591551175916

访问控制授权申请指导:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/access-token-overview-V5

ACCESS.BLUETOOTH权限需要为用户授权才可使用,用户授权demo可参考:

let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();

try { let context = getContext(this); atManager.requestPermissionsFromUser(context, [‘ohos.permission.ACCESS_BLUETOOTH’], (err: BusinessError, data: PermissionRequestResult)=>{ console.info(‘data:’ + JSON.stringify(data)); console.info(‘data permissions:’ + data.permissions); console.info(‘data authResults:’ + data.authResults); }); } catch(err) { console.log(<span class="hljs-keyword">catch</span> err-&gt;${<span class="hljs-built_in">JSON</span>.stringify(err)}); }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

在HarmonyOS鸿蒙Next系统中申请蓝牙权限,通常需要按照以下步骤进行:

  1. 检查设备支持:首先,确保设备支持蓝牙功能。可以通过调用相关API(如BluetoothAdapter.getBluetoothAdapter())来检查设备是否具备蓝牙适配器。
  2. 配置权限:在应用的配置文件中(如AndroidManifest.xml或module.json),需要声明蓝牙权限。例如,添加<uses-permission ohos:name="ohos.permission.BLUETOOTH"/>以及相关的蓝牙使用权限,如ohos.permission.ACCESS_BLUETOOTHohos.permission.DISCOVER_BLUETOOTHohos.permission.USE_BLUETOOTH
  3. 请求权限:在代码中,通过调用系统提供的权限请求接口(如requestPermissionsFromUser())来请求用户授权蓝牙权限。
  4. 处理权限结果:根据用户的授权结果,进行相应的处理。如果用户同意授权,则可以继续执行蓝牙相关的操作;如果用户拒绝授权,则需要引导用户在系统设置中手动授予权限。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部