HarmonyOS 调用蓝牙接口access.enableBluetooth\(\)报错 2900099, errMessage: BussinessError 2900099: Operation failed?

如题:HarmonyOS 调用蓝牙接口access.enableBluetooth()报错 2900099, errMessage: BussinessError 2900099: Operation failed?

阅读 526
1 个回答

示例demo如下:

import { BusinessError } from '@ohos.base';
import { access } from '@kit.ConnectivityKit';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
@Entry
@Component
struct TestBluetooth {
  @State message: string = 'Hello World';
  private atManager = abilityAccessCtrl.createAtManager();
  private appContext: common.Context = getContext(this);

  async requestPermissionsFn() {
    console.info(`requestPermissionsFn entry`);
    try {
      this.atManager.requestPermissionsFromUser(this.appContext, [
        'ohos.permission.ACCESS_BLUETOOTH'
      ]).then(() => {
        console.info(`request Permissions success!`);

      })
    } catch (err) {
      console.info(`requestPermissionsFromUser call Failed! error: ${err.code}`);
    }
  }
  aboutToAppear(): void {
    this.requestPermissionsFn();
  }
  build() {
    RelativeContainer() {
      Text(this.message)
        .id('TestBluetoothHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(()=>{
          try {
            access.enableBluetooth();
            console.info('wangyi success')
          } catch (err) {
            console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
          }
        })
    }
    .height('100%')
    .width('100%')
  }
}

是因为已经开启蓝牙了,才会报错。

logo
HarmonyOS
子站问答
访问
宣传栏