@ohos.rpc (RPC)
The RPC module implements communication between processes, including inter-process communication (IPC) on a single device and remote procedure call (RPC) between processes on difference devices. IPC is implemented based on the Binder driver, and RPC is based on the DSoftBus driver.
NOTE
The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
This module supports return of error codes since API version 9.
Modules to Import
import { rpc } from '@kit.IPCKit';
ErrorCode9+
The APIs of this module return exceptions since API version 9. The following table lists the error codes.
System capability: SystemCapability.Communication.IPC.Core
| Name | Value | Description |
|---|---|---|
| CHECK_PARAM_ERROR | 401 | Parameter check failed. |
| OS_MMAP_ERROR | 1900001 | Failed to call mmap. |
| OS_IOCTL_ERROR | 1900002 | Failed to call ioctl with the shared memory file descriptor. |
| WRITE_TO_ASHMEM_ERROR | 1900003 | Failed to write data to the shared memory. |
| READ_FROM_ASHMEM_ERROR | 1900004 | Failed to read data from the shared memory. |
| ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | This operation is allowed only on the proxy object. |
| ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | This operation is allowed only on the remote object. |
| COMMUNICATION_ERROR | 1900007 | Failed to communicate with the remote object over IPC. |
| PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | Invalid proxy or remote object. |
| WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | Failed to write data to MessageSequence. |
| READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | Failed to read data from MessageSequence. |
| PARCEL_MEMORY_ALLOC_ERROR | 1900011 | Failed to allocate memory during serialization. |
| CALL_JS_METHOD_ERROR | 1900012 | Failed to invoke the JS callback. |
| OS_DUP_ERROR | 1900013 | Failed to call dup. |
TypeCode12+
Since API version 12, writeArrayBuffer and readArrayBuffer are added to pass ArrayBuffer data. The specific TypedArray type is determined by the TypeCode defined as follows.
System capability: SystemCapability.Communication.IPC.Core
| Name | Value | Description |
|---|---|---|
| INT8_ARRAY | 0 | The TypedArray type is INT8_ARRAY. |
| UINT8_ARRAY | 1 | The TypedArray type is UINT8_ARRAY. |
| INT16_ARRAY | 2 | The TypedArray type is INT16_ARRAY. |
| UINT16_ARRAY | 3 | The TypedArray type is UINT16_ARRAY. |
| INT32_ARRAY | 4 | The TypedArray type is INT32_ARRAY. |
| UINT32_ARRAY | 5 | The TypedArray type is UINT32_ARRAY. |
| FLOAT32_ARRAY | 6 | The TypedArray type is FLOAT32_ARRAY. |
| FLOAT64_ARRAY | 7 | The TypedArray type is FLOAT64_ARRAY. |
| BIGINT64_ARRAY | 8 | The TypedArray type is BIGINT64_ARRAY. |
| BIGUINT64_ARRAY | 9 | The TypedArray type is BIGUINT64_ARRAY. |
MessageSequence9+
Provides APIs for reading and writing data in specific format. During RPC or IPC, the sender can use the write() method provided by MessageSequence to write data in specific format to a MessageSequence object. The receiver can use the read() method provided by MessageSequence to read data in specific format from a MessageSequence object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects.
create9+
static create(): MessageSequence
Creates a MessageSequence object. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| MessageSequence | MessageSequence object created. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
hilog.info(0x0000, 'testTag', 'data is ' + data);
// When the MessageSequence object is no longer used, the service calls the reclaim method to release resources.
data.reclaim();
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
reclaim9+
reclaim(): void
Reclaims the MessageSequence object that is no longer used.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let reply = rpc.MessageSequence.create();
reply.reclaim();
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeRemoteObject9+
writeRemoteObject(obj: IRemoteObject): void
Serializes the remote object and writes it to the MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| obj | IRemoteObject | Yes | Remote object to serialize and write to the MessageSequence object. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900008 | The proxy or remote object is invalid. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let data = rpc.MessageSequence.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readRemoteObject9+
readRemoteObject(): IRemoteObject
Reads the remote object from MessageSequence. You can use this API to deserialize the MessageSequence object to generate an IRemoteObject. The remote object is read in the order in which it is written to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject | Remote object obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900008 | The proxy or remote object is invalid. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let data = rpc.MessageSequence.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);
let proxy = data.readRemoteObject();
hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeInterfaceToken9+
writeInterfaceToken(token: string): void
Writes an interface token to this MessageSequence object. The remote object can use this interface token to verify the communication.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| token | string | Yes | Interface token to write. The length of the string must be less than 40960 bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInterfaceToken("aaa");
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readInterfaceToken9+
readInterfaceToken(): string
Reads the interface token from this MessageSequence object. The interface token is read in the sequence in which it is written to the MessageSequence object. The local object can use it to verify the communication.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface token obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInterfaceToken("aaa");
let interfaceToken = data.readInterfaceToken();
hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getSize9+
getSize(): number
Obtains the data size of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Size of the MessageSequence instance obtained, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let size = data.getSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getCapacity9+
getCapacity(): number
Obtains the capacity of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Capacity of the obtained MessageSequence object, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let result = data.getCapacity();
hilog.info(0x0000, 'testTag', 'capacity is ' + result);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
setSize9+
setSize(size: number): void
Sets the size of the data contained in this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Data size to set, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeString('Hello World');
data.setSize(16);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
setCapacity9+
setCapacity(size: number): void
Sets the storage capacity of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Storage capacity of the MessageSequence object to set, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
| 1900011 | Memory allocation failed. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.setCapacity(100);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getWritableBytes9+
getWritableBytes(): number
Obtains the writable capacity (in bytes) of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Writable capacity of the MessageSequence instance, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.setCapacity(100);
let getWritableBytes = data.getWritableBytes();
hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getReadableBytes9+
getReadableBytes(): number
Obtains the readable capacity of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Readable capacity of the MessageSequence instance, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeString("hello world");
let result = data.getReadableBytes();
hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getReadPosition9+
getReadPosition(): number
Obtains the read position of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Read position obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeString("hello world");
let readPos = data.getReadPosition();
hilog.info(0x0000, 'testTag', 'readPos is ' + readPos);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getWritePosition9+
getWritePosition(): number
Obtains the write position of this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Write position obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInt(10);
let bwPos = data.getWritePosition();
hilog.info(0x0000, 'testTag', 'bwPos is ' + bwPos);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
rewindRead9+
rewindRead(pos: number): void
Moves the read pointer to the specified position.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| pos | number | Yes | Position from which data is to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInt(12);
data.writeString("sequence");
let number = data.readInt();
hilog.info(0x0000, 'testTag', 'number is ' + number);
data.rewindRead(0);
let number2 = data.readInt();
hilog.info(0x0000, 'testTag', 'rewindRead is ' + number2);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
rewindWrite9+
rewindWrite(pos: number): void
Moves the write pointer to the specified position.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| pos | number | Yes | Position from which data is to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInt(4);
data.rewindWrite(0);
data.writeInt(5);
let number = data.readInt();
hilog.info(0x0000, 'testTag', 'rewindWrite is: ' + number);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeByte9+
writeByte(val: number): void
Writes a byte value to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Byte value to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeByte(2);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readByte9+
readByte(): number
Reads the byte value from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Byte value read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeByte(2);
let ret = data.readByte();
hilog.info(0x0000, 'testTag', 'readByte is: ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeShort9+
writeShort(val: number): void
Writes a short integer to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Short integer to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeShort(8);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readShort9+
readShort(): number
Reads the short integer from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Short integer read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeShort(8);
let ret = data.readShort();
hilog.info(0x0000, 'testTag', 'readShort is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeInt9+
writeInt(val: number): void
Writes an integer to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Integer to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInt(10);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readInt9+
readInt(): number
Reads the integer from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Integer read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeInt(10);
let ret = data.readInt();
hilog.info(0x0000, 'testTag', 'readInt is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeLong9+
writeLong(val: number): void
Writes a long integer to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Long integer to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeLong(10000);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readLong9+
readLong(): number
Reads the long integer from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Long integer read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeLong(10000);
let ret = data.readLong();
hilog.info(0x0000, 'testTag', 'readLong is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeFloat9+
writeFloat(val: number): void
Writes a double value to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Double value to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeFloat(1.2);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readFloat9+
readFloat(): number
Reads the double value from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Double value read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeFloat(1.2);
let ret = data.readFloat();
hilog.info(0x0000, 'testTag', 'readFloat is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeDouble9+
writeDouble(val: number): void
Writes a double value to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Double value to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeDouble(10.2);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readDouble9+
readDouble(): number
Reads the double value from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Double value read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeDouble(10.2);
let ret = data.readDouble();
hilog.info(0x0000, 'testTag', 'readDouble is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeBoolean9+
writeBoolean(val: boolean): void
Writes a Boolean value to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | boolean | Yes | Boolean value to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeBoolean(false);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readBoolean9+
readBoolean(): boolean
Reads the Boolean value from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Boolean value read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeBoolean(false);
let ret = data.readBoolean();
hilog.info(0x0000, 'testTag', 'readBoolean is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeChar9+
writeChar(val: number): void
Writes a character to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Char value to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeChar(97);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readChar9+
readChar(): number
Reads the character from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Char value read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeChar(97);
let ret = data.readChar();
hilog.info(0x0000, 'testTag', 'readChar is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeString9+
writeString(val: string): void
Writes a string to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | string | Yes | String to write. The length of the string must be less than 40960 bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeString('abc');
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readString9+
readString(): string
Reads the string from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | String read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeString('abc');
let ret = data.readString();
hilog.info(0x0000, 'testTag', 'readString is ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeParcelable9+
writeParcelable(val: Parcelable): void
Writes a Parcelable object to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | Parcelable | Yes | Parcelable object to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor( num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let data = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readParcelable9+
readParcelable(dataIn: Parcelable): void
Reads the Parcelable object from this MessageSequence object to the specified object (dataIn).
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | Parcelable | Yes | Parcelable object to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect. |
| 1900010 | Failed to read data from the message sequence. |
| 1900012 | Failed to call the JS callback function. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor( num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let data = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
let ret = new MyParcelable(0, "");
data.readParcelable(ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeByteArray9+
writeByteArray(byteArray: number[]): void
Writes a byte array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| byteArray | number[] | Yes | Byte array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array. 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
data.writeByteArray(ByteArrayVar);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readByteArray9+
readByteArray(dataIn: number[]): void
Reads the byte array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Byte array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
data.writeByteArray(ByteArrayVar);
let array: Array<number> = new Array(5);
data.readByteArray(array);
hilog.info(0x0000, 'testTag', 'readByteArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readByteArray9+
readByteArray(): number[]
Reads the byte array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Byte array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
data.writeByteArray(ByteArrayVar);
let array = data.readByteArray();
hilog.info(0x0000, 'testTag', 'readByteArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeShortArray9+
writeShortArray(shortArray: number[]): void
Writes a short array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| shortArray | number[] | Yes | Short array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeShortArray([11, 12, 13]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readShortArray9+
readShortArray(dataIn: number[]): void
Reads the short array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Short array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeShortArray([11, 12, 13]);
let array: Array<number> = new Array(3);
data.readShortArray(array);
hilog.info(0x0000, 'testTag', 'readShortArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readShortArray9+
readShortArray(): number[]
Reads the short array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Short array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeShortArray([11, 12, 13]);
let array = data.readShortArray();
hilog.info(0x0000, 'testTag', 'readShortArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeIntArray9+
writeIntArray(intArray: number[]): void
Writes an integer array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| intArray | number[] | Yes | Integer array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeIntArray([100, 111, 112]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readIntArray9+
readIntArray(dataIn: number[]): void
Reads the integer array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Integer array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeIntArray([100, 111, 112]);
let array: Array<number> = new Array(3);
data.readIntArray(array);
hilog.info(0x0000, 'testTag', 'readIntArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readIntArray9+
readIntArray(): number[]
Reads the integer array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Integer array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeIntArray([100, 111, 112]);
let array = data.readIntArray();
hilog.info(0x0000, 'testTag', 'readIntArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeLongArray9+
writeLongArray(longArray: number[]): void
Writes a long array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| longArray | number[] | Yes | Long array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeLongArray([1111, 1112, 1113]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readLongArray9+
readLongArray(dataIn: number[]): void
Reads the long array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Long array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeLongArray([1111, 1112, 1113]);
let array: Array<number> = new Array(3);
data.readLongArray(array);
hilog.info(0x0000, 'testTag', 'readLongArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readLongArray9+
readLongArray(): number[]
Reads the long integer array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Long array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeLongArray([1111, 1112, 1113]);
let array = data.readLongArray();
hilog.info(0x0000, 'testTag', 'readLongArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeFloatArray9+
writeFloatArray(floatArray: number[]): void
Writes a double array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| floatArray | number[] | Yes | Double array to write. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeFloatArray([1.2, 1.3, 1.4]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readFloatArray9+
readFloatArray(dataIn: number[]): void
Reads the double array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Double array to read. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeFloatArray([1.2, 1.3, 1.4]);
let array: Array<number> = new Array(3);
data.readFloatArray(array);
hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readFloatArray9+
readFloatArray(): number[]
Reads the double array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Double array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeFloatArray([1.2, 1.3, 1.4]);
let array = data.readFloatArray();
hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeDoubleArray9+
writeDoubleArray(doubleArray: number[]): void
Writes a double array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| doubleArray | number[] | Yes | Double array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The type of the element in the array is incorrect. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeDoubleArray([11.1, 12.2, 13.3]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readDoubleArray9+
readDoubleArray(dataIn: number[]): void
Reads the double array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Double array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeDoubleArray([11.1, 12.2, 13.3]);
let array: Array<number> = new Array(3);
data.readDoubleArray(array);
hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readDoubleArray9+
readDoubleArray(): number[]
Reads the double array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Double array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeDoubleArray([11.1, 12.2, 13.3]);
let array = data.readDoubleArray();
hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeBooleanArray9+
writeBooleanArray(booleanArray: boolean[]): void
Writes a Boolean array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| booleanArray | boolean[] | Yes | Boolean array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeBooleanArray([false, true, false]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readBooleanArray9+
readBooleanArray(dataIn: boolean[]): void
Reads the Boolean array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | boolean[] | Yes | Boolean array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeBooleanArray([false, true, false]);
let array: Array<boolean> = new Array(3);
data.readBooleanArray(array);
hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readBooleanArray9+
readBooleanArray(): boolean[]
Reads the Boolean array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean[] | Boolean array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeBooleanArray([false, true, false]);
let array = data.readBooleanArray();
hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeCharArray9+
writeCharArray(charArray: number[]): void
Writes a character array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| charArray | number[] | Yes | Character array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeCharArray([97, 98, 88]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readCharArray9+
readCharArray(dataIn: number[]): void
Reads the character array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Character array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeCharArray([97, 98, 88]);
let array: Array<number> = new Array(3);
data.readCharArray(array);
hilog.info(0x0000, 'testTag', 'readCharArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readCharArray9+
readCharArray(): number[]
Reads the character array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Character array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeCharArray([97, 98, 88]);
let array = data.readCharArray();
hilog.info(0x0000, 'testTag', 'readCharArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeStringArray9+
writeStringArray(stringArray: string[]): void
Writes a string array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The string length is greater than or equal to 40960 bytes; 5.The number of bytes copied to the buffer is different from the length of the obtained string. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeStringArray(["abc", "def"]);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readStringArray9+
readStringArray(dataIn: string[]): void
Reads the string array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | string[] | Yes | String array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeStringArray(["abc", "def"]);
let array: Array<string> = new Array(2);
data.readStringArray(array);
hilog.info(0x0000, 'testTag', 'readStringArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readStringArray9+
readStringArray(): string[]
Reads the string array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string[] | String array read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
data.writeStringArray(["abc", "def"]);
let array = data.readStringArray();
hilog.info(0x0000, 'testTag', 'readStringArray is ' + array);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeNoException9+
writeNoException(): void
Writes information to this MessageSequence object indicating that no exception occurred.
System capability: SystemCapability.Communication.IPC.Core
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called');
try {
reply.writeNoException();
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message);
}
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
}
readException9+
readException(): void
Reads the exception information from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, sendMessageRequest() of the proxy object is called to send a message.
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let data = rpc.MessageSequence.create();
let reply = rpc.MessageSequence.create();
data.writeNoException();
data.writeInt(6);
if (proxy != undefined) {
proxy.sendMessageRequest(1, data, reply, option)
.then((result: rpc.RequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendMessageRequest got result');
result.reply.readException();
let num = result.reply.readInt();
hilog.info(0x0000, 'testTag', 'reply num: ' + num);
} else {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendMessageRequest got exception: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
}
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeParcelableArray9+
writeParcelableArray(parcelableArray: Parcelable[]): void
Writes the Parcelable array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| parcelableArray | Parcelable[] | Yes | Parcelable array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let parcelable2 = new MyParcelable(2, "bbb");
let parcelable3 = new MyParcelable(3, "ccc");
let a = [parcelable, parcelable2, parcelable3];
let data = rpc.MessageSequence.create();
data.writeParcelableArray(a);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message);
}
readParcelableArray9+
readParcelableArray(parcelableArray: Parcelable[]): void
Reads the Parcelable array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| parcelableArray | Parcelable[] | Yes | Parcelable array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The length of the array passed when reading is not equal to the length passed when writing to the array; 5.The element does not exist in the array. |
| 1900010 | Failed to read data from the message sequence. |
| 1900012 | Failed to call the JS callback function. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let parcelable2 = new MyParcelable(2, "bbb");
let parcelable3 = new MyParcelable(3, "ccc");
let a = [parcelable, parcelable2, parcelable3];
let data = rpc.MessageSequence.create();
data.writeParcelableArray(a);
let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")];
data.readParcelableArray(b);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message);
}
writeRemoteObjectArray9+
writeRemoteObjectArray(objectArray: IRemoteObject[]): void
Writes an IRemoteObject array to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| objectArray | IRemoteObject[] | Yes | IRemoteObject array to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The element does not exist in the array; 5.The obtained remoteObject is null. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageSequence.create();
data.writeRemoteObjectArray(a);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readRemoteObjectArray9+
readRemoteObjectArray(objects: IRemoteObject[]): void
Reads the IRemoteObject array from this MessageSequence object and writes it to the created empty array.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| objects | IRemoteObject[] | Yes | IRemoteObject array to read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The length of the array passed when reading is not equal to the length passed when writing to the array. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageSequence.create();
data.writeRemoteObjectArray(a);
let b: Array<rpc.IRemoteObject> = new Array(3);
data.readRemoteObjectArray(b);
hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readRemoteObjectArray9+
readRemoteObjectArray(): IRemoteObject[]
Reads the IRemoteObject array from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject[] | The IRemoteObject array is returned. If an empty array is written, null is returned. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageSequence.create();
let b = data.readRemoteObjectArray();
hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
closeFileDescriptor9+
static closeFileDescriptor(fd: number): void
Closes a file descriptor. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to close. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
rpc.MessageSequence.closeFileDescriptor(file.fd);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
dupFileDescriptor9+
static dupFileDescriptor(fd: number): number
Duplicates a file descriptor. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to duplicate. |
Return value
| Type | Description |
|---|---|
| number | New file descriptor. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900013 | Failed to call dup. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
rpc.MessageSequence.dupFileDescriptor(file.fd);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
containFileDescriptors9+
containFileDescriptors(): boolean
Checks whether this MessageSequence object contains file descriptors.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the MessageSequence object contains file descriptors; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
let containFD = sequence.containFileDescriptors();
hilog.info(0x0000, 'testTag', 'sequence after write fd containFd result is ' + containFD);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeFileDescriptor9+
writeFileDescriptor(fd: number): void
Writes a file descriptor to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
sequence.writeFileDescriptor(file.fd);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readFileDescriptor9+
readFileDescriptor(): number
Reads the file descriptor from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | File descriptor read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
sequence.writeFileDescriptor(file.fd);
let readFD = sequence.readFileDescriptor();
hilog.info(0x0000, 'testTag', 'readFileDescriptor is ' + readFD);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeAshmem9+
writeAshmem(ashmem: Ashmem): void
Writes an anonymous shared object to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| ashmem | Ashmem | Yes | Anonymous shared object to write. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter is not an instance of the Ashmem object. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let ashmem = rpc.Ashmem.create("ashmem", 1024);
// Write data to ashmem.
let buffer = new ArrayBuffer(1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
ashmem.mapReadWriteAshmem();
ashmem.writeDataToAshmem(buffer, size, 0);
// Write the ashmem object to the messageSequence object.
sequence.writeAshmem(ashmem);
// Write the size of the transferred data to this MessageSequence object.
sequence.writeInt(size);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readAshmem9+
readAshmem(): Ashmem
Reads the anonymous shared object from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| Ashmem | Anonymous share object obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let ashmem = rpc.Ashmem.create("ashmem", 1024);
// Write data to ashmem.
let buffer = new ArrayBuffer(1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
ashmem.mapReadWriteAshmem();
ashmem.writeDataToAshmem(buffer, size, 0);
// Write the size of the transferred data to this MessageSequence object.
sequence.writeInt(size);
// Write the ashmem object to the messageSequence object.
sequence.writeAshmem(ashmem);
// Read the size of the transferred data.
let dataSize = sequence.readInt();
// Read the ashmem object from this MessageSequence object.
let ashmem1 = sequence.readAshmem();
// Read data from the ashmem object.
ashmem1.mapReadWriteAshmem();
let readResult = ashmem1.readDataFromAshmem(dataSize, 0);
let readInt32View = new Int32Array(readResult);
hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readInt32View);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getRawDataCapacity9+
getRawDataCapacity(): number
Obtains the maximum amount of raw data that can be held by this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Maximum amount of raw data that MessageSequence can hold, that is, 128 MB. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let result = sequence.getRawDataCapacity();
hilog.info(0x0000, 'testTag', 'sequence get RawDataCapacity result is ' + result);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeRawData(deprecated)
writeRawData(rawData: number[], size: number): void
Writes raw data to this MessageSequence object.
NOTE
This API is supported since API version 9 and deprecated since API version 11. Use writeRawDataBuffer instead.
This API cannot be called for multiple times in one parcel communication.
When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| rawData | number[] | Yes | Raw data to write. The size cannot exceed 128 MB. |
| size | number | Yes | Size of the raw data, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The transferred size cannot be obtained; 5.The transferred size is less than or equal to 0; 6.The element does not exist in the array; 7.Failed to obtain typedArray information; 8.The array is not of type int32; 9.The length of typedarray is smaller than the size of the original data sent. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let arr = [1, 2, 3, 4, 5];
sequence.writeRawData(arr, arr.length);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeRawDataBuffer11+
writeRawDataBuffer(rawData: ArrayBuffer, size: number): void
Writes raw data to this MessageSequence object.
NOTE
This API cannot be called for multiple times in one parcel communication. When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| rawData | ArrayBuffer | Yes | Raw data to write. The size cannot exceed 128 MB. |
| size | number | Yes | Size of the raw data, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain arrayBuffer information; 4.The transferred size cannot be obtained; 5.The transferred size is less than or equal to 0; 6.The transferred size is greater than the byte length of ArrayBuffer. |
| 1900009 | Failed to write data to the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let buffer = new ArrayBuffer(64 * 1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
let sequence = rpc.MessageSequence.create();
sequence.writeRawDataBuffer(buffer, size);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readRawData(deprecated)
readRawData(size: number): number[]
Reads raw data from this MessageSequence object.
NOTE
This API is supported since API version 9 and deprecated since API version 11. Use readRawDataBuffer instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the raw data to read. |
Return value
| Type | Description |
|---|---|
| number[] | Raw data obtained, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let sequence = rpc.MessageSequence.create();
let arr = [1, 2, 3, 4, 5];
sequence.writeRawData(arr, arr.length);
let size = arr.length;
let result = sequence.readRawData(size);
hilog.info(0x0000, 'testTag', 'sequence read raw data result is ' + result);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readRawDataBuffer11+
readRawDataBuffer(size: number): ArrayBuffer
Reads raw data from this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the raw data to read. |
Return value
| Type | Description |
|---|---|
| ArrayBuffer | Raw data obtained, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900010 | Failed to read data from the message sequence. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let buffer = new ArrayBuffer(64 * 1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
let sequence = rpc.MessageSequence.create();
sequence.writeRawDataBuffer(buffer, size);
let result = sequence.readRawDataBuffer(size);
let readInt32View = new Int32Array(result);
hilog.info(0x0000, 'testTag', 'sequence read raw data result is ' + readInt32View);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeArrayBuffer12+
writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void
Writes data of the ArrayBuffer type to this MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| buf | ArrayBuffer | Yes | Data to write. |
| typeCode | TypeCode | Yes | TypedArray type of the ArrayBuffer data. The underlying write mode is determined based on the enum value of TypeCode passed by the service. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The parameter is an empty array; 2.The number of parameters is incorrect; 3.The parameter type does not match; 4.The obtained value of typeCode is incorrect; 5.Failed to obtain arrayBuffer information. |
| 1900009 | Failed to write data to the message sequence. |
Example
// In this example, the value of TypeCode is Int16Array.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let buffer = new ArrayBuffer(10);
let int16View = new Int16Array(buffer);
for (let i = 0; i < int16View.length; i++) {
int16View[i] = i * 2 + 1;
}
data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readArrayBuffer12+
readArrayBuffer(typeCode: TypeCode): ArrayBuffer
Reads data of the ArrayBuffer type from this MessageSequence.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| typeCode | TypeCode | Yes | TypedArray type of the ArrayBuffer data. The underlying read mode is determined based on the enum value of TypeCode passed by the service. |
Return value
| Type | Description |
|---|---|
| ArrayBuffer | Data of the ArrayBuffer type read, in bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The obtained value of typeCode is incorrect; |
| 1900010 | Failed to read data from the message sequence. |
Example
// In this example, the value of TypeCode is Int16Array.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let data = rpc.MessageSequence.create();
let buffer = new ArrayBuffer(10);
let int16View = new Int16Array(buffer);
for (let i = 0; i < int16View.length; i++) {
int16View[i] = i * 2 + 1;
}
data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY);
let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY);
let readInt16View = new Int16Array(result);
hilog.info(0x0000, 'testTag', 'read ArrayBuffer result is ' + readInt16View);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
MessageParcel(deprecated)
Provides APIs for reading and writing data in specific format. During RPC, the sender can use the write() method provided by MessageParcel to write data in specific format to a MessageParcel object. The receiver can use the read() method provided by MessageParcel to read data in specific format from a MessageParcel object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use MessageSequence instead.
System capability: SystemCapability.Communication.IPC.Core
create(deprecated)
static create(): MessageParcel
Creates a MessageParcel object. This method is a static method.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use create instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| MessageParcel | MessageParcel object created. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
hilog.info(0x0000, 'testTag', 'data is ' + data);
// When the MessageParcel object is no longer used, the service calls the reclaim method to release resources.
data.reclaim();
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
reclaim(deprecated)
reclaim(): void
Reclaims the MessageParcel object that is no longer used.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use reclaim instead.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let reply = rpc.MessageParcel.create();
reply.reclaim();
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeRemoteObject(deprecated)
writeRemoteObject(object: IRemoteObject): boolean
Serializes a remote object and writes it to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeRemoteObject instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| object | IRemoteObject | Yes | Remote object to serialize and write to the MessageParcel object. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
// Process services based on the actual service logic.
return true;
}
}
try {
let data = rpc.MessageParcel.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readRemoteObject(deprecated)
readRemoteObject(): IRemoteObject
Reads the remote object from this MessageParcel object. You can use this method to deserialize the MessageParcel object to generate an IRemoteObject. The remote objects are read in the order in which they are written to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readRemoteObject instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject | Remote object obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel,
option: rpc.MessageOption): boolean {
// Process services based on the actual service logic.
return true;
}
}
try {
let data = rpc.MessageParcel.create();
let testRemoteObject = new TestRemoteObject("testObject");
data.writeRemoteObject(testRemoteObject);
let proxy = data.readRemoteObject();
hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeInterfaceToken(deprecated)
writeInterfaceToken(token: string): boolean
Writes an interface token to this MessageParcel object. The remote object can use this interface token to verify the communication.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeInterfaceToken instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| token | string | Yes | Interface token to write. The length of the string must be less than 40960 bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeInterfaceToken("aaa");
hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readInterfaceToken(deprecated)
readInterfaceToken(): string
Reads the interface token from this MessageParcel object. The interface token is read in the sequence in which it is written to the MessageParcel object. The local object can use it to verify the communication.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readInterfaceToken instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface token obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeInterfaceToken("aaa");
let interfaceToken = data.readInterfaceToken();
hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getSize(deprecated)
getSize(): number
Obtains the data size of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getSize instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Size of the MessageParcel object obtained, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(1);
let size = data.getSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getCapacity(deprecated)
getCapacity(): number
Obtains the capacity of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getCapacity instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | MessageParcel capacity obtained, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.getCapacity();
hilog.info(0x0000, 'testTag', 'capacity is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
setSize(deprecated)
setSize(size: number): boolean
Sets the size of data contained in this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use setSize instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Data size to set, in bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let setSize = data.setSize(16);
hilog.info(0x0000, 'testTag', 'setSize is ' + setSize);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
setCapacity(deprecated)
setCapacity(size: number): boolean
Sets the storage capacity of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use setCapacity instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Storage capacity to set, in bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.setCapacity(100);
hilog.info(0x0000, 'testTag', 'setCapacity is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getWritableBytes(deprecated)
getWritableBytes(): number
Obtains the writable capacity of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getWritableBytes instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | MessageParcel writable capacity obtained, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(1);
let getWritableBytes = data.getWritableBytes();
hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getReadableBytes(deprecated)
getReadableBytes(): number
Obtains the readable capacity of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getReadableBytes instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | MessageParcel object readable capacity, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(1);
let result = data.getReadableBytes();
hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getReadPosition(deprecated)
getReadPosition(): number
Obtains the read position of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getReadPosition instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Current read position of the MessageParcel object. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let readPos = data.getReadPosition();
hilog.info(0x0000, 'testTag', 'readPos is ' + readPos);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getWritePosition(deprecated)
getWritePosition(): number
Obtains the write position of this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getWritePosition instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Current write position of the MessageParcel object. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(10);
let bwPos = data.getWritePosition();
hilog.info(0x0000, 'testTag', 'bwPos is ' + bwPos);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
rewindRead(deprecated)
rewindRead(pos: number): boolean
Moves the read pointer to the specified position.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use rewindRead instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| pos | number | Yes | Position from which data is to read. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the read position changes; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(12);
data.writeString("parcel");
let number = data.readInt();
hilog.info(0x0000, 'testTag', 'number is ' + number);
data.rewindRead(0);
let number2 = data.readInt();
hilog.info(0x0000, 'testTag', 'rewindRead is ' + number2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
rewindWrite(deprecated)
rewindWrite(pos: number): boolean
Moves the write pointer to the specified position.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use rewindWrite instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| pos | number | Yes | Position from which data is to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the write position changes; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
data.writeInt(4);
data.rewindWrite(0);
data.writeInt(5);
let number = data.readInt();
hilog.info(0x0000, 'testTag', 'rewindWrite is ' + number);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeByte(deprecated)
writeByte(val: number): boolean
Writes a Byte value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeByte instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Byte value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeByte(2);
hilog.info(0x0000, 'testTag', 'writeByte is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readByte(deprecated)
readByte(): number
Reads the byte value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readByte instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Byte value read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeByte(2);
hilog.info(0x0000, 'testTag', 'writeByte is ' + result);
let ret = data.readByte();
hilog.info(0x0000, 'testTag', 'readByte is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeShort(deprecated)
writeShort(val: number): boolean
Writes a short int value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeShort instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Short integer to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeShort(8);
hilog.info(0x0000, 'testTag', 'writeShort is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readShort(deprecated)
readShort(): number
Reads the short integer from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readShort instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Short integer read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeShort(8);
hilog.info(0x0000, 'testTag', 'writeShort is ' + result);
let ret = data.readShort();
hilog.info(0x0000, 'testTag', 'readShort is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeInt(deprecated)
writeInt(val: number): boolean
Writes an int value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeInt instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Integer to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeInt(10);
hilog.info(0x0000, 'testTag', 'writeInt is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readInt(deprecated)
readInt(): number
Reads the integer from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readInt instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Integer read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeInt(10);
hilog.info(0x0000, 'testTag', 'writeInt is ' + result);
let ret = data.readInt();
hilog.info(0x0000, 'testTag', 'readInt is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeLong(deprecated)
writeLong(val: number): boolean
Writes a long int value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeLong instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Long int value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeLong(10000);
hilog.info(0x0000, 'testTag', 'writeLong is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readLong(deprecated)
readLong(): number
Reads the long int value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readLong instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Long integer read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeLong(10000);
hilog.info(0x0000, 'testTag', 'writeLong is ' + result);
let ret = data.readLong();
hilog.info(0x0000, 'testTag', 'readLong is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeFloat(deprecated)
writeFloat(val: number): boolean
Writes a double value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeFloat instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Double value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeFloat(1.2);
hilog.info(0x0000, 'testTag', 'writeFloat is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readFloat(deprecated)
readFloat(): number
Reads the double value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readFloat instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Double value read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeFloat(1.2);
hilog.info(0x0000, 'testTag', 'writeFloat is ' + result);
let ret = data.readFloat();
hilog.info(0x0000, 'testTag', 'readFloat is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeDouble(deprecated)
writeDouble(val: number): boolean
Writes a double value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeDouble instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Double value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeDouble(10.2);
hilog.info(0x0000, 'testTag', 'writeDouble is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readDouble(deprecated)
readDouble(): number
Reads the double value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readDouble instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Double value read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeDouble(10.2);
hilog.info(0x0000, 'testTag', 'writeDouble is ' + result);
let ret = data.readDouble();
hilog.info(0x0000, 'testTag', 'readDouble is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeBoolean(deprecated)
writeBoolean(val: boolean): boolean
Writes a Boolean value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeBoolean instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | boolean | Yes | Boolean value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeBoolean(false);
hilog.info(0x0000, 'testTag', 'writeBoolean is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readBoolean(deprecated)
readBoolean(): boolean
Reads the Boolean value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readBoolean instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Boolean value read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeBoolean(false);
hilog.info(0x0000, 'testTag', 'writeBoolean is ' + result);
let ret = data.readBoolean();
hilog.info(0x0000, 'testTag', 'readBoolean is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeChar(deprecated)
writeChar(val: number): boolean
Writes a single character value to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeChar instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | number | Yes | Char value to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeChar(97);
hilog.info(0x0000, 'testTag', 'writeChar is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readChar(deprecated)
readChar(): number
Reads the single character value from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readChar instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Char value read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeChar(97);
hilog.info(0x0000, 'testTag', 'writeChar is ' + result);
let ret = data.readChar();
hilog.info(0x0000, 'testTag', 'readChar is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeString(deprecated)
writeString(val: string): boolean
Writes a string to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeString instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | string | Yes | String to write. The length of the string must be less than 40960 bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeString('abc');
hilog.info(0x0000, 'testTag', 'writeString is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readString(deprecated)
readString(): string
Reads the string from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readString instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | String read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeString('abc');
hilog.info(0x0000, 'testTag', 'writeString is ' + result);
let ret = data.readString();
hilog.info(0x0000, 'testTag', 'readString is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeSequenceable(deprecated)
writeSequenceable(val: Sequenceable): boolean
Writes a Sequenceable object to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeParcelable instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| val | Sequenceable | Yes | Sequenceable object to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readSequenceable(deprecated)
readSequenceable(dataIn: Sequenceable): boolean
Reads member variables from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readParcelable instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | Sequenceable | Yes | Object that reads member variables from the MessageParcel object. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeByteArray(deprecated)
writeByteArray(byteArray: number[]): boolean
Writes a byte array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeByteArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| byteArray | number[] | Yes | Byte array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
hilog.info(0x0000, 'testTag', 'writeByteArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readByteArray(deprecated)
readByteArray(dataIn: number[]): void
Reads the byte array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readByteArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Byte array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
let array: Array<number> = new Array(5);
data.readByteArray(array);
hilog.info(0x0000, 'testTag', 'readByteArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readByteArray(deprecated)
readByteArray(): number[]
Reads the byte array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readByteArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Byte array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let ByteArrayVar = [1, 2, 3, 4, 5];
let result = data.writeByteArray(ByteArrayVar);
hilog.info(0x0000, 'testTag', 'writeByteArray is ' + result);
let array = data.readByteArray();
hilog.info(0x0000, 'testTag', 'readByteArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeShortArray(deprecated)
writeShortArray(shortArray: number[]): boolean
Writes a short array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeShortArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| shortArray | number[] | Yes | Short array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readShortArray(deprecated)
readShortArray(dataIn: number[]): void
Reads the short array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readShortArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Short array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result);
let array: Array<number> = new Array(3);
data.readShortArray(array);
hilog.info(0x0000, 'testTag', 'readShortArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readShortArray(deprecated)
readShortArray(): number[]
Reads the short array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readShortArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Short array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeShortArray([11, 12, 13]);
hilog.info(0x0000, 'testTag', 'writeShortArray is ' + result);
let array = data.readShortArray();
hilog.info(0x0000, 'testTag', 'readShortArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeIntArray(deprecated)
writeIntArray(intArray: number[]): boolean
Writes an integer array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeIntArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| intArray | number[] | Yes | Integer array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readIntArray(deprecated)
readIntArray(dataIn: number[]): void
Reads the integer array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readIntArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Integer array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result);
let array: Array<number> = new Array(3);
data.readIntArray(array);
hilog.info(0x0000, 'testTag', 'readIntArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readIntArray(deprecated)
readIntArray(): number[]
Reads the integer array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readIntArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Integer array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeIntArray([100, 111, 112]);
hilog.info(0x0000, 'testTag', 'writeIntArray is ' + result);
let array = data.readIntArray();
hilog.info(0x0000, 'testTag', 'readIntArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeLongArray(deprecated)
writeLongArray(longArray: number[]): boolean
Writes a long array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeLongArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| longArray | number[] | Yes | Long array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readLongArray(deprecated)
readLongArray(dataIn: number[]): void
Reads the long array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readLongArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Long array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result);
let array: Array<number> = new Array(3);
data.readLongArray(array);
hilog.info(0x0000, 'testTag', 'readLongArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readLongArray(deprecated)
readLongArray(): number[]
Reads the long array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readLongArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Long array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeLongArray([1111, 1112, 1113]);
hilog.info(0x0000, 'testTag', 'writeLongArray is ' + result);
let array = data.readLongArray();
hilog.info(0x0000, 'testTag', 'readLongArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeFloatArray(deprecated)
writeFloatArray(floatArray: number[]): boolean
Writes a double array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeFloatArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| floatArray | number[] | Yes | Double array to write. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readFloatArray(deprecated)
readFloatArray(dataIn: number[]): void
Reads the double array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readFloatArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Double array to read. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result);
let array: Array<number> = new Array(3);
data.readFloatArray(array);
hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readFloatArray(deprecated)
readFloatArray(): number[]
Reads the double array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readFloatArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Double array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeFloatArray([1.2, 1.3, 1.4]);
hilog.info(0x0000, 'testTag', 'writeFloatArray is ' + result);
let array = data.readFloatArray();
hilog.info(0x0000, 'testTag', 'readFloatArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeDoubleArray(deprecated)
writeDoubleArray(doubleArray: number[]): boolean
Writes a double array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeDoubleArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| doubleArray | number[] | Yes | Double array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readDoubleArray(deprecated)
readDoubleArray(dataIn: number[]): void
Reads the double array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readDoubleArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Double array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result);
let array: Array<number> = new Array(3);
data.readDoubleArray(array);
hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readDoubleArray(deprecated)
readDoubleArray(): number[]
Reads the double array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readDoubleArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Double array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
hilog.info(0x0000, 'testTag', 'writeDoubleArray is ' + result);
let array = data.readDoubleArray();
hilog.info(0x0000, 'testTag', 'readDoubleArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeBooleanArray(deprecated)
writeBooleanArray(booleanArray: boolean[]): boolean
Writes a Boolean array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeBooleanArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| booleanArray | boolean[] | Yes | Boolean array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readBooleanArray(deprecated)
readBooleanArray(dataIn: boolean[]): void
Reads the Boolean array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readBooleanArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | boolean[] | Yes | Boolean array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result);
let array: Array<boolean> = new Array(3);
data.readBooleanArray(array);
hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readBooleanArray(deprecated)
readBooleanArray(): boolean[]
Reads the Boolean array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readBooleanArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean[] | Boolean array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeBooleanArray([false, true, false]);
hilog.info(0x0000, 'testTag', 'writeBooleanArray is ' + result);
let array = data.readBooleanArray();
hilog.info(0x0000, 'testTag', 'readBooleanArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeCharArray(deprecated)
writeCharArray(charArray: number[]): boolean
Writes a single character array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeCharArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| charArray | number[] | Yes | Character array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 88]);
hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readCharArray(deprecated)
readCharArray(dataIn: number[]): void
Reads the character array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readCharArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | number[] | Yes | Character array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 99]);
hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result);
let array: Array<number> = new Array(3);
data.readCharArray(array);
hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readCharArray(deprecated)
readCharArray(): number[]
Reads the single character array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readCharArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number[] | Character array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeCharArray([97, 98, 99]);
hilog.info(0x0000, 'testTag', 'writeCharArray is ' + result);
let array = data.readCharArray();
hilog.info(0x0000, 'testTag', 'readCharArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeStringArray(deprecated)
writeStringArray(stringArray: string[]): boolean
Writes a string array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeStringArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readStringArray(deprecated)
readStringArray(dataIn: string[]): void
Reads the string array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readStringArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | string[] | Yes | String array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result);
let array: Array<string> = new Array(2);
data.readStringArray(array);
hilog.info(0x0000, 'testTag', 'readStringArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readStringArray(deprecated)
readStringArray(): string[]
Reads the string array from this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use readStringArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string[] | String array read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let data = rpc.MessageParcel.create();
let result = data.writeStringArray(["abc", "def"]);
hilog.info(0x0000, 'testTag', 'writeStringArray is ' + result);
let array = data.readStringArray();
hilog.info(0x0000, 'testTag', 'readStringArray is ' + array);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeNoException(deprecated)
writeNoException(): void
Writes information to this MessageParcel object indicating that no exception occurred.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeNoException instead.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called');
reply.writeNoException();
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
}
readException(deprecated)
readException(): void
Reads the exception information from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readException instead.
System capability: SystemCapability.Communication.IPC.Core
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, sendRequest() of the proxy object is called to send a message.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeNoException();
data.writeString('hello');
if (proxy != undefined) {
let a = proxy.sendRequest(1, data, reply, option) as Object;
let b = a as Promise<rpc.SendRequestResult>;
b.then((result: rpc.SendRequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendRequest got result');
result.reply.readException();
let msg = result.reply.readString();
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendRequest got exception: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
}
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeSequenceableArray(deprecated)
writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean
Writes a Sequenceable array to this MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use writeParcelableArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sequenceableArray | Sequenceable[] | Yes | Sequenceable array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let sequenceable2 = new MySequenceable(2, "bbb");
let sequenceable3 = new MySequenceable(3, "ccc");
let a = [sequenceable, sequenceable2, sequenceable3];
let data = rpc.MessageParcel.create();
let result = data.writeSequenceableArray(a);
hilog.info(0x0000, 'testTag', 'writeSequenceableArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readSequenceableArray(deprecated)
readSequenceableArray(sequenceableArray: Sequenceable[]): void
Reads the Sequenceable array from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readParcelableArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sequenceableArray | Sequenceable[] | Yes | Sequenceable array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let sequenceable2 = new MySequenceable(2, "bbb");
let sequenceable3 = new MySequenceable(3, "ccc");
let a = [sequenceable, sequenceable2, sequenceable3];
let data = rpc.MessageParcel.create();
let result = data.writeSequenceableArray(a);
hilog.info(0x0000, 'testTag', 'writeSequenceableArray is ' + result);
let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")];
data.readSequenceableArray(b);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeRemoteObjectArray(deprecated)
writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean
Writes an IRemoteObject array to this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeRemoteObjectArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| objectArray | IRemoteObject[] | Yes | IRemoteObject array to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel,
option: rpc.MessageOption): boolean {
// The specific processing is determined by the service.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
let result = data.writeRemoteObjectArray(a);
hilog.info(0x0000, 'testTag', 'writeRemoteObjectArray is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readRemoteObjectArray(deprecated)
readRemoteObjectArray(objects: IRemoteObject[]): void
Reads the IRemoteObject array from this MessageParcel object and writes it to the created empty array.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readRemoteObjectArray instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| objects | IRemoteObject[] | Yes | IRemoteObject array to read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel,
option: rpc.MessageOption): boolean {
// The specific processing is determined by the service.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"),
new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
data.writeRemoteObjectArray(a);
let b: Array<rpc.IRemoteObject> = new Array(3);
data.readRemoteObjectArray(b);
hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readRemoteObjectArray(deprecated)
readRemoteObjectArray(): IRemoteObject[]
Reads the IRemoteObject array from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readRemoteObjectArray instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject[] | IRemoteObject object array obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel,
option: rpc.MessageOption): boolean {
// The specific processing is determined by the service.
return true;
}
}
try {
let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"),
new TestRemoteObject("testObject3")];
let data = rpc.MessageParcel.create();
let result = data.writeRemoteObjectArray(a);
hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + result);
let b = data.readRemoteObjectArray();
hilog.info(0x0000, 'testTag', 'readRemoteObjectArray is ' + b);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
closeFileDescriptor(deprecated)
static closeFileDescriptor(fd: number): void
Closes a file descriptor. This API is a static method.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use closeFileDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to close. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
rpc.MessageParcel.closeFileDescriptor(file.fd);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
dupFileDescriptor(deprecated)
static dupFileDescriptor(fd: number) :number
Duplicates a file descriptor. This API is a static method.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use dupFileDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to duplicate. |
Return value
| Type | Description |
|---|---|
| number | New file descriptor. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
rpc.MessageParcel.dupFileDescriptor(file.fd);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
containFileDescriptors(deprecated)
containFileDescriptors(): boolean
Checks whether this MessageParcel object contains file descriptors.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use containFileDescriptors instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the MessageParcel object contains file descriptors; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
let writeResult = parcel.writeFileDescriptor(file.fd);
hilog.info(0x0000, 'testTag', 'parcel writeFd result is ' + writeResult);
let containFD = parcel.containFileDescriptors();
hilog.info(0x0000, 'testTag', 'parcel after write fd containFd result is ' + containFD);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeFileDescriptor(deprecated)
writeFileDescriptor(fd: number): boolean
Writes a file descriptor to this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeFileDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| fd | number | Yes | File descriptor to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
let writeResult = parcel.writeFileDescriptor(file.fd);
hilog.info(0x0000, 'testTag', 'parcel writeFd result is ' + writeResult);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readFileDescriptor(deprecated)
readFileDescriptor(): number
Reads the file descriptor from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readFileDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | File descriptor read. |
Example
import { rpc } from '@kit.IPCKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let filePath = "path/to/file";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
parcel.writeFileDescriptor(file.fd);
let readFD = parcel.readFileDescriptor();
hilog.info(0x0000, 'testTag', 'parcel read fd is ' + readFD);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeAshmem(deprecated)
writeAshmem(ashmem: Ashmem): boolean
Writes an anonymous shared object to this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeAshmem instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| ashmem | Ashmem | Yes | Anonymous shared object to write. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
let isWriteSuccess = parcel.writeAshmem(ashmem);
hilog.info(0x0000, 'testTag', 'write ashmem to result is ' + isWriteSuccess);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readAshmem(deprecated)
readAshmem(): Ashmem
Reads the anonymous shared object from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readAshmem instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| Ashmem | Anonymous share object obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
let isWriteSuccess = parcel.writeAshmem(ashmem);
hilog.info(0x0000, 'testTag', 'write ashmem to result is ' + isWriteSuccess);
let readAshmem = parcel.readAshmem();
hilog.info(0x0000, 'testTag', 'read ashmem to result is ' + readAshmem);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getRawDataCapacity(deprecated)
getRawDataCapacity(): number
Obtains the maximum amount of raw data that can be held by this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use getRawDataCapacity instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Maximum amount of raw data that MessageParcel can hold, that is, 128 MB. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let result = parcel.getRawDataCapacity();
hilog.info(0x0000, 'testTag', 'parcel get RawDataCapacity result is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeRawData(deprecated)
writeRawData(rawData: number[], size: number): boolean
Writes raw data to this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeRawDataBuffer instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| rawData | number[] | Yes | Raw data to write. The size cannot exceed 128 MB. |
| size | number | Yes | Size of the raw data, in bytes. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let arr = [1, 2, 3, 4, 5];
let isWriteSuccess = parcel.writeRawData(arr, arr.length);
hilog.info(0x0000, 'testTag', 'parcel write raw data result is ' + isWriteSuccess);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
readRawData(deprecated)
readRawData(size: number): number[]
Reads raw data from this MessageParcel object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readRawDataBuffer instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the raw data to read. |
Return value
| Type | Description |
|---|---|
| number[] | Raw data obtained, in bytes. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let parcel = new rpc.MessageParcel();
let arr = [1, 2, 3, 4, 5];
let isWriteSuccess = parcel.writeRawData(arr, arr.length);
hilog.info(0x0000, 'testTag', 'parcel write raw data result is ' + isWriteSuccess);
let result = parcel.readRawData(5);
hilog.info(0x0000, 'testTag', 'parcel read raw data result is ' + result);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
Parcelable9+
Writes an object to a MessageSequence and reads it from the MessageSequence during IPC.
System capability: SystemCapability.Communication.IPC.Core
marshalling9+
marshalling(dataOut: MessageSequence): boolean
Marshals this Parcelable object into a MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataOut | MessageSequence | Yes | MessageSequence object to which the Parcelable object is to be marshaled. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
hilog.info(0x0000, 'testTag', 'readInt is ' + this.num + ' readString is ' + this.str);
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let data = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
let ret = new MyParcelable(0, "");
data.readParcelable(ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
unmarshalling9+
unmarshalling(dataIn: MessageSequence): boolean
Unmarshals this Parcelable object from a MessageSequence object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | MessageSequence | Yes | MessageSequence object from which the Parcelable object is to be unmarshaled. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyParcelable implements rpc.Parcelable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageSequence: rpc.MessageSequence): boolean {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageSequence: rpc.MessageSequence): boolean {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
hilog.info(0x0000, 'testTag', 'readInt is ' + this.num + ' readString is ' + this.str);
return true;
}
}
try {
let parcelable = new MyParcelable(1, "aaa");
let data = rpc.MessageSequence.create();
data.writeParcelable(parcelable);
let ret = new MyParcelable(0, "");
data.readParcelable(ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
Sequenceable(deprecated)
Writes objects of classes to a MessageParcel and reads them from the MessageParcel during IPC.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use Parcelable instead.
System capability: SystemCapability.Communication.IPC.Core
marshalling(deprecated)
marshalling(dataOut: MessageParcel): boolean
Marshals the sequenceable object into a MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use marshalling instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataOut | MessageParcel | Yes | MessageParcel object to which the sequenceable object is to be marshaled. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
unmarshalling(deprecated)
unmarshalling(dataIn: MessageParcel): boolean
Unmarshals this sequenceable object from a MessageParcel object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use unmarshalling instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataIn | MessageParcel | Yes | MessageParcel object in which the sequenceable object is to be unmarshaled. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MySequenceable implements rpc.Sequenceable {
num: number = 0;
str: string = '';
constructor(num: number, str: string) {
this.num = num;
this.str = str;
}
marshalling(messageParcel: rpc.MessageParcel): boolean {
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
return true;
}
unmarshalling(messageParcel: rpc.MessageParcel): boolean {
this.num = messageParcel.readInt();
this.str = messageParcel.readString();
return true;
}
}
try {
let sequenceable = new MySequenceable(1, "aaa");
let data = rpc.MessageParcel.create();
let result = data.writeSequenceable(sequenceable);
hilog.info(0x0000, 'testTag', 'writeSequenceable is ' + result);
let ret = new MySequenceable(0, "");
let result2 = data.readSequenceable(ret);
hilog.info(0x0000, 'testTag', 'readSequenceable is ' + result2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
IRemoteBroker
Represents the holder of a remote proxy object. It is used to obtain a proxy object.
System capability: SystemCapability.Communication.IPC.Core
asObject
asObject(): IRemoteObject
Obtains a proxy or remote object. This API must be implemented by its derived classes.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject | Returns the RemoteObject if it is the caller; returns the IRemoteObject, the holder of this RemoteProxy object, if the caller is a RemoteProxy object. |
Example
import { rpc } from '@kit.IPCKit';
class TestAbility extends rpc.RemoteObject {
asObject() {
return this;
}
}
let remoteObject = new TestAbility("testObject").asObject();
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, asObject() of the proxy object is called to obtain the proxy or remote object.
import { rpc } from '@kit.IPCKit';
class TestProxy {
remote: rpc.IRemoteObject;
constructor(remote: rpc.IRemoteObject) {
this.remote = remote;
}
asObject() {
return this.remote;
}
}
if (proxy != undefined) {
let iRemoteObject = new TestProxy(proxy).asObject();
}
DeathRecipient
Subscribes to death notifications of a remote object. When the remote object is dead, the local end will receive a notification and onRemoteDied will be called. A remote object is dead when the process holding the object is terminated or the device of the remote object is shut down or restarted. If the local and remote objects belong to different devices, the remote object is dead when the device holding the remote object is detached from the network.
System capability: SystemCapability.Communication.IPC.Core
onRemoteDied
onRemoteDied(): void
Called to perform subsequent operations when a death notification of the remote object is received.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
RequestResult9+
Defines the response to the request.
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| errCode | number | No | No | Error code. |
| code | number | No | No | Message code. |
| data | MessageSequence | No | No | MessageSequence object sent to the remote process. |
| reply | MessageSequence | No | No | MessageSequence object returned by the remote process. |
SendRequestResult(deprecated)
Defines the response to the request.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use RequestResult instead.
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| errCode | number | No | No | Error code. |
| code | number | No | No | Message code. |
| data | MessageParcel | No | No | MessageParcel object sent to the remote process. |
| reply | MessageParcel | No | No | MessageParcel object returned by the remote process. |
CallingInfo23+
Defines the IPC context, including the PID and UID, local and remote device IDs, and whether the API is invoked on the same device.
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| callerPid | number | Yes | No | PID of the caller. |
| callerUid | number | Yes | No | UID of the caller. |
| callerTokenId | number | Yes | No | Token ID of the caller. |
| remoteDeviceId | string | Yes | No | Remote device ID. This parameter is valid only in RPC scenarios. |
| localDeviceId | string | Yes | No | Local device ID. This parameter is valid only in RPC scenarios. |
| isLocalCalling | boolean | Yes | No | Whether the peer end of the current communication is a process on the local device. Returns true if the local and peer processes are on the same device; returns false otherwise. |
IRemoteObject
Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages.
System capability: SystemCapability.Communication.IPC.Core
getLocalInterface9+
getLocalInterface(descriptor: string): IRemoteBroker
Obtains the string of the interface descriptor.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| descriptor | string | Yes | Interface descriptor. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | IRemoteBroker object bound to the specified interface token. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
queryLocalInterface(deprecated)
queryLocalInterface(descriptor: string): IRemoteBroker
Obtains the string of the interface descriptor.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getLocalInterface instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| descriptor | string | Yes | Interface descriptor. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | IRemoteBroker object bound to the specified interface token. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message does not contain any content. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the message is sent successfully; returns false otherwise. |
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult>
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendMessageRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<RequestResult> | Promise used to return a requestResult instance. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult>
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<SendRequestResult> | Promise used to return a sendRequestResult instance. |
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<RequestResult> | Yes | Callback used to return the result. When the message is sent successfully, the data returned by the server can be read from RequestResult. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. |
registerDeathRecipient9+
registerDeathRecipient(recipient: DeathRecipient, flags: number): void
Registers a callback for receiving death notifications of the remote object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to register. |
| flags | number | Yes | Flag of the death notification. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The callback used to receive remote object death notifications is empty. |
| 1900005 | Operation allowed only for the proxy object. |
| 1900008 | The proxy or remote object is invalid. |
addDeathRecipient(deprecated)
addDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Adds a callback for receiving death notifications of the remote object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use registerDeathRecipient instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to register. |
| flags | number | Yes | Flag of the death notification. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the callback is added successfully; returns false otherwise. |
unregisterDeathRecipient9+
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void
Unregisters from the callback used to receive death notifications of the remote object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to unregister. |
| flags | number | Yes | Flag of the death notification. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The callback used to receive remote object death notifications is empty. |
| 1900005 | Operation allowed only for the proxy object. |
| 1900008 | The proxy or remote object is invalid. |
removeDeathRecipient(deprecated)
removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Removes the callback used to receive death notifications of the remote object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use unregisterDeathRecipient instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to unregister. |
| flags | number | Yes | Flag of the death notification. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the callback is removed; returns false otherwise. |
getDescriptor9+
getDescriptor(): string
Obtains the interface descriptor (which is a string) of this object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900008 | The proxy or remote object is invalid. |
getInterfaceDescriptor(deprecated)
getInterfaceDescriptor(): string
Obtains the interface descriptor (which is a string) of this object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
isObjectDead
isObjectDead(): boolean
Checks whether this object is dead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the object is dead; returns false otherwise. |
RemoteProxy
Provides APIs to implement IRemoteObject.
System capability: SystemCapability.Communication.IPC.Core
Properties
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| PING_TRANSACTION | number | Yes | No | Internal instruction code used to test whether the IPC service is normal. |
| DUMP_TRANSACTION | number | Yes | No | Internal instruction code used to obtain IPC service status information. |
| INTERFACE_TRANSACTION | number | Yes | No | Internal instruction code used to obtain the remote interface token. |
| MIN_TRANSACTION_ID | number | Yes | No | Minimum valid instruction code. |
| MAX_TRANSACTION_ID | number | Yes | No | Maximum valid instruction code. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 7 and deprecated since API version 8. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the message is sent successfully; returns false otherwise. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, sendRequest() of the proxy object is called to send a message.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeInt(1);
data.writeString("hello");
if (proxy != undefined) {
let ret: boolean = proxy.sendRequest(1, data, reply, option);
if (ret) {
hilog.info(0x0000, 'testTag', 'sendRequest got result');
let msg = reply.readString();
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendRequest failed');
}
hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
}
} catch (error) {
hilog.error(0x0000, 'testTag', 'error: ' + error);
}
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult>
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendMessageRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<RequestResult> | Promise used to return a requestResult instance. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, sendMessageRequest() of the proxy object is called to send a message.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let data = rpc.MessageSequence.create();
let reply = rpc.MessageSequence.create();
data.writeInt(1);
data.writeString("hello");
if (proxy != undefined) {
proxy.sendMessageRequest(1, data, reply, option)
.then((result: rpc.RequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendMessageRequest got result');
let num = result.reply.readInt();
let msg = result.reply.readString();
hilog.info(0x0000, 'testTag', 'reply num: ' + num);
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
}
} catch (error) {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + error);
}
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult>
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<SendRequestResult> | Promise used to return a sendRequestResult instance. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, sendRequest() of the proxy object is called to send a message.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeInt(1);
data.writeString("hello");
if (proxy != undefined) {
let a = proxy.sendRequest(1, data, reply, option) as Object;
let b = a as Promise<rpc.SendRequestResult>;
b.then((result: rpc.SendRequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendRequest got result');
let num = result.reply.readInt();
let msg = result.reply.readString();
hilog.info(0x0000, 'testTag', 'reply num: ' + num);
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
}
} catch (error) {
hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + error);
}
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked at certain time after the response to RequestResult is returned, and the reply contains the returned information.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<RequestResult> | Yes | Callback used to return the result. When the message is sent successfully, the data returned by the server can be read from RequestResult. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. |
getLocalInterface9+
getLocalInterface(interfaceDes: string): IRemoteBroker
Obtains the LocalInterface object of an interface token.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| interfaceDes | string | Yes | Interface descriptor. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | Returns Null by default, which indicates a proxy interface. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | check param failed |
| 1900006 | Operation allowed only for the remote object. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, getLocalInterface() of the proxy object is called to obtain the interface descriptor.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
if (proxy != undefined) {
try {
let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject");
hilog.info(0x0000, 'testTag', 'getLocalInterface is ' + broker);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message);
}
}
queryLocalInterface(deprecated)
queryLocalInterface(interface: string): IRemoteBroker
Obtains the LocalInterface object of an interface token.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getLocalInterface instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| interface | string | Yes | Interface descriptor. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | Returns Null by default, which indicates a proxy interface. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, queryLocalInterface() of the proxy object is called to obtain the interface descriptor.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
if (proxy != undefined) {
let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject");
hilog.info(0x0000, 'testTag', 'queryLocalInterface is ' + broker);
}
registerDeathRecipient9+
registerDeathRecipient(recipient: DeathRecipient, flags: number): void
Registers a callback for receiving death notifications of the remote object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to register. |
| flags | number | Yes | Flag of the death notification. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The callback used to receive remote object death notifications is empty. |
| 1900008 | The proxy or remote object is invalid. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, registerDeathRecipient() of the proxy object is called to register a callback for receiving the death notification of the remote object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
if (proxy != undefined) {
try {
let deathRecipient = new MyDeathRecipient();
proxy.registerDeathRecipient(deathRecipient, 0);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message);
}
}
addDeathRecipient(deprecated)
addDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Adds a callback for receiving death notifications of the remote object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use registerDeathRecipient instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to add. |
| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to 0. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the callback is added successfully; returns false otherwise. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, addDeathRecipient() of the proxy object is called to add a callback for receiving the death notification of the remove object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
if (proxy != undefined) {
let deathRecipient = new MyDeathRecipient();
proxy.addDeathRecipient(deathRecipient, 0);
}
unregisterDeathRecipient9+
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void
Unregisters from the callback used to receive death notifications of the remote object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to unregister. |
| flags | number | Yes | Flag of the death notification. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The callback used to receive remote object death notifications is empty. |
| 1900008 | The proxy or remote object is invalid. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, unregisterDeathRecipient() of the proxy object is called to unregister the callback for receiving the death notification of the remote object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
if (proxy != undefined) {
try {
let deathRecipient = new MyDeathRecipient();
proxy.registerDeathRecipient(deathRecipient, 0);
proxy.unregisterDeathRecipient(deathRecipient, 0);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message);
}
}
removeDeathRecipient(deprecated)
removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Removes the callback used to receive death notifications of the remote object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use unregisterDeathRecipient instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| recipient | DeathRecipient | Yes | Callback to remove. |
| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to 0. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the callback is removed; returns false otherwise. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, removeDeathRecipient() of the proxy object is called to remove the callback used to receive the death notification of the remote object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
if (proxy != undefined) {
let deathRecipient = new MyDeathRecipient();
proxy.addDeathRecipient(deathRecipient, 0);
proxy.removeDeathRecipient(deathRecipient, 0);
}
getDescriptor9+
getDescriptor(): string
Obtains the interface descriptor (which is a string) of this object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900007 | communication failed. |
| 1900008 | The proxy or remote object is invalid. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, getDescriptor() of the proxy object is called to obtain the interface descriptor of the object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
if (proxy != undefined) {
try {
let descriptor: string = proxy.getDescriptor();
hilog.info(0x0000, 'testTag', 'descriptor is ' + descriptor);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message);
}
}
getInterfaceDescriptor(deprecated)
getInterfaceDescriptor(): string
Obtains the interface descriptor of this proxy object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, getInterfaceDescriptor() of the proxy object is called to obtain the interface descriptor of the current proxy object.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
if (proxy != undefined) {
let descriptor: string = proxy.getInterfaceDescriptor();
hilog.info(0x0000, 'testTag', 'descriptor is ' + descriptor);
}
isObjectDead
isObjectDead(): boolean
Checks whether the RemoteObject is dead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if RemoteObject is dead; returns false otherwise. |
Example
NOTE
In the sample code provided in this topic, this.getUIContext().getHostContext() is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.
// If the FA model is used, import featureAbility from @kit.AbilityKit.
// import { featureAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { Want, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let proxy: rpc.IRemoteObject | undefined;
let connect: common.ConnectOptions = {
onConnect: (elementName, remoteProxy) => {
hilog.info(0x0000, 'testTag', 'js onConnect called');
proxy = remoteProxy;
},
onDisconnect: (elementName) => {
hilog.info(0x0000, 'testTag', 'onDisconnect');
},
onFailed: () => {
hilog.info(0x0000, 'testTag', 'onFailed');
}
};
let want: Want = {
// Obtain the package name and ability name on the server.
bundleName: "com.ohos.server",
abilityName: "com.ohos.server.EntryAbility",
};
// Use this method to connect to the ability for the FA model.
// FA.connectAbility(want,connect);
// Save the connection ID, which will be used for the subsequent service disconnection.
let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext
// Save the connection ID, which will be used for the subsequent service disconnection.
let connectionId = context.connectServiceExtensionAbility(want, connect);
The proxy object in the onConnect callback can be assigned a value only after the ability is connected asynchronously. Then, isObjectDead() of the proxy object is called to check whether this object is dead.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
if (proxy != undefined) {
let isDead: boolean = proxy.isObjectDead();
hilog.info(0x0000, 'testTag', 'isObjectDead is ' + isDead);
}
MessageOption
Defines the options used to construct the MessageOption object.
System capability: SystemCapability.Communication.IPC.Core
Properties
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| TF_SYNC | number | Yes | No | Synchronous call. |
| TF_ASYNC | number | Yes | No | Asynchronous call. |
| TF_ACCEPT_FDS | number | Yes | No | Indication to sendMessageRequest9+ for passing the file descriptor. |
| TF_WAIT_TIME | number | Yes | No | RPC wait time, in seconds. This parameter cannot be used in IPC. The default waiting time is 8 seconds. You are advised not to change the waiting time. |
constructor9+
constructor(async?: boolean)
A constructor used to create a MessageOption object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| async | boolean | No | Whether to execute the call asynchronously. The value true means to execute the call asynchronously; the value false means to execute the call synchronously. The default value is synchronous. |
Example
import { rpc } from '@kit.IPCKit';
class TestRemoteObject extends rpc.MessageOption {
constructor(async: boolean) {
super(async);
}
}
constructor
constructor(syncFlags?: number, waitTime?: number)
A constructor used to create a MessageOption object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| syncFlags | number | No | Call flag to set. The options are as follows: 0 (synchronous call) and 1 (asynchronous call). The default value is synchronous. |
| waitTime | number | No | Maximum wait time for an RPC call, in seconds. The default value is TF_WAIT_TIME. |
Example
import { rpc } from '@kit.IPCKit';
class TestRemoteObject extends rpc.MessageOption {
constructor(syncFlags?: number,waitTime?: number) {
super(syncFlags,waitTime);
}
}
isAsync9+
isAsync(): boolean
Checks whether sendMessageRequest is called asynchronously.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if SendMessageRequest is called asynchronously; returns false if it is called synchronously. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let result = option.isAsync();
} catch (error) {
hilog.info(0x0000, 'testTag', 'error ' + error);
}
setAsync9+
setAsync(isAsync: boolean): void
Sets whether to call sendMessageRequest asynchronously.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| isAsync | boolean | Yes | Whether to execute the call asynchronously. The value true means to execute the call asynchronously; the value false means to execute the call synchronously. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
option.setAsync(true);
} catch (error) {
hilog.info(0x0000, 'testTag', 'error ' + error);
}
getFlags
getFlags(): number
Obtains the call flag, which can be synchronous or asynchronous.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Call flag obtained. 0: synchronous call flag; 1: asynchronous call flag. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
hilog.info(0x0000, 'testTag', 'create object successfully');
let flag = option.getFlags();
hilog.info(0x0000, 'testTag', 'run getFlags success, flag is ' + flag);
option.setFlags(rpc.MessageOption.TF_ASYNC);
hilog.info(0x0000, 'testTag', 'run setFlags success');
let flag2 = option.getFlags();
hilog.info(0x0000, 'testTag', 'run getFlags success, flag2 is ' + flag2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
setFlags
setFlags(flags: number): void
Sets the call flag, which can be synchronous or asynchronous.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| flags | number | Yes | Call flag to set. 0: synchronous call flag; 1: asynchronous call flag. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
option.setFlags(rpc.MessageOption.TF_ASYNC);
hilog.info(0x0000, 'testTag', 'run setFlags success');
let flag = option.getFlags();
hilog.info(0x0000, 'testTag', 'run getFlags success, flag is ' + flag);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getWaitTime
getWaitTime(): number
Obtains the maximum wait time for an RPC call.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Maximum wait time for an RPC call, in seconds. The default value is TF_WAIT_TIME. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
let time = option.getWaitTime();
hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time);
option.setWaitTime(16);
let time2 = option.getWaitTime();
hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
setWaitTime
setWaitTime(waitTime: number): void
Sets the maximum wait time for an RPC call.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| waitTime | number | Yes | Maximum wait time for an RPC call, in seconds. The upper limit is 3000 seconds. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let option = new rpc.MessageOption();
option.setWaitTime(16);
let time = option.getWaitTime();
hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
IPCSkeleton
Obtains IPC context, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device.
System capability: SystemCapability.Communication.IPC.Core
getContextObject
static getContextObject(): IRemoteObject
Obtains the system capability manager. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| IRemoteObject | System capability manager obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let samgr = rpc.IPCSkeleton.getContextObject();
hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
getCallingPid
static getCallingPid(): number
Obtains the PID of the caller. This API is a static method, which is called by the RemoteObject object in the IPC context onRemoteMessageRequest. If the method is not called in the IPC context, the PID of the current process is returned.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | PID of the caller. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callerPid = rpc.IPCSkeleton.getCallingPid();
hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
getCallingUid
static getCallingUid(): number
Obtains the UID of the caller. This API is a static method, which is called by the RemoteObject object in the IPC context onRemoteMessageRequest. If the method is not called in the IPC context, the UID of the current process is returned.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | UID of the caller. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callerUid = rpc.IPCSkeleton.getCallingUid();
hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
getCallingTokenId8+
static getCallingTokenId(): number
Obtains the caller's token ID, which is used to verify the caller identity.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Token ID of the caller obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callerTokenId = rpc.IPCSkeleton.getCallingTokenId();
hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
getCallingDeviceID
static getCallingDeviceID(): string
Obtains the ID of the device hosting the caller's process. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Device ID obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID();
hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
getLocalDeviceID
static getLocalDeviceID(): string
Obtains the local device ID. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Local device ID obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID();
hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
isLocalCalling
static isLocalCalling(): boolean
Checks whether the peer process is a process of the local device. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the local and peer processes are on the same device; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let isLocalCalling = rpc.IPCSkeleton.isLocalCalling();
hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
flushCmdBuffer9+
static flushCmdBuffer(object: IRemoteObject): void
Flushes all suspended commands from the specified RemoteProxy to the corresponding RemoteObject. This API is a static method. You are advised to call this API before performing any sensitive operation.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| object | IRemoteObject | Yes | RemoteProxy specified. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let remoteObject = new TestRemoteObject("aaa");
rpc.IPCSkeleton.flushCmdBuffer(remoteObject);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message);
}
flushCommands(deprecated)
static flushCommands(object: IRemoteObject): number
Flushes all suspended commands from the specified RemoteProxy to the corresponding RemoteObject. This API is a static method. You are advised to call this API before performing any sensitive operation.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use flushCmdBuffer instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| object | IRemoteObject | Yes | RemoteProxy specified. |
Return value
| Type | Description |
|---|---|
| number | Returns 0 if the operation is successful; returns an error code if the input object is null or a RemoteObject, or if the operation fails. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let remoteObject = new TestRemoteObject("aaa");
let ret = rpc.IPCSkeleton.flushCommands(remoteObject);
hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message);
}
resetCallingIdentity
static resetCallingIdentity(): string
Resets the UID and PID of the remote user to those of the local user. This API is a static method and is used in scenarios such as identity authentication.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | String containing the UID and PID of the remote user. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
restoreCallingIdentity9+
static restoreCallingIdentity(identity: string): void
Restores the UID and PID to those of the remote user. This API is a static method. It is usually called after resetCallingIdentity, and the UID and PID of the remote user returned by resetCallingIdentity are required. This API is supported only in the IPC context onRemoteMessageRequest; otherwise, it returns directly.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| identity | string | Yes | A string containing the UID and PID of the remote user. The length of the string must be less than 40960 bytes. are returned by resetCallingIdentity. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity);
rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
setCallingIdentity(deprecated)
static setCallingIdentity(identity: string): boolean
Sets the UID and PID to those of the remote user. This API is a static method. It is usually called after resetCallingIdentity, and the UID and PID of the remote user returned by resetCallingIdentity are required.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use restoreCallingIdentity instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| identity | string | Yes | String containing the remote user's UID and PID, which are returned by resetCallingIdentity. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class Stub extends rpc.RemoteObject {
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
try {
let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity);
let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity);
hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
return true;
}
}
RemoteObject
Provides methods to implement RemoteObject. The service provider must inherit from this class.
System capability: SystemCapability.Communication.IPC.Core
constructor
constructor(descriptor: string)
A constructor used to create a RemoteObject object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| descriptor | string | Yes | Interface descriptor. The length of the string must be less than 40960 bytes. |
Example
import { rpc } from '@kit.IPCKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
}
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 7 and deprecated since API version 8. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the message is sent successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class testRemoteObject extends rpc.RemoteObject {
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel,
option: rpc.MessageOption): boolean {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeInt(1);
data.writeString("hello");
let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option);
if (ret) {
hilog.info(0x0000, 'testTag', 'sendRequest got result');
let msg = reply.readString();
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendRequest failed');
}
hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult>
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendMessageRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<RequestResult> | Promise used to return a RequestResult instance. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
let option = new rpc.MessageOption();
let data = rpc.MessageSequence.create();
let reply = rpc.MessageSequence.create();
data.writeInt(1);
data.writeString("hello");
testRemoteObject.sendMessageRequest(1, data, reply, option)
.then((result: rpc.RequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendMessageRequest got result');
let num = result.reply.readInt();
let msg = result.reply.readString();
hilog.info(0x0000, 'testTag', 'reply num: ' + num);
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendMessageRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
} catch (error) {
hilog.error(0x0000, 'testTag', 'sendMessageRequest failed, error: ' + error);
}
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult>
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a promise will be fulfilled immediately and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a promise will be fulfilled when the response to sendRequest is returned, and the reply message contains the returned information. This API returns the result asynchronously through a promise.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
Return value
| Type | Description |
|---|---|
| Promise<SendRequestResult> | Promise used to return a sendRequestResult instance. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create();
data.writeInt(1);
data.writeString("hello");
let a = testRemoteObject.sendRequest(1, data, reply, option) as Object;
let b = a as Promise<rpc.SendRequestResult>;
b.then((result: rpc.SendRequestResult) => {
if (result.errCode === 0) {
hilog.info(0x0000, 'testTag', 'sendRequest got result');
let num = result.reply.readInt();
let msg = result.reply.readString();
hilog.info(0x0000, 'testTag', 'reply num: ' + num);
hilog.info(0x0000, 'testTag', 'reply msg: ' + msg);
} else {
hilog.error(0x0000, 'testTag', 'sendRequest failed, errCode: ' + result.errCode);
}
}).catch((e: Error) => {
hilog.error(0x0000, 'testTag', 'sendRequest failed, error: ' + JSON.stringify(e));
}).finally (() => {
hilog.info(0x0000, 'testTag', 'sendRequest ends, reclaim parcel');
data.reclaim();
reply.reclaim();
});
} catch (error) {
hilog.error(0x0000, 'testTag', 'error: ' + error);
}
sendMessageRequest9+
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void
Sends a MessageSequence message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked when the response to sendMessageRequest is returned, and the reply message contains the returned information.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageSequence | Yes | MessageSequence object holding the data to send. |
| reply | MessageSequence | Yes | MessageSequence object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<RequestResult> | Yes | Callback used to return the result. When the message is sent successfully, the data returned by the server can be read from RequestResult. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain the passed object instance. |
sendRequest(deprecated)
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void
Sends a MessageParcel message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in options, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in options, a callback will be invoked when the response to sendRequest is returned, and the reply message contains the returned information.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use sendMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Message code [1-16777215] called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool. |
| data | MessageParcel | Yes | MessageParcel object holding the data to send. |
| reply | MessageParcel | Yes | MessageParcel object that receives the response. |
| options | MessageOption | Yes | Request sending mode, which can be synchronous (default) or asynchronous. |
| callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. |
onRemoteMessageRequest23+
onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callingInfo?: CallingInfo): boolean | Promise<boolean>
Provides a response to sendMessageRequest(). The server processes the request and returns a response in this API. The IPC context can be obtained from the input parameter callingInfo.
NOTE
You are advised to override the onRemoteMessageRequest method with the CallingInfo parameter to implement synchronous and asynchronous message processing. If both onRemoteRequest and onRemoteMessageRequest are overridden, only onRemoteMessageRequest takes effect.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Service request code sent by the remote end. |
| data | MessageSequence | Yes | MessageSequence object that holds the parameters called by the client. |
| reply | MessageSequence | Yes | MessageSequence object to which the result is written. |
| options | MessageOption | Yes | Whether the operation is synchronous or asynchronous. |
| callingInfo | CallingInfo | No | IPC context. |
Return value
| Type | Description |
|---|---|
| boolean | Promise<boolean> | - If the request is processed synchronously in onRemoteMessageRequest, a Boolean value is returned. The value true means that the operation is successful, and false means the opposite. - If the request is processed asynchronously in onRemoteMessageRequest, a promise object is returned. The value true means that the operation is successful, and false means the opposite. |
Example
// Override **onRemoteMessageRequest** to process requests synchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption, callingInfo?: rpc.CallingInfo): boolean | Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called');
let pid = callingInfo?.callerPid;
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
}
Example
// Override **onRemoteMessageRequest** to process requests asynchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption, callingInfo?: rpc.CallingInfo): Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called');
let pid = callingInfo?.callerPid;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
await new Promise((resolve: (data: rpc.RequestResult) => void) => {
setTimeout(resolve, 100);
})
return true;
}
}
Example
// Override **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called');
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
// Only onRemoteMessageRequest is executed.
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption, callingInfo?: rpc.CallingInfo): boolean | Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called');
let pid = callingInfo?.callerPid;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
return true;
}
}
onRemoteMessageRequest9+
onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise<boolean>
Called to return a response to sendMessageRequest(). The server processes the request synchronously or asynchronously and returns the result in this API.
NOTE
You are advised to override onRemoteMessageRequest preferentially, which can implement synchronous and asynchronous message processing. If both onRemoteRequest and onRemoteMessageRequest are overridden, only onRemoteMessageRequest takes effect.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Service request code sent by the remote end. |
| data | MessageSequence | Yes | MessageSequence object that holds the parameters called by the client. |
| reply | MessageSequence | Yes | MessageSequence object to which the result is written. |
| options | MessageOption | Yes | Whether the operation is synchronous or asynchronous. |
Return value
| Type | Description |
|---|---|
| boolean | Promise<boolean> | - If the request is processed synchronously in onRemoteMessageRequest, a Boolean value is returned. The value true means that the operation is successful, and false means the opposite. - If the request is processed asynchronously in onRemoteMessageRequest, a promise object is returned. The value true means that the operation is successful, and false means the opposite. |
Example
// Override **onRemoteMessageRequest** to process requests synchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called');
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
}
Example
// Override **onRemoteMessageRequest** to process requests asynchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called');
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
await new Promise((resolve: (data: rpc.RequestResult) => void) => {
setTimeout(resolve, 100);
})
return true;
}
}
Example
// Override **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously.
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called');
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
// Only onRemoteMessageRequest is executed.
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called');
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
return true;
}
}
onRemoteRequest(deprecated)
onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
Called to return a response to sendRequest(). The server processes the request and returns a response in this function.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use onRemoteMessageRequest instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | Service request code sent by the remote end. |
| data | MessageParcel | Yes | MessageParcel object that holds the parameters called by the client. |
| reply | MessageParcel | Yes | MessageParcel object carrying the result. |
| options | MessageOption | Yes | Whether the operation is synchronous or asynchronous. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
if (code === 1) {
hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called');
return true;
} else {
hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code);
return false;
}
}
}
getCallingUid
getCallingUid(): number
Obtains the UID of the remote process.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | UID of the remote process obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid());
} catch (error) {
hilog.error(0x0000, 'testTag', 'error: ' + error);
}
getCallingPid
getCallingPid(): number
Obtains the PID of the remote process.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | PID of the remote process obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid());
} catch (error) {
hilog.error(0x0000, 'testTag', 'error: ' + error);
}
getLocalInterface9+
getLocalInterface(descriptor: string): IRemoteBroker
Obtains the string of the interface descriptor.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| descriptor | string | Yes | String of the interface descriptor. The length of the string must be less than 40960 bytes. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | IRemoteBroker object bound to the specified interface token. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
testRemoteObject.getLocalInterface("testObject");
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
queryLocalInterface(deprecated)
queryLocalInterface(descriptor: string): IRemoteBroker
Checks whether the remote object corresponding to the specified interface token exists.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getLocalInterface instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| descriptor | string | Yes | Interface descriptor. |
Return value
| Type | Description |
|---|---|
| IRemoteBroker | Returns the remote object if a match is found; returns Null otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
testRemoteObject.queryLocalInterface("testObject");
} catch (error) {
hilog.error(0x0000, 'testTag', 'error: ' + error);
}
getDescriptor9+
getDescriptor(): string
Obtains the interface descriptor of this object. The interface descriptor is a string.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900008 | The proxy or remote object is invalid. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testObject = new TestRemoteObject("ipcTest");
let descriptor = testObject.getDescriptor();
hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
getInterfaceDescriptor(deprecated)
getInterfaceDescriptor(): string
Obtains the interface descriptor.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getDescriptor instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| string | Interface descriptor obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
}
onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
option: rpc.MessageOption): boolean | Promise<boolean> {
// Process services based on the actual service logic.
return true;
}
}
try {
let testRemoteObject = new TestRemoteObject("testObject");
let descriptor = testRemoteObject.getInterfaceDescriptor();
hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
modifyLocalInterface9+
modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void
Binds an interface descriptor to an IRemoteBroker object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| localInterface | IRemoteBroker | Yes | IRemoteBroker object. |
| descriptor | string | Yes | IRemoteBroker object bound to the interface descriptor. The length of the descriptor must be less than 40960 bytes. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The string length is greater than or equal to 40960 bytes; 4.The number of bytes copied to the buffer is different from the length of the obtained string. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
try {
this.modifyLocalInterface(this, descriptor);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
}
registerDeathRecipient(recipient: MyDeathRecipient, flags: number) {
// Implement the method logic based on service requirements.
}
unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) {
// Implement the method logic based on service requirements.
}
}
let testRemoteObject = new TestRemoteObject("testObject");
attachLocalInterface(deprecated)
attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void
Binds an interface descriptor to an IRemoteBroker object.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use modifyLocalInterface instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| localInterface | IRemoteBroker | Yes | IRemoteBroker object. |
| descriptor | string | Yes | Interface descriptor. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
class MyDeathRecipient implements rpc.DeathRecipient {
onRemoteDied() {
hilog.info(0x0000, 'testTag', 'server died');
}
}
class TestRemoteObject extends rpc.RemoteObject {
constructor(descriptor: string) {
super(descriptor);
this.attachLocalInterface(this, descriptor);
}
addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
// Implement the method logic based on service requirements.
return true;
}
removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
// Implement the method logic based on service requirements.
return true;
}
}
let testRemoteObject = new TestRemoteObject("testObject");
Ashmem8+
Provides methods related to anonymous shared memory objects, including creating, closing, mapping, and unmapping an Ashmem object, reading data from and writing data to an Ashmem object, obtaining the Ashmem size, and setting Ashmem protection.
The shared memory applies only to cross-process communication within the local device.
System capability: SystemCapability.Communication.IPC.Core
Properties
System capability: SystemCapability.Communication.IPC.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| PROT_EXEC | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is executable. |
| PROT_NONE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory cannot be accessed. |
| PROT_READ | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. |
| PROT_WRITE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. |
create9+
static create(name: string, size: number): Ashmem
Creates an Ashmem object with the specified name and size. This API is a static method.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| name | string | Yes | Name of the Ashmem object to create. The length of the Ashmem name cannot be 0. |
| size | number | Yes | Size of the Ashmem object, in bytes. The value must be greater than 0. |
Return value
| Type | Description |
|---|---|
| Ashmem | Returns the Ashmem object if it is created successfully; returns null otherwise. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The Ashmem name passed is empty; 4.The Ashmem size passed is less than or equal to 0. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
hilog.info(0x0000, 'testTag', 'create ashmem: ' + ashmem);
let size = ashmem.getAshmemSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
createAshmem(deprecated)
static createAshmem(name: string, size: number): Ashmem
Creates an Ashmem object with the specified name and size. This API is a static method.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use create instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| name | string | Yes | Name of the Ashmem object to create. |
| size | number | Yes | Size (in bytes) of the Ashmem object to create. |
Return value
| Type | Description |
|---|---|
| Ashmem | Returns the Ashmem object if it is created successfully; returns null otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
hilog.info(0x0000, 'testTag', 'create ashmem: ' + ashmem);
let size = ashmem.getAshmemSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error ' + error);
}
create9+
static create(ashmem: Ashmem): Ashmem
Creates an Ashmem object by copying the file descriptor of an existing Ashmem object. The two Ashmem objects point to the same shared memory region.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| ashmem | Ashmem | Yes | Existing Ashmem object. |
Return value
| Type | Description |
|---|---|
| Ashmem | Ashmem object created. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The passed parameter is not an Ashmem object; 3.The ashmem instance for obtaining packaging is empty. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let ashmem2 = rpc.Ashmem.create(ashmem);
let size = ashmem2.getAshmemSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
createAshmemFromExisting(deprecated)
static createAshmemFromExisting(ashmem: Ashmem): Ashmem
Creates an Ashmem object by copying the file descriptor of an existing Ashmem object. The two Ashmem objects point to the same shared memory region.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use create instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| ashmem | Ashmem | Yes | Existing Ashmem object. |
Return value
| Type | Description |
|---|---|
| Ashmem | Ashmem object created. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem);
let size = ashmem2.getAshmemSize();
hilog.info(0x0000, 'testTag', 'size is ' + size);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
closeAshmem8+
closeAshmem(): void
Closes this Ashmem object.
NOTE
Before closing the Ashmem object, you need to remove the address mapping.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.closeAshmem();
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
unmapAshmem8+
unmapAshmem(): void
Deletes the mappings for the specified address range of this Ashmem object.
System capability: SystemCapability.Communication.IPC.Core
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.unmapAshmem();
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
getAshmemSize8+
getAshmemSize(): number
Obtains the memory size of this Ashmem object.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| number | Ashmem size obtained. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let size = ashmem.getAshmemSize();
hilog.info(0x0000, 'testTag', ' size is ' + size);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
mapTypedAshmem9+
mapTypedAshmem(mapType: number): void
Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this Ashmem object.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The passed mapType exceeds the maximum protection level. |
| 1900001 | Failed to call mmap. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapTypedAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
mapAshmem(deprecated)
mapAshmem(mapType: number): boolean
Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this Ashmem object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use mapTypedAshmem instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the mapping is created; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let mapReadAndWrite = ashmem.mapAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE);
hilog.info(0x0000, 'testTag', 'map ashmem result is ' + mapReadAndWrite);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
mapReadWriteAshmem9+
mapReadWriteAshmem(): void
Maps the shared file to the readable and writable virtual address space of the process.
System capability: SystemCapability.Communication.IPC.Core
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900001 | Failed to call mmap. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadWriteAshmem();
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
mapReadAndWriteAshmem(deprecated)
mapReadAndWriteAshmem(): boolean
Maps the shared file to the readable and writable virtual address space of the process.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use mapReadWriteAshmem instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the mapping is created; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let mapResult = ashmem.mapReadAndWriteAshmem();
hilog.info(0x0000, 'testTag', 'map ashmem result is ' + mapResult);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
mapReadonlyAshmem9+
mapReadonlyAshmem(): void
Maps the shared file to the read-only virtual address space of the process.
System capability: SystemCapability.Communication.IPC.Core
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 1900001 | Failed to call mmap. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadonlyAshmem();
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
mapReadOnlyAshmem(deprecated)
mapReadOnlyAshmem(): boolean
Maps the shared file to the read-only virtual address space of the process.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use mapReadonlyAshmem instead.
System capability: SystemCapability.Communication.IPC.Core
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the mapping is created; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let mapResult = ashmem.mapReadOnlyAshmem();
hilog.info(0x0000, 'testTag', 'Ashmem mapReadOnlyAshmem result is ' + mapResult);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
setProtectionType9+
setProtectionType(protectionType: number): void
Sets the protection level of the memory region to which the shared file is mapped.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| protectionType | number | Yes | Protection type to set. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900002 | Failed to call ioctl. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.setProtectionType(rpc.Ashmem.PROT_READ);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message);
}
setProtection(deprecated)
setProtection(protectionType: number): boolean
Sets the protection level of the memory region to which the shared file is mapped.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use setProtectionType instead.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| protectionType | number | Yes | Protection type to set. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the operation is successful; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let result = ashmem.setProtection(rpc.Ashmem.PROT_READ);
hilog.info(0x0000, 'testTag', 'Ashmem setProtection result is ' + result);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'error ' + error);
}
writeDataToAshmem11+
writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void
Writes data to the shared file associated with this Ashmem object.
NOTE
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| buf | ArrayBuffer | Yes | Data to write. |
| size | number | Yes | Size of the data to write. |
| offset | number | Yes | Start position of the data to write in the memory region associated with this Ashmem object. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.Failed to obtain arrayBuffer information. |
| 1900003 | Failed to write data to the shared memory. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let buffer = new ArrayBuffer(1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadWriteAshmem();
ashmem.writeDataToAshmem(buffer, size, 0);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
writeAshmem(deprecated)
writeAshmem(buf: number[], size: number, offset: number): void
Writes data to the shared file associated with this Ashmem object.
NOTE
This API is supported since API version 9 and deprecated since API version 11. Use writeDataToAshmem instead.
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| buf | number[] | Yes | Data to write. |
| size | number | Yes | Size of the data to write. |
| offset | number | Yes | Start position of the data to write in the memory region associated with this Ashmem object. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match; 3.The element does not exist in the array. |
| 1900003 | Failed to write data to the shared memory. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadWriteAshmem();
let ByteArrayVar = [1, 2, 3, 4, 5];
ashmem.writeAshmem(ByteArrayVar, 5, 0);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message);
}
writeToAshmem(deprecated)
writeToAshmem(buf: number[], size: number, offset: number): boolean
Writes data to the shared file associated with this Ashmem object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use writeDataToAshmem instead.
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| buf | number[] | Yes | Data to write. |
| size | number | Yes | Size of the data to write. |
| offset | number | Yes | Start position of the data to write in the memory region associated with this Ashmem object. |
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the data is written successfully; returns false otherwise. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let mapResult = ashmem.mapReadAndWriteAshmem();
hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult);
let ByteArrayVar = [1, 2, 3, 4, 5];
let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
hilog.info(0x0000, 'testTag', 'write to Ashmem result is ' + writeResult);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}
readDataFromAshmem11+
readDataFromAshmem(size: number, offset: number): ArrayBuffer
Reads data from the shared file associated with this Ashmem object.
NOTE
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the data to read. |
| offset | number | Yes | Start position of the data to read in the memory region associated with this Ashmem object. |
Return value
| Type | Description |
|---|---|
| ArrayBuffer | Data read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900004 | Failed to read data from the shared memory. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let buffer = new ArrayBuffer(1024);
let int32View = new Int32Array(buffer);
for (let i = 0; i < int32View.length; i++) {
int32View[i] = i * 2 + 1;
}
let size = buffer.byteLength;
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadWriteAshmem();
ashmem.writeDataToAshmem(buffer, size, 0);
let readResult = ashmem.readDataFromAshmem(size, 0);
let readInt32View = new Int32Array(readResult);
hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readInt32View);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readAshmem(deprecated)
readAshmem(size: number, offset: number): number[]
Reads data from the shared file associated with this Ashmem object.
NOTE
This API is supported since API version 9 and deprecated since API version 11. Use readDataFromAshmem instead.
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the data to read. |
| offset | number | Yes | Start position of the data to read in the memory region associated with this Ashmem object. |
Return value
| Type | Description |
|---|---|
| number[] | Data read. |
Error codes
For details about the error codes, see RPC Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.The number of parameters is incorrect; 2.The parameter type does not match. |
| 1900004 | Failed to read data from the shared memory. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
ashmem.mapReadWriteAshmem();
let ByteArrayVar = [1, 2, 3, 4, 5];
ashmem.writeAshmem(ByteArrayVar, 5, 0);
let readResult = ashmem.readAshmem(5, 0);
hilog.info(0x0000, 'testTag', 'read from Ashmem result is ' + readResult);
} catch (error) {
let e: BusinessError = error as BusinessError;
hilog.error(0x0000, 'testTag', 'errorCode ' + e.code);
hilog.error(0x0000, 'testTag', 'errorMessage ' + e.message);
}
readFromAshmem(deprecated)
readFromAshmem(size: number, offset: number): number[]
Reads data from the shared file associated with this Ashmem object.
NOTE
This API is supported since API version 8 and deprecated since API version 9. Use readDataFromAshmem instead.
Before writing an Ashmem object, you need to call mapReadWriteAshmem for mapping.
System capability: SystemCapability.Communication.IPC.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| size | number | Yes | Size of the data to read. |
| offset | number | Yes | Start position of the data to read in the memory region associated with this Ashmem object. |
Return value
| Type | Description |
|---|---|
| number[] | Data read. |
Example
import { rpc } from '@kit.IPCKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
try {
let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
let mapResult = ashmem.mapReadAndWriteAshmem();
hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult);
let ByteArrayVar = [1, 2, 3, 4, 5];
let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
hilog.info(0x0000, 'testTag', 'write to Ashmem result is ' + writeResult);
let readResult = ashmem.readFromAshmem(5, 0);
hilog.info(0x0000, 'testTag', 'read to Ashmem result is ' + readResult);
} catch (error) {
hilog.error(0x0000, 'testTag', 'error is ' + error);
}