IPC/RPC is used to implement object communication across processes (one-to-one mapping between the client proxy and the server stub).
Before performing IPC&RPC inter-process communication, obtain the proxy object of the server through Ability Kit.
Third-party applications cannot implement inter-process communication. A third-party application can only connect to the ServiceExtensionAbility provided by the system through connectServiceExtensionAbility, and communicate through the returned proxy object ServiceExtensionAbility, thereby achieving communication between the third-party application and system services.
Starting from API version 20, on 2-in-1 devices, you can use the AppServiceExtensionAbility component to provide background service capabilities for applications. A third-party application can connect to AppServiceExtensionAbility through connectAppServiceExtensionAbility, and communicate with AppServiceExtensionAbility through the returned proxy object, thereby achieving communication between third-party applications. For detailed development steps, see AppServiceExtensionAbility.
Third-party applications can also perform inter-process communication by dynamically subscribing to common events.
The complete IPC&RPC communication development process involves the implementation of the system ServiceExtensionAbility. Therefore, this guide provides only the client-side sample code.
Create a want variable, and specify the bundle name and component name of the application where the ability is located.
Create a connect variable, and specify the callback that is called when the binding is successful, the binding fails, or the ability is disconnected.
Connect to the service, obtain the service proxy, and register the death listener.
Send a message from the client to the server.
Tear down the connection and remove the death listener of the service proxy when the communication is over.
In the IPC (inter-process communication on the same device) scenario, the following is an example of the client:
Import dependencies and define required variables.
import { BusinessError } from '@kit.BasicServicesKit';
import { Want, common } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { PromptAction } from '@kit.ArkUI';
import { JSON } from '@kit.ArkTS';
let proxy: rpc.IRemoteObject | undefined;
let connectId: number | undefined;
// Death notification
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server is dead');
}
}
let deathRecipient = new MyDeathRecipient(); Connect to the service, obtain the proxy object, send information to the server, and tear down the connection when the communication is over.
// Connect to the service.
function connectAbility(context:common.UIAbilityContext, promptAction: PromptAction) {
hilog.info(0x00000, 'testTag', 'begin to connect Ability');
let want: Want = {
bundleName: 'com.example.ipc_stub',
abilityName: 'ServiceAbility',
};
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x00000, 'testTag', 'onConnect. elementName is :' + JSON.stringify(elementName));
proxy = remoteProxy;
// Register a death listener on the client.
try {
proxy.registerDeathRecipient(deathRecipient, 0);
hilog.info(0x00000, 'testTag', 'registerDeathRecipient success');
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'register failed, code is ' + code + ', message is ' + message);
}
// ...
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect. elementName is ' + JSON.stringify(elementName));
// Remove the death listener from the client.
try {
proxy?.unregisterDeathRecipient(deathRecipient, 0);
hilog.info(0x00000, 'testTag', 'unregisterDeathRecipient success');
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'unregister failed, code is ' + code + ', message is ' + message);
}
proxy = undefined;
// ...
},
onFailed: (code: number) => {
hilog.info(0x0000, 'testTag', 'onFailed. code is ' + code);
// ...
},
}
try {
connectId = context.connectServiceExtensionAbility(want, connect);
hilog.info(0x00000, 'testTag', 'begin to connect Ability end');
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'connectAbility failed, code is ' + code + ', message is ' + message);
}
}
// Close the connection.
function disconnectAbility(context: common.UIAbilityContext) {
hilog.info(0x00000, 'testTag', 'begin to disconnect Ability. connectId is ' + connectId);
if (connectId != undefined) {
try {
context.disconnectServiceExtensionAbility(connectId);
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'disconnect failed, code is ' + code + ', message is ' + message);
}
}
}
// Send a message.
async function sendString(promptAction: PromptAction) : Promise <void> {
hilog.info(0x00000, 'testTag', 'begin to send String');
let option = new rpc.MessageOption();
let data = rpc.MessageSequence.create();
let reply = rpc.MessageSequence.create();
// Write parameters in data. The following uses the string as an example.
data.writeString('hello world');
if (proxy != undefined) {
await proxy.sendMessageRequest(1, data, reply, option)
.then((result: rpc.RequestResult) => {
if (result.errCode != 0) {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode is ' + result.errCode);
}
// Read the result from result.reply.
let str = result.reply.readString();
hilog.info(0x0000, 'testTag', 'sendMessageRequest receive str is ' + str);
// ...
})
.catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error is ' + JSON.stringify(e));
// ...
})
.finally(() => {
data.reclaim();
reply.reclaim();
})
} else {
hilog.error(0x0000, 'testTag', 'proxy is invalid');
// ...
}
hilog.info(0x0000, 'testTag', 'sendString end');
} In the RPC (inter-process communication across devices) scenario, the following is an example of a client:
Import dependencies and define required variables.
import { BusinessError } from '@kit.BasicServicesKit';
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { distributedDeviceManager } from '@kit.DistributedServiceKit';
import { abilityAccessCtrl, PermissionRequestResult, common, Want} from '@kit.AbilityKit';
import { JSON } from '@kit.ArkTS';
import { PromptAction } from '@kit.ArkUI';
let proxy: rpc.IRemoteObject | undefined;
let connectId: number | undefined;
let dmInstance: distributedDeviceManager.DeviceManager;
let deviceList: Array<distributedDeviceManager.DeviceBasicInfo> | undefined;
let deviceId: string| undefined;
// Death notification
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server is dead');
}
};
let deathRecipient = new MyDeathRecipient(); Obtain the permission for multi-device collaboration, obtain the peer device ID (unique network identifier of the device, which can be retrieved via distributedDeviceManager) in the networking scenario, connect to the service, acquire the proxy object, and send messages to the server. Disconnect once the communication between the proxy object and server ends.
// Obtain the permission.
function getPermission(context:common.UIAbilityContext) {
hilog.info(0x00000, 'testTag', 'begin to requestPermissions');
try {
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(context, ['ohos.permission.DISTRIBUTED_DATASYNC'],
(err: BusinessError, data: PermissionRequestResult) => {
if (err) {
hilog.error(0x0000, 'testTag', 'requestPermissions failed, code is ' + err.code);
hilog.error(0x0000, 'testTag', 'requestPermissions failed, message is ' + err.message);
} else {
hilog.info(0x0000, 'testTag', 'requestPermissions success, result is ' + JSON.stringify(data));
hilog.info(0x0000, 'testTag', 'data permissions is ' + data.permissions);
hilog.info(0x0000, 'testTag', 'data authResults is ' + data.authResults);
hilog.info(0x0000, 'testTag', 'data dialogShownResults is ' + data.dialogShownResults);
}
});
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'getPermission failed, code is ' + code + ', message is ' + message);
}
}
// Obtain the peer device information.
function getDeviceId(promptAction: PromptAction) {
hilog.info(0x00000, 'testTag', 'begin to getDeviceId');
try {
dmInstance = distributedDeviceManager.createDeviceManager('com.example.rpc_client');
hilog.info(0x0000, 'testTag', 'createDeviceManager success');
deviceList = dmInstance.getAvailableDeviceListSync();
hilog.info(0x0000, 'testTag', 'deviceList is ' + JSON.stringify(deviceList));
if (deviceList.length !== 0) {
deviceId = deviceList[0].networkId;
hilog.info(0x0000, 'testTag', 'networkId is ' + deviceId);
// ...
}
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'getDeviceId failed, code is ' + code + ', message is ' + message);
// ...
}
}
// Connect to the service.
function connectAbility(context:common.UIAbilityContext, promptAction: PromptAction) {
hilog.info(0x00000, 'testTag', 'begin to connect Ability');
let want: Want = {
bundleName: 'com.example.rpc_stub',
abilityName: 'ServiceAbility',
deviceId: deviceId,
}
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x00000, 'testTag', 'onConnect. elementName is ' + JSON.stringify(elementName));
proxy = remoteProxy;
// Register a death listener on the client.
try {
proxy.registerDeathRecipient(deathRecipient, 0);
hilog.info(0x00000, 'testTag', 'registerDeathRecipient success');
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'register failed, code is ' + code + ', message is ' + message);
};
// ...
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect. elementName is ' + JSON.stringify(elementName));
// Remove the death listener from the client.
try {
proxy?.unregisterDeathRecipient(deathRecipient, 0);
hilog.info(0x00000, 'testTag', 'unregisterDeathRecipient success');
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'unregister failed, code is ' + code + ', message is ' + message);
}
proxy = undefined;
// ...
},
onFailed: (code: number) => {
hilog.info(0x0000, 'testTag', 'onFailed. code is ' + code);
// ...
},
}
try {
connectId = context.connectServiceExtensionAbility(want, connect);
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'connectService failed, code is ' + code + ', message is ' + message);
}
}
// Close the connection.
function disconnectAbility(context: common.UIAbilityContext) {
hilog.info(0x00000, 'testTag', 'begin to disconnect Ability');
if (connectId != undefined) {
try {
context.disconnectServiceExtensionAbility(connectId);
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(0x0000, 'testTag', 'disconnectService failed, code is ' + code + ', message is ' + message);
}
}
}
// Send a message.
async function sendString(promptAction: PromptAction) : Promise <void> {
hilog.info(0x00000, 'testTag', 'begin to send string');
let option = new rpc.MessageOption();
let data = rpc.MessageSequence.create();
let reply = rpc.MessageSequence.create();
// Write parameters in data. The following uses the string as an example.
data.writeString('hello world');
if (proxy != undefined) {
await proxy.sendMessageRequest(1, data, reply, option)
.then((result: rpc.RequestResult) => {
if (result.errCode != 0) {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode is ' + result.errCode);
}
// Read the result from result.reply.
let str = result.reply.readString();
hilog.info(0x0000, 'testTag', 'sendMessageRequest receive str is ' + str);
// A dialog box is displayed, indicating that the message is sent successfully.
// ...
})
.catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error is ' + JSON.stringify(e));
// A dialog box is displayed, indicating that the message fails to be sent.
// ...
})
.finally(() => {
data.reclaim();
reply.reclaim();
})
} else {
hilog.error(0x0000, 'testTag', 'proxy is invalid');
// A dialog box is displayed, indicating that the message fails to be sent.
// ...
}
}
For the end-to-end complete example of IPC and RPC development, see the following: