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

蓝牙扫描报错:

[napi_bluetooth_access.cpp(EnableBluetooth:67)]bluetoothManager napi assert failed.
  • 1.
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);
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
HarmonyOS
2024-12-24 14:43:36
868浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

请参考:

  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)
            }
          })
        });
      }
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
分享
微博
QQ
微信
回复
2024-12-24 17:11:34
相关问题
ERROR: Schema validate failed.
1686浏览 • 1回复 待解决
Error: Install eTS 3.1.0.0 failed.
4856浏览 • 1回复 待解决
HarmonyOS Napi的问题
725浏览 • 1回复 待解决
HarmonyOS NAPI开发相关问题
1216浏览 • 1回复 待解决
HarmonyOS NAPI接口实现
848浏览 • 1回复 待解决
HarmonyOS Napi Arraybuffer无法创建
779浏览 • 1回复 待解决
HarmonyOS napi共享内存指导
747浏览 • 1回复 待解决
NAPI开发问题
809浏览 • 1回复 待解决