HarmonyOS 蓝牙扫描报错 \[napi\_bluetooth\_access.cpp\(EnableBluetooth:67\)\]bluetoothManager napi assert failed.?

蓝牙扫描报错:

[napi_bluetooth_access.cpp(EnableBluetooth:67)]bluetoothManager napi assert failed.
import { access, ble } from '@kit.ConnectivityKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { abilityAccessCtrl } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

export class LST_BLESDK {
  private static instance: LST_BLESDK;
  static sharedBLESDK(): LST_BLESDK {
    if (!LST_BLESDK.instance) {
      LST_BLESDK.instance = new LST_BLESDK();
    }
    return LST_BLESDK.instance;
  }

  onReceiveEvent(data: Array<ble.ScanResult>) {
    console.info('BLE scan device find result = ' + JSON.stringify(data));
  }
  requestPermission = async () => {
    let permissionRequestResult = await abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this),
      [
        'ohos.permission.USE_BLUETOOTH'
      ]);
    // 如果权限列表中有-1,说明用户拒绝了授权
    if (permissionRequestResult.authResults[0] === 0) {
      hilog.info(0x0000, 'testTag', '%{public}s', 'request permission success');
    }
  }
  async connectDevice() {
    await this.requestPermission()
    this.enableBluetooth()
    try {
      ble.on("BLEDeviceFind", this.onReceiveEvent);
      let scanOptions: ble.ScanOptions = {
        interval: 500,
        dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER,
        matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE
      }
      ble.startBLEScan(null, scanOptions);
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }
  }

  enableBluetooth() {
    access.enableBluetooth();
    access.on('stateChange', (data) => {
      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);
    })
  }
}
阅读 380
1 个回答

请参考:

  1. 蓝牙协议sdk暂不支持,protobuf协议已支持。protobuf:https://gitee.com/openharmony-tpc/protobuf
  2. 获取server的services信息,测试没问题。可以参考:

    /**
     * client端获取蓝牙低功耗设备的所有服务,即服务发现
     */
    getServices() {
      if (this.gattClient) {
        this.gattServiceInfo = undefined;
        this.gattClient.getServices().then((result: Array<ble.GattService>) => {
          console.info('getServices successfully:' + JSON.stringify(result));
          result.filter(item => {
            if (item.serviceUuid == this.serviceUuid) {
              this.gattServiceInfo = item;
              Utils.ArrayBuffer2String(item?.characteristics[0]?.characteristicValue)
            }
          })
        });
      }
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏