HarmonyOS 打开蓝牙,报201 Permission denied.

调用以下代码打开蓝牙,报 201 Permission denied.

import { access } from '@kit.ConnectivityKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
try {
  access.enableBluetooth();
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

权限已开启

{
  // 蓝牙
  "name": "ohos.permission.ACCESS_BLUETOOTH",
"reason": "$string:app_name",
"usedScene": {
  "when":"inuse"
}
},
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

module.json5文件内:

"requestPermissions": [
{
  "name": "ohos.permission.ACCESS_BLUETOOTH",
"reason": "$string:EntryAbility_desc",
"usedScene": {
  "abilities": [
  "EntryAbility"
  ],
  "when": "always"
}
}
]

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(catch err->${JSON.stringify(err)});
}

为用户授权参考链接如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-abilityaccessctrl-V5

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 调用系统完整性接口201
29浏览 • 1回复 待解决
在读取媒体文件open: permission denied
2915浏览 • 1回复 待解决
HarmonyOS checkSysIntegrity 返回201
31浏览 • 1回复 待解决
DevEco Studio打开历史工程空指针错误
4968浏览 • 1回复 待解决
使用FilePick上传uri失败,报错201
1803浏览 • 1回复 待解决
HarmonyOS the map permission is not enabled
8浏览 • 0回复 待解决
后台长时任务启动失败报错码201
2351浏览 • 1回复 待解决
The Map permission is not enabled
1688浏览 • 1回复 待解决