通过返回模板hiaiversion A1A2A3.X1X2X3.Y1Y2Y3.Z1Z2Z3指定X1是否为0来区分是否支持NPU。若X1为0,则表示不支持NPU;若X1为非0,则表示支持NPU。
步骤如下:
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
*/
#include "napi/native_api.h"
#include <hilog/log.h>
#include <cstring>
#include "CANNKit/hiai_helper.h"
static napi_value IsNpuDevice(napi_env env, napi_callback_info info)
{
const char *hiaiVersion = HMS_HiAI_GetVersion();
napi_value result;
napi_get_boolean(env, true, &result);
if (hiaiVersion == nullptr || strlen(hiaiVersion) != 15) { /* 15: hiaiVersion返回的字符串长度 */
OH_LOG_ERROR(LOG_APP, "HiAI version is null or not supported");
napi_get_boolean(env, false, &result);
} else if (hiaiVersion[4] == '0') { /* 返回的hiaiVersion字符串A1A2A3.X1X2X3.Y1Y2Y3.Z1Z2Z3中X1为0表示不支持NPU */
OH_LOG_INFO(LOG_APP, "NPU is not supported");
napi_get_boolean(env, false, &result);
}
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{"isNpuDevice", nullptr, IsNpuDevice, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void *)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } # the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(SkeletonDetectionNpuDemo)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)
include_directories(${HMOS_SDK_NATIVE}/sysroot/usr/lib)
FIND_LIBRARY(cann_lib hiai_foundation)
add_library(entry SHARED napi_init.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so
libhilog_ndk.z.so
librawfile.z.so
${cann_lib}
) export const isNpuDevice : () => boolean;
工程目录“\entry\src\main\ets\pages\Index.ets”文件示例参考如下:
import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';
const DOMAIN = 0x0000;
@Entry
@Component
struct Index {
@State text: string = '请检查';
private isNpuDevice: boolean = false;
build() {
Row() {
Column() {
Button('请点击检查是否支持NPU')
.onClick(() => {
this.isNpuDevice = testNapi.isNpuDevice(); // 调用接口检查是否支持NPU
hilog.info(DOMAIN, 'testTag', 'If support NPU: %{public}s', this.isNpuDevice);
this.text = this.isNpuDevice ? '支持' : '不支持';
})
.margin(20)
Text(this.text)
}
.width('100%')
}
.height('100%')
}
}