鸿蒙ACE框架-使用JS调用C++(1)

[[397654]]

想了解更多内容,请访问:

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.

 鸿蒙ACE框架-使用JS调用C++(1)

1.下载编译烧录代码

https://blog.csdn.net/qq_33259323/article/details/116143820

2.编写测试用例

打开foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.h

 
 
 
 
  1. static JSIValue ToggleLed(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); 
 
 
 
 
  1. JSI::SetModuleAPI(exports, "toggleLed", AppModule::ToggleLed); 

打开foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.cpp

 
 
 
 
  1. JSIValue AppModule::ToggleLed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) 
  2.     HILOG_ERROR(HILOG_MODULE_ACE, "led button pressed."); 
  3.     printf("led button pressed\n"); 
  4.   
  5.     return JSI::CreateUndefined(); 

 

之后就进行编译烧录,C++层的就弄好了

3.编写HAP

打开C:\Users\XX\AppData\Local\Huawei\Sdk\js\2.1.1.18\api\smartVision\@system.app.d.ts添加API

static toggleLed(): void;

然后打开DEVECO在你的页面上写个按钮和加个点击事件

 
 
 
 
  1. <input class="btu" type="button" value="进入" onclick="led"></input> 

 
 
 
 
  1. import router from '@system.router'
  2. import app from '@system.app' 
  3.   
  4. export default { 
  5.     data: { 
  6.   
  7.     }, 
  8.     onInit() { 
  9.   
  10.     }, 
  11.     led: function(){ 
  12.          
  13.         app.toggleLed(); 
  14.     } 

 然后把编写好的HAP包装到3516上面

https://blog.csdn.net/qq_33259323/article/details/111307192

4.运行

参考:https://harmonyos./posts/3112

想了解更多内容,请访问:

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.

 

THE END