LHDC has a built-in HDC server within its application library. We utilize the wireless debugging feature to enable local communication between the server and the client.
First, you need to clone the entire OpenHarmony 6.0 environment, then replace the repository under developtools/hdc with this repository. After that, execute the build command:
rm -rf out; ./build.sh --product {your_device} --build-target hdc_napi --target-cpu=arm64 --ccache
The build output is located at out/{your_device}/developtools/hdc/libhdc.z.so.
Note: This is a blocking API. Process the results in a new thread.
in Index.d.ts:
export function executeCommand(command: string): string; //blocking, should use a thread.
export function startDaemon(): boolean;
export function startShell(): boolean;
export function stopShell(): boolean;
export function onData(callback: (data: string) => void): void;
export function sendInput(data: string): void;ArkTs examples:
startServer() {
let task = new taskpool.Task(backgroundStartDaemon, this.workPath);
taskpool.execute(task).then((result: Object) => {
const retCode = result as number;
hilog.info(DOMAIN, TAG, 'startDaemon returned: %{public}d', retCode);
this.appendLog(`[Server] OK: ${retCode}`);
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, TAG, 'Server start failed: %{public}s', error.message);
this.appendLog(`[Server] Fail: ${error.message}`);
});
}
executeCmd() {
let cmd = this.cmdInput;
let task = new taskpool.Task(backgroundExecuteCommand, cmd);
taskpool.execute(task).then((result: Object) => {
const output = result as string;
this.appendLog(`[Cmd] Output:\n${output}`);
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, TAG, 'Command execution failed: %{public}s', error.message);
this.appendLog(`[Cmd] Fail: ${error.message}`);
});
}