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);
})
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS 调用蓝牙接口access.enableBluetooth()报错 2900099, errMessage: BussinessError 2900099: Operation failed
38浏览 • 1回复 待解决
staticLibrary har模块,怎么断点调试napi cpp代码
714浏览 • 1回复 待解决
编译报错:hvigor ERROR: Error, Schema validate failed.
2719浏览 • 1回复 待解决
USE_BLUETOOTH和ACCESS_BLUETOOTH的区别是什么
543浏览 • 1回复 待解决
error: install failed due to grant request permissions failed.
4410浏览 • 1回复 待解决
HarmonyOS 调用access.enableBluetooth()申请打开蓝牙,用户点击拒绝,如何捕获用户操作状态?
167浏览 • 1回复 待解决
HarmonyOS 在napi_init.cpp中调用C代码怎么接收返回的参数
681浏览 • 1回复 待解决
ERROR: Schema validate failed.
740浏览 • 1回复 待解决
HarmonyOS 调用蓝牙enableBluetooth api 报2900099错误
46浏览 • 1回复 待解决
Error: Install eTS 3.1.0.0 failed.
3953浏览 • 1回复 待解决
HarmonyOS cpp下有什么方法判断当前的napi_env是js主线程的env
67浏览 • 1回复 待解决
HarmonyOS Napi的问题
24浏览 • 1回复 待解决
NAPI 的 napi_throw_error 不会导致应用崩溃
1145浏览 • 1回复 待解决
HarmonyOS napi_call_threadsafe_function中napi_tsfn_nonblocking和napi_tsfn_blocking有什么区别
66浏览 • 1回复 待解决
HarmonyOS NAPI开发相关问题
412浏览 • 1回复 待解决
预览出现问题:Preview failed. View details in the PreviewerLog window
1238浏览 • 1回复 待解决
NAPI 通过 napi_wrap 创建的对象似乎没有被销毁
1903浏览 • 1回复 待解决
HarmonyOS 上层调用NAPI方法,传入文件路径,NAPI层怎么打开这个文件
56浏览 • 1回复 待解决
OpenHarmony 9 napi找不到native_node_api,引入头文件报错'napi/native_node_api.h' file not found
3127浏览 • 1回复 待解决
HarmonyOS Napi Arraybuffer无法创建
41浏览 • 1回复 待解决
HarmonyOS NAPI接口实现
76浏览 • 1回复 待解决
HarmonyOS napi共享内存指导
20浏览 • 1回复 待解决
HarmonyOS 开发蓝牙相关的功能,在module.json5中配置了ohos.permission.ACCESS_BLUETOOTH,在index如何唤起用户使用蓝牙的权限弹框
87浏览 • 1回复 待解决
napi_get_arraybuffer_info 和 napi_create_arraybuffer 的使用
2124浏览 • 1回复 待解决
HarmonyOS Failed to scan dir 无法扫描缓存目录
30浏览 • 1回复 待解决
请参考:
蓝牙协议sdk暂不支持,protobuf协议已支持。protobuf:https://gitee.com/openharmony-tpc/protobuf
获取server的services信息,测试没问题。可以参考: