Call C++ with JS (1)

1. Download the compile and burn code blog.csdn.net/qq\_3325932…

2. Write test cases open Foundation \ace\ace_engine_lite\frameworks\ SRC \core\modules\app_module.h

static JSIValue ToggleLed(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum);

JSI::SetModuleAPI(exports, "toggleLed", AppModule::ToggleLed);
Copy the code

Ace open foundation \ \ ace_engine_lite \ frameworks \ SRC \ core \ modules \ app_module CPP

JSIValue AppModule::ToggleLed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
{
    HILOG_ERROR(HILOG_MODULE_ACE, "led button pressed.");
    printf("led button pressed\n");
 
    return JSI::CreateUndefined();
}
Copy the code

After compiling and burning, C++ layer is ready

C:\Users\XX\AppData\Local\Huawei\Sdk\js\2.1.1.18\ API \smartVision\@system.app.d.ts add API

static toggleLed(): void;

Then open DEVECO and write a button and add a click event to your page

</input> import router from '@system. Router '; </input> import router from '@system. import app from '@system.app' export default { data: { }, onInit() { }, led: function(){ app.toggleLed(); }}Copy the code

Then put the written HAP package on 3516

Blog.csdn.net/qq\_3325932…

4. Run

Reference: harmonyos.51cto.com/posts/3112

By Kawaii Meow

For more information, visit Harmonyos.51cto.com, a collaboration between 51CTO and Huawei