js-apis-rpc.md 287.8 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.rpc (RPC)
Z
zengyawen 已提交
2

A
Annie_wang 已提交
3
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.
Z
zengyawen 已提交
4

A
Annie_wang 已提交
5 6
> **NOTE**
>
A
Annie_wang 已提交
7
> - 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.
A
Annie_wang 已提交
8
>
A
Annie_wang 已提交
9
> - This module supports return of error codes since API version 9.
Z
zengyawen 已提交
10 11 12 13 14 15 16

## Modules to Import

```
import rpc from '@ohos.rpc';
```

A
Annie_wang 已提交
17 18 19 20 21 22
## ErrorCode<sup>9+</sup>

The APIs of this module return exceptions since API version 9. The following table lists the error codes.

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  | 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.                        |
A
Annie_wang 已提交
39

Z
zengyawen 已提交
40

A
Annie_wang 已提交
41
## MessageSequence<sup>9+</sup>
Z
zengyawen 已提交
42

A
Annie_wang 已提交
43 44
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.
Z
zengyawen 已提交
45 46 47

### create

A
Annie_wang 已提交
48
  static create(): MessageSequence
Z
zengyawen 已提交
49

A
Annie_wang 已提交
50
  Creates a **MessageSequence** object. This API is a static method.
Z
zengyawen 已提交
51

A
annie_wangli 已提交
52
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
53

A
Annie_wang 已提交
54
**Return value**
A
Annie_wang 已提交
55

A
Annie_wang 已提交
56 57 58
  | Type           | Description                           |
  | --------------- | ------------------------------- |
  | MessageSequence | **MessageSequence** object created.|
Z
zengyawen 已提交
59

A
Annie_wang 已提交
60
**Example**
A
annie_wangli 已提交
61

A
Annie_wang 已提交
62
  ```ts
A
Annie_wang 已提交
63
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
64 65 66 67 68 69 70
  console.log("RpcClient: data is " + data);
  ```

### reclaim

reclaim(): void

A
Annie_wang 已提交
71
Reclaims the **MessageSequence** object that is no longer used.
Z
zengyawen 已提交
72

A
annie_wangli 已提交
73
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
74

A
Annie_wang 已提交
75
**Example**
A
annie_wangli 已提交
76

A
Annie_wang 已提交
77
  ```ts
A
Annie_wang 已提交
78
  let reply = rpc.MessageSequence.create();
Z
zengyawen 已提交
79 80 81 82 83
  reply.reclaim();
  ```

### writeRemoteObject

A
Annie_wang 已提交
84
writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void
Z
zengyawen 已提交
85

A
Annie_wang 已提交
86
Serializes a remote object and writes it to this **MessageSequence** object.
A
annie_wangli 已提交
87 88 89

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
90
**Parameters**
A
Annie_wang 已提交
91

A
Annie_wang 已提交
92 93 94
  | Name| Type                           | Mandatory| Description                                     |
  | ------ | ------------------------------- | ---- | ----------------------------------------- |
  | object | [IRemoteObject](#iremoteobject) | Yes  | Remote object to serialize and write to the **MessageSequence** object.|
Z
zengyawen 已提交
95

A
Annie_wang 已提交
96
**Error codes**
A
Annie_wang 已提交
97

A
Annie_wang 已提交
98 99
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
100
  | ID| Error Message|
A
Annie_wang 已提交
101 102 103
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
104

A
Annie_wang 已提交
105
**Example**
A
annie_wangli 已提交
106

A
Annie_wang 已提交
107
  ```ts
Z
zengyawen 已提交
108 109 110 111 112
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
A
Annie_wang 已提交
113
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
114
  let testRemoteObject = new TestRemoteObject("testObject");
A
Annie_wang 已提交
115 116 117 118 119 120
  try {
      data.writeRemoteObject(testRemoteObject);
  } catch(error) {
      console.info("Rpc write remote object fail, errorCode " + error.code);
      console.info("Rpc write remote object fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
121 122 123 124 125 126
  ```

### readRemoteObject

readRemoteObject(): IRemoteObject

A
Annie_wang 已提交
127
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.
Z
zengyawen 已提交
128

A
annie_wangli 已提交
129
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
130

A
Annie_wang 已提交
131
**Return value**
A
Annie_wang 已提交
132

A
Annie_wang 已提交
133 134 135
  | Type                           | Description              |
  | ------------------------------- | ------------------ |
  | [IRemoteObject](#iremoteobject) | Remote object obtained.|
Z
zengyawen 已提交
136

A
Annie_wang 已提交
137 138 139 140
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
141
  | ID| Error Message|
A
Annie_wang 已提交
142 143 144
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
145

A
Annie_wang 已提交
146
**Example**
A
annie_wangli 已提交
147

A
Annie_wang 已提交
148
  ```ts
Z
zengyawen 已提交
149 150 151 152 153
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
A
Annie_wang 已提交
154
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
155
  let testRemoteObject = new TestRemoteObject("testObject");
A
Annie_wang 已提交
156 157 158 159 160 161 162
  try {
      data.writeRemoteObject(testRemoteObject);
      let proxy = data.readRemoteObject();
  } catch(error) {
      console.info("Rpc write remote object fail, errorCode " + error.code);
      console.info("Rpc write remote object fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
163 164 165 166
  ```

### writeInterfaceToken

A
Annie_wang 已提交
167
writeInterfaceToken(token: string): void
Z
zengyawen 已提交
168

A
Annie_wang 已提交
169
Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication.
Z
zengyawen 已提交
170

A
annie_wangli 已提交
171
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
172

A
Annie_wang 已提交
173
**Parameters**
A
Annie_wang 已提交
174

A
Annie_wang 已提交
175 176 177
  | Name| Type  | Mandatory| Description              |
  | ------ | ------ | ---- | ------------------ |
  | token  | string | Yes  | Interface token to write.|
Z
zengyawen 已提交
178

A
Annie_wang 已提交
179
**Error codes**
A
Annie_wang 已提交
180

A
Annie_wang 已提交
181 182
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
183
  | ID| Error Message|
A
Annie_wang 已提交
184 185
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
186

A
Annie_wang 已提交
187
**Example**
A
annie_wangli 已提交
188

A
Annie_wang 已提交
189
  ```ts
A
Annie_wang 已提交
190 191 192 193 194 195 196
  let data = rpc.MessageSequence.create();
  try {
      data.writeInterfaceToken("aaa");
  } catch(error) {
      console.info("rpc write interface fail, errorCode " + error.code);
      console.info("rpc write interface fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
197 198 199 200 201 202
  ```

### readInterfaceToken

readInterfaceToken(): string

A
Annie_wang 已提交
203
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.
Z
zengyawen 已提交
204

A
annie_wangli 已提交
205
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
206

A
Annie_wang 已提交
207
**Return value**
A
Annie_wang 已提交
208

A
Annie_wang 已提交
209 210 211
  | Type  | Description                    |
  | ------ | ------------------------ |
  | string | Interface token obtained.|
Z
zengyawen 已提交
212

A
Annie_wang 已提交
213 214 215 216
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
217
  | ID| Error Message|
A
Annie_wang 已提交
218 219
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
220

A
Annie_wang 已提交
221
**Example**
A
annie_wangli 已提交
222

A
Annie_wang 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236
```ts
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        try {
            let interfaceToken = data.readInterfaceToken();
            console.log("RpcServer: interfaceToken is " + interfaceToken);
        } catch(error) {
            console.info("RpcServer: read interfaceToken failed, errorCode " + error.code);
            console.info("RpcServer: read interfaceToken failed, errorMessage " + error.message);
        }
        return true;
    }
}
```
Z
zengyawen 已提交
237 238 239 240 241

### getSize

getSize(): number

A
Annie_wang 已提交
242
Obtains the data size of this **MessageSequence** object.
Z
zengyawen 已提交
243

A
annie_wangli 已提交
244
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
245

A
Annie_wang 已提交
246
**Return value**
A
Annie_wang 已提交
247

A
Annie_wang 已提交
248 249 250
  | Type  | Description                                           |
  | ------ | ----------------------------------------------- |
  | number | Size of the **MessageSequence** object obtained, in bytes.|
Z
zengyawen 已提交
251

A
Annie_wang 已提交
252
**Example**
A
annie_wangli 已提交
253

A
Annie_wang 已提交
254
  ```ts
A
Annie_wang 已提交
255
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
256 257 258 259 260 261 262 263
  let size = data.getSize();
  console.log("RpcClient: size is " + size);
  ```

### getCapacity

getCapacity(): number

A
Annie_wang 已提交
264
Obtains the capacity of this **MessageSequence** object.
Z
zengyawen 已提交
265

A
annie_wangli 已提交
266
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
267

A
Annie_wang 已提交
268
**Return value**
A
Annie_wang 已提交
269

A
Annie_wang 已提交
270
  | Type  | Description |
A
Annie_wang 已提交
271 272
  | ------ | ----- |
  | number | **MessageSequence** capacity obtained, in bytes.|
Z
zengyawen 已提交
273

A
Annie_wang 已提交
274
**Example**
A
annie_wangli 已提交
275

A
Annie_wang 已提交
276
  ```ts
A
Annie_wang 已提交
277
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
278 279 280 281 282 283
  let result = data.getCapacity();
  console.log("RpcClient: capacity is " + result);
  ```

### setSize

A
Annie_wang 已提交
284
setSize(size: number): void
Z
zengyawen 已提交
285

A
Annie_wang 已提交
286
Sets the size of the data contained in this **MessageSequence** object.
Z
zengyawen 已提交
287

A
annie_wangli 已提交
288
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
289

A
Annie_wang 已提交
290
**Parameters**
A
Annie_wang 已提交
291

A
Annie_wang 已提交
292
  | Name| Type  | Mandatory| Description  |
A
Annie_wang 已提交
293 294
  | ------ | ------ | ---- | ------ |
  | size   | number | Yes  | Data size to set, in bytes.|
Z
zengyawen 已提交
295

A
Annie_wang 已提交
296
**Example**
A
annie_wangli 已提交
297

A
Annie_wang 已提交
298
  ```ts
A
Annie_wang 已提交
299 300 301 302 303 304 305 306
  let data = rpc.MessageSequence.create();
  try {
      data.setSize(16);
      console.log("RpcClient: setSize is " + data.getSize());
  } catch(error) {
      console.info("rpc set size of MessageSequence fail, errorCode " + error.code);
      console.info("rpc set size of MessageSequence fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
307 308 309 310
  ```

### setCapacity

A
Annie_wang 已提交
311
setCapacity(size: number): void
Z
zengyawen 已提交
312

A
Annie_wang 已提交
313
Sets the storage capacity of this **MessageSequence** object.
Z
zengyawen 已提交
314

A
annie_wangli 已提交
315
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
316

A
Annie_wang 已提交
317
**Parameters**
A
Annie_wang 已提交
318

A
Annie_wang 已提交
319 320 321
  | Name| Type  | Mandatory| Description                                         |
  | ------ | ------ | ---- | --------------------------------------------- |
  | size   | number | Yes  | Storage capacity of the **MessageSequence** object to set, in bytes.|
Z
zengyawen 已提交
322

A
Annie_wang 已提交
323
**Error codes**
A
Annie_wang 已提交
324

A
Annie_wang 已提交
325 326
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
327
  | ID| Error Message|
A
Annie_wang 已提交
328 329
  | -------- | -------- |
  | 1900011  | parcel memory alloc failed |
Z
zengyawen 已提交
330

A
Annie_wang 已提交
331
**Example**
A
annie_wangli 已提交
332

A
Annie_wang 已提交
333
  ```ts
A
Annie_wang 已提交
334 335 336 337 338 339 340 341
  let data = rpc.MessageSequence.create();
  try {
      data.setCapacity(100);
      console.log("RpcClient: setCapacity is " + data.getCapacity());
  } catch(error) {
      console.info("rpc memory alloc fail, errorCode " + error.code);
      console.info("rpc memory alloc fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
342 343 344 345 346 347
  ```

### getWritableBytes

getWritableBytes(): number

A
Annie_wang 已提交
348
Obtains the writable capacity (in bytes) of this **MessageSequence** object.
Z
zengyawen 已提交
349

A
annie_wangli 已提交
350
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
351

A
Annie_wang 已提交
352
**Return value**
A
Annie_wang 已提交
353

A
Annie_wang 已提交
354
  | Type  | Description  |
A
Annie_wang 已提交
355 356
  | ------ | ------ |
  | number | Writable capacity of the **MessageSequence** instance, in bytes.|
Z
zengyawen 已提交
357

A
Annie_wang 已提交
358
**Example**
A
annie_wangli 已提交
359

A
Annie_wang 已提交
360 361 362 363 364 365 366 367 368
```ts
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let getWritableBytes = data.getWritableBytes();
        console.log("RpcServer: getWritableBytes is " + getWritableBytes);
        return true;
    }
}
```
Z
zengyawen 已提交
369 370 371 372 373

### getReadableBytes

getReadableBytes(): number

A
Annie_wang 已提交
374
Obtains the readable capacity of this **MessageSequence** object.
Z
zengyawen 已提交
375

A
annie_wangli 已提交
376
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
377

A
Annie_wang 已提交
378
**Return value**
A
Annie_wang 已提交
379

A
Annie_wang 已提交
380
  | Type  | Description   |
A
Annie_wang 已提交
381 382
  | ------ | ------- |
  | number | Readable capacity of the **MessageSequence** instance, in bytes.|
Z
zengyawen 已提交
383

A
Annie_wang 已提交
384
**Example**
A
annie_wangli 已提交
385

A
Annie_wang 已提交
386 387 388 389 390 391 392 393
  ```ts
class Stub extends rpc.RemoteObject {
    onRemoteRequest(code, data, reply, option) {
        let result = data.getReadableBytes();
        console.log("RpcServer: getReadableBytes is " + result);
        return true;
    }
}
Z
zengyawen 已提交
394 395 396 397 398 399
  ```

### getReadPosition

getReadPosition(): number

A
Annie_wang 已提交
400
Obtains the read position of this **MessageSequence** object.
Z
zengyawen 已提交
401

A
annie_wangli 已提交
402
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
403

A
Annie_wang 已提交
404
**Return value**
A
Annie_wang 已提交
405

A
Annie_wang 已提交
406
  | Type  | Description  |
A
Annie_wang 已提交
407 408
  | ------ | ------ |
  | number | Read position obtained.|
Z
zengyawen 已提交
409

A
Annie_wang 已提交
410
**Example**
A
annie_wangli 已提交
411

A
Annie_wang 已提交
412
  ```ts
A
Annie_wang 已提交
413
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
414 415 416 417 418 419 420 421
  let readPos = data.getReadPosition();
  console.log("RpcClient: readPos is " + readPos);
  ```

### getWritePosition

getWritePosition(): number

A
Annie_wang 已提交
422
Obtains the write position of this **MessageSequence** object.
Z
zengyawen 已提交
423

A
annie_wangli 已提交
424
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
425

A
Annie_wang 已提交
426
**Return value**
A
Annie_wang 已提交
427

A
Annie_wang 已提交
428
  | Type  | Description |
A
Annie_wang 已提交
429 430
  | ------ | ----- |
  | number | Write position obtained.|
Z
zengyawen 已提交
431

A
Annie_wang 已提交
432
**Example**
A
annie_wangli 已提交
433

A
Annie_wang 已提交
434
  ```ts
A
Annie_wang 已提交
435
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
436 437 438 439 440 441 442
  data.writeInt(10);
  let bwPos = data.getWritePosition();
  console.log("RpcClient: bwPos is " + bwPos);
  ```

### rewindRead

A
Annie_wang 已提交
443
rewindRead(pos: number): void
Z
zengyawen 已提交
444 445 446

Moves the read pointer to the specified position.

A
annie_wangli 已提交
447
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
448

A
Annie_wang 已提交
449
**Parameters**
A
Annie_wang 已提交
450

A
Annie_wang 已提交
451
  | Name| Type  | Mandatory| Description   |
A
Annie_wang 已提交
452 453
  | ------ | ------ | ---- | ------- |
  | pos    | number | Yes  | Position from which data is to read.|
Z
zengyawen 已提交
454

A
Annie_wang 已提交
455
**Example**
A
annie_wangli 已提交
456

A
Annie_wang 已提交
457
  ```ts
A
Annie_wang 已提交
458
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
459
  data.writeInt(12);
A
Annie_wang 已提交
460
  data.writeString("sequence");
Z
zengyawen 已提交
461 462
  let number = data.readInt();
  console.log("RpcClient: number is " + number);
A
Annie_wang 已提交
463 464 465 466 467 468
  try {
      data.rewindRead(0);
  } catch(error) {
      console.info("rpc rewind read data fail, errorCode " + error.code);
      console.info("rpc rewind read data fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
469 470 471 472 473 474
  let number2 = data.readInt();
  console.log("RpcClient: rewindRead is " + number2);
  ```

### rewindWrite

A
Annie_wang 已提交
475
rewindWrite(pos: number): void
Z
zengyawen 已提交
476 477 478

Moves the write pointer to the specified position.

A
annie_wangli 已提交
479
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
480

A
Annie_wang 已提交
481
**Parameters**
A
Annie_wang 已提交
482

A
Annie_wang 已提交
483
  | Name| Type  | Mandatory| Description |
A
Annie_wang 已提交
484 485
  | ------ | ------ | ---- | ----- |
  | pos    | number | Yes  | Position from which data is to write.|
Z
zengyawen 已提交
486

A
Annie_wang 已提交
487
**Example**
A
annie_wangli 已提交
488

A
Annie_wang 已提交
489
  ```ts
A
Annie_wang 已提交
490
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
491
  data.writeInt(4);
A
Annie_wang 已提交
492 493 494 495 496 497
  try {
      data.rewindWrite(0);
  } catch(error) {
      console.info("rpc rewind read data fail, errorCode " + error.code);
      console.info("rpc rewind read data fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
498 499 500 501 502 503 504
  data.writeInt(5);
  let number = data.readInt();
  console.log("RpcClient: rewindWrite is: " + number);
  ```

### writeByte

A
Annie_wang 已提交
505
writeByte(val: number): void
Z
zengyawen 已提交
506

A
Annie_wang 已提交
507
Writes a byte value to this **MessageSequence** object.
Z
zengyawen 已提交
508

A
annie_wangli 已提交
509
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
510

A
Annie_wang 已提交
511
**Parameters**
A
Annie_wang 已提交
512

A
Annie_wang 已提交
513 514 515
  | Name| Type  | Mandatory| Description |
  | ------ | ------ | ---- | ----- |
  | val    | number | Yes  | Byte value to write.|
Z
zengyawen 已提交
516

A
Annie_wang 已提交
517
**Error codes**
A
Annie_wang 已提交
518

A
Annie_wang 已提交
519 520
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
521
  | ID| Error Message|
A
Annie_wang 已提交
522 523
  | -------- | -------  |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
524

A
Annie_wang 已提交
525
**Example**
A
annie_wangli 已提交
526

A
Annie_wang 已提交
527
  ```ts
A
Annie_wang 已提交
528 529 530 531 532 533 534
  let data = rpc.MessageSequence.create();
  try {
      data.writeByte(2);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
535 536 537 538 539 540
  ```

### readByte

readByte(): number

A
Annie_wang 已提交
541
Reads the byte value from this **MessageSequence** object.
Z
zengyawen 已提交
542

A
annie_wangli 已提交
543
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
544

A
Annie_wang 已提交
545
**Return value**
A
Annie_wang 已提交
546

A
Annie_wang 已提交
547
  | Type  | Description |
A
Annie_wang 已提交
548 549
  | ------ | ----- |
  | number | Byte value read.|
Z
zengyawen 已提交
550

A
Annie_wang 已提交
551 552 553 554
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
555 556 557
  | ID| Error Message|
  | ------- | --------  |
  | 1900010 | read data from message sequence failed |
A
Annie_wang 已提交
558

A
Annie_wang 已提交
559
**Example**
A
annie_wangli 已提交
560

A
Annie_wang 已提交
561
  ```ts
A
Annie_wang 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575
  let data = rpc.MessageSequence.create();
  try {
      data.writeByte(2);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readByte();
      console.log("RpcClient: readByte is: " + ret);
  } catch(error) {
    console.info("rpc write byte fail, errorCode " + error.code);
    console.info("rpc write byte fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
576 577 578 579
  ```

### writeShort

A
Annie_wang 已提交
580
writeShort(val: number): void
Z
zengyawen 已提交
581

A
Annie_wang 已提交
582
Writes a short integer to this **MessageSequence** object.
Z
zengyawen 已提交
583

A
annie_wangli 已提交
584
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
585

A
Annie_wang 已提交
586
**Parameters**
A
Annie_wang 已提交
587

A
Annie_wang 已提交
588
  | Name| Type  | Mandatory| Description|
A
Annie_wang 已提交
589 590
  | ------ | ------ | ---  | ---  |
  | val    | number | Yes  | Short integer to write.|
Z
zengyawen 已提交
591

A
Annie_wang 已提交
592
**Error codes**
A
Annie_wang 已提交
593

A
Annie_wang 已提交
594 595
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
596
  | ID| Error Message|
A
Annie_wang 已提交
597 598
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
599

A
Annie_wang 已提交
600
**Example**
A
annie_wangli 已提交
601

A
Annie_wang 已提交
602
  ```ts
A
Annie_wang 已提交
603 604 605 606 607 608 609
  let data = rpc.MessageSequence.create();
  try {
      data.writeShort(8);
  } catch(error) {
      console.info("rpc write short fail, errorCode " + error.code);
      console.info("rpc write short fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
610 611 612 613 614 615
  ```

### readShort

readShort(): number

A
Annie_wang 已提交
616
Reads the short integer from this **MessageSequence** object.
Z
zengyawen 已提交
617

A
annie_wangli 已提交
618
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
619

A
Annie_wang 已提交
620
**Return value**
A
Annie_wang 已提交
621

A
Annie_wang 已提交
622 623 624
  | Type  | Description          |
  | ------ | -------------- |
  | number | Short integer read.|
A
Annie_wang 已提交
625 626 627 628 629

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
630
  | ID| Error Message|
A
Annie_wang 已提交
631 632
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
633

A
Annie_wang 已提交
634
**Example**
A
annie_wangli 已提交
635

A
Annie_wang 已提交
636
  ```ts
A
Annie_wang 已提交
637 638 639 640 641 642 643 644
  let data = rpc.MessageSequence.create();
  try {
      data.writeShort(8);
  } catch(error) {
      console.info("rpc write short fail, errorCode " + error.code);
      console.info("rpc write short fail, errorMessage" + error.message);
  }
  try {
A
Annie_wang 已提交
645 646
      let ret = data.readShort();
      console.log("RpcClient: readByte is: " + ret);
A
Annie_wang 已提交
647 648 649 650
  } catch(error) {
      console.info("rpc read short fail, errorCode " + error.code);
      console.info("rpc read short fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
651 652 653 654
  ```

### writeInt

A
Annie_wang 已提交
655
writeInt(val: number): void
Z
zengyawen 已提交
656

A
Annie_wang 已提交
657
Writes an integer to this **MessageSequence** object.
Z
zengyawen 已提交
658

A
annie_wangli 已提交
659
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
660

A
Annie_wang 已提交
661
**Parameters**
A
Annie_wang 已提交
662

A
Annie_wang 已提交
663 664 665
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Integer to write.|
Z
zengyawen 已提交
666

A
Annie_wang 已提交
667
**Error codes**
A
Annie_wang 已提交
668

A
Annie_wang 已提交
669 670
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
671
  | ID| Error Message|
A
Annie_wang 已提交
672 673
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
674

A
Annie_wang 已提交
675
**Example**
A
annie_wangli 已提交
676

A
Annie_wang 已提交
677
  ```ts
A
Annie_wang 已提交
678 679 680 681 682 683 684
  let data = rpc.MessageSequence.create();
  try {
      data.writeInt(10);
  } catch(error) {
      console.info("rpc write int fail, errorCode " + error.code);
      console.info("rpc write int fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
685 686 687 688 689 690
  ```

### readInt

readInt(): number

A
Annie_wang 已提交
691
Reads the integer from this **MessageSequence** object.
Z
zengyawen 已提交
692

A
annie_wangli 已提交
693
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
694

A
Annie_wang 已提交
695
**Return value**
A
Annie_wang 已提交
696

A
Annie_wang 已提交
697 698 699
  | Type  | Description        |
  | ------ | ------------ |
  | number | Integer read.|
A
Annie_wang 已提交
700 701 702 703 704

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
705
  | ID| Error Message|
A
Annie_wang 已提交
706 707
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
708

A
Annie_wang 已提交
709
**Example**
A
annie_wangli 已提交
710

A
Annie_wang 已提交
711
  ```ts
A
Annie_wang 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725
  let data = rpc.MessageSequence.create();
  try {
      data.writeInt(10);
  } catch(error) {
      console.info("rpc write int fail, errorCode " + error.code);
      console.info("rpc write int fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readInt();
      console.log("RpcClient: readInt is " + ret);
  } catch(error) {
      console.info("rpc read int fail, errorCode " + error.code);
      console.info("rpc read int fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
726 727 728 729
  ```

### writeLong

A
Annie_wang 已提交
730
writeLong(val: number): void
Z
zengyawen 已提交
731

A
Annie_wang 已提交
732
Writes a long integer to this **MessageSequence** object.
Z
zengyawen 已提交
733

A
annie_wangli 已提交
734
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
735

A
Annie_wang 已提交
736
**Parameters**
A
Annie_wang 已提交
737

A
Annie_wang 已提交
738 739 740
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Long integer to write.|
Z
zengyawen 已提交
741

A
Annie_wang 已提交
742
**Error codes**
A
Annie_wang 已提交
743

A
Annie_wang 已提交
744 745
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
746
  | ID| Error Message|
A
Annie_wang 已提交
747 748
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
749

A
Annie_wang 已提交
750
**Example**
A
annie_wangli 已提交
751

A
Annie_wang 已提交
752
  ```ts
A
Annie_wang 已提交
753 754 755 756 757 758 759
  let data = rpc.MessageSequence.create();
  try {
      data.writeLong(10000);
  } catch(error) {
      console.info("rpc write long fail, errorCode " + error.code);
      console.info("rpc write long fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
760 761 762 763 764 765
  ```

### readLong

readLong(): number

A
Annie_wang 已提交
766
Reads the long integer from this **MessageSequence** object.
Z
zengyawen 已提交
767

A
annie_wangli 已提交
768
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
769

A
Annie_wang 已提交
770
**Return value**
A
Annie_wang 已提交
771

A
Annie_wang 已提交
772 773 774
  | Type  | Description          |
  | ------ | -------------- |
  | number | Long integer read.|
A
Annie_wang 已提交
775 776 777 778 779

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
780
  | ID| Error Message|
A
Annie_wang 已提交
781 782
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
783

A
Annie_wang 已提交
784
**Example**
A
annie_wangli 已提交
785

A
Annie_wang 已提交
786
  ```ts
A
Annie_wang 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800
  let data = rpc.MessageSequence.create();
  try {
      data.writeLong(10000);
  } catch(error) {
      console.info("rpc write long fail, errorCode " + error.code);
      console.info("rpc write long fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readLong();
      console.log("RpcClient: readLong is " + ret);
  } catch(error) {
      console.info("rpc read long fail, errorCode " + error.code);
      console.info("rpc read long fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
801 802 803 804
  ```

### writeFloat

A
Annie_wang 已提交
805
writeFloat(val: number): void
Z
zengyawen 已提交
806

A
Annie_wang 已提交
807
Writes a floating-point number to this **MessageSequence** object.
Z
zengyawen 已提交
808

A
annie_wangli 已提交
809
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
810

A
Annie_wang 已提交
811
**Parameters**
A
Annie_wang 已提交
812

A
Annie_wang 已提交
813 814 815
  | Name| Type  | Mandatory| Description |
  | ------ | ------ | ---- | ----- |
  | val    | number | Yes  | Floating-point number to write.|
Z
zengyawen 已提交
816

A
Annie_wang 已提交
817
**Error codes**
A
Annie_wang 已提交
818

A
Annie_wang 已提交
819 820
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
821
  | ID| Error Message|
A
Annie_wang 已提交
822 823
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
824

A
Annie_wang 已提交
825
**Example**
A
annie_wangli 已提交
826

A
Annie_wang 已提交
827
  ```ts
A
Annie_wang 已提交
828 829 830 831 832 833 834
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloat(1.2);
  } catch(error) {
      console.info("rpc write float fail, errorCode " + error.code);
      console.info("rpc write float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
835 836 837 838 839 840
  ```

### readFloat

readFloat(): number

A
Annie_wang 已提交
841
Reads the floating-pointer number from this **MessageSequence** object.
Z
zengyawen 已提交
842

A
annie_wangli 已提交
843
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
844

A
Annie_wang 已提交
845
**Return value**
A
Annie_wang 已提交
846

A
Annie_wang 已提交
847 848 849
  | Type  | Description        |
  | ------ | ------------ |
  | number | Floating-point number read.|
A
Annie_wang 已提交
850 851 852 853 854

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
855
  | ID| Error Message|
A
Annie_wang 已提交
856 857
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
858

A
Annie_wang 已提交
859
**Example**
A
annie_wangli 已提交
860

A
Annie_wang 已提交
861
  ```ts
A
Annie_wang 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloat(1.2);
  } catch(error) {
      console.info("rpc write float fail, errorCode " + error.code);
      console.info("rpc write float fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readFloat();
      console.log("RpcClient: readFloat is " + ret);
  } catch(error) {
      console.info("rpc read float fail, errorCode " + error.code);
      console.info("rpc read float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
876 877 878 879
  ```

### writeDouble

A
Annie_wang 已提交
880
writeDouble(val: number): void
Z
zengyawen 已提交
881

A
Annie_wang 已提交
882
Writes a double-precision floating-point number to this **MessageSequence** object.
Z
zengyawen 已提交
883

A
annie_wangli 已提交
884
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
885

A
Annie_wang 已提交
886
**Parameters**
A
Annie_wang 已提交
887

A
Annie_wang 已提交
888 889 890
  | Name| Type  | Mandatory| Description                  |
  | ------ | ------ | ---- | ---------------------- |
  | val    | number | Yes  | Double-precision floating-point number to write.|
Z
zengyawen 已提交
891

A
Annie_wang 已提交
892
**Error codes**
A
Annie_wang 已提交
893

A
Annie_wang 已提交
894 895
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
896
  | ID| Error Message|
A
Annie_wang 已提交
897 898
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
899

A
Annie_wang 已提交
900
**Example**
A
annie_wangli 已提交
901

A
Annie_wang 已提交
902
  ```ts
A
Annie_wang 已提交
903 904 905 906 907 908 909
  let data = rpc.MessageSequence.create();
  try {
      data.writeDouble(10.2);
  } catch(error) {
      console.info("rpc read float fail, errorCode " + error.code);
      console.info("rpc read float fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
910 911 912 913 914 915
  ```

### readDouble

readDouble(): number

A
Annie_wang 已提交
916
Reads the double-precision floating-point number from this **MessageSequence** object.
Z
zengyawen 已提交
917

A
annie_wangli 已提交
918
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
919

A
Annie_wang 已提交
920
**Return value**
A
Annie_wang 已提交
921

A
Annie_wang 已提交
922 923 924
  | Type  | Description              |
  | ------ | ------------------ |
  | number | Double-precision floating-point number read.|
A
Annie_wang 已提交
925 926 927 928 929

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
930
  | ID| Error Message|
A
Annie_wang 已提交
931 932
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
933

A
Annie_wang 已提交
934
**Example**
A
annie_wangli 已提交
935

A
Annie_wang 已提交
936
  ```ts
A
Annie_wang 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950
  let data = rpc.MessageSequence.create();
  try {
      data.writeDouble(10.2);
  } catch(error) {
      console.info("rpc write double fail, errorCode " + error.code);
      console.info("rpc write double fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readDouble();
      console.log("RpcClient: readDouble is " + ret);
  } catch(error) {
      console.info("rpc read double fail, errorCode " + error.code);
      console.info("rpc read double fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
951 952 953 954
  ```

### writeBoolean

A
Annie_wang 已提交
955
writeBoolean(val: boolean): void
Z
zengyawen 已提交
956

A
Annie_wang 已提交
957
Writes a Boolean value to this **MessageSequence** object.
Z
zengyawen 已提交
958

A
annie_wangli 已提交
959
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
960

A
Annie_wang 已提交
961
**Parameters**
A
Annie_wang 已提交
962

A
Annie_wang 已提交
963 964 965
  | Name| Type   | Mandatory| Description            |
  | ------ | ------- | ---- | ---------------- |
  | val    | boolean | Yes  | Boolean value to write.|
Z
zengyawen 已提交
966

A
Annie_wang 已提交
967
**Error codes**
A
Annie_wang 已提交
968

A
Annie_wang 已提交
969 970
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
971
  | ID| Error Message|
A
Annie_wang 已提交
972 973
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
974

A
Annie_wang 已提交
975
**Example**
A
annie_wangli 已提交
976

A
Annie_wang 已提交
977
  ```ts
A
Annie_wang 已提交
978 979 980 981 982 983 984
  let data = rpc.MessageSequence.create();
  try {
      data.writeBoolean(false);
  } catch(error) {
      console.info("rpc write boolean fail, errorCode " + error.code);
      console.info("rpc write boolean fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
985 986 987 988 989 990
  ```

### readBoolean

readBoolean(): boolean

A
Annie_wang 已提交
991
Reads the Boolean value from this **MessageSequence** object.
Z
zengyawen 已提交
992

A
annie_wangli 已提交
993
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
994

A
Annie_wang 已提交
995
**Return value**
A
Annie_wang 已提交
996

A
Annie_wang 已提交
997 998 999
  | Type   | Description                |
  | ------- | -------------------- |
  | boolean | Boolean value read.|
Z
zengyawen 已提交
1000

A
Annie_wang 已提交
1001 1002 1003 1004
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1005
  | ID| Error Message|
A
Annie_wang 已提交
1006 1007
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1008

A
Annie_wang 已提交
1009
**Example**
A
annie_wangli 已提交
1010

A
Annie_wang 已提交
1011
  ```ts
A
Annie_wang 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
  let data = rpc.MessageSequence.create();
  try {
      data.writeBoolean(false);
  } catch(error) {
      console.info("rpc write boolean fail, errorCode " + error.code);
      console.info("rpc write boolean fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readBoolean();
      console.log("RpcClient: readBoolean is " + ret);
  } catch(error) {
      console.info("rpc read boolean fail, errorCode " + error.code);
      console.info("rpc read boolean fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1026 1027 1028 1029
  ```

### writeChar

A
Annie_wang 已提交
1030
writeChar(val: number): void
Z
zengyawen 已提交
1031

A
Annie_wang 已提交
1032
Writes a character to this **MessageSequence** object.
Z
zengyawen 已提交
1033

A
annie_wangli 已提交
1034
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1035

A
Annie_wang 已提交
1036
**Parameters**
A
Annie_wang 已提交
1037

A
Annie_wang 已提交
1038 1039 1040
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | val    | number | Yes  | Single character to write.|
Z
zengyawen 已提交
1041

A
Annie_wang 已提交
1042
**Error codes**
A
Annie_wang 已提交
1043

A
Annie_wang 已提交
1044 1045
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1046
  | ID| Error Message|
A
Annie_wang 已提交
1047 1048
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1049

A
Annie_wang 已提交
1050
**Example**
A
annie_wangli 已提交
1051

A
Annie_wang 已提交
1052
  ```ts
A
Annie_wang 已提交
1053 1054 1055 1056 1057 1058 1059
  let data = rpc.MessageSequence.create();
  try {
      data.writeChar(97);
  } catch(error) {
      console.info("rpc write char fail, errorCode " + error.code);
      console.info("rpc write char fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1060 1061 1062 1063 1064 1065
  ```

### readChar

readChar(): number

A
Annie_wang 已提交
1066
Reads the character from this **MessageSequence** object.
Z
zengyawen 已提交
1067

A
annie_wangli 已提交
1068
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1069

A
Annie_wang 已提交
1070
**Return value**
A
Annie_wang 已提交
1071

A
Annie_wang 已提交
1072 1073 1074
  | Type  | Description|
  | ------ | ---- |
  | number | Character read.|
A
Annie_wang 已提交
1075 1076 1077 1078 1079

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1080
  | ID| Error Message|
A
Annie_wang 已提交
1081 1082
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1083

A
Annie_wang 已提交
1084
**Example**
A
annie_wangli 已提交
1085

A
Annie_wang 已提交
1086
  ```ts
A
Annie_wang 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  let data = rpc.MessageSequence.create();
  try {
      data.writeChar(97);
  } catch(error) {
      console.info("rpc write char fail, errorCode " + error.code);
      console.info("rpc write char fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readChar();
      console.log("RpcClient: readChar is " + ret);
  } catch(error) {
      console.info("rpc read char fail, errorCode " + error.code);
      console.info("rpc read char fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1101 1102 1103 1104
  ```

### writeString

A
Annie_wang 已提交
1105
writeString(val: string): void
Z
zengyawen 已提交
1106

A
Annie_wang 已提交
1107
Writes a string to this **MessageSequence** object.
Z
zengyawen 已提交
1108

A
annie_wangli 已提交
1109
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1110

A
Annie_wang 已提交
1111
**Parameters**
A
Annie_wang 已提交
1112

A
Annie_wang 已提交
1113 1114 1115
  | Name| Type  | Mandatory| Description                                     |
  | ------ | ------ | ---- | ----------------------------------------- |
  | val    | string | Yes  | String to write. The length of the string must be less than 40960 bytes.|
Z
zengyawen 已提交
1116

A
Annie_wang 已提交
1117
**Error codes**
A
Annie_wang 已提交
1118

A
Annie_wang 已提交
1119 1120
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1121
  | ID| Error Message|
A
Annie_wang 已提交
1122 1123
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1124

A
Annie_wang 已提交
1125
**Example**
A
annie_wangli 已提交
1126

A
Annie_wang 已提交
1127
  ```ts
A
Annie_wang 已提交
1128 1129 1130 1131 1132 1133 1134
  let data = rpc.MessageSequence.create();
  try {
      data.writeString('abc');
  } catch(error) {
      console.info("rpc write string fail, errorCode " + error.code);
      console.info("rpc write string fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1135 1136 1137 1138 1139 1140
  ```

### readString

readString(): string

A
Annie_wang 已提交
1141
Reads the string from this **MessageSequence** object.
Z
zengyawen 已提交
1142

A
annie_wangli 已提交
1143
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1144

A
Annie_wang 已提交
1145
**Return value**
A
Annie_wang 已提交
1146

A
Annie_wang 已提交
1147 1148 1149
  | Type  | Description          |
  | ------ | -------------- |
  | string | String read.|
Z
zengyawen 已提交
1150

A
Annie_wang 已提交
1151 1152 1153 1154
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1155
  | ID| Error Message|
A
Annie_wang 已提交
1156 1157
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1158

A
Annie_wang 已提交
1159
**Example**
A
annie_wangli 已提交
1160

A
Annie_wang 已提交
1161
  ```ts
A
Annie_wang 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  let data = rpc.MessageSequence.create();
  try {
      data.writeString('abc');
  } catch(error) {
      console.info("rpc write string fail, errorCode " + error.code);
      console.info("rpc write string fail, errorMessage" + error.message);
  }
  try {
      let ret = data.readString();
      console.log("RpcClient: readString is " + ret);
  } catch(error) {
      console.info("rpc read string fail, errorCode " + error.code);
      console.info("rpc read string fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1176 1177
  ```

A
Annie_wang 已提交
1178
### writeParcelable
Z
zengyawen 已提交
1179

A
Annie_wang 已提交
1180
writeParcelable(val: Parcelable): void
Z
zengyawen 已提交
1181

A
Annie_wang 已提交
1182
Writes a **Parcelable** object to this **MessageSequence** object.
Z
zengyawen 已提交
1183

A
annie_wangli 已提交
1184
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1185

A
Annie_wang 已提交
1186
**Parameters**
A
Annie_wang 已提交
1187

A
Annie_wang 已提交
1188 1189 1190
  | Name| Type| Mandatory| Description|
  | ------ | --------- | ---- | ------ |
  | val    | Parcelable | Yes  | **Parcelable** object to write.|
Z
zengyawen 已提交
1191

A
Annie_wang 已提交
1192
**Error codes**
A
Annie_wang 已提交
1193

A
Annie_wang 已提交
1194 1195
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1196
  | ID| Error Message|
A
Annie_wang 已提交
1197 1198
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1199

A
Annie_wang 已提交
1200
**Example**
A
annie_wangli 已提交
1201

A
Annie_wang 已提交
1202
  ```ts
Z
zengyawen 已提交
1203
  class MySequenceable {
A
Annie_wang 已提交
1204 1205 1206
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
1207
          this.num = num;
A
Annie_wang 已提交
1208
          this.str = str;
Z
zengyawen 已提交
1209
      }
A
Annie_wang 已提交
1210 1211 1212
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
1213 1214
          return true;
      }
A
Annie_wang 已提交
1215 1216 1217
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
1218 1219 1220
          return true;
      }
  }
A
Annie_wang 已提交
1221 1222 1223 1224 1225 1226 1227 1228
  let parcelable = new MySequenceable(1, "aaa");
  let data = rpc.MessageSequence.create();
  try {
      data.writeParcelable(parcelable);
  } catch(error) {
      console.info("rpc write parcelable fail, errorCode " + error.code);
      console.info("rpc write parcelable fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1229 1230
  ```

A
Annie_wang 已提交
1231
### readParcelable
Z
zengyawen 已提交
1232

A
Annie_wang 已提交
1233
readParcelable(dataIn: Parcelable): void
Z
zengyawen 已提交
1234

A
Annie_wang 已提交
1235
Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**).
Z
zengyawen 已提交
1236

A
annie_wangli 已提交
1237
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1238

A
Annie_wang 已提交
1239
**Parameters**
A
Annie_wang 已提交
1240

A
Annie_wang 已提交
1241 1242
  | Name| Type                     | Mandatory| Description                                     |
  | ------ | ------------------------- | ---- | ----------------------------------------- |
A
Annie_wang 已提交
1243
  | dataIn | Parcelable                | Yes  | **Parcelable** object to read.|
Z
zengyawen 已提交
1244

A
Annie_wang 已提交
1245
**Error codes**
A
Annie_wang 已提交
1246

A
Annie_wang 已提交
1247 1248
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1249
  | ID| Error Message|
A
Annie_wang 已提交
1250 1251 1252
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
  | 1900012  | call js callback function failed |
Z
zengyawen 已提交
1253

A
Annie_wang 已提交
1254
**Example**
A
annie_wangli 已提交
1255

A
Annie_wang 已提交
1256
  ```ts
Z
zengyawen 已提交
1257
  class MySequenceable {
A
Annie_wang 已提交
1258 1259 1260
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
1261
          this.num = num;
A
Annie_wang 已提交
1262
          this.str = str;
Z
zengyawen 已提交
1263
      }
A
Annie_wang 已提交
1264 1265 1266
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
1267 1268
          return true;
      }
A
Annie_wang 已提交
1269 1270 1271
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
1272 1273 1274
          return true;
      }
  }
A
Annie_wang 已提交
1275 1276 1277
  let parcelable = new MySequenceable(1, "aaa");
  let data = rpc.MessageSequence.create();
  data.writeParcelable(parcelable);
Z
zengyawen 已提交
1278
  let ret = new MySequenceable(0, "");
A
Annie_wang 已提交
1279 1280 1281 1282 1283 1284
  try {
      data.readParcelable(ret);
  }catch(error) {
      console.info("rpc read parcelable fail, errorCode " + error.code);
      console.info("rpc read parcelable fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1285 1286 1287 1288
  ```

### writeByteArray

A
Annie_wang 已提交
1289
writeByteArray(byteArray: number[]): void
Z
zengyawen 已提交
1290

A
Annie_wang 已提交
1291
Writes a byte array to this **MessageSequence** object.
Z
zengyawen 已提交
1292

A
annie_wangli 已提交
1293
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1294

A
Annie_wang 已提交
1295
**Parameters**
A
Annie_wang 已提交
1296

A
Annie_wang 已提交
1297 1298 1299
  | Name   | Type    | Mandatory| Description              |
  | --------- | -------- | ---- | ------------------ |
  | byteArray | number[] | Yes  | Byte array to write.|
Z
zengyawen 已提交
1300

A
Annie_wang 已提交
1301
**Error codes**
A
Annie_wang 已提交
1302

A
Annie_wang 已提交
1303 1304
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1305
  | ID| Error Message|
A
Annie_wang 已提交
1306 1307
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1308

A
Annie_wang 已提交
1309
**Example**
A
annie_wangli 已提交
1310

A
Annie_wang 已提交
1311
  ```ts
A
Annie_wang 已提交
1312
  let data = rpc.MessageSequence.create();
A
Annie_wang 已提交
1313
  let ByteArrayVar = [1, 2, 3, 4, 5];
A
Annie_wang 已提交
1314 1315 1316 1317 1318 1319
  try {
      data.writeByteArray(ByteArrayVar);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1320 1321 1322 1323
  ```

### readByteArray

A
Annie_wang 已提交
1324
readByteArray(dataIn: number[]): void
Z
zengyawen 已提交
1325

A
Annie_wang 已提交
1326
Reads a byte array from this **MessageSequence** object.
Z
zengyawen 已提交
1327

A
annie_wangli 已提交
1328
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1329

A
Annie_wang 已提交
1330
**Parameters**
A
Annie_wang 已提交
1331

A
Annie_wang 已提交
1332 1333 1334
  | Name| Type    | Mandatory| Description              |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | Yes  | Byte array to read.|
A
Annie_wang 已提交
1335 1336 1337 1338 1339

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1340
  | ID| Error Message|
A
Annie_wang 已提交
1341 1342
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1343

A
Annie_wang 已提交
1344
**Example**
A
annie_wangli 已提交
1345

A
Annie_wang 已提交
1346
  ```ts
A
Annie_wang 已提交
1347
  let data = rpc.MessageSequence.create();
A
Annie_wang 已提交
1348
  let ByteArrayVar = [1, 2, 3, 4, 5];
A
Annie_wang 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  try {
      data.writeByteArray(ByteArrayVar);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
  try {
      let array = new Array(5);
      data.readByteArray(array);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1362 1363 1364 1365 1366 1367
  ```

### readByteArray

readByteArray(): number[]

A
Annie_wang 已提交
1368
Reads the byte array from this **MessageSequence** object.
Z
zengyawen 已提交
1369

A
annie_wangli 已提交
1370
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1371

A
Annie_wang 已提交
1372
**Return value**
A
Annie_wang 已提交
1373

A
Annie_wang 已提交
1374 1375 1376
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | Byte array read.|
Z
zengyawen 已提交
1377

A
Annie_wang 已提交
1378 1379 1380 1381
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1382
  | ID| Error Message|
A
Annie_wang 已提交
1383 1384
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1385

A
Annie_wang 已提交
1386
**Example**
A
annie_wangli 已提交
1387

A
Annie_wang 已提交
1388
  ```ts
A
Annie_wang 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
  let data = rpc.MessageSequence.create();
  let byteArrayVar = [1, 2, 3, 4, 5];
  try {
      data.writeByteArray(byteArrayVar);
  } catch(error) {
      console.info("rpc write byteArray fail, errorCode " + error.code);
      console.info("rpc write byteArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readByteArray();
      console.log("RpcClient: readByteArray is " + array);
  } catch(error) {
      console.info("rpc read byteArray fail, errorCode " + error.code);
      console.info("rpc read byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1404 1405 1406 1407
  ```

### writeShortArray

A
Annie_wang 已提交
1408
writeShortArray(shortArray: number[]): void
Z
zengyawen 已提交
1409

A
Annie_wang 已提交
1410
Writes a short array to this **MessageSequence** object.
Z
zengyawen 已提交
1411

A
annie_wangli 已提交
1412
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1413

A
Annie_wang 已提交
1414
**Parameters**
A
Annie_wang 已提交
1415

A
Annie_wang 已提交
1416 1417 1418
  | Name    | Type    | Mandatory| Description                |
  | ---------- | -------- | ---- | -------------------- |
  | shortArray | number[] | Yes  | Short array to write.|
Z
zengyawen 已提交
1419

A
Annie_wang 已提交
1420
**Error codes**
A
Annie_wang 已提交
1421

A
Annie_wang 已提交
1422 1423
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1424
  | ID| Error Message|
A
Annie_wang 已提交
1425 1426
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1427

A
Annie_wang 已提交
1428
**Example**
A
annie_wangli 已提交
1429

A
Annie_wang 已提交
1430
  ```ts
A
Annie_wang 已提交
1431 1432 1433 1434 1435 1436 1437
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc read byteArray fail, errorCode " + error.code);
      console.info("rpc read byteArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1438 1439 1440 1441
  ```

### readShortArray

A
Annie_wang 已提交
1442
readShortArray(dataIn: number[]): void
Z
zengyawen 已提交
1443

A
Annie_wang 已提交
1444
Reads a short array from this **MessageSequence** object.
Z
zengyawen 已提交
1445

A
annie_wangli 已提交
1446
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1447

A
Annie_wang 已提交
1448
**Parameters**
A
Annie_wang 已提交
1449

A
Annie_wang 已提交
1450 1451 1452
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | Yes  | Short array to read.|
A
Annie_wang 已提交
1453 1454 1455 1456 1457

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1458
  | ID| Error Message|
A
Annie_wang 已提交
1459 1460
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1461

A
Annie_wang 已提交
1462
**Example**
A
annie_wangli 已提交
1463

A
Annie_wang 已提交
1464
  ```ts
A
Annie_wang 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc write shortArray fail, errorCode " + error.code);
      console.info("rpc write shortArray fail, errorMessage" + error.message);
  }
  try {
      let array = new Array(3);
      data.readShortArray(array);
  } catch(error) {
      console.info("rpc read shortArray fail, errorCode " + error.code);
      console.info("rpc read shortArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1479 1480 1481 1482 1483 1484
  ```

### readShortArray

readShortArray(): number[]

A
Annie_wang 已提交
1485
Reads the short array from this **MessageSequence** object.
Z
zengyawen 已提交
1486

A
annie_wangli 已提交
1487
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1488

A
Annie_wang 已提交
1489
**Return value**
A
Annie_wang 已提交
1490

A
Annie_wang 已提交
1491 1492 1493
  | Type    | Description            |
  | -------- | ---------------- |
  | number[] | Short array read.|
Z
zengyawen 已提交
1494

A
Annie_wang 已提交
1495 1496 1497 1498
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1499
  | ID| Error Message|
A
Annie_wang 已提交
1500 1501
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1502

A
Annie_wang 已提交
1503
**Example**
A
annie_wangli 已提交
1504

A
Annie_wang 已提交
1505
  ```ts
A
Annie_wang 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  let data = rpc.MessageSequence.create();
  try {
      data.writeShortArray([11, 12, 13]);
  } catch(error) {
      console.info("rpc write shortArray fail, errorCode " + error.code);
      console.info("rpc write shortArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readShortArray();
      console.log("RpcClient: readShortArray is " + array);
  } catch(error) {
      console.info("rpc read shortArray fail, errorCode " + error.code);
      console.info("rpc read shortArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1520 1521 1522 1523
  ```

### writeIntArray

A
Annie_wang 已提交
1524
writeIntArray(intArray: number[]): void
Z
zengyawen 已提交
1525

A
Annie_wang 已提交
1526
Writes an integer array to this **MessageSequence** object.
Z
zengyawen 已提交
1527

A
annie_wangli 已提交
1528
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1529

A
Annie_wang 已提交
1530
**Parameters**
A
Annie_wang 已提交
1531

A
Annie_wang 已提交
1532 1533 1534
  | Name  | Type    | Mandatory| Description              |
  | -------- | -------- | ---- | ------------------ |
  | intArray | number[] | Yes  | Integer array to write.|
Z
zengyawen 已提交
1535

A
Annie_wang 已提交
1536
**Error codes**
A
Annie_wang 已提交
1537

A
Annie_wang 已提交
1538 1539
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1540
  | ID| Error Message|
A
Annie_wang 已提交
1541 1542
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1543

A
Annie_wang 已提交
1544
**Example**
A
annie_wangli 已提交
1545

A
Annie_wang 已提交
1546
  ```ts
A
Annie_wang 已提交
1547 1548 1549 1550 1551 1552 1553
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1554 1555 1556 1557
  ```

### readIntArray

A
Annie_wang 已提交
1558
readIntArray(dataIn: number[]): void
Z
zengyawen 已提交
1559

A
Annie_wang 已提交
1560
Reads an integer array from this **MessageSequence** object.
Z
zengyawen 已提交
1561

A
annie_wangli 已提交
1562
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1563

A
Annie_wang 已提交
1564
**Parameters**
A
Annie_wang 已提交
1565

A
Annie_wang 已提交
1566 1567 1568
  | Name| Type    | Mandatory| Description              |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | Yes  | Integer array to read.|
A
Annie_wang 已提交
1569 1570 1571 1572 1573

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1574
  | ID| Error Message|
A
Annie_wang 已提交
1575 1576
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1577

A
Annie_wang 已提交
1578
**Example**
A
annie_wangli 已提交
1579

A
Annie_wang 已提交
1580
  ```ts
A
Annie_wang 已提交
1581 1582 1583 1584 1585 1586 1587
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1588
  let array = new Array(3);
A
Annie_wang 已提交
1589 1590 1591 1592 1593 1594
  try {
      data.readIntArray(array);
  } catch(error) {
      console.info("rpc read intArray fail, errorCode " + error.code);
      console.info("rpc read intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1595 1596 1597 1598 1599 1600
  ```

### readIntArray

readIntArray(): number[]

A
Annie_wang 已提交
1601
Reads the integer array from this **MessageSequence** object.
Z
zengyawen 已提交
1602

A
annie_wangli 已提交
1603
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1604

A
Annie_wang 已提交
1605
**Return value**
A
Annie_wang 已提交
1606

A
Annie_wang 已提交
1607 1608 1609
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | Integer array read.|
Z
zengyawen 已提交
1610

A
Annie_wang 已提交
1611 1612 1613 1614
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1615
  | ID| Error Message|
A
Annie_wang 已提交
1616 1617
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1618

A
Annie_wang 已提交
1619
**Example**
A
annie_wangli 已提交
1620

A
Annie_wang 已提交
1621
  ```ts
A
Annie_wang 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
  let data = rpc.MessageSequence.create();
  try {
      data.writeIntArray([100, 111, 112]);
  } catch(error) {
      console.info("rpc write intArray fail, errorCode " + error.code);
      console.info("rpc write intArray fail, errorMessage" + error.message);
  }
  try {
    let array = data.readIntArray();
    console.log("RpcClient: readIntArray is " + array);
  } catch(error) {
      console.info("rpc read intArray fail, errorCode " + error.code);
      console.info("rpc read intArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1636 1637 1638 1639
  ```

### writeLongArray

A
Annie_wang 已提交
1640
writeLongArray(longArray: number[]): void
Z
zengyawen 已提交
1641

A
Annie_wang 已提交
1642
Writes a long array to this **MessageSequence** object.
Z
zengyawen 已提交
1643

A
annie_wangli 已提交
1644
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1645

A
Annie_wang 已提交
1646
**Parameters**
A
Annie_wang 已提交
1647

A
Annie_wang 已提交
1648 1649 1650
  | Name   | Type    | Mandatory| Description                |
  | --------- | -------- | ---- | -------------------- |
  | longArray | number[] | Yes  | Long array to write.|
Z
zengyawen 已提交
1651

A
Annie_wang 已提交
1652
**Error codes**
A
Annie_wang 已提交
1653

A
Annie_wang 已提交
1654 1655
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1656
  | ID| Error Message|
A
Annie_wang 已提交
1657 1658
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1659

A
Annie_wang 已提交
1660
**Example**
A
annie_wangli 已提交
1661

A
Annie_wang 已提交
1662
  ```ts
A
Annie_wang 已提交
1663 1664 1665 1666 1667 1668 1669
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
  }catch(error){
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1670 1671 1672 1673
  ```

### readLongArray

A
Annie_wang 已提交
1674
readLongArray(dataIn: number[]): void
Z
zengyawen 已提交
1675

A
Annie_wang 已提交
1676
Reads a long array from this **MessageSequence** object.
Z
zengyawen 已提交
1677

A
annie_wangli 已提交
1678
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1679

A
Annie_wang 已提交
1680
**Parameters**
A
Annie_wang 已提交
1681

A
Annie_wang 已提交
1682 1683 1684
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | Yes  | Long array to read.|
A
Annie_wang 已提交
1685 1686 1687 1688 1689

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1690
  | ID| Error Message|
A
Annie_wang 已提交
1691 1692
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1693

A
Annie_wang 已提交
1694
**Example**
A
annie_wangli 已提交
1695

A
Annie_wang 已提交
1696
  ```ts
A
Annie_wang 已提交
1697 1698 1699 1700 1701 1702 1703
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
  } catch(error) {
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1704
  let array = new Array(3);
A
Annie_wang 已提交
1705 1706 1707 1708 1709 1710
  try {
      data.readLongArray(array);
  } catch(error) {
      console.info("rpc read longArray fail, errorCode " + error.code);
      console.info("rpc read longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1711 1712 1713 1714 1715 1716
  ```

### readLongArray

readLongArray(): number[]

A
Annie_wang 已提交
1717
Reads the long array from this **MessageSequence** object.
Z
zengyawen 已提交
1718

A
annie_wangli 已提交
1719
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1720

A
Annie_wang 已提交
1721
**Return value**
A
Annie_wang 已提交
1722

A
Annie_wang 已提交
1723 1724 1725
  | Type    | Description            |
  | -------- | ---------------- |
  | number[] | Long array read.|
Z
zengyawen 已提交
1726

A
Annie_wang 已提交
1727 1728 1729 1730
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1731
  | ID| Error Message|
A
Annie_wang 已提交
1732 1733
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
1734

A
Annie_wang 已提交
1735
**Example**
A
annie_wangli 已提交
1736

A
Annie_wang 已提交
1737
  ```ts
A
Annie_wang 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
  let data = rpc.MessageSequence.create();
  try {
      data.writeLongArray([1111, 1112, 1113]);
  } catch(error) {
      console.info("rpc write longArray fail, errorCode " + error.code);
      console.info("rpc write longArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readLongArray();
      console.log("RpcClient: readLongArray is " + array);
  } catch(error) {
      console.info("rpc read longArray fail, errorCode " + error.code);
      console.info("rpc read longArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1752 1753 1754 1755
  ```

### writeFloatArray

A
Annie_wang 已提交
1756
writeFloatArray(floatArray: number[]): void
Z
zengyawen 已提交
1757

A
Annie_wang 已提交
1758
Writes a floating-point array to this **MessageSequence** object.
Z
zengyawen 已提交
1759

A
annie_wangli 已提交
1760
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1761

A
Annie_wang 已提交
1762
**Parameters**
A
Annie_wang 已提交
1763

A
Annie_wang 已提交
1764 1765 1766
  | Name    | Type    | Mandatory| Description                                                                                                                   |
  | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
  | floatArray | number[] | Yes  | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.|
Z
zengyawen 已提交
1767

A
Annie_wang 已提交
1768
**Error codes**
A
Annie_wang 已提交
1769

A
Annie_wang 已提交
1770 1771
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1772
  | ID| Error Message|
A
Annie_wang 已提交
1773 1774
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1775

A
Annie_wang 已提交
1776
**Example**
A
annie_wangli 已提交
1777

A
Annie_wang 已提交
1778
  ```ts
A
Annie_wang 已提交
1779 1780 1781 1782 1783 1784 1785
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
  } catch(error) {
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1786 1787 1788 1789
  ```

### readFloatArray

A
Annie_wang 已提交
1790
readFloatArray(dataIn: number[]): void
Z
zengyawen 已提交
1791

A
Annie_wang 已提交
1792
Reads a floating-point array from this **MessageSequence** object.
A
annie_wangli 已提交
1793 1794

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1795

A
Annie_wang 已提交
1796
**Parameters**
A
Annie_wang 已提交
1797

A
Annie_wang 已提交
1798 1799 1800
  | Name| Type    | Mandatory| Description                                                                                                                   |
  | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
  | dataIn | number[] | Yes  | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.|
A
Annie_wang 已提交
1801 1802 1803 1804

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).
Z
zengyawen 已提交
1805

A
Annie_wang 已提交
1806
  | ID| Error Message|
A
Annie_wang 已提交
1807 1808
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1809

A
Annie_wang 已提交
1810
**Example**
A
annie_wangli 已提交
1811

A
Annie_wang 已提交
1812
  ```ts
A
Annie_wang 已提交
1813 1814 1815 1816 1817 1818 1819
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
  }catch(error){
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1820
  let array = new Array(3);
A
Annie_wang 已提交
1821 1822 1823 1824 1825 1826
  try {
      data.readFloatArray(array);
  } catch(error) {
      console.info("rpc read floatArray fail, errorCode " + error.code);
      console.info("rpc read floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1827 1828 1829 1830 1831 1832
  ```

### readFloatArray

readFloatArray(): number[]

A
Annie_wang 已提交
1833
Reads the floating-point array from this **MessageSequence** object.
Z
zengyawen 已提交
1834

A
annie_wangli 已提交
1835
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1836

A
Annie_wang 已提交
1837
**Return value**
A
Annie_wang 已提交
1838

A
Annie_wang 已提交
1839 1840 1841
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | Floating-point array read.|
A
Annie_wang 已提交
1842 1843 1844 1845 1846

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1847
  | ID| Error Message|
A
Annie_wang 已提交
1848 1849
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1850

A
Annie_wang 已提交
1851
**Example**
A
annie_wangli 已提交
1852

A
Annie_wang 已提交
1853
  ```ts
A
Annie_wang 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
  let data = rpc.MessageSequence.create();
  try {
      data.writeFloatArray([1.2, 1.3, 1.4]);
  } catch(error) {
      console.info("rpc write floatArray fail, errorCode " + error.code);
      console.info("rpc write floatArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readFloatArray();
      console.log("RpcClient: readFloatArray is " + array);
  } catch(error) {
      console.info("rpc read floatArray fail, errorCode " + error.code);
      console.info("rpc read floatArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1868 1869 1870 1871
  ```

### writeDoubleArray

A
Annie_wang 已提交
1872
writeDoubleArray(doubleArray: number[]): void
Z
zengyawen 已提交
1873

A
Annie_wang 已提交
1874
Writes a double-precision floating-point array to this **MessageSequence** object.
Z
zengyawen 已提交
1875

A
annie_wangli 已提交
1876
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1877

A
Annie_wang 已提交
1878
**Parameters**
A
Annie_wang 已提交
1879

A
Annie_wang 已提交
1880 1881 1882
  | Name     | Type    | Mandatory| Description                    |
  | ----------- | -------- | ---- | ------------------------ |
  | doubleArray | number[] | Yes  | Double-precision floating-point array to write.|
Z
zengyawen 已提交
1883

A
Annie_wang 已提交
1884
**Error codes**
A
Annie_wang 已提交
1885

A
Annie_wang 已提交
1886 1887
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1888
  | ID| Error Message|
A
Annie_wang 已提交
1889 1890
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
1891

A
Annie_wang 已提交
1892
**Example**
A
annie_wangli 已提交
1893

A
Annie_wang 已提交
1894
  ```ts
A
Annie_wang 已提交
1895 1896 1897 1898 1899 1900 1901
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1902 1903 1904 1905
  ```

### readDoubleArray

A
Annie_wang 已提交
1906
readDoubleArray(dataIn: number[]): void
Z
zengyawen 已提交
1907

A
Annie_wang 已提交
1908
Reads a double-precision floating-point array from this **MessageSequence** object.
Z
zengyawen 已提交
1909

A
annie_wangli 已提交
1910
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1911

A
Annie_wang 已提交
1912
**Parameters**
A
Annie_wang 已提交
1913

A
Annie_wang 已提交
1914 1915 1916
  | Name| Type    | Mandatory| Description                    |
  | ------ | -------- | ---- | ------------------------ |
  | dataIn | number[] | Yes  | Double-precision floating-point array to read.|
A
Annie_wang 已提交
1917 1918 1919 1920 1921

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1922
  | ID| Error Message|
A
Annie_wang 已提交
1923 1924
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1925

A
Annie_wang 已提交
1926
**Example**
A
annie_wangli 已提交
1927

A
Annie_wang 已提交
1928
  ```ts
A
Annie_wang 已提交
1929 1930 1931 1932 1933 1934 1935
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1936
  let array = new Array(3);
A
Annie_wang 已提交
1937 1938 1939 1940 1941 1942
  try {
      data.readDoubleArray(array);
  } catch(error) {
      console.info("rpc read doubleArray fail, errorCode " + error.code);
      console.info("rpc read doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1943 1944 1945 1946 1947 1948
  ```

### readDoubleArray

readDoubleArray(): number[]

A
Annie_wang 已提交
1949
Reads the double-precision floating-point array from this **MessageSequence** object.
Z
zengyawen 已提交
1950

A
annie_wangli 已提交
1951
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1952

A
Annie_wang 已提交
1953
**Return value**
A
Annie_wang 已提交
1954

A
Annie_wang 已提交
1955 1956 1957
  | Type    | Description                |
  | -------- | -------------------- |
  | number[] | Double-precision floating-point array read.|
A
Annie_wang 已提交
1958 1959 1960 1961 1962

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
1963
  | ID| Error Message|
A
Annie_wang 已提交
1964 1965
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
1966

A
Annie_wang 已提交
1967
**Example**
A
annie_wangli 已提交
1968

A
Annie_wang 已提交
1969
  ```ts
A
Annie_wang 已提交
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
  let data = rpc.MessageSequence.create();
  try {
      data.writeDoubleArray([11.1, 12.2, 13.3]);
  } catch(error) {
      console.info("rpc write doubleArray fail, errorCode " + error.code);
      console.info("rpc write doubleArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readDoubleArray();
      console.log("RpcClient: readDoubleArray is " + array);
  } catch(error) {
      console.info("rpc read doubleArray fail, errorCode " + error.code);
      console.info("rpc read doubleArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
1984 1985 1986 1987
  ```

### writeBooleanArray

A
Annie_wang 已提交
1988
writeBooleanArray(booleanArray: boolean[]): void
Z
zengyawen 已提交
1989

A
Annie_wang 已提交
1990
Writes a Boolean array to this **MessageSequence** object.
Z
zengyawen 已提交
1991

A
annie_wangli 已提交
1992
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
1993

A
Annie_wang 已提交
1994
**Parameters**
A
Annie_wang 已提交
1995

A
Annie_wang 已提交
1996 1997 1998
  | Name      | Type     | Mandatory| Description              |
  | ------------ | --------- | ---- | ------------------ |
  | booleanArray | boolean[] | Yes  | Boolean array to write.|
Z
zengyawen 已提交
1999

A
Annie_wang 已提交
2000
**Error codes**
A
Annie_wang 已提交
2001

A
Annie_wang 已提交
2002 2003
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2004
  | ID| Error Message|
A
Annie_wang 已提交
2005 2006
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2007

A
Annie_wang 已提交
2008
**Example**
A
annie_wangli 已提交
2009

A
Annie_wang 已提交
2010
  ```ts
A
Annie_wang 已提交
2011 2012 2013 2014 2015 2016 2017
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2018 2019 2020 2021
  ```

### readBooleanArray

A
Annie_wang 已提交
2022
readBooleanArray(dataIn: boolean[]): void
Z
zengyawen 已提交
2023

A
Annie_wang 已提交
2024
Reads a Boolean array from this **MessageSequence** object.
Z
zengyawen 已提交
2025

A
annie_wangli 已提交
2026
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2027

A
Annie_wang 已提交
2028
**Parameters**
A
Annie_wang 已提交
2029

A
Annie_wang 已提交
2030 2031 2032
  | Name| Type     | Mandatory| Description              |
  | ------ | --------- | ---- | ------------------ |
  | dataIn | boolean[] | Yes  | Boolean array to read.|
A
Annie_wang 已提交
2033 2034 2035 2036 2037

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2038
  | ID| Error Message|
A
Annie_wang 已提交
2039 2040
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
2041

A
Annie_wang 已提交
2042
**Example**
A
annie_wangli 已提交
2043

A
Annie_wang 已提交
2044
  ```ts
A
Annie_wang 已提交
2045 2046 2047 2048 2049 2050 2051
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2052
  let array = new Array(3);
A
Annie_wang 已提交
2053 2054 2055 2056 2057 2058
  try {
      data.readBooleanArray(array);
  } catch(error) {
      console.info("rpc read booleanArray fail, errorCode " + error.code);
      console.info("rpc read booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2059 2060 2061 2062 2063 2064
  ```

### readBooleanArray

readBooleanArray(): boolean[]

A
Annie_wang 已提交
2065
Reads the Boolean array from this **MessageSequence** object.
Z
zengyawen 已提交
2066

A
annie_wangli 已提交
2067
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2068

A
Annie_wang 已提交
2069
**Return value**
A
Annie_wang 已提交
2070

A
Annie_wang 已提交
2071 2072 2073
  | Type     | Description          |
  | --------- | -------------- |
  | boolean[] | Boolean array read.|
A
annie_wangli 已提交
2074

A
Annie_wang 已提交
2075 2076 2077 2078
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2079
  | ID| Error Message|
A
Annie_wang 已提交
2080 2081
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
2082 2083

**Example**
Z
zengyawen 已提交
2084

A
Annie_wang 已提交
2085
  ```ts
A
Annie_wang 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
  let data = rpc.MessageSequence.create();
  try {
      data.writeBooleanArray([false, true, false]);
  } catch(error) {
      console.info("rpc write booleanArray fail, errorCode " + error.code);
      console.info("rpc write booleanArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readBooleanArray();
      console.log("RpcClient: readBooleanArray is " + array);
  } catch(error) {
      console.info("rpc read booleanArray fail, errorCode " + error.code);
      console.info("rpc read booleanArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2100 2101 2102 2103
  ```

### writeCharArray

A
Annie_wang 已提交
2104
writeCharArray(charArray: number[]): void
Z
zengyawen 已提交
2105

A
Annie_wang 已提交
2106
Writes a character array to this **MessageSequence** object.
Z
zengyawen 已提交
2107

A
annie_wangli 已提交
2108
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2109

A
Annie_wang 已提交
2110
**Parameters**
A
Annie_wang 已提交
2111

A
Annie_wang 已提交
2112 2113 2114
  | Name   | Type    | Mandatory| Description                  |
  | --------- | -------- | ---- | ---------------------- |
  | charArray | number[] | Yes  | Character array to write.|
Z
zengyawen 已提交
2115

A
Annie_wang 已提交
2116
**Error codes**
A
Annie_wang 已提交
2117

A
Annie_wang 已提交
2118 2119
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2120
  | ID| Error Message|
A
Annie_wang 已提交
2121 2122
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2123

A
Annie_wang 已提交
2124
**Example**
A
annie_wangli 已提交
2125

A
Annie_wang 已提交
2126
  ```ts
A
Annie_wang 已提交
2127 2128 2129 2130 2131 2132 2133
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2134 2135 2136 2137
  ```

### readCharArray

A
Annie_wang 已提交
2138
readCharArray(dataIn: number[]): void
Z
zengyawen 已提交
2139

A
Annie_wang 已提交
2140
Reads a character array from this **MessageSequence** object.
Z
zengyawen 已提交
2141

A
annie_wangli 已提交
2142
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2143

A
Annie_wang 已提交
2144
**Parameters**
A
Annie_wang 已提交
2145

A
Annie_wang 已提交
2146 2147 2148
  | Name| Type    | Mandatory| Description                  |
  | ------ | -------- | ---- | ---------------------- |
  | dataIn | number[] | Yes  | Character array to read.|
A
Annie_wang 已提交
2149 2150 2151 2152 2153

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2154
  | ID| Error Message|
A
Annie_wang 已提交
2155 2156
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
2157

A
Annie_wang 已提交
2158
**Example**
A
annie_wangli 已提交
2159

A
Annie_wang 已提交
2160
  ```ts
A
Annie_wang 已提交
2161 2162 2163 2164 2165 2166 2167
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2168
  let array = new Array(3);
A
Annie_wang 已提交
2169 2170 2171 2172 2173 2174
  try {
      data.readCharArray(array);
  } catch(error) {
      console.info("rpc read charArray fail, errorCode " + error.code);
      console.info("rpc read charArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2175 2176 2177 2178
  ```

### readCharArray

A
annie_wangli 已提交
2179
readCharArray(): number[]
Z
zengyawen 已提交
2180

A
Annie_wang 已提交
2181
Reads the character array from this **MessageSequence** object.
Z
zengyawen 已提交
2182

A
annie_wangli 已提交
2183
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2184

A
Annie_wang 已提交
2185
**Return value**
A
Annie_wang 已提交
2186

A
Annie_wang 已提交
2187 2188 2189
  | Type    | Description              |
  | -------- | ------------------ |
  | number[] | Character array read.|
Z
zengyawen 已提交
2190

A
Annie_wang 已提交
2191
**Error codes**
A
annie_wangli 已提交
2192

A
Annie_wang 已提交
2193 2194
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2195
  | ID| Error Message|
A
Annie_wang 已提交
2196 2197
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
2198 2199

**Example**
Z
zengyawen 已提交
2200

A
Annie_wang 已提交
2201
  ```ts
A
Annie_wang 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
  let data = rpc.MessageSequence.create();
  try {
      data.writeCharArray([97, 98, 88]);
  } catch(error) {
      console.info("rpc write charArray fail, errorCode " + error.code);
      console.info("rpc write charArray fail, errorMessage" + error.message);
  }
  let array = new Array(3);
  try {
      let array = data.readCharArray();
      console.log("RpcClient: readCharArray is " + array);
  } catch(error) {
      console.info("rpc read charArray fail, errorCode " + error.code);
      console.info("rpc read charArray fail, errorMessage" + error.message);
  }
  ```
Z
zengyawen 已提交
2218 2219 2220

### writeStringArray

A
Annie_wang 已提交
2221
writeStringArray(stringArray: string[]): void
Z
zengyawen 已提交
2222

A
Annie_wang 已提交
2223
Writes a string array to this **MessageSequence** object.
Z
zengyawen 已提交
2224

A
annie_wangli 已提交
2225
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2226

A
Annie_wang 已提交
2227
**Parameters**
A
Annie_wang 已提交
2228

A
Annie_wang 已提交
2229 2230 2231
  | 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.|
Z
zengyawen 已提交
2232

A
Annie_wang 已提交
2233
**Error codes**
A
Annie_wang 已提交
2234

A
Annie_wang 已提交
2235 2236
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2237
  | ID| Error Message|
A
Annie_wang 已提交
2238 2239
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2240

A
Annie_wang 已提交
2241
**Example**
A
annie_wangli 已提交
2242

A
Annie_wang 已提交
2243
  ```ts
A
Annie_wang 已提交
2244 2245 2246 2247 2248 2249 2250
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2251 2252 2253 2254
  ```

### readStringArray

A
Annie_wang 已提交
2255
readStringArray(dataIn: string[]): void
Z
zengyawen 已提交
2256

A
Annie_wang 已提交
2257
Reads a string array from this **MessageSequence** object.
Z
zengyawen 已提交
2258

A
annie_wangli 已提交
2259
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2260

A
Annie_wang 已提交
2261
**Parameters**
A
Annie_wang 已提交
2262

A
Annie_wang 已提交
2263 2264 2265
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | string[] | Yes  | String array to read.|
A
Annie_wang 已提交
2266 2267 2268 2269 2270

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2271
  | ID| Error Message|
A
Annie_wang 已提交
2272 2273
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
2274

A
Annie_wang 已提交
2275
**Example**
A
annie_wangli 已提交
2276

A
Annie_wang 已提交
2277
  ```ts
A
Annie_wang 已提交
2278 2279 2280 2281 2282 2283 2284
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2285
  let array = new Array(2);
A
Annie_wang 已提交
2286 2287 2288 2289 2290 2291
  try {
      data.readStringArray(array);
  } catch(error) {
      console.info("rpc read stringArray fail, errorCode " + error.code);
      console.info("rpc read stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2292 2293 2294 2295 2296 2297
  ```

### readStringArray

readStringArray(): string[]

A
Annie_wang 已提交
2298
Reads the string array from this **MessageSequence** object.
Z
zengyawen 已提交
2299

A
annie_wangli 已提交
2300
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2301

A
Annie_wang 已提交
2302
**Return value**
A
Annie_wang 已提交
2303

A
Annie_wang 已提交
2304 2305 2306
  | Type    | Description            |
  | -------- | ---------------- |
  | string[] | String array read.|
Z
zengyawen 已提交
2307

A
Annie_wang 已提交
2308 2309 2310 2311
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2312
  | ID| Error Message|
A
Annie_wang 已提交
2313 2314
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
2315

A
Annie_wang 已提交
2316
**Example**
A
annie_wangli 已提交
2317

A
Annie_wang 已提交
2318
  ```ts
A
Annie_wang 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
  let data = rpc.MessageSequence.create();
  try {
      data.writeStringArray(["abc", "def"]);
  } catch(error) {
      console.info("rpc write stringArray fail, errorCode " + error.code);
      console.info("rpc write stringArray fail, errorMessage" + error.message);
  }
  try {
      let array = data.readStringArray();
      console.log("RpcClient: readStringArray is " + array);
  } catch(error) {
      console.info("rpc read stringArray fail, errorCode " + error.code);
      console.info("rpc read stringArray fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2333 2334
  ```

A
Annie_wang 已提交
2335
### writeNoException
Z
zengyawen 已提交
2336 2337 2338

writeNoException(): void

A
Annie_wang 已提交
2339
Writes information to this **MessageSequence** object indicating that no exception occurred.
Z
zengyawen 已提交
2340

A
annie_wangli 已提交
2341
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2342

A
Annie_wang 已提交
2343 2344 2345 2346
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2347
  | ID| Error Message|
A
Annie_wang 已提交
2348 2349
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
A
Annie_wang 已提交
2350

A
Annie_wang 已提交
2351
**Example**
A
annie_wangli 已提交
2352

A
Annie_wang 已提交
2353
  ```ts
Z
zengyawen 已提交
2354 2355 2356 2357
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
2358

Z
zengyawen 已提交
2359 2360 2361
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
              console.log("RpcServer: onRemoteRequest called");
A
Annie_wang 已提交
2362 2363 2364 2365 2366 2367
              try {
                  reply.writeNoException();
              } catch(error) {
                  console.info("rpc write no exception fail, errorCode " + error.code);
                  console.info("rpc write no exception fail, errorMessage" + error.message);
              }
Z
zengyawen 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```

A
Annie_wang 已提交
2377
### readException
Z
zengyawen 已提交
2378 2379 2380

readException(): void

A
Annie_wang 已提交
2381
Reads the exception information from this **MessageSequence** object.
Z
zengyawen 已提交
2382

A
annie_wangli 已提交
2383
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2384

A
Annie_wang 已提交
2385 2386 2387 2388
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2389
  | ID| Error Message|
A
Annie_wang 已提交
2390 2391
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
2392

A
Annie_wang 已提交
2393
**Example**
A
annie_wangli 已提交
2394

A
Annie_wang 已提交
2395
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
2396

A
Annie_wang 已提交
2397
  ```ts
A
Annie_wang 已提交
2398 2399 2400
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
2415
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
2416
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
2417
  };
A
Annie_wang 已提交
2418 2419 2420 2421 2422
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
2423 2424
  ```

A
Annie_wang 已提交
2425
  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.
A
Annie_wang 已提交
2426 2427

  ```ts
Z
zengyawen 已提交
2428
  let option = new rpc.MessageOption();
A
Annie_wang 已提交
2429 2430
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
Z
zengyawen 已提交
2431 2432
  data.writeInt(1);
  data.writeString("hello");
A
Annie_wang 已提交
2433
  proxy.sendMessageRequest(1, data, reply, option)
Z
zengyawen 已提交
2434 2435
      .then(function(errCode) {
          if (errCode === 0) {
A
Annie_wang 已提交
2436 2437 2438 2439 2440 2441 2442
              console.log("sendMessageRequest got result");
              try {
                  reply.readException();
              } catch(error) {
                  console.info("rpc read exception fail, errorCode " + error.code);
                  console.info("rpc read no exception fail, errorMessage" + error.message);
              }
Z
zengyawen 已提交
2443 2444 2445
              let msg = reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
A
Annie_wang 已提交
2446
              console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode);
Z
zengyawen 已提交
2447 2448
          }
      }).catch(function(e) {
A
Annie_wang 已提交
2449
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
Z
zengyawen 已提交
2450
      }).finally (() => {
A
Annie_wang 已提交
2451
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
Z
zengyawen 已提交
2452 2453 2454 2455 2456
          data.reclaim();
          reply.reclaim();
      });
  ```

A
Annie_wang 已提交
2457
### writeParcelableArray
Z
zengyawen 已提交
2458

A
Annie_wang 已提交
2459
writeParcelableArray(parcelableArray: Parcelable[]): void
Z
zengyawen 已提交
2460

A
Annie_wang 已提交
2461
Writes a **Parcelable** array to this **MessageSequence** object.
Z
zengyawen 已提交
2462

A
annie_wangli 已提交
2463
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2464

A
Annie_wang 已提交
2465
**Parameters**
A
Annie_wang 已提交
2466

A
Annie_wang 已提交
2467 2468 2469
  | Name         | Type        | Mandatory| Description                      |
  | --------------- | ------------ | ---- | -------------------------- |
  | parcelableArray | Parcelable[] | Yes  | **Parcelable** array to write.|
Z
zengyawen 已提交
2470

A
Annie_wang 已提交
2471
**Error codes**
A
Annie_wang 已提交
2472

A
Annie_wang 已提交
2473 2474
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2475
  | ID| Error Message|
A
Annie_wang 已提交
2476 2477
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2478

A
Annie_wang 已提交
2479
**Example**
A
annie_wangli 已提交
2480

A
Annie_wang 已提交
2481
  ```ts
A
Annie_wang 已提交
2482
  class MyParcelable {
A
Annie_wang 已提交
2483 2484 2485
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
2486
          this.num = num;
A
Annie_wang 已提交
2487
          this.str = str;
Z
zengyawen 已提交
2488
      }
A
Annie_wang 已提交
2489 2490 2491
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
2492 2493
          return true;
      }
A
Annie_wang 已提交
2494 2495 2496
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
2497 2498 2499
          return true;
      }
  }
A
Annie_wang 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
  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();
  try {
      data.writeParcelableArray(a);
  } catch(error) {
      console.info("rpc write parcelable array fail, errorCode " + error.code);
      console.info("rpc write parcelable array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2511 2512
  ```

A
Annie_wang 已提交
2513
### readParcelableArray
Z
zengyawen 已提交
2514

A
Annie_wang 已提交
2515
readParcelableArray(parcelableArray: Parcelable[]): void
Z
zengyawen 已提交
2516

A
Annie_wang 已提交
2517
Reads a **Parcelable** array from this **MessageSequence** object.
Z
zengyawen 已提交
2518

A
annie_wangli 已提交
2519
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2520

A
Annie_wang 已提交
2521
**Parameters**
A
Annie_wang 已提交
2522

A
Annie_wang 已提交
2523 2524 2525
  | Name         | Type        | Mandatory| Description                      |
  | --------------- | ------------ | ---- | -------------------------- |
  | parcelableArray | Parcelable[] | Yes  | **Parcelable** array to read.|
A
Annie_wang 已提交
2526 2527 2528 2529 2530

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2531
  | ID| Error Message|
A
Annie_wang 已提交
2532 2533 2534
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
  | 1900012  | call js callback function failed |
Z
zengyawen 已提交
2535

A
Annie_wang 已提交
2536
**Example**
A
annie_wangli 已提交
2537

A
Annie_wang 已提交
2538
  ```ts
A
Annie_wang 已提交
2539
  class MyParcelable {
A
Annie_wang 已提交
2540 2541 2542
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
2543
          this.num = num;
A
Annie_wang 已提交
2544
          this.str = str;
Z
zengyawen 已提交
2545
      }
A
Annie_wang 已提交
2546 2547 2548
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
Z
zengyawen 已提交
2549 2550
          return true;
      }
A
Annie_wang 已提交
2551 2552 2553
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
Z
zengyawen 已提交
2554 2555 2556
          return true;
      }
  }
A
Annie_wang 已提交
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
  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();
  let result = data.writeParcelableArray(a);
  console.log("RpcClient: writeParcelableArray is " + result);
  let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")];
  try {
      data.readParcelableArray(b);
  } catch(error) {
      console.info("rpc read parcelable array fail, errorCode " + error.code);
      console.info("rpc read parcelable array fail, errorMessage" + error.message);
  }
  data.readParcelableArray(b);
Z
zengyawen 已提交
2572 2573
  ```

A
Annie_wang 已提交
2574
### writeRemoteObjectArray
Z
zengyawen 已提交
2575

A
Annie_wang 已提交
2576
writeRemoteObjectArray(objectArray: IRemoteObject[]): void
Z
zengyawen 已提交
2577

A
Annie_wang 已提交
2578
Writes an array of **IRemoteObject** objects to this **MessageSequence** object.
Z
zengyawen 已提交
2579

A
annie_wangli 已提交
2580
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2581

A
Annie_wang 已提交
2582
**Parameters**
A
Annie_wang 已提交
2583

A
Annie_wang 已提交
2584 2585 2586
  | Name     | Type           | Mandatory| Description                                          |
  | ----------- | --------------- | ---- | ---------------------------------------------- |
  | objectArray | IRemoteObject[] | Yes  | Array of **IRemoteObject** objects to write.|
Z
zengyawen 已提交
2587

A
Annie_wang 已提交
2588
**Error codes**
A
Annie_wang 已提交
2589

A
Annie_wang 已提交
2590 2591
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2592
  | ID| Error Message|
A
Annie_wang 已提交
2593 2594
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2595

A
Annie_wang 已提交
2596
**Example**
A
annie_wangli 已提交
2597

A
Annie_wang 已提交
2598
  ```ts
Z
zengyawen 已提交
2599 2600 2601
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
A
Annie_wang 已提交
2602
          this.modifyLocalInterface(this, descriptor);
A
Annie_wang 已提交
2603
      }
A
Annie_wang 已提交
2604

A
Annie_wang 已提交
2605 2606 2607
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2608 2609
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
A
Annie_wang 已提交
2610
  let data = rpc.MessageSequence.create();
Z
zengyawen 已提交
2611
  let result = data.writeRemoteObjectArray(a);
A
Annie_wang 已提交
2612 2613 2614 2615 2616 2617
  try {
      data.writeRemoteObjectArray(a);
  } catch(error) {
      console.info("rpc write remote object array fail, errorCode " + error.code);
      console.info("rpc write remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2618 2619 2620
  console.log("RpcClient: writeRemoteObjectArray is " + result);
  ```

A
Annie_wang 已提交
2621
### readRemoteObjectArray
Z
zengyawen 已提交
2622 2623 2624

readRemoteObjectArray(objects: IRemoteObject[]): void

A
Annie_wang 已提交
2625
Reads an array of **IRemoteObject** objects from this **MessageSequence** object.
Z
zengyawen 已提交
2626

A
annie_wangli 已提交
2627
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2628

A
Annie_wang 已提交
2629
**Parameters**
A
Annie_wang 已提交
2630

A
Annie_wang 已提交
2631 2632 2633
  | Name | Type           | Mandatory| Description                                          |
  | ------- | --------------- | ---- | ---------------------------------------------- |
  | objects | IRemoteObject[] | Yes  | **IRemoteObject** array to read.|
A
Annie_wang 已提交
2634 2635 2636 2637 2638

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2639
  | ID| Error Message|
A
Annie_wang 已提交
2640 2641
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
2642

A
Annie_wang 已提交
2643
**Example**
A
annie_wangli 已提交
2644

A
Annie_wang 已提交
2645
  ```ts
A
Annie_wang 已提交
2646 2647 2648 2649 2650
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
2651 2652 2653
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
A
Annie_wang 已提交
2654
          this.modifyLocalInterface(this, descriptor);
A
Annie_wang 已提交
2655
      }
A
Annie_wang 已提交
2656

A
Annie_wang 已提交
2657 2658 2659
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2660 2661
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
A
Annie_wang 已提交
2662 2663
  let data = rpc.MessageSequence.create();
  data.writeRemoteObjectArray(a);
Z
zengyawen 已提交
2664
  let b = new Array(3);
A
Annie_wang 已提交
2665 2666 2667 2668 2669 2670
  try {
      data.readRemoteObjectArray(b);
  } catch(error) {
      console.info("rpc read remote object array fail, errorCode " + error.code);
      console.info("rpc read remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2671 2672 2673
  data.readRemoteObjectArray(b);
  ```

A
Annie_wang 已提交
2674
### readRemoteObjectArray
Z
zengyawen 已提交
2675 2676 2677

readRemoteObjectArray(): IRemoteObject[]

A
Annie_wang 已提交
2678
Reads the **IRemoteObject** object array from this **MessageSequence** object.
Z
zengyawen 已提交
2679

A
annie_wangli 已提交
2680
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2681

A
Annie_wang 已提交
2682
**Return value**
A
Annie_wang 已提交
2683

A
Annie_wang 已提交
2684 2685 2686
  | Type           | Description                       |
  | --------------- | --------------------------- |
  | IRemoteObject[] | **IRemoteObject** object array read.|
A
Annie_wang 已提交
2687 2688 2689 2690 2691

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2692
  | ID| Error Message|
A
Annie_wang 已提交
2693 2694
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
Z
zengyawen 已提交
2695

A
Annie_wang 已提交
2696
**Example**
A
annie_wangli 已提交
2697

A
Annie_wang 已提交
2698
  ```ts
Z
zengyawen 已提交
2699 2700 2701
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
A
Annie_wang 已提交
2702
          this.modifyLocalInterface(this, descriptor);
A
Annie_wang 已提交
2703
      }
A
Annie_wang 已提交
2704
 
A
Annie_wang 已提交
2705 2706 2707
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
2708 2709
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
A
Annie_wang 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718
  let data = rpc.MessageSequence.create();
  data.writeRemoteObjectArray(a);
  try {
      let b = data.readRemoteObjectArray();
      console.log("RpcClient: readRemoteObjectArray is " + b);
  } catch(error) {
      console.info("rpc read remote object array fail, errorCode " + error.code);
      console.info("rpc read remote object array fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2719 2720 2721
  ```


A
Annie_wang 已提交
2722
### closeFileDescriptor<sup>9+</sup>
Z
zengyawen 已提交
2723 2724 2725

static closeFileDescriptor(fd: number): void

A
Annie_wang 已提交
2726
Closes a file descriptor. This API is a static method.
Z
zengyawen 已提交
2727

A
annie_wangli 已提交
2728
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2729

A
Annie_wang 已提交
2730
**Parameters**
A
Annie_wang 已提交
2731

A
Annie_wang 已提交
2732 2733 2734
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | fd     | number | Yes  | File descriptor to close.|
Z
zengyawen 已提交
2735

A
Annie_wang 已提交
2736
**Example**
A
annie_wangli 已提交
2737

A
Annie_wang 已提交
2738
  ```ts
A
Annie_wang 已提交
2739
  import fs from '@ohos.file.fs';
Z
zengyawen 已提交
2740
  let filePath = "path/to/file";
A
Annie_wang 已提交
2741
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
A
Annie_wang 已提交
2742
  try {
A
Annie_wang 已提交
2743
      rpc.MessageSequence.closeFileDescriptor(file.fd);
A
Annie_wang 已提交
2744 2745 2746 2747
  } catch(error) {
      console.info("rpc close file descriptor fail, errorCode " + error.code);
      console.info("rpc close file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2748 2749
  ```

A
Annie_wang 已提交
2750
### dupFileDescriptor
Z
zengyawen 已提交
2751 2752 2753

static dupFileDescriptor(fd: number) :number

A
Annie_wang 已提交
2754
Duplicates a file descriptor. This API is a static method.
Z
zengyawen 已提交
2755

A
annie_wangli 已提交
2756
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2757

A
Annie_wang 已提交
2758
**Parameters**
A
Annie_wang 已提交
2759

A
Annie_wang 已提交
2760 2761 2762
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | fd     | number | Yes  | File descriptor to duplicate.|
Z
zengyawen 已提交
2763

A
Annie_wang 已提交
2764
**Return value**
A
Annie_wang 已提交
2765

A
Annie_wang 已提交
2766 2767 2768
  | Type  | Description                |
  | ------ | -------------------- |
  | number | New file descriptor.|
Z
zengyawen 已提交
2769

A
Annie_wang 已提交
2770 2771 2772 2773
**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2774
  | ID| Error Message|
A
Annie_wang 已提交
2775 2776
  | -------- | -------- |
  | 1900013  | call os dup function failed |
A
Annie_wang 已提交
2777

A
Annie_wang 已提交
2778
**Example**
A
annie_wangli 已提交
2779

A
Annie_wang 已提交
2780
  ```ts
A
Annie_wang 已提交
2781
  import fs from '@ohos.file.fs';
Z
zengyawen 已提交
2782
  let filePath = "path/to/file";
A
Annie_wang 已提交
2783
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
A
Annie_wang 已提交
2784
  try {
A
Annie_wang 已提交
2785
      let newFd = rpc.MessageSequence.dupFileDescriptor(file.fd);
A
Annie_wang 已提交
2786 2787 2788 2789
  } catch(error) {
      console.info("rpc dup file descriptor fail, errorCode " + error.code);
      console.info("rpc dup file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2790 2791
  ```

A
Annie_wang 已提交
2792
### containFileDescriptors
Z
zengyawen 已提交
2793 2794 2795

containFileDescriptors(): boolean

A
Annie_wang 已提交
2796
Checks whether this **MessageSequence** object contains file descriptors.
Z
zengyawen 已提交
2797

A
annie_wangli 已提交
2798
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2799

A
Annie_wang 已提交
2800
**Return value**
A
Annie_wang 已提交
2801

A
Annie_wang 已提交
2802 2803 2804
  | Type   | Description                                                                |
  | ------- | -------------------------------------------------------------------- |
  | boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.|
Z
zengyawen 已提交
2805

A
Annie_wang 已提交
2806
**Example**
A
annie_wangli 已提交
2807

A
Annie_wang 已提交
2808

A
Annie_wang 已提交
2809
  ```ts
A
Annie_wang 已提交
2810
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
2811
  let sequence = new rpc.MessageSequence();
Z
zengyawen 已提交
2812
  let filePath = "path/to/file";
A
Annie_wang 已提交
2813
  let r1 = sequence.containFileDescriptors();
A
Annie_wang 已提交
2814
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
A
Annie_wang 已提交
2815
  try {
A
Annie_wang 已提交
2816
      sequence.writeFileDescriptor(file.fd);
A
Annie_wang 已提交
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
  try {
      let containFD = sequence.containFileDescriptors();
      console.log("RpcTest: sequence after write fd containFd result is : " + containFD);
  } catch(error) {
      console.info("rpc contain file descriptor fail, errorCode " + error.code);
      console.info("rpc contain file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2828 2829
  ```

A
Annie_wang 已提交
2830
### writeFileDescriptor
Z
zengyawen 已提交
2831

A
Annie_wang 已提交
2832
writeFileDescriptor(fd: number): void
Z
zengyawen 已提交
2833

A
Annie_wang 已提交
2834
Writes a file descriptor to this **MessageSequence** object.
Z
zengyawen 已提交
2835

A
annie_wangli 已提交
2836
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
2837

A
Annie_wang 已提交
2838
**Parameters**
A
Annie_wang 已提交
2839

A
Annie_wang 已提交
2840 2841 2842
  | Name| Type  | Mandatory| Description        |
  | ------ | ------ | ---- | ------------ |
  | fd     | number | Yes  | File descriptor to write.|
Z
zengyawen 已提交
2843

A
Annie_wang 已提交
2844
**Error codes**
A
Annie_wang 已提交
2845

A
Annie_wang 已提交
2846 2847
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2848
  | ID| Error Message|
A
Annie_wang 已提交
2849 2850
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
Z
zengyawen 已提交
2851

A
Annie_wang 已提交
2852
**Example**
A
annie_wangli 已提交
2853

A
Annie_wang 已提交
2854
  ```ts
A
Annie_wang 已提交
2855
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
2856
  let sequence = new rpc.MessageSequence();
Z
zengyawen 已提交
2857
  let filePath = "path/to/file";
A
Annie_wang 已提交
2858
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
A
Annie_wang 已提交
2859
  try {
A
Annie_wang 已提交
2860
      sequence.writeFileDescriptor(file.fd);
A
Annie_wang 已提交
2861 2862 2863 2864
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
Z
zengyawen 已提交
2865 2866
  ```

A
Annie_wang 已提交
2867 2868 2869 2870
### readFileDescriptor

readFileDescriptor(): number

A
Annie_wang 已提交
2871
Reads the file descriptor from this **MessageSequence** object.
A
Annie_wang 已提交
2872 2873 2874 2875 2876

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
2877 2878 2879
  | Type  | Description            |
  | ------ | ---------------- |
  | number | File descriptor read.|
A
Annie_wang 已提交
2880 2881 2882 2883 2884

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2885
  | ID| Error Message|
A
Annie_wang 已提交
2886 2887
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
2888 2889 2890

**Example**

A
Annie_wang 已提交
2891
  ```ts
A
Annie_wang 已提交
2892
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
2893 2894
  let sequence = new rpc.MessageSequence();
  let filePath = "path/to/file";
A
Annie_wang 已提交
2895
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
A
Annie_wang 已提交
2896
  try {
A
Annie_wang 已提交
2897
      sequence.writeFileDescriptor(file.fd);
A
Annie_wang 已提交
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
  } catch(error) {
      console.info("rpc write file descriptor fail, errorCode " + error.code);
      console.info("rpc write file descriptor fail, errorMessage" + error.message);
  }
  try {
      let readFD = sequence.readFileDescriptor();
  } catch(error) {
      console.info("rpc read file descriptor fail, errorCode " + error.code);
      console.info("rpc read file descriptor fail, errorMessage" + error.message);
  }
  ```

### writeAshmem

writeAshmem(ashmem: Ashmem): void

Writes an anonymous shared object to this **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
2920 2921 2922
  | Name| Type  | Mandatory| Description                                 |
  | ------ | ------ | ---- | ------------------------------------- |
  | ashmem | Ashmem | Yes  | Anonymous shared object to write.|
A
Annie_wang 已提交
2923 2924 2925 2926 2927

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2928
  | ID| Error Message|
A
Annie_wang 已提交
2929 2930
  | -------- | ------- |
  | 1900003  | write to ashmem failed |
A
Annie_wang 已提交
2931 2932 2933

**Example**

A
Annie_wang 已提交
2934
  ```ts
A
Annie_wang 已提交
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
  let sequence = new rpc.MessageSequence();
  let ashmem;
  try {
      ashmem = rpc.Ashmem.create("ashmem", 1024);
  } catch(error) {
      console.info("rpc create ashmem fail, errorCode " + error.code);
      console.info("rpc creat ashmem fail, errorMessage" + error.message);
  }
  try {
      sequence.writeAshmem(ashmem);
  } catch(error) {
      console.info("rpc write ashmem fail, errorCode " + error.code);
      console.info("rpc write ashmem fail, errorMessage" + error.message);
  }
  ```


### readAshmem

readAshmem(): Ashmem

Reads the anonymous shared object from this **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
2962 2963 2964
  | Type  | Description              |
  | ------ | ------------------ |
  | Ashmem | Anonymous share object read.|
A
Annie_wang 已提交
2965 2966 2967 2968 2969

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
2970
  | ID| Error Message|
A
Annie_wang 已提交
2971 2972
  | -------- | -------- |
  | 1900004  | read from ashmem failed |
A
Annie_wang 已提交
2973 2974 2975

**Example**

A
Annie_wang 已提交
2976
  ```ts
A
Annie_wang 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
  let sequence = new rpc.MessageSequence();
  let ashmem;
  try {
      ashmem = rpc.Ashmem.create("ashmem", 1024);
  } catch(error) {
      console.info("rpc create ashmem fail, errorCode " + error.code);
      console.info("rpc creat ashmem fail, errorMessage" + error.message);
  }
  try {
      sequence.writeAshmem(ashmem);
  } catch(error) {
      console.info("rpc write ashmem fail, errorCode " + error.code);
      console.info("rpc write ashmem fail, errorMessage" + error.message);
  }
  try {
      let readAshmem = sequence.readAshmem();
      console.log("RpcTest: read ashmem to result is : " + readAshmem);
  } catch(error) {
      console.info("rpc read ashmem fail, errorCode " + error.code);
      console.info("rpc read ashmem fail, errorMessage" + error.message);
  }
  ```

### getRawDataCapacity

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**

A
Annie_wang 已提交
3010 3011 3012
  | Type  | Description                                                        |
  | ------ | ------------------------------------------------------------ |
  | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageSequence** object.|
A
Annie_wang 已提交
3013 3014 3015

**Example**

A
Annie_wang 已提交
3016
  ```ts
A
Annie_wang 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
  let sequence = new rpc.MessageSequence();
  let result = sequence.getRawDataCapacity();
  console.log("RpcTest: sequence get RawDataCapacity result is : " + result);
  ```

### writeRawData

writeRawData(rawData: number[], size: number): void

Writes raw data to this **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3032 3033 3034 3035
  | Name | Type    | Mandatory| Description                              |
  | ------- | -------- | ---- | ---------------------------------- |
  | rawData | number[] | Yes  | Raw data to write.                |
  | size    | number   | Yes  | Size of the raw data, in bytes.|
A
Annie_wang 已提交
3036 3037 3038 3039 3040

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
3041
  | ID| Error Message|
A
Annie_wang 已提交
3042 3043
  | -------- | -------- |
  | 1900009  | write data to message sequence failed |
A
Annie_wang 已提交
3044 3045 3046

**Example**

A
Annie_wang 已提交
3047
  ```ts
A
Annie_wang 已提交
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
  let sequence = new rpc.MessageSequence();
  let arr = [1, 2, 3, 4, 5];
  try {
      sequence.writeRawData(arr, arr.length);
  } catch(error) {
      console.info("rpc write rawdata fail, errorCode " + error.code);
      console.info("rpc write rawdata fail, errorMessage" + error.message);
  }
  ```

### readRawData

readRawData(size: number): number[]

Reads raw data from this **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3068 3069 3070
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | size   | number | Yes  | Size of the raw data to read.|
A
Annie_wang 已提交
3071 3072 3073

**Return value**

A
Annie_wang 已提交
3074 3075 3076
  | Type    | Description                          |
  | -------- | ------------------------------ |
  | number[] | Raw data read, in bytes.|
A
Annie_wang 已提交
3077 3078 3079 3080 3081

**Error codes**

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
3082
  | ID| Error Message|
A
Annie_wang 已提交
3083 3084
  | -------- | -------- |
  | 1900010  | read data from message sequence failed |
A
Annie_wang 已提交
3085 3086 3087

**Example**

A
Annie_wang 已提交
3088
  ```ts
A
Annie_wang 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109
  let sequence = new rpc.MessageSequence();
  let arr = [1, 2, 3, 4, 5];
  try {
      sequence.writeRawData(arr, arr.length);
  } catch(error) {
      console.info("rpc write rawdata fail, errorCode " + error.code);
      console.info("rpc write rawdata fail, errorMessage" + error.message);
  }
  try {
      let result = sequence.readRawData(5);
      console.log("RpcTest: sequence read raw data result is : " + result);
  } catch(error) {
      console.info("rpc read rawdata fail, errorCode " + error.code);
      console.info("rpc read rawdata fail, errorMessage" + error.message);
  }
  ```

## MessageParcel<sup>(deprecated)</sup>

>This class is no longer maintained since API version 9. You are advised to use [MessageSequence](#messagesequence9).

A
Annie_wang 已提交
3110 3111
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.
A
Annie_wang 已提交
3112 3113 3114

### create

A
Annie_wang 已提交
3115
static create(): MessageParcel
A
Annie_wang 已提交
3116 3117 3118 3119 3120 3121 3122

Creates a **MessageParcel** object. This method is a static method.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3123 3124 3125
  | Type         | Description                         |
  | ------------- | ----------------------------- |
  | MessageParcel | **MessageParcel** object created.|
A
Annie_wang 已提交
3126 3127 3128

**Example**

A
Annie_wang 已提交
3129
  ```ts
A
Annie_wang 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
  let data = rpc.MessageParcel.create();
  console.log("RpcClient: data is " + data);
  ```

### reclaim

reclaim(): void

Reclaims the **MessageParcel** object that is no longer used.

**System capability**: SystemCapability.Communication.IPC.Core

**Example**

A
Annie_wang 已提交
3144
  ```ts
A
Annie_wang 已提交
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
  let reply = rpc.MessageParcel.create();
  reply.reclaim();
  ```

### writeRemoteObject

writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean

Serializes a remote object and writes it to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3159 3160 3161
  | Name| Type                           | Mandatory| Description                                   |
  | ------ | ------------------------------- | ---- | --------------------------------------- |
  | object | [IRemoteObject](#iremoteobject) | Yes  | Remote object to serialize and write to the **MessageParcel** object.|
A
Annie_wang 已提交
3162 3163 3164

**Return value**

A
Annie_wang 已提交
3165 3166 3167
  | Type   | Description                                     |
  | ------- | ----------------------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3168 3169 3170

**Example**

A
Annie_wang 已提交
3171
  ```ts
A
Annie_wang 已提交
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
  }
  let data = rpc.MessageParcel.create();
  let testRemoteObject = new TestRemoteObject("testObject");
  data.writeRemoteObject(testRemoteObject);
  ```

### readRemoteObject

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.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3206 3207 3208
  | Type                           | Description              |
  | ------------------------------- | ------------------ |
  | [IRemoteObject](#iremoteobject) | Remote object obtained.|
A
Annie_wang 已提交
3209 3210 3211

**Example**

A
Annie_wang 已提交
3212
  ```ts
A
Annie_wang 已提交
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
  }
  let data = rpc.MessageParcel.create();
  let testRemoteObject = new TestRemoteObject("testObject");
  data.writeRemoteObject(testRemoteObject);
  let proxy = data.readRemoteObject();
  ```

### writeInterfaceToken

writeInterfaceToken(token: string): boolean

Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3248 3249 3250
  | Name| Type  | Mandatory| Description              |
  | ------ | ------ | ---- | ------------------ |
  | token  | string | Yes  | Interface token to write.|
A
Annie_wang 已提交
3251 3252 3253

**Return value**

A
Annie_wang 已提交
3254 3255 3256
  | Type   | Description                                     |
  | ------- | ----------------------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3257 3258 3259

**Example**

A
Annie_wang 已提交
3260
  ```ts
A
Annie_wang 已提交
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
  let data = rpc.MessageParcel.create();
  let result = data.writeInterfaceToken("aaa");
  console.log("RpcServer: writeInterfaceToken is " + result);
  ```


### readInterfaceToken

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.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3277 3278 3279
  | Type  | Description                    |
  | ------ | ------------------------ |
  | string | Interface token obtained.|
A
Annie_wang 已提交
3280 3281 3282

**Example**

A
Annie_wang 已提交
3283
  ```ts
A
Annie_wang 已提交
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
  class Stub extends rpc.RemoteObject {
      onRemoteMessageRequest(code, data, reply, option) {
          let interfaceToken = data.readInterfaceToken();
          console.log("RpcServer: interfaceToken is " + interfaceToken);
          return true;
      }
  }
  ```

### getSize

getSize(): number

Obtains the data size of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3303 3304 3305
  | Type  | Description                                         |
  | ------ | --------------------------------------------- |
  | number | Size of the **MessageParcel** object obtained, in bytes.|
A
Annie_wang 已提交
3306 3307 3308

**Example**

A
Annie_wang 已提交
3309
  ```ts
A
Annie_wang 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
  let data = rpc.MessageParcel.create();
  let size = data.getSize();
  console.log("RpcClient: size is " + size);
  ```

### getCapacity

getCapacity(): number

Obtains the capacity of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3325 3326 3327
  | Type  | Description                                         |
  | ------ | --------------------------------------------- |
  | number | **MessageParcel** capacity obtained, in bytes.|
A
Annie_wang 已提交
3328 3329 3330

**Example**

A
Annie_wang 已提交
3331
  ```ts
A
Annie_wang 已提交
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
  let data = rpc.MessageParcel.create();
  let result = data.getCapacity();
  console.log("RpcClient: capacity is " + result);
  ```

### setSize

setSize(size: number): boolean

Sets the size of data contained in this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3347 3348 3349
  | Name| Type  | Mandatory| Description                                       |
  | ------ | ------ | ---- | ------------------------------------------- |
  | size   | number | Yes  | Data size to set, in bytes.|
A
Annie_wang 已提交
3350 3351 3352

**Return value**

A
Annie_wang 已提交
3353 3354 3355
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3356 3357 3358

**Example**

A
Annie_wang 已提交
3359
  ```ts
A
Annie_wang 已提交
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
  let data = rpc.MessageParcel.create();
  let setSize = data.setSize(16);
  console.log("RpcClient: setSize is " + setSize);
  ```

### setCapacity

setCapacity(size: number): boolean

Sets the storage capacity of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3375 3376 3377
  | Name| Type  | Mandatory| Description                                       |
  | ------ | ------ | ---- | ------------------------------------------- |
  | size   | number | Yes  | Storage capacity to set, in bytes.|
A
Annie_wang 已提交
3378 3379 3380

**Return value**

A
Annie_wang 已提交
3381 3382 3383
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3384 3385 3386

**Example**

A
Annie_wang 已提交
3387
  ```ts
A
Annie_wang 已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
  let data = rpc.MessageParcel.create();
  let result = data.setCapacity(100);
  console.log("RpcClient: setCapacity is " + result);
  ```

### getWritableBytes

getWritableBytes(): number

Obtains the writable capacity of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3403 3404 3405
  | Type  | Description                                               |
  | ------ | --------------------------------------------------- |
  | number | **MessageParcel** writable capacity obtained, in bytes.|
A
Annie_wang 已提交
3406 3407 3408

**Example**

A
Annie_wang 已提交
3409
  ```ts
A
Annie_wang 已提交
3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
  class Stub extends rpc.RemoteObject {
      onRemoteMessageRequest(code, data, reply, option) {
          let getWritableBytes = data.getWritableBytes();
          console.log("RpcServer: getWritableBytes is " + getWritableBytes);
          return true;
      }
  }
  ```

### getReadableBytes

getReadableBytes(): number

Obtains the readable capacity of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3429 3430 3431
  | Type  | Description                                               |
  | ------ | --------------------------------------------------- |
  | number | **MessageParcel** object readable capacity, in bytes.|
A
Annie_wang 已提交
3432 3433 3434

**Example**

A
Annie_wang 已提交
3435
  ```ts
A
Annie_wang 已提交
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
  class Stub extends rpc.RemoteObject {
      onRemoteRequest(code, data, reply, option) {
          let result = data.getReadableBytes();
          console.log("RpcServer: getReadableBytes is " + result);
          return true;
      }
  }
  ```

### getReadPosition

getReadPosition(): number

Obtains the read position of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3455 3456 3457
  | Type  | Description                                   |
  | ------ | --------------------------------------- |
  | number | Current read position of the **MessageParcel** object.|
A
Annie_wang 已提交
3458 3459 3460

**Example**

A
Annie_wang 已提交
3461
  ```ts
A
Annie_wang 已提交
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
  let data = rpc.MessageParcel.create();
  let readPos = data.getReadPosition();
  console.log("RpcClient: readPos is " + readPos);
  ```

### getWritePosition

getWritePosition(): number

Obtains the write position of this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3477 3478 3479
  | Type  | Description                                   |
  | ------ | --------------------------------------- |
  | number | Current write position of the **MessageParcel** object.|
A
Annie_wang 已提交
3480 3481 3482

**Example**

A
Annie_wang 已提交
3483
  ```ts
A
Annie_wang 已提交
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
  let data = rpc.MessageParcel.create();
  data.writeInt(10);
  let bwPos = data.getWritePosition();
  console.log("RpcClient: bwPos is " + bwPos);
  ```

### rewindRead

rewindRead(pos: number): boolean

Moves the read pointer to the specified position.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3500 3501 3502
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | pos    | number | Yes  | Position from which data is to read.|
A
Annie_wang 已提交
3503 3504 3505

**Return value**

A
Annie_wang 已提交
3506 3507 3508
  | Type   | Description                                             |
  | ------- | ------------------------------------------------- |
  | boolean | Returns **true** if the read position changes; returns **false** otherwise.|
A
Annie_wang 已提交
3509 3510 3511

**Example**

A
Annie_wang 已提交
3512
  ```ts
A
Annie_wang 已提交
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
  let data = rpc.MessageParcel.create();
  data.writeInt(12);
  data.writeString("parcel");
  let number = data.readInt();
  console.log("RpcClient: number is " + number);
  data.rewindRead(0);
  let number2 = data.readInt();
  console.log("RpcClient: rewindRead is " + number2);
  ```

### rewindWrite

rewindWrite(pos: number): boolean

Moves the write pointer to the specified position.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3533 3534 3535
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | pos    | number | Yes  | Position from which data is to write.|
A
Annie_wang 已提交
3536 3537 3538

**Return value**

A
Annie_wang 已提交
3539 3540 3541
  | Type   | Description                                         |
  | ------- | --------------------------------------------- |
  | boolean | Returns **true** if the write position changes; returns **false** otherwise.|
A
Annie_wang 已提交
3542 3543 3544

**Example**

A
Annie_wang 已提交
3545
  ```ts
A
Annie_wang 已提交
3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563
  let data = rpc.MessageParcel.create();
  data.writeInt(4);
  data.rewindWrite(0);
  data.writeInt(5);
  let number = data.readInt();
  console.log("RpcClient: rewindWrite is: " + number);
  ```

### writeByte

writeByte(val: number): boolean

Writes a Byte value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3564 3565 3566
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Byte value to write.|
A
Annie_wang 已提交
3567 3568 3569

**Return value**

A
Annie_wang 已提交
3570 3571 3572
  | Type   | Description                         |
  | ------- | ----------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3573 3574 3575

**Example**

A
Annie_wang 已提交
3576
  ```ts
A
Annie_wang 已提交
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
  let data = rpc.MessageParcel.create();
  let result = data.writeByte(2);
  console.log("RpcClient: writeByte is: " + result);
  ```

### readByte

readByte(): number

Reads the Byte value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3592 3593 3594
  | Type  | Description        |
  | ------ | ------------ |
  | number | Byte value read.|
A
Annie_wang 已提交
3595 3596 3597

**Example**

A
Annie_wang 已提交
3598
  ```ts
A
Annie_wang 已提交
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
  let data = rpc.MessageParcel.create();
  let result = data.writeByte(2);
  console.log("RpcClient: writeByte is: " + result);
  let ret = data.readByte();
  console.log("RpcClient: readByte is: " + ret);
  ```

### writeShort

writeShort(val: number): boolean

Writes a Short int value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3616 3617 3618
  | Name| Type  | Mandatory| Description              |
  | ------ | ------ | ---- | ------------------ |
  | val    | number | Yes  | Short int value to write.|
A
Annie_wang 已提交
3619 3620 3621

**Return value**

A
Annie_wang 已提交
3622 3623 3624
  | Type   | Description                         |
  | ------- | ----------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3625 3626 3627

**Example**

A
Annie_wang 已提交
3628
  ```ts
A
Annie_wang 已提交
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
  let data = rpc.MessageParcel.create();
  let result = data.writeShort(8);
  console.log("RpcClient: writeShort is: " + result);
  ```

### readShort

readShort(): number

Reads the Short int value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3644 3645 3646
  | Type  | Description          |
  | ------ | -------------- |
  | number | Short int value read.|
A
Annie_wang 已提交
3647 3648 3649

**Example**

A
Annie_wang 已提交
3650
  ```ts
A
Annie_wang 已提交
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
  let data = rpc.MessageParcel.create();
  let result = data.writeShort(8);
  console.log("RpcClient: writeShort is: " + result);
  let ret = data.readShort();
  console.log("RpcClient: readShort is: " + ret);
  ```

### writeInt

writeInt(val: number): boolean

Writes an Int value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3668 3669 3670
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Int value to write.|
A
Annie_wang 已提交
3671 3672 3673

**Return value**

A
Annie_wang 已提交
3674 3675 3676
  | Type   | Description                         |
  | ------- | ----------------------------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
3677 3678 3679

**Example**

A
Annie_wang 已提交
3680
  ```ts
A
Annie_wang 已提交
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
  let data = rpc.MessageParcel.create();
  let result = data.writeInt(10);
  console.log("RpcClient: writeInt is " + result);
  ```

### readInt

readInt(): number

Reads the Int value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3696 3697 3698
  | Type  | Description        |
  | ------ | ------------ |
  | number | Int value read.|
A
Annie_wang 已提交
3699 3700 3701

**Example**

A
Annie_wang 已提交
3702
  ```ts
A
Annie_wang 已提交
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
  let data = rpc.MessageParcel.create();
  let result = data.writeInt(10);
  console.log("RpcClient: writeInt is " + result);
  let ret = data.readInt();
  console.log("RpcClient: readInt is " + ret);
  ```

### writeLong

writeLong(val: number): boolean

Writes a Long int value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3720 3721 3722
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Long int value to write.|
A
Annie_wang 已提交
3723 3724 3725

**Return value**

A
Annie_wang 已提交
3726 3727 3728
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3729 3730 3731

**Example**

A
Annie_wang 已提交
3732
  ```ts
A
Annie_wang 已提交
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747
  let data = rpc.MessageParcel.create();
  let result = data.writeLong(10000);
  console.log("RpcClient: writeLong is " + result);
  ```

### readLong

readLong(): number

Reads the Long int value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3748 3749 3750
  | Type  | Description          |
  | ------ | -------------- |
  | number | Long int value read.|
A
Annie_wang 已提交
3751 3752 3753

**Example**

A
Annie_wang 已提交
3754
  ```ts
A
Annie_wang 已提交
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
  let data = rpc.MessageParcel.create();
  let result = data.writeLong(10000);
  console.log("RpcClient: writeLong is " + result);
  let ret = data.readLong();
  console.log("RpcClient: readLong is " + ret);
  ```

### writeFloat

writeFloat(val: number): boolean

Writes a Float value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3772 3773 3774
  | Name| Type  | Mandatory| Description            |
  | ------ | ------ | ---- | ---------------- |
  | val    | number | Yes  | Float value to write.|
A
Annie_wang 已提交
3775 3776 3777

**Return value**

A
Annie_wang 已提交
3778 3779 3780
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3781 3782 3783

**Example**

A
Annie_wang 已提交
3784
  ```ts
A
Annie_wang 已提交
3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
  let data = rpc.MessageParcel.create();
  let result = data.writeFloat(1.2);
  console.log("RpcClient: writeFloat is " + result);
  ```

### readFloat

readFloat(): number

Reads the Float value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3800 3801 3802
  | Type  | Description        |
  | ------ | ------------ |
  | number | Float value read.|
A
Annie_wang 已提交
3803 3804 3805

**Example**

A
Annie_wang 已提交
3806
  ```ts
A
Annie_wang 已提交
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
  let data = rpc.MessageParcel.create();
  let result = data.writeFloat(1.2);
  console.log("RpcClient: writeFloat is " + result);
  let ret = data.readFloat();
  console.log("RpcClient: readFloat is " + ret);
  ```

### writeDouble

writeDouble(val: number): boolean

Writes a Double value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3824 3825 3826
  | Name| Type  | Mandatory| Description                  |
  | ------ | ------ | ---- | ---------------------- |
  | val    | number | Yes  | Double value to write.|
A
Annie_wang 已提交
3827 3828 3829

**Return value**

A
Annie_wang 已提交
3830 3831 3832
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3833 3834 3835

**Example**

A
Annie_wang 已提交
3836
  ```ts
A
Annie_wang 已提交
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
  let data = rpc.MessageParcel.create();
  let result = data.writeDouble(10.2);
  console.log("RpcClient: writeDouble is " + result);
  ```

### readDouble

readDouble(): number

Reads the Double value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3852 3853 3854
  | Type  | Description              |
  | ------ | ------------------ |
  | number | Double value read.|
A
Annie_wang 已提交
3855 3856 3857

**Example**

A
Annie_wang 已提交
3858
  ```ts
A
Annie_wang 已提交
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
  let data = rpc.MessageParcel.create();
  let result = data.writeDouble(10.2);
  console.log("RpcClient: writeDouble is " + result);
  let ret = data.readDouble();
  console.log("RpcClient: readDouble is " + ret);
  ```

### writeBoolean

writeBoolean(val: boolean): boolean

Writes a Boolean value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3876 3877 3878
  | Name| Type   | Mandatory| Description            |
  | ------ | ------- | ---- | ---------------- |
  | val    | boolean | Yes  | Boolean value to write.|
A
Annie_wang 已提交
3879 3880 3881

**Return value**

A
Annie_wang 已提交
3882 3883 3884
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3885 3886 3887

**Example**

A
Annie_wang 已提交
3888
  ```ts
A
Annie_wang 已提交
3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903
  let data = rpc.MessageParcel.create();
  let result = data.writeBoolean(false);
  console.log("RpcClient: writeBoolean is " + result);
  ```

### readBoolean

readBoolean(): boolean

Reads the Boolean value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3904 3905 3906
  | Type   | Description                |
  | ------- | -------------------- |
  | boolean | Boolean value read.|
A
Annie_wang 已提交
3907 3908 3909

**Example**

A
Annie_wang 已提交
3910
  ```ts
A
Annie_wang 已提交
3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
  let data = rpc.MessageParcel.create();
  let result = data.writeBoolean(false);
  console.log("RpcClient: writeBoolean is " + result);
  let ret = data.readBoolean();
  console.log("RpcClient: readBoolean is " + ret);
  ```

### writeChar

writeChar(val: number): boolean

Writes a Char value to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3928 3929 3930
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | val    | number | Yes  | Char value to write.|
A
Annie_wang 已提交
3931 3932 3933

**Return value**

A
Annie_wang 已提交
3934 3935 3936
  | Type   | Description                         |
  | ------- | ----------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3937 3938 3939

**Example**

A
Annie_wang 已提交
3940
  ```ts
A
Annie_wang 已提交
3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
  let data = rpc.MessageParcel.create();
  let result = data.writeChar(97);
  console.log("RpcClient: writeChar is " + result);
  ```

### readChar

readChar(): number

Reads the Char value from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
3956 3957 3958
  | Type  | Description            |
  | ------ | ---------------- |
  | number | Char value read.|
A
Annie_wang 已提交
3959 3960 3961

**Example**

A
Annie_wang 已提交
3962
  ```ts
A
Annie_wang 已提交
3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979
  let data = rpc.MessageParcel.create();
  let result = data.writeChar(97);
  console.log("RpcClient: writeChar is " + result);
  let ret = data.readChar();
  console.log("RpcClient: readChar is " + ret);
  ```

### writeString

writeString(val: string): boolean

Writes a string to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
3980 3981 3982
  | Name| Type  | Mandatory| Description                                     |
  | ------ | ------ | ---- | ----------------------------------------- |
  | val    | string | Yes  | String to write. The length of the string must be less than 40960 bytes.|
A
Annie_wang 已提交
3983 3984 3985

**Return value**

A
Annie_wang 已提交
3986 3987 3988
  | Type   | Description                             |
  | ------- | --------------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
3989 3990 3991

**Example**

A
Annie_wang 已提交
3992
  ```ts
A
Annie_wang 已提交
3993 3994 3995 3996 3997 3998 3999 4000 4001
  let data = rpc.MessageParcel.create();
  let result = data.writeString('abc');
  console.log("RpcClient: writeString  is " + result);
  ```

### readString

readString(): string

A
Annie_wang 已提交
4002
Reads the string from this **MessageParcel** object.
A
Annie_wang 已提交
4003 4004 4005 4006 4007

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4008 4009 4010
  | Type  | Description          |
  | ------ | -------------- |
  | string | String read.|
A
Annie_wang 已提交
4011 4012 4013

**Example**

A
Annie_wang 已提交
4014
  ```ts
A
Annie_wang 已提交
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
  let data = rpc.MessageParcel.create();
  let result = data.writeString('abc');
  console.log("RpcClient: writeString  is " + result);
  let ret = data.readString();
  console.log("RpcClient: readString is " + ret);
  ```

### writeSequenceable

writeSequenceable(val: Sequenceable): boolean

Writes a sequenceable object to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4032 4033 4034
  | Name| Type                         | Mandatory| Description                |
  | ------ | ----------------------------- | ---- | -------------------- |
  | val    | [Sequenceable](#sequenceable) | Yes  | Sequenceable object to write.|
A
Annie_wang 已提交
4035 4036 4037

**Return value**

A
Annie_wang 已提交
4038 4039
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4040
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4041 4042 4043

**Example**

A
Annie_wang 已提交
4044
  ```ts
A
Annie_wang 已提交
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  ```

### readSequenceable

readSequenceable(dataIn: Sequenceable): boolean

Reads member variables from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4079 4080
  | Name| Type                         | Mandatory   | Description                                          |
  | ------ | ----------------------------- | ------- | ---------------------------------------------- |
A
Annie_wang 已提交
4081
  | dataIn | [Sequenceable](#sequenceabledeprecated) | Yes  | Object that reads member variables from the **MessageParcel** object.|
A
Annie_wang 已提交
4082 4083 4084

**Return value**

A
Annie_wang 已提交
4085 4086
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
4087
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
4088 4089 4090

**Example**

A
Annie_wang 已提交
4091
  ```ts
A
Annie_wang 已提交
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: writeSequenceable is " + result2);
  ```

### writeByteArray

writeByteArray(byteArray: number[]): boolean

Writes a byte array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4129 4130 4131
  | Name   | Type    | Mandatory| Description              |
  | --------- | -------- | ---- | ------------------ |
  | byteArray | number[] | Yes  | Byte array to write.|
A
Annie_wang 已提交
4132 4133 4134

**Return value**

A
Annie_wang 已提交
4135 4136
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4137
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4138 4139 4140

**Example**

A
Annie_wang 已提交
4141
  ```ts
A
Annie_wang 已提交
4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  ```

### readByteArray

readByteArray(dataIn: number[]): void

Reads a byte array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4158 4159 4160
  | Name| Type    | Mandatory| Description              |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | Yes  | Byte array to read.|
A
Annie_wang 已提交
4161 4162 4163

**Example**

A
Annie_wang 已提交
4164
  ```ts
A
Annie_wang 已提交
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  let array = new Array(5);
  data.readByteArray(array);
  ```

### readByteArray

readByteArray(): number[]

Reads the byte array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4183 4184 4185
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | Byte array read.|
A
Annie_wang 已提交
4186 4187 4188

**Example**

A
Annie_wang 已提交
4189
  ```ts
A
Annie_wang 已提交
4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
  let data = rpc.MessageParcel.create();
  let ByteArrayVar = [1, 2, 3, 4, 5];
  let result = data.writeByteArray(ByteArrayVar);
  console.log("RpcClient: writeByteArray is " + result);
  let array = data.readByteArray();
  console.log("RpcClient: readByteArray is " + array);
  ```

### writeShortArray

writeShortArray(shortArray: number[]): boolean

Writes a short array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4208 4209 4210
  | Name    | Type    | Mandatory| Description                |
  | ---------- | -------- | ---- | -------------------- |
  | shortArray | number[] | Yes  | Short array to write.|
A
Annie_wang 已提交
4211 4212 4213

**Return value**

A
Annie_wang 已提交
4214 4215
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4216
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4217 4218 4219

**Example**

A
Annie_wang 已提交
4220
  ```ts
A
Annie_wang 已提交
4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  ```

### readShortArray

readShortArray(dataIn: number[]): void

Reads a short array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4236 4237 4238
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | Yes  | Short array to read.|
A
Annie_wang 已提交
4239 4240 4241

**Example**

A
Annie_wang 已提交
4242
  ```ts
A
Annie_wang 已提交
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  let array = new Array(3);
  data.readShortArray(array);
  ```

### readShortArray

readShortArray(): number[]

Reads the short array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4260 4261 4262
  | Type    | Description            |
  | -------- | ---------------- |
  | number[] | Short array read.|
A
Annie_wang 已提交
4263 4264 4265

**Example**

A
Annie_wang 已提交
4266
  ```ts
A
Annie_wang 已提交
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
  let data = rpc.MessageParcel.create();
  let result = data.writeShortArray([11, 12, 13]);
  console.log("RpcClient: writeShortArray is " + result);
  let array = data.readShortArray();
 console.log("RpcClient: readShortArray is " + array);
  ```

### writeIntArray

writeIntArray(intArray: number[]): boolean

Writes an integer array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4284 4285 4286
  | Name  | Type    | Mandatory| Description              |
  | -------- | -------- | ---- | ------------------ |
  | intArray | number[] | Yes  | Integer array to write.|
A
Annie_wang 已提交
4287 4288 4289

**Return value**

A
Annie_wang 已提交
4290 4291
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4292
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4293 4294 4295

**Example**

A
Annie_wang 已提交
4296
  ```ts
A
Annie_wang 已提交
4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  ```

### readIntArray

readIntArray(dataIn: number[]): void

Reads an integer array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4312 4313 4314
  | Name| Type    | Mandatory| Description              |
  | ------ | -------- | ---- | ------------------ |
  | dataIn | number[] | Yes  | Integer array to read.|
A
Annie_wang 已提交
4315 4316 4317

**Example**

A
Annie_wang 已提交
4318
  ```ts
A
Annie_wang 已提交
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  let array = new Array(3);
  data.readIntArray(array);
  ```

### readIntArray

readIntArray(): number[]

Reads the integer array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4336 4337 4338
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | Integer array read.|
A
Annie_wang 已提交
4339 4340 4341

**Example**

A
Annie_wang 已提交
4342
  ```ts
A
Annie_wang 已提交
4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359
  let data = rpc.MessageParcel.create();
  let result = data.writeIntArray([100, 111, 112]);
  console.log("RpcClient: writeIntArray is " + result);
  let array = data.readIntArray();
  console.log("RpcClient: readIntArray is " + array);
  ```

### writeLongArray

writeLongArray(longArray: number[]): boolean

Writes a long array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4360 4361 4362
  | Name   | Type    | Mandatory| Description                |
  | --------- | -------- | ---- | -------------------- |
  | longArray | number[] | Yes  | Long array to write.|
A
Annie_wang 已提交
4363 4364 4365

**Return value**

A
Annie_wang 已提交
4366 4367 4368
  | Type   | Description                         |
  | ------- | ----------------------------- |
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4369 4370 4371

**Example**

A
Annie_wang 已提交
4372
  ```ts
A
Annie_wang 已提交
4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  ```

### readLongArray

readLongArray(dataIn: number[]): void

Reads a long array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4388 4389 4390
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | number[] | Yes  | Long array to read.|
A
Annie_wang 已提交
4391 4392 4393

**Example**

A
Annie_wang 已提交
4394
  ```ts
A
Annie_wang 已提交
4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  let array = new Array(3);
  data.readLongArray(array);
  ```

### readLongArray

readLongArray(): number[]

Reads the long array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4412 4413 4414
 | Type    | Description            |
 | -------- | ---------------- |
 | number[] | Long array read.|
A
Annie_wang 已提交
4415 4416 4417

**Example**

A
Annie_wang 已提交
4418
  ```ts
A
Annie_wang 已提交
4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
  let data = rpc.MessageParcel.create();
  let result = data.writeLongArray([1111, 1112, 1113]);
  console.log("RpcClient: writeLongArray is " + result);
  let array = data.readLongArray();
  console.log("RpcClient: readLongArray is " + array);
  ```

### writeFloatArray

writeFloatArray(floatArray: number[]): boolean

Writes a FloatArray to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4436
  | Name| Type| Mandatory| Description |
A
Annie_wang 已提交
4437 4438
  | ---------- | -------- | ---- | --- |
  | floatArray | number[] | Yes  | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.|
A
Annie_wang 已提交
4439 4440 4441

**Return value**

A
Annie_wang 已提交
4442 4443
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4444
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4445 4446 4447

**Example**

A
Annie_wang 已提交
4448
  ```ts
A
Annie_wang 已提交
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  ```

### readFloatArray

readFloatArray(dataIn: number[]): void

Reads a FloatArray from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4464
  | Name| Type    | Mandatory| Description  |
A
Annie_wang 已提交
4465 4466
  | ------ | -------- | ---- | ------ |
  | dataIn | number[] | Yes  | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.|
A
Annie_wang 已提交
4467 4468 4469

**Example**

A
Annie_wang 已提交
4470
  ```ts
A
Annie_wang 已提交
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  let array = new Array(3);
  data.readFloatArray(array);
  ```

### readFloatArray

readFloatArray(): number[]

Reads the FloatArray from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4488 4489 4490
  | Type    | Description          |
  | -------- | -------------- |
  | number[] | FloatArray read.|
A
Annie_wang 已提交
4491 4492 4493

**Example**

A
Annie_wang 已提交
4494
  ```ts
A
Annie_wang 已提交
4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511
  let data = rpc.MessageParcel.create();
  let result = data.writeFloatArray([1.2, 1.3, 1.4]);
  console.log("RpcClient: writeFloatArray is " + result);
  let array = data.readFloatArray();
  console.log("RpcClient: readFloatArray is " + array);
  ```

### writeDoubleArray

writeDoubleArray(doubleArray: number[]): boolean

Writes a DoubleArray to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4512 4513 4514
  | Name     | Type    | Mandatory| Description                    |
  | ----------- | -------- | ---- | ------------------------ |
  | doubleArray | number[] | Yes  | DoubleArray to write.|
A
Annie_wang 已提交
4515 4516 4517

**Return value**

A
Annie_wang 已提交
4518 4519
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4520
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4521 4522 4523

**Example**

A
Annie_wang 已提交
4524
  ```ts
A
Annie_wang 已提交
4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  ```

### readDoubleArray

readDoubleArray(dataIn: number[]): void

Reads a DoubleArray from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4540 4541 4542
  | Name| Type    | Mandatory| Description                    |
  | ------ | -------- | ---- | ------------------------ |
  | dataIn | number[] | Yes  | DoubleArray to read.|
A
Annie_wang 已提交
4543 4544 4545

**Example**

A
Annie_wang 已提交
4546
  ```ts
A
Annie_wang 已提交
4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  let array = new Array(3);
  data.readDoubleArray(array);
  ```

### readDoubleArray

readDoubleArray(): number[]

Reads the DoubleArray from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4564 4565 4566
  | Type    | Description                |
  | -------- | -------------------- |
  | number[] | DoubleArray read.|
A
Annie_wang 已提交
4567 4568 4569

**Example**

A
Annie_wang 已提交
4570
  ```ts
A
Annie_wang 已提交
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
  let data = rpc.MessageParcel.create();
  let result = data.writeDoubleArray([11.1, 12.2, 13.3]);
  console.log("RpcClient: writeDoubleArray is " + result);
  let array = data.readDoubleArray();
  console.log("RpcClient: readDoubleArray is " + array);
  ```

### writeBooleanArray

writeBooleanArray(booleanArray: boolean[]): boolean

Writes a Boolean array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4588 4589 4590
  | Name      | Type     | Mandatory| Description              |
  | ------------ | --------- | ---- | ------------------ |
  | booleanArray | boolean[] | Yes  | Boolean array to write.|
A
Annie_wang 已提交
4591 4592 4593

**Return value**

A
Annie_wang 已提交
4594 4595
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4596
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4597 4598 4599

**Example**

A
Annie_wang 已提交
4600
  ```ts
A
Annie_wang 已提交
4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  ```

### readBooleanArray

readBooleanArray(dataIn: boolean[]): void

Reads a Boolean array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4616 4617 4618
  | Name| Type     | Mandatory| Description              |
  | ------ | --------- | ---- | ------------------ |
  | dataIn | boolean[] | Yes  | Boolean array to read.|
A
Annie_wang 已提交
4619 4620 4621

**Example**

A
Annie_wang 已提交
4622
  ```ts
A
Annie_wang 已提交
4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  let array = new Array(3);
  data.readBooleanArray(array);
  ```

### readBooleanArray

readBooleanArray(): boolean[]

Reads the Boolean array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4640 4641 4642
  | Type     | Description          |
  | --------- | -------------- |
  | boolean[] | Boolean array read.|
A
Annie_wang 已提交
4643 4644 4645

**Example**

A
Annie_wang 已提交
4646
  ```ts
A
Annie_wang 已提交
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
  let data = rpc.MessageParcel.create();
  let result = data.writeBooleanArray([false, true, false]);
  console.log("RpcClient: writeBooleanArray is " + result);
  let array = data.readBooleanArray();
  console.log("RpcClient: readBooleanArray is " + array);
  ```

### writeCharArray

writeCharArray(charArray: number[]): boolean

Writes a character array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4664 4665 4666
  | Name   | Type    | Mandatory| Description                  |
  | --------- | -------- | ---- | ---------------------- |
  | charArray | number[] | Yes  | Character array to write.|
A
Annie_wang 已提交
4667 4668 4669

**Return value**

A
Annie_wang 已提交
4670 4671
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4672
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4673 4674 4675

**Example**

A
Annie_wang 已提交
4676
  ```ts
A
Annie_wang 已提交
4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 88]);
  console.log("RpcClient: writeCharArray is " + result);
  ```

### readCharArray

readCharArray(dataIn: number[]): void

Reads a character array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4692 4693 4694
  | Name| Type    | Mandatory| Description                  |
  | ------ | -------- | ---- | ---------------------- |
  | dataIn | number[] | Yes  | Character array to read.|
A
Annie_wang 已提交
4695 4696 4697

**Example**

A
Annie_wang 已提交
4698
  ```ts
A
Annie_wang 已提交
4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 99]);
  console.log("RpcClient: writeCharArray is " + result);
  let array = new Array(3);
  data.readCharArray(array);
  ```

### readCharArray

readCharArray(): number[]

Reads the character array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4716 4717 4718
  | Type    | Description              |
  | -------- | ------------------ |
  | number[] | Character array read.|
A
Annie_wang 已提交
4719 4720 4721

**Example**

A
Annie_wang 已提交
4722
  ```ts
A
Annie_wang 已提交
4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
  let data = rpc.MessageParcel.create();
  let result = data.writeCharArray([97, 98, 99]);
  console.log("RpcClient: writeCharArray is " + result);
  let array = data.readCharArray();
  console.log("RpcClient: readCharArray is " + array);
  ```

### writeStringArray

writeStringArray(stringArray: string[]): boolean

Writes a string array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4740
  | Name     | Type    | Mandatory| Description            |
A
Annie_wang 已提交
4741 4742
  | ----------- | -------- | ---- | ---------------- |
  | stringArray | string[] | Yes  | String array to write. The length of a single element in the array must be less than 40960 bytes.|
A
Annie_wang 已提交
4743 4744 4745

**Return value**

A
Annie_wang 已提交
4746
  | Type   | Description|
A
Annie_wang 已提交
4747
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4748
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4749 4750 4751

**Example**

A
Annie_wang 已提交
4752
  ```ts
A
Annie_wang 已提交
4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  ```

### readStringArray

readStringArray(dataIn: string[]): void

Reads a string array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4768 4769 4770
  | Name| Type    | Mandatory| Description                |
  | ------ | -------- | ---- | -------------------- |
  | dataIn | string[] | Yes  | String array to read.|
A
Annie_wang 已提交
4771 4772 4773

**Example**

A
Annie_wang 已提交
4774
  ```ts
A
Annie_wang 已提交
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  let array = new Array(2);
  data.readStringArray(array);
  ```

### readStringArray

readStringArray(): string[]

Reads the string array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
4792 4793 4794
  | Type    | Description            |
  | -------- | ---------------- |
  | string[] | String array read.|
A
Annie_wang 已提交
4795 4796 4797

**Example**

A
Annie_wang 已提交
4798
  ```ts
A
Annie_wang 已提交
4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815
  let data = rpc.MessageParcel.create();
  let result = data.writeStringArray(["abc", "def"]);
  console.log("RpcClient: writeStringArray is " + result);
  let array = data.readStringArray();
  console.log("RpcClient: readStringArray is " + array);
  ```

### writeNoException<sup>8+</sup>

writeNoException(): void

Writes information to this **MessageParcel** object indicating that no exception occurred.

**System capability**: SystemCapability.Communication.IPC.Core

**Example**

A
Annie_wang 已提交
4816
  ```ts
A
Annie_wang 已提交
4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
              console.log("RpcServer: onRemoteRequest called");
              reply.writeNoException();
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```

### readException<sup>8+</sup>

readException(): void

Reads the exception information from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Example**
A
Annie_wang 已提交
4857 4858

  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
4859

A
Annie_wang 已提交
4860
  ```ts
A
Annie_wang 已提交
4861 4862 4863
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
4879
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
4880
  };
A
Annie_wang 已提交
4881 4882 4883 4884 4885
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
4886 4887
  ```

A
Annie_wang 已提交
4888
  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.
A
Annie_wang 已提交
4889 4890

  ```ts
A
Annie_wang 已提交
4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendMessageRequest(1, data, reply, option)
      .then(function(errCode) {
          if (errCode === 0) {
              console.log("sendMessageRequest got result");
              reply.readException();
              let msg = reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendMessageRequest failed, errCode: " + errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

### writeSequenceableArray

writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean

Writes a sequenceable array to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4925 4926 4927
  | Name           | Type          | Mandatory| Description                      |
  | ----------------- | -------------- | ---- | -------------------------- |
  | sequenceableArray | Sequenceable[] | Yes  | Sequenceable array to write.|
A
Annie_wang 已提交
4928 4929 4930

**Return value**

A
Annie_wang 已提交
4931 4932
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
4933
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
4934 4935 4936

**Example**

A
Annie_wang 已提交
4937
  ```ts
A
Annie_wang 已提交
4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  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);
  console.log("RpcClient: writeSequenceableArray is " + result);
  ```

### readSequenceableArray<sup>8+</sup>

readSequenceableArray(sequenceableArray: Sequenceable[]): void

Reads a sequenceable array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
4975 4976 4977
  | Name           | Type          | Mandatory| Description                      |
  | ----------------- | -------------- | ---- | -------------------------- |
  | sequenceableArray | Sequenceable[] | Yes  | Sequenceable array to read.|
A
Annie_wang 已提交
4978 4979 4980

**Example**

A
Annie_wang 已提交
4981
  ```ts
A
Annie_wang 已提交
4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020
  class MySequenceable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  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);
  console.log("RpcClient: writeSequenceableArray is " + result);
  let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")];
  data.readSequenceableArray(b);
  ```

### writeRemoteObjectArray<sup>8+</sup>

writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean

Writes an array of **IRemoteObject** objects to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5021
  | Name     | Type           | Mandatory| Description |
A
Annie_wang 已提交
5022 5023
  | ----------- | --------------- | ---- | ----- |
  | objectArray | IRemoteObject[] | Yes  | Array of **IRemoteObject** objects to write.|
A
Annie_wang 已提交
5024 5025 5026

**Return value**

A
Annie_wang 已提交
5027
  | Type   | Description                                                                                                                |
A
Annie_wang 已提交
5028
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5029
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
A
Annie_wang 已提交
5030 5031 5032

**Example**

A
Annie_wang 已提交
5033
  ```ts
A
Annie_wang 已提交
5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  console.log("RpcClient: writeRemoteObjectArray is " + result);
  ```

### readRemoteObjectArray<sup>8+</sup>

readRemoteObjectArray(objects: IRemoteObject[]): void

Reads an **IRemoteObject** array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5073
  | Name | Type           | Mandatory| Description     |
A
Annie_wang 已提交
5074 5075
  | ------- | --------------- | ---- | --------- |
  | objects | IRemoteObject[] | Yes  | **IRemoteObject** array to read.|
A
Annie_wang 已提交
5076 5077 5078

**Example**

A
Annie_wang 已提交
5079
  ```ts
A
Annie_wang 已提交
5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
         return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  let b = new Array(3);
  data.readRemoteObjectArray(b);
  ```

### readRemoteObjectArray<sup>8+</sup>

readRemoteObjectArray(): IRemoteObject[]

Reads the **IRemoteObject** array from this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
5120 5121
  | Type           | Description                       |
  | --------------- | --------------------------- |
A
Annie_wang 已提交
5122
  | IRemoteObject[] | **IRemoteObject** object array obtained.|
A
Annie_wang 已提交
5123 5124 5125

**Example**

A
Annie_wang 已提交
5126
  ```ts
A
Annie_wang 已提交
5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
  }
  let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")];
  let data = rpc.MessageParcel.create();
  let result = data.writeRemoteObjectArray(a);
  console.log("RpcClient: readRemoteObjectArray is " + result);
  let b = data.readRemoteObjectArray();
  console.log("RpcClient: readRemoteObjectArray is " + b);
  ```

### closeFileDescriptor<sup>8+</sup>

static closeFileDescriptor(fd: number): void

A
Annie_wang 已提交
5162
Closes a file descriptor. This API is a static method.
A
Annie_wang 已提交
5163 5164 5165 5166 5167

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5168 5169 5170
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | fd     | number | Yes  | File descriptor to close.|
A
Annie_wang 已提交
5171 5172 5173

**Example**

A
Annie_wang 已提交
5174
  ```ts
A
Annie_wang 已提交
5175
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
5176
  let filePath = "path/to/file";
A
Annie_wang 已提交
5177 5178
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  rpc.MessageParcel.closeFileDescriptor(file.fd);
A
Annie_wang 已提交
5179 5180 5181 5182 5183 5184
  ```

### dupFileDescriptor<sup>8+</sup>

static dupFileDescriptor(fd: number) :number

A
Annie_wang 已提交
5185
Duplicates a file descriptor. This API is a static method.
A
Annie_wang 已提交
5186 5187 5188 5189 5190

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5191 5192 5193
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | fd     | number | Yes  | File descriptor to duplicate.|
A
Annie_wang 已提交
5194 5195 5196

**Return value**

A
Annie_wang 已提交
5197 5198 5199
  | Type  | Description                |
  | ------ | -------------------- |
  | number | New file descriptor.|
A
Annie_wang 已提交
5200 5201 5202

**Example**

A
Annie_wang 已提交
5203
  ```ts
A
Annie_wang 已提交
5204
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
5205
  let filePath = "path/to/file";
A
Annie_wang 已提交
5206 5207
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let newFd = rpc.MessageParcel.dupFileDescriptor(file.fd);
A
Annie_wang 已提交
5208 5209 5210 5211 5212 5213
  ```

### containFileDescriptors<sup>8+</sup>

containFileDescriptors(): boolean

A
Annie_wang 已提交
5214
Checks whether this **MessageParcel** object contains file descriptors.
A
Annie_wang 已提交
5215 5216 5217 5218 5219

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
5220 5221
  | Type   | Description                                         |
  | ------- | --------------------------------------------- |
A
Annie_wang 已提交
5222
  | boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.|
A
Annie_wang 已提交
5223 5224 5225

**Example**

A
Annie_wang 已提交
5226
  ```ts
A
Annie_wang 已提交
5227
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
5228 5229 5230
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
  let r1 = parcel.containFileDescriptors();
A
Annie_wang 已提交
5231 5232
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let writeResult = parcel.writeFileDescriptor(file.fd);
A
Annie_wang 已提交
5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247
  console.log("RpcTest: parcel writeFd result is : " + writeResult);
  let containFD = parcel.containFileDescriptors();
  console.log("RpcTest: parcel after write fd containFd result is : " + containFD);
  ```

### writeFileDescriptor<sup>8+</sup>

writeFileDescriptor(fd: number): boolean

Writes a file descriptor to this **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5248 5249 5250
  | Name| Type  | Mandatory| Description        |
  | ------ | ------ | ---- | ------------ |
  | fd     | number | Yes  | File descriptor to write.|
A
Annie_wang 已提交
5251 5252 5253

**Return value**

A
Annie_wang 已提交
5254 5255
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5256
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
5257 5258 5259

**Example**

A
Annie_wang 已提交
5260
  ```ts
A
Annie_wang 已提交
5261
  import fs from '@ohos.file.fs';
A
Annie_wang 已提交
5262 5263
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
A
Annie_wang 已提交
5264 5265
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let writeResult = parcel.writeFileDescriptor(file.fd);
A
Annie_wang 已提交
5266 5267 5268 5269
  console.log("RpcTest: parcel writeFd result is : " + writeResult);
  ```

### readFileDescriptor<sup>8+</sup>
Z
zengyawen 已提交
5270 5271 5272

readFileDescriptor(): number

A
annie_wangli 已提交
5273
Reads the file descriptor from this **MessageParcel** object.
Z
zengyawen 已提交
5274

A
annie_wangli 已提交
5275
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5276

A
Annie_wang 已提交
5277
**Return value**
A
Annie_wang 已提交
5278

A
Annie_wang 已提交
5279 5280 5281
  | Type  | Description            |
  | ------ | ---------------- |
  | number | File descriptor read.|
Z
zengyawen 已提交
5282

A
Annie_wang 已提交
5283
**Example**
A
annie_wangli 已提交
5284

A
Annie_wang 已提交
5285
  ```ts
A
Annie_wang 已提交
5286
  import fs from '@ohos.file.fs';
Z
zengyawen 已提交
5287 5288
  let parcel = new rpc.MessageParcel();
  let filePath = "path/to/file";
A
Annie_wang 已提交
5289 5290
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let writeResult = parcel.writeFileDescriptor(file.fd);
Z
zengyawen 已提交
5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
  let readFD = parcel.readFileDescriptor();
  console.log("RpcTest: parcel read fd is : " + readFD);
  ```

### writeAshmem<sup>8+</sup>

writeAshmem(ashmem: Ashmem): boolean

Writes an anonymous shared object to this **MessageParcel** object.

A
annie_wangli 已提交
5301
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5302

A
Annie_wang 已提交
5303
**Parameters**
A
Annie_wang 已提交
5304

A
Annie_wang 已提交
5305 5306 5307
  | Name| Type  | Mandatory| Description                               |
  | ------ | ------ | ---- | ----------------------------------- |
  | ashmem | Ashmem | Yes  | Anonymous shared object to write.|
Z
zengyawen 已提交
5308

A
Annie_wang 已提交
5309
**Return value**
A
Annie_wang 已提交
5310

A
Annie_wang 已提交
5311 5312
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5313
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
Z
zengyawen 已提交
5314

A
Annie_wang 已提交
5315
**Example**
A
annie_wangli 已提交
5316

A
Annie_wang 已提交
5317
  ```ts
Z
zengyawen 已提交
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329
  let parcel = new rpc.MessageParcel();
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
  let isWriteSuccess = parcel.writeAshmem(ashmem);
  console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);
  ```

### readAshmem<sup>8+</sup>

readAshmem(): Ashmem

Reads the anonymous shared object from this **MessageParcel** object.

A
annie_wangli 已提交
5330
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5331

A
Annie_wang 已提交
5332
**Return value**
A
Annie_wang 已提交
5333

A
Annie_wang 已提交
5334 5335 5336
  | Type  | Description              |
  | ------ | ------------------ |
  | Ashmem | Anonymous share object obtained.|
Z
zengyawen 已提交
5337

A
Annie_wang 已提交
5338
**Example**
A
annie_wangli 已提交
5339

A
Annie_wang 已提交
5340
  ```ts
Z
zengyawen 已提交
5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
  let parcel = new rpc.MessageParcel();
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024);
  let isWriteSuccess = parcel.writeAshmem(ashmem);
  console.log("RpcTest: write ashmem to result is : " + isWriteSuccess);
  let readAshmem = parcel.readAshmem();
  console.log("RpcTest: read ashmem to result is : " + readAshmem);
  ```

### getRawDataCapacity<sup>8+</sup>

getRawDataCapacity(): number

Obtains the maximum amount of raw data that can be held by this **MessageParcel** object.

A
annie_wangli 已提交
5355
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5356

A
Annie_wang 已提交
5357
**Return value**
A
Annie_wang 已提交
5358

A
Annie_wang 已提交
5359 5360 5361
  | Type  | Description                                                      |
  | ------ | ---------------------------------------------------------- |
  | number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageParcel** object.|
Z
zengyawen 已提交
5362

A
Annie_wang 已提交
5363
**Example**
A
annie_wangli 已提交
5364

A
Annie_wang 已提交
5365
  ```ts
Z
zengyawen 已提交
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
  let parcel = new rpc.MessageParcel();
  let result = parcel.getRawDataCapacity();
  console.log("RpcTest: parcel get RawDataCapacity result is : " + result);
  ```

### writeRawData<sup>8+</sup>

writeRawData(rawData: number[], size: number): boolean

Writes raw data to this **MessageParcel** object.

A
annie_wangli 已提交
5377
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5378

A
Annie_wang 已提交
5379
**Parameters**
A
Annie_wang 已提交
5380

A
Annie_wang 已提交
5381 5382 5383 5384
  | Name | Type    | Mandatory| Description                              |
  | ------- | -------- | ---- | ---------------------------------- |
  | rawData | number[] | Yes  | Raw data to write.                |
  | size    | number   | Yes  | Size of the raw data, in bytes.|
Z
zengyawen 已提交
5385

A
Annie_wang 已提交
5386
**Return value**
A
Annie_wang 已提交
5387

A
Annie_wang 已提交
5388 5389
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5390
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
Z
zengyawen 已提交
5391

A
Annie_wang 已提交
5392
**Example**
A
annie_wangli 已提交
5393

A
Annie_wang 已提交
5394
  ```ts
Z
zengyawen 已提交
5395
  let parcel = new rpc.MessageParcel();
A
Annie_wang 已提交
5396
  let arr = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
5397 5398 5399 5400 5401 5402 5403 5404 5405 5406
  let isWriteSuccess = parcel.writeRawData(arr, arr.length);
  console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);
  ```

### readRawData<sup>8+</sup>

readRawData(size: number): number[]

Reads raw data from this **MessageParcel** object.

A
annie_wangli 已提交
5407
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5408

A
Annie_wang 已提交
5409
**Parameters**
A
Annie_wang 已提交
5410

A
Annie_wang 已提交
5411 5412 5413
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | size   | number | Yes  | Size of the raw data to read.|
Z
zengyawen 已提交
5414

A
Annie_wang 已提交
5415
**Return value**
A
Annie_wang 已提交
5416

A
Annie_wang 已提交
5417 5418 5419
  | Type    | Description                          |
  | -------- | ------------------------------ |
  | number[] | Raw data obtained, in bytes.|
Z
zengyawen 已提交
5420

A
Annie_wang 已提交
5421
**Example**
A
annie_wangli 已提交
5422

A
Annie_wang 已提交
5423
  ```ts
Z
zengyawen 已提交
5424
  let parcel = new rpc.MessageParcel();
A
Annie_wang 已提交
5425
  let arr = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
5426 5427 5428 5429 5430 5431
  let isWriteSuccess = parcel.writeRawData(arr, arr.length);
  console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess);
  let result = parcel.readRawData(5);
  console.log("RpcTest: parcel read raw data result is : " + result);
  ```

A
Annie_wang 已提交
5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
## Parcelable<sup>9+</sup>

Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC.

### marshalling

marshalling(dataOut: MessageSequence): boolean

Marshals this **Parcelable** object into a **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5446 5447 5448
  | Name | Type           | Mandatory| Description                                       |
  | ------- | --------------- | ---- | ------------------------------------------- |
  | dataOut | MessageSequence | Yes  | **MessageSequence** object to which the **Parcelable** object is to be marshaled.|
A
Annie_wang 已提交
5449 5450 5451

**Return value**

A
Annie_wang 已提交
5452 5453
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5454
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
5455 5456
**Example**

A
Annie_wang 已提交
5457
  ```ts
A
Annie_wang 已提交
5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494
  class MyParcelable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
          return true;
      }
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
          return true;
      }
  }
  let parcelable = new MyParcelable(1, "aaa");
  let data = rpc.MessageSequence.create();
  let result = data.writeParcelable(parcelable);
  console.log("RpcClient: writeParcelable is " + result);
  let ret = new MyParcelable(0, "");
  let result2 = data.readParcelable(ret);
  console.log("RpcClient: readParcelable is " + result2);
  ```

### unmarshalling

unmarshalling(dataIn: MessageSequence): boolean

Unmarshals this **Parcelable** object from a **MessageSequence** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5495 5496 5497
  | Name| Type           | Mandatory| Description                                           |
  | ------ | --------------- | ---- | ----------------------------------------------- |
  | dataIn | MessageSequence | Yes  | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.|
A
Annie_wang 已提交
5498 5499 5500

**Return value**

A
Annie_wang 已提交
5501 5502
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
5503
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
5504 5505 5506

**Example**

A
Annie_wang 已提交
5507
  ```ts
A
Annie_wang 已提交
5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539
  class MyParcelable {
      num: number;
      str: string;
      constructor(num, str) {
          this.num = num;
          this.str = str;
      }
      marshalling(messageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
          return true;
      }
      unmarshalling(messageSequence) {
          this.num = messageSequence.readInt();
          this.str = messageSequence.readString();
          return true;
      }
  }
  let parcelable = new MyParcelable(1, "aaa");
  let data = rpc.MessageSequence.create();
  let result = data.writeParcelable(parcelable);
  console.log("RpcClient: writeParcelable is " + result);
  let ret = new MyParcelable(0, "");
  let result2 = data.readParcelable(ret);
  console.log("RpcClient: readParcelable is " + result2);
  ```

## Sequenceable<sup>(deprecated)</sup>

>This class is no longer maintained since API version 9. You are advised to use the [Parcelable](#parcelable9).

Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC.
Z
zengyawen 已提交
5540 5541 5542 5543 5544

### marshalling

marshalling(dataOut: MessageParcel): boolean

A
annie_wangli 已提交
5545 5546 5547
Marshals the sequenceable object into a **MessageParcel** object.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5548

A
Annie_wang 已提交
5549
**Parameters**
A
Annie_wang 已提交
5550

A
Annie_wang 已提交
5551 5552
  | Name | Type                                     | Mandatory| Description                                     |
  | ------- | ----------------------------------------- | ---- | ----------------------------------------- |
A
Annie_wang 已提交
5553
  | dataOut | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object to which the sequenceable object is to be marshaled.|
Z
zengyawen 已提交
5554

A
Annie_wang 已提交
5555
**Return value**
A
Annie_wang 已提交
5556

A
Annie_wang 已提交
5557 5558
  | Type   | Description                             |
  | ------- | --------------------------------  |
A
Annie_wang 已提交
5559
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
Annie_wang 已提交
5560
**Example**
A
annie_wangli 已提交
5561

A
Annie_wang 已提交
5562
  ```ts
Z
zengyawen 已提交
5563
  class MySequenceable {
A
Annie_wang 已提交
5564 5565 5566
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
5567
          this.num = num;
A
Annie_wang 已提交
5568
          this.str = str;
Z
zengyawen 已提交
5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: readSequenceable is " + result2);
  ```

### unmarshalling

A
Annie_wang 已提交
5592
unmarshalling(dataIn: MessageParcel): boolean
Z
zengyawen 已提交
5593 5594 5595

Unmarshals this sequenceable object from a **MessageParcel** object.

A
annie_wangli 已提交
5596 5597
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5598
**Parameters**
A
Annie_wang 已提交
5599

A
Annie_wang 已提交
5600 5601
  | Name| Type                                     | Mandatory| Description                                         |
  | ------ | ----------------------------------------- | ---- | --------------------------------------------- |
A
Annie_wang 已提交
5602
  | dataIn | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object in which the sequenceable object is to be unmarshaled.|
Z
zengyawen 已提交
5603

A
Annie_wang 已提交
5604
**Return value**
A
Annie_wang 已提交
5605

A
Annie_wang 已提交
5606 5607
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
5608
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
5609

A
Annie_wang 已提交
5610
**Example**
A
annie_wangli 已提交
5611

A
Annie_wang 已提交
5612
  ```ts
Z
zengyawen 已提交
5613
  class MySequenceable {
A
Annie_wang 已提交
5614 5615 5616
      num: number;
      str: string;
      constructor(num, str) {
Z
zengyawen 已提交
5617
          this.num = num;
A
Annie_wang 已提交
5618
          this.str = str;
Z
zengyawen 已提交
5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641
      }
      marshalling(messageParcel) {
          messageParcel.writeInt(this.num);
          messageParcel.writeString(this.str);
          return true;
      }
      unmarshalling(messageParcel) {
          this.num = messageParcel.readInt();
          this.str = messageParcel.readString();
          return true;
      }
  }
  let sequenceable = new MySequenceable(1, "aaa");
  let data = rpc.MessageParcel.create();
  let result = data.writeSequenceable(sequenceable);
  console.log("RpcClient: writeSequenceable is " + result);
  let ret = new MySequenceable(0, "");
  let result2 = data.readSequenceable(ret);
  console.log("RpcClient: readSequenceable is " + result2);
  ```

## IRemoteBroker

A
Annie_wang 已提交
5642
Provides the holder of a remote proxy object.
Z
zengyawen 已提交
5643 5644 5645 5646 5647

### asObject

asObject(): IRemoteObject

A
Annie_wang 已提交
5648
Obtains a proxy or remote object. This API must be implemented by its derived classes.
Z
zengyawen 已提交
5649

A
annie_wangli 已提交
5650 5651
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5652
**Return value**
A
Annie_wang 已提交
5653

A
Annie_wang 已提交
5654 5655
  | Type | Description |
  | ----- | ----- |
A
Annie_wang 已提交
5656
  | [IRemoteObject](#iremoteobject) | Returns the **RemoteObject** if it is the caller; returns the [IRemoteObject](#iremoteobject), the holder of this **RemoteProxy** object, if the caller is a [RemoteProxy](#remoteproxy) object.|
Z
zengyawen 已提交
5657

A
Annie_wang 已提交
5658
**Example**
A
annie_wangli 已提交
5659

A
Annie_wang 已提交
5660
  ```ts
Z
zengyawen 已提交
5661 5662 5663 5664 5665
  class TestAbility extends rpc.RemoteObject {
      asObject() {
          return this;
      }
  }
A
Annie_wang 已提交
5666
  let remoteObject = new TestAbility("testObject").asObject();
Z
zengyawen 已提交
5667 5668
  ```

A
Annie_wang 已提交
5669
**Example**
A
annie_wangli 已提交
5670

A
Annie_wang 已提交
5671
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
5672

A
Annie_wang 已提交
5673
  ```ts
A
Annie_wang 已提交
5674 5675 5676
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
5677 5678
  let proxy;
  let connect = {
A
Annie_wang 已提交
5679
      onConnect: function(elementName, remoteProxy) {
A
Annie_wang 已提交
5680 5681 5682
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
A
Annie_wang 已提交
5683
      onDisconnect: function(elementName) {
A
Annie_wang 已提交
5684 5685
          console.log("RpcClient: onDisconnect");
      },
A
Annie_wang 已提交
5686
      onFailed: function() {
A
Annie_wang 已提交
5687 5688 5689 5690 5691 5692 5693
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
      "abilityName": "com.ohos.server.EntryAbility",
  };
A
Annie_wang 已提交
5694
  
A
Annie_wang 已提交
5695 5696
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
A
Annie_wang 已提交
5697
  
A
Annie_wang 已提交
5698 5699 5700 5701 5702
  globalThis.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.

A
Annie_wang 已提交
5703
  ```ts
Z
zengyawen 已提交
5704
  class TestProxy {
A
Annie_wang 已提交
5705
      remote: rpc.RemoteObject;
Z
zengyawen 已提交
5706 5707 5708 5709 5710 5711 5712
      constructor(remote) {
          this.remote = remote;
      }
      asObject() {
          return this.remote;
      }
  }
A
Annie_wang 已提交
5713
  let iRemoteObject = new TestProxy(proxy).asObject();
Z
zengyawen 已提交
5714 5715 5716 5717
  ```

## DeathRecipient

A
annie_wangli 已提交
5718
Subscribes to death notifications of a remote object. When the remote object is dead, the local end will receive a notification and **[onRemoteDied](#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. 
Z
zengyawen 已提交
5719 5720 5721 5722 5723 5724 5725

### onRemoteDied

onRemoteDied(): void

Called to perform subsequent operations when a death notification of the remote object is received.

A
annie_wangli 已提交
5726 5727
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5728
**Example**
A
annie_wangli 已提交
5729

A
Annie_wang 已提交
5730
  ```ts
Z
zengyawen 已提交
5731 5732
  class MyDeathRecipient {
      onRemoteDied() {
A
Annie_wang 已提交
5733
          console.log("server died");
Z
zengyawen 已提交
5734 5735 5736 5737
      }
  }
  ```

A
Annie_wang 已提交
5738
## RequestResult<sup>9+</sup>
Z
zengyawen 已提交
5739 5740 5741

Defines the response to the request.

A
annie_wangli 已提交
5742 5743
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5744 5745 5746 5747 5748 5749
  | Name   | Type           | Readable| Writable| Description                                 |
  | ------- | --------------- | ---- | ---- |-------------------------------------- |
  | errCode | number          | Yes  | No  | Error code.                             |
  | code    | number          | Yes  | No  | Message code.                           |
  | data    | MessageSequence | Yes  | No  | **MessageSequence** object sent to the remote process.|
  | reply   | MessageSequence | Yes  | No  | **MessageSequence** object returned by the remote process.  |
A
Annie_wang 已提交
5750 5751 5752 5753 5754 5755 5756 5757

## SendRequestResult<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [RequestResult](#requestresult9).

Defines the response to the request.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5758

A
Annie_wang 已提交
5759 5760 5761 5762 5763 5764
  | Name   | Type         | Readable| Writable| Description                               |
  | ------- | ------------- | ---- | ---- | ----------------------------------- |
  | errCode | number        | Yes  | No  | Error code.                           |
  | code    | number        | Yes  | No  | Message code.                         |
  | data    | MessageParcel | Yes  | No  | **MessageParcel** object sent to the remote process.|
  | reply   | MessageParcel | Yes  | No  | **MessageParcel** object returned by the remote process.  |
Z
zengyawen 已提交
5765 5766 5767

## IRemoteObject

A
annie_wangli 已提交
5768
Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages.
Z
zengyawen 已提交
5769

A
Annie_wang 已提交
5770 5771 5772 5773
### getLocalInterface<sup>9+</sup>

getLocalInterface(descriptor: string): IRemoteBroker

A
Annie_wang 已提交
5774
Obtains the interface descriptor.
A
Annie_wang 已提交
5775 5776 5777 5778 5779

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5780 5781 5782
  | Name    | Type  | Mandatory| Description                |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | Yes  | Interface descriptor.|
A
Annie_wang 已提交
5783 5784 5785

**Return value**

A
Annie_wang 已提交
5786 5787 5788
  | Type         | Description                                         |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.|
Z
zengyawen 已提交
5789

A
Annie_wang 已提交
5790 5791 5792
### queryLocalInterface<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9).
Z
zengyawen 已提交
5793 5794 5795

queryLocalInterface(descriptor: string): IRemoteBroker

A
Annie_wang 已提交
5796
Queries the interface descriptor.
Z
zengyawen 已提交
5797

A
annie_wangli 已提交
5798 5799
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5800
**Parameters**
A
Annie_wang 已提交
5801

A
Annie_wang 已提交
5802 5803 5804
  | Name    | Type  | Mandatory| Description                |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | Yes  | Interface descriptor.|
Z
zengyawen 已提交
5805

A
Annie_wang 已提交
5806
**Return value**
A
Annie_wang 已提交
5807

A
Annie_wang 已提交
5808 5809 5810
  | Type         | Description                                         |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.|
Z
zengyawen 已提交
5811

A
Annie_wang 已提交
5812
### sendRequest<sup>(deprecated)</sup>
Z
zengyawen 已提交
5813

A
Annie_wang 已提交
5814
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
A
Annie_wang 已提交
5815

A
Annie_wang 已提交
5816
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
Z
zengyawen 已提交
5817

A
Annie_wang 已提交
5818
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.
A
annie_wangli 已提交
5819 5820

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5821

A
Annie_wang 已提交
5822
**Parameters**
A
Annie_wang 已提交
5823

A
Annie_wang 已提交
5824 5825 5826
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
5827 5828
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
5829
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
Z
zengyawen 已提交
5830

A
Annie_wang 已提交
5831
**Return value**
A
Annie_wang 已提交
5832

A
Annie_wang 已提交
5833 5834
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
5835
  | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.|
A
Annie_wang 已提交
5836 5837


A
Annie_wang 已提交
5838 5839
### sendRequest<sup>8+(deprecated)</sup>

A
Annie_wang 已提交
5840
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
A
Annie_wang 已提交
5841

A
Annie_wang 已提交
5842
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
A
Annie_wang 已提交
5843

A
Annie_wang 已提交
5844
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.
A
Annie_wang 已提交
5845 5846 5847

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5848
**Parameters**
A
Annie_wang 已提交
5849

A
Annie_wang 已提交
5850 5851 5852
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------  | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
5853 5854
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
5855
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
5856 5857 5858

**Return value**

A
Annie_wang 已提交
5859 5860 5861
  | Type                            | Description                                         |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | Promise used to return the **sendRequestResult** object.|
A
Annie_wang 已提交
5862 5863 5864 5865 5866 5867


### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;

A
Annie_wang 已提交
5868
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.
A
Annie_wang 已提交
5869 5870 5871 5872 5873

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5874 5875 5876
  | Name | Type                                | Mandatory| Description                                                                                  |
  | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
5877 5878
  | data    | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
  | reply   | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
A
Annie_wang 已提交
5879
  | options | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
5880 5881 5882

**Return value**

A
Annie_wang 已提交
5883 5884 5885
  | Type                        | Description                                     |
  | ---------------------------- | ----------------------------------------- |
  | Promise&lt;RequestResult&gt; | Promise used to return the **requestResult** object.|
A
Annie_wang 已提交
5886 5887 5888 5889 5890 5891


### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

A
Annie_wang 已提交
5892
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.
A
Annie_wang 已提交
5893 5894 5895 5896 5897

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5898 5899 5900
  | Name  | Type                                | Mandatory| Description                                                                                  |
  | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
5901 5902
  | data     | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
  | reply    | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
A
Annie_wang 已提交
5903 5904
  | options  | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | callback | AsyncCallback&lt;RequestResult&gt;   | Yes  | Callback for receiving the sending result.                                                                  |
A
Annie_wang 已提交
5905 5906 5907 5908 5909 5910 5911

### sendRequest<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).

sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void

A
Annie_wang 已提交
5912
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.
A
Annie_wang 已提交
5913 5914 5915 5916 5917

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5918 5919 5920 5921 5922 5923 5924
  | Name  | Type                                     | Mandatory| Description                                                                                  |
  | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
  | data     | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
  | options  | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | callback | AsyncCallback&lt;SendRequestResult&gt;    | Yes  | Callback for receiving the sending result.                                                                  |
A
Annie_wang 已提交
5925 5926 5927 5928 5929

### registerDeathRecipient<sup>9+</sup>

registerDeathRecipient(recipient: DeathRecipient, flags: number): void

A
Annie_wang 已提交
5930
Registers a callback for receiving death notifications of the remote object. The callback will be called if the remote object process matching the **RemoteProxy** object is killed.
A
Annie_wang 已提交
5931 5932 5933 5934 5935

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
5936 5937 5938 5939
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to register.|
  | flags     | number                            | Yes  | Flag of the death notification.|
A
Annie_wang 已提交
5940

A
Annie_wang 已提交
5941
**Error codes**
A
Annie_wang 已提交
5942 5943 5944

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
5945
  | ID| Error Message|
A
Annie_wang 已提交
5946 5947
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
A
Annie_wang 已提交
5948

A
Annie_wang 已提交
5949
### addDeathrecipient<sup>(deprecated)</sup>
A
Annie_wang 已提交
5950

A
Annie_wang 已提交
5951
>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9).
A
Annie_wang 已提交
5952

A
Annie_wang 已提交
5953
addDeathRecipient(recipient: DeathRecipient, flags: number): boolean
A
Annie_wang 已提交
5954

A
Annie_wang 已提交
5955
Adds a callback for receiving death notifications of the remote object. This method is called if the remote object process matching the **RemoteProxy** object is killed.
A
Annie_wang 已提交
5956 5957 5958

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
5959
**Parameters**
A
Annie_wang 已提交
5960

A
Annie_wang 已提交
5961 5962 5963 5964
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to add.|
  | flags     | number                            | Yes  | Flag of the death notification.|
Z
zengyawen 已提交
5965

A
Annie_wang 已提交
5966
**Return value**
A
Annie_wang 已提交
5967

A
Annie_wang 已提交
5968 5969
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
5970
  | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.|
Z
zengyawen 已提交
5971 5972


A
Annie_wang 已提交
5973
### unregisterDeathRecipient<sup>9+</sup>
Z
zengyawen 已提交
5974

A
Annie_wang 已提交
5975 5976
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void

A
Annie_wang 已提交
5977
Unregisters the callback used to receive death notifications of the remote object.
A
annie_wangli 已提交
5978 5979

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
5980

A
Annie_wang 已提交
5981
**Parameters**
A
Annie_wang 已提交
5982

A
Annie_wang 已提交
5983 5984 5985 5986
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to unregister.|
  | flags     | number                            | Yes  | Flag of the death notification.|
Z
zengyawen 已提交
5987

A
Annie_wang 已提交
5988
**Error codes**
Z
zengyawen 已提交
5989

A
Annie_wang 已提交
5990
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).
Z
zengyawen 已提交
5991

A
Annie_wang 已提交
5992
  | ID| Error Message|
A
Annie_wang 已提交
5993 5994
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
Z
zengyawen 已提交
5995

A
Annie_wang 已提交
5996 5997 5998 5999 6000 6001 6002
### removeDeathRecipient<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9).

removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean

Removes the callback used to receive death notifications of the remote object.
Z
zengyawen 已提交
6003

A
annie_wangli 已提交
6004 6005
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
6006
**Parameters**
A
Annie_wang 已提交
6007

A
Annie_wang 已提交
6008 6009 6010 6011
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to remove.|
  | flags     | number                            | Yes  | Flag of the death notification.|
Z
zengyawen 已提交
6012

A
Annie_wang 已提交
6013
**Return value**
A
Annie_wang 已提交
6014

A
Annie_wang 已提交
6015 6016
  | Type   | Description                                    |
  | ------- | -----------------------------------------|
A
Annie_wang 已提交
6017
  | boolean | Returns **true** if the callback is removed; returns **false** otherwise.|
Z
zengyawen 已提交
6018

A
Annie_wang 已提交
6019
### getDescriptor<sup>9+</sup>
Z
zengyawen 已提交
6020

A
Annie_wang 已提交
6021
getDescriptor(): string
Z
zengyawen 已提交
6022

A
Annie_wang 已提交
6023
Obtains the interface descriptor (which is a string) of this object.
Z
zengyawen 已提交
6024

A
annie_wangli 已提交
6025 6026
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
6027
**Return value**
A
Annie_wang 已提交
6028

A
Annie_wang 已提交
6029 6030 6031
  | Type  | Description            |
  | ------ | ---------------- |
  | string | Interface descriptor obtained.|
Z
zengyawen 已提交
6032

A
Annie_wang 已提交
6033
**Error codes**
A
Annie_wang 已提交
6034

A
Annie_wang 已提交
6035 6036
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
6037
  | ID| Error Message|
A
Annie_wang 已提交
6038 6039
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
Z
zengyawen 已提交
6040 6041


A
Annie_wang 已提交
6042 6043 6044
### getInterfaceDescriptor<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9).
Z
zengyawen 已提交
6045 6046 6047

getInterfaceDescriptor(): string

A
Annie_wang 已提交
6048
Obtains the interface descriptor (which is a string) of this object.
A
annie_wangli 已提交
6049 6050

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6051

A
Annie_wang 已提交
6052
**Return value**
A
Annie_wang 已提交
6053

A
Annie_wang 已提交
6054 6055 6056
  | Type  | Description            |
  | ------ | ---------------- |
  | string | Interface descriptor obtained.|
Z
zengyawen 已提交
6057 6058 6059 6060 6061 6062


### isObjectDead

isObjectDead(): boolean

A
annie_wangli 已提交
6063
Checks whether this object is dead.
Z
zengyawen 已提交
6064

A
annie_wangli 已提交
6065 6066
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
6067
**Return value**
A
Annie_wang 已提交
6068

A
Annie_wang 已提交
6069 6070
  | Type   | Description                              |
  | ------- | ---------------------------------- |
A
Annie_wang 已提交
6071
  | boolean | Returns **true** if the object is dead; returns **false** otherwise.|
Z
zengyawen 已提交
6072 6073 6074 6075


## RemoteProxy

A
Annie_wang 已提交
6076
Provides APIs to implement **IRemoteObject**.
Z
zengyawen 已提交
6077

A
annie_wangli 已提交
6078 6079
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
6080
| Name                 | Value                     | Description                             |
A
annie_wangli 已提交
6081 6082
| --------------------- | ----------------------- | --------------------------------- |
| PING_TRANSACTION      | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.|
A
Annie_wang 已提交
6083 6084
| DUMP_TRANSACTION      | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. |
| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. |
A
annie_wangli 已提交
6085 6086 6087
| MIN_TRANSACTION_ID    | 1 (0x00000001)          | Minimum valid instruction code.                 |
| MAX_TRANSACTION_ID    | 16777215 (0x00FFFFFF)   | Maximum valid instruction code.                 |

A
Annie_wang 已提交
6088
### sendRequest<sup>(deprecated)</sup>
Z
zengyawen 已提交
6089

A
Annie_wang 已提交
6090
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
Z
zengyawen 已提交
6091

A
Annie_wang 已提交
6092
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
A
Annie_wang 已提交
6093

A
Annie_wang 已提交
6094
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.
A
annie_wangli 已提交
6095 6096

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6097

A
Annie_wang 已提交
6098
**Parameters**
A
Annie_wang 已提交
6099

A
Annie_wang 已提交
6100 6101 6102
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
6103 6104
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
6105
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
Z
zengyawen 已提交
6106

A
Annie_wang 已提交
6107
**Return value**
A
Annie_wang 已提交
6108

A
Annie_wang 已提交
6109 6110
  | Type   | Description                            |
  | ------- | ---------------------------------|
A
Annie_wang 已提交
6111
  | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.|
A
Annie_wang 已提交
6112 6113

**Example**
Z
zengyawen 已提交
6114

A
Annie_wang 已提交
6115
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6116

A
Annie_wang 已提交
6117
  ```ts
A
Annie_wang 已提交
6118 6119 6120
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
6135
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6136
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6137
  };
A
Annie_wang 已提交
6138
  
A
Annie_wang 已提交
6139 6140
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
A
Annie_wang 已提交
6141
  
A
Annie_wang 已提交
6142 6143 6144 6145 6146
  globalThis.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.

A
Annie_wang 已提交
6147
  ```ts
Z
zengyawen 已提交
6148 6149 6150 6151 6152
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
A
Annie_wang 已提交
6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163
  let ret: boolean = proxy.sendRequest(1, data, reply, option);
  if (ret) {
      console.log("sendRequest got result");
      let msg = reply.readString();
      console.log("RPCTest: reply msg: " + msg);
  } else {
      console.log("RPCTest: sendRequest failed");
  }
  console.log("RPCTest: sendRequest ends, reclaim parcel");
  data.reclaim();
  reply.reclaim();
Z
zengyawen 已提交
6164 6165
  ```

A
Annie_wang 已提交
6166 6167 6168 6169
### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;

A
Annie_wang 已提交
6170
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.
A
Annie_wang 已提交
6171 6172 6173 6174 6175

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
6176 6177 6178
  | Name | Type                                | Mandatory| Description                                                                                  |
  | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
6179
  | data    | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
A
Annie_wang 已提交
6180 6181
  | reply   | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
  | options | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
6182 6183 6184

**Return value**

A
Annie_wang 已提交
6185 6186 6187
  | Type                        | Description                                     |
  | ---------------------------- | ----------------------------------------- |
  | Promise&lt;RequestResult&gt; | Promise used to return the **requestResult** object.|
A
Annie_wang 已提交
6188 6189 6190

**Example**

A
Annie_wang 已提交
6191
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6192

A
Annie_wang 已提交
6193
  ```ts
A
Annie_wang 已提交
6194 6195 6196
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6212
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6213
  };
A
Annie_wang 已提交
6214
  
A
Annie_wang 已提交
6215 6216 6217 6218 6219 6220
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
6221
  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.
A
Annie_wang 已提交
6222 6223

  ```ts
A
Annie_wang 已提交
6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247
  let option = new rpc.MessageOption();
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendMessageRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendMessageRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

A
Annie_wang 已提交
6248 6249
### sendRequest<sup>8+(deprecated)</sup>

A
Annie_wang 已提交
6250
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
A
Annie_wang 已提交
6251

A
Annie_wang 已提交
6252
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
A
Annie_wang 已提交
6253

A
Annie_wang 已提交
6254
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.
A
Annie_wang 已提交
6255 6256 6257 6258

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**
A
Annie_wang 已提交
6259

A
Annie_wang 已提交
6260 6261 6262
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
6263 6264
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
6265
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
6266 6267

**Return value**
A
Annie_wang 已提交
6268

A
Annie_wang 已提交
6269 6270 6271
  | Type                            | Description                                         |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | Promise used to return the **sendRequestResult** object.|
A
Annie_wang 已提交
6272 6273

**Example**
A
annie_wangli 已提交
6274

A
Annie_wang 已提交
6275
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6276

A
Annie_wang 已提交
6277
  ```ts
A
Annie_wang 已提交
6278 6279 6280
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
6295
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6296
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6297
  };
A
Annie_wang 已提交
6298 6299 6300 6301 6302
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6303 6304
  ```

A
Annie_wang 已提交
6305
  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.
A
Annie_wang 已提交
6306 6307

  ```ts
Z
zengyawen 已提交
6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

A
Annie_wang 已提交
6332 6333 6334 6335
### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

A
Annie_wang 已提交
6336
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 **sendMessageRequest** is returned, and the reply contains the returned information.
A
Annie_wang 已提交
6337 6338 6339 6340 6341

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
6342 6343 6344
  | Name  | Type                                | Mandatory| Description                                                                                  |
  | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
6345 6346
  | data     | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
  | reply    | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
A
Annie_wang 已提交
6347 6348
  | options  | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | callback | AsyncCallback&lt;RequestResult&gt;   | Yes  | Callback for receiving the sending result.                                                                  |
A
Annie_wang 已提交
6349 6350

**Example**
A
Annie_wang 已提交
6351 6352

  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6353

A
Annie_wang 已提交
6354
  ```ts
A
Annie_wang 已提交
6355 6356 6357
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6373
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387
  };
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
A
Annie_wang 已提交
6388 6389 6390 6391 6392
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6393 6394
  ```

A
Annie_wang 已提交
6395
  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.
A
Annie_wang 已提交
6396 6397

  ```ts
A
Annie_wang 已提交
6398 6399 6400 6401 6402 6403
  let option = new rpc.MessageOption();
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
  data.writeInt(1);
  data.writeString("hello");
  try {
A
Annie_wang 已提交
6404
      proxy.sendMessageRequest(1, data, reply, option, sendRequestCallback);
A
Annie_wang 已提交
6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416
  } catch(error) {
      console.info("rpc send sequence request fail, errorCode " + error.code);
      console.info("rpc send sequence request fail, errorMessage " + error.message);
  }
  ```

### sendRequest<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).

sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void

A
Annie_wang 已提交
6417
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.
A
Annie_wang 已提交
6418 6419 6420 6421 6422

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
6423 6424 6425 6426 6427 6428 6429
  | Name  | Type                                     | Mandatory| Description                                                                                  |
  | -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code     | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
  | data     | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
  | options  | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | callback | AsyncCallback&lt;SendRequestResult&gt;    | Yes  | Callback for receiving the sending result.                                                                  |
A
Annie_wang 已提交
6430 6431 6432

**Example**

A
Annie_wang 已提交
6433
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6434

A
Annie_wang 已提交
6435
  ```ts
A
Annie_wang 已提交
6436 6437 6438
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6454
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6455
  };
A
Annie_wang 已提交
6456
 function sendRequestCallback(result) {
A
Annie_wang 已提交
6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
A
Annie_wang 已提交
6469 6470 6471 6472 6473
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6474 6475
  ```

A
Annie_wang 已提交
6476
  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.
A
Annie_wang 已提交
6477 6478

  ```ts
A
Annie_wang 已提交
6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496
  let option = new rpc.MessageOption();
  let data = rpc.MessageParcel.create();
  let reply = rpc.MessageParcel.create();
  data.writeInt(1);
  data.writeString("hello");
  proxy.sendRequest(1, data, reply, option, sendRequestCallback);
  ```

### getLocalInterface<sup>9+</sup>

getLocalInterface(interface: string): IRemoteBroker

Obtains the **LocalInterface** object of an interface token.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
6497 6498 6499
  | Name   | Type  | Mandatory| Description                  |
  | --------- | ------ | ---- | ---------------------- |
  | interface | string | Yes  | Interface descriptor.|
A
Annie_wang 已提交
6500 6501 6502

**Return value**

A
Annie_wang 已提交
6503 6504 6505
  | Type         | Description                                      |
  | ------------- | ------------------------------------------ |
  | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.|
A
Annie_wang 已提交
6506

A
Annie_wang 已提交
6507
**Error codes**
A
Annie_wang 已提交
6508 6509 6510

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
6511
  | ID| Error Message|
A
Annie_wang 已提交
6512 6513
  | -------- | -------- |
  | 1900006  | only remote object permitted |
A
Annie_wang 已提交
6514 6515 6516

**Example**

A
Annie_wang 已提交
6517
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6518

A
Annie_wang 已提交
6519
  ```ts
A
Annie_wang 已提交
6520 6521 6522
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6523 6524 6525 6526 6527 6528
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
A
Annie_wang 已提交
6529
      onDisconnect: function(elementName) {
A
Annie_wang 已提交
6530 6531 6532 6533 6534 6535 6536
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
Annie_wang 已提交
6537 6538
      "bundleName": "com.ohos.server",
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6539
  };
A
Annie_wang 已提交
6540 6541 6542 6543 6544
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6545 6546
  ```

A
Annie_wang 已提交
6547
  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.
A
Annie_wang 已提交
6548 6549

  ```ts
A
Annie_wang 已提交
6550 6551 6552 6553 6554 6555 6556 6557 6558 6559
  try {
      let broker = proxy.getLocalInterface("testObject");
      console.log("RpcClient: getLocalInterface is " + broker);
  } catch(error) {
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
  }
  ```

### queryLocalInterface<sup>(deprecated)</sup>
A
Annie_wang 已提交
6560

A
Annie_wang 已提交
6561 6562 6563
>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9).

queryLocalInterface(interface: string): IRemoteBroker
A
Annie_wang 已提交
6564

A
Annie_wang 已提交
6565
Obtains the **LocalInterface** object of an interface token.
A
Annie_wang 已提交
6566 6567 6568 6569

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**
A
Annie_wang 已提交
6570

A
Annie_wang 已提交
6571 6572 6573
  | Name   | Type  | Mandatory| Description                  |
  | --------- | ------ | ---- | ---------------------- |
  | interface | string | Yes  | Interface descriptor.|
A
Annie_wang 已提交
6574 6575

**Return value**
A
Annie_wang 已提交
6576

A
Annie_wang 已提交
6577 6578 6579
  | Type         | Description                                      |
  | ------------- | ------------------------------------------ |
  | IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.|
A
Annie_wang 已提交
6580 6581 6582

**Example**

A
Annie_wang 已提交
6583
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6584

A
Annie_wang 已提交
6585
  ```ts
A
Annie_wang 已提交
6586 6587 6588
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6589 6590 6591 6592 6593 6594
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
A
Annie_wang 已提交
6595
      onDisconnect: function(elementName) {
A
Annie_wang 已提交
6596 6597 6598 6599 6600 6601 6602
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
Annie_wang 已提交
6603 6604
      "bundleName": "com.ohos.server",
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6605
  };
A
Annie_wang 已提交
6606
  
A
Annie_wang 已提交
6607 6608 6609 6610 6611 6612
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
6613
  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.
A
Annie_wang 已提交
6614 6615

  ```ts
A
Annie_wang 已提交
6616 6617
  let broker = proxy.queryLocalInterface("testObject");
  console.log("RpcClient: queryLocalInterface is " + broker);
A
Annie_wang 已提交
6618
  ```
Z
zengyawen 已提交
6619

A
Annie_wang 已提交
6620
### registerDeathRecipient<sup>9+</sup>
Z
zengyawen 已提交
6621

A
Annie_wang 已提交
6622 6623
registerDeathRecipient(recipient: DeathRecipient, flags: number): void

A
Annie_wang 已提交
6624
Registers a callback for receiving death notifications of the remote object. The callback will be invoked when the remote object process matching the **RemoteProxy** object is killed.
Z
zengyawen 已提交
6625

A
annie_wangli 已提交
6626
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6627

A
Annie_wang 已提交
6628
**Parameters**
A
Annie_wang 已提交
6629

A
Annie_wang 已提交
6630 6631 6632 6633
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to register.|
  | flags     | number                            | Yes  | Flag of the death notification.|
A
Annie_wang 已提交
6634

A
Annie_wang 已提交
6635
**Error codes**
A
Annie_wang 已提交
6636 6637 6638

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
6639
  | ID| Error Message|
A
Annie_wang 已提交
6640 6641
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
Z
zengyawen 已提交
6642

A
Annie_wang 已提交
6643
**Example**
A
annie_wangli 已提交
6644

A
Annie_wang 已提交
6645
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6646

A
Annie_wang 已提交
6647
  ```ts
A
Annie_wang 已提交
6648 6649 6650
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
6665
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6666
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6667
  };
A
Annie_wang 已提交
6668 6669 6670 6671 6672
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6673 6674
  ```

A
Annie_wang 已提交
6675
  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.
A
Annie_wang 已提交
6676

A
Annie_wang 已提交
6677
  ```ts
A
Annie_wang 已提交
6678 6679 6680
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
Z
zengyawen 已提交
6681 6682
      }
  }
A
Annie_wang 已提交
6683 6684
  let deathRecipient = new MyDeathRecipient();
  try {
A
Annie_wang 已提交
6685
      proxy.registerDeathRecipient(deathRecipient, 0);
A
Annie_wang 已提交
6686 6687 6688 6689
  } catch(error) {
      console.info("proxy register deathRecipient fail, errorCode " + error.code);
      console.info("proxy register deathRecipient fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
6690 6691
  ```

A
Annie_wang 已提交
6692
### addDeathRecippient<sup>(deprecated)</sup>
Z
zengyawen 已提交
6693

A
Annie_wang 已提交
6694 6695 6696
>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9).

addDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Z
zengyawen 已提交
6697

A
Annie_wang 已提交
6698
Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy.
Z
zengyawen 已提交
6699

A
annie_wangli 已提交
6700
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6701

A
Annie_wang 已提交
6702
**Parameters**
A
Annie_wang 已提交
6703

A
Annie_wang 已提交
6704 6705 6706 6707
  | Name   | Type                             | Mandatory| Description                             |
  | --------- | --------------------------------- | ---- | --------------------------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to add.         |
  | flags     | number                            | Yes  | Flag of the death notification. This parameter is reserved. It is set to **0**.|
Z
zengyawen 已提交
6708

A
Annie_wang 已提交
6709
**Return value**
A
Annie_wang 已提交
6710

A
Annie_wang 已提交
6711 6712
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
6713
  | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.|
Z
zengyawen 已提交
6714

A
Annie_wang 已提交
6715
**Example**
A
annie_wangli 已提交
6716

A
Annie_wang 已提交
6717
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6718

A
Annie_wang 已提交
6719
  ```ts
A
Annie_wang 已提交
6720 6721 6722
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6723 6724 6725 6726 6727 6728
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
A
Annie_wang 已提交
6729
      onDisconnect: function(elementName) {
Z
zengyawen 已提交
6730 6731 6732 6733 6734 6735 6736
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
Annie_wang 已提交
6737
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6738
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6739
  };
A
Annie_wang 已提交
6740
  
A
Annie_wang 已提交
6741 6742 6743 6744 6745 6746
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
6747
  The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecippient()** of the proxy object is called to add a callback for receiving the death notification of the remove object.
A
Annie_wang 已提交
6748 6749

  ```ts
A
Annie_wang 已提交
6750 6751 6752 6753 6754 6755
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  let deathRecipient = new MyDeathRecipient();
A
Annie_wang 已提交
6756
  proxy.addDeathRecipient(deathRecipient, 0);
Z
zengyawen 已提交
6757 6758
  ```

A
Annie_wang 已提交
6759
### unregisterDeathRecipient<sup>9+</sup>
Z
zengyawen 已提交
6760

A
Annie_wang 已提交
6761
unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void
Z
zengyawen 已提交
6762

A
Annie_wang 已提交
6763
Unregisters the callback used to receive death notifications of the remote object.
Z
zengyawen 已提交
6764

A
annie_wangli 已提交
6765
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6766

A
Annie_wang 已提交
6767
**Parameters**
A
Annie_wang 已提交
6768

A
Annie_wang 已提交
6769 6770 6771 6772
  | Name   | Type                             | Mandatory| Description          |
  | --------- | --------------------------------- | ---- | -------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to unregister.|
  | flags     | number                            | Yes  | Flag of the death notification.|
Z
zengyawen 已提交
6773

A
Annie_wang 已提交
6774
**Error codes**
A
Annie_wang 已提交
6775

A
Annie_wang 已提交
6776 6777
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
6778
  | ID| Error Message|
A
Annie_wang 已提交
6779 6780
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
Z
zengyawen 已提交
6781

A
Annie_wang 已提交
6782
**Example**
A
annie_wangli 已提交
6783

A
Annie_wang 已提交
6784
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6785

A
Annie_wang 已提交
6786
  ```ts
A
Annie_wang 已提交
6787 6788 6789
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
6804
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6805
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6806
  };
A
Annie_wang 已提交
6807
  
A
Annie_wang 已提交
6808 6809 6810 6811 6812 6813
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
6814
  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.
A
Annie_wang 已提交
6815 6816

  ```ts
Z
zengyawen 已提交
6817 6818
  class MyDeathRecipient {
      onRemoteDied() {
A
Annie_wang 已提交
6819
          console.log("server died");
Z
zengyawen 已提交
6820 6821 6822
      }
  }
  let deathRecipient = new MyDeathRecipient();
A
Annie_wang 已提交
6823 6824 6825 6826 6827 6828 6829
  try {
      proxy.registerDeathRecippient(deathRecipient, 0);
      proxy.unregisterDeathRecippient(deathRecipient, 0);
  } catch(error) {
      console.info("proxy register deathRecipient fail, errorCode " + error.code);
      console.info("proxy register deathRecipient fail, errorMessage " + error.message);
  }
Z
zengyawen 已提交
6830 6831
  ```

A
Annie_wang 已提交
6832 6833 6834
### removeDeathRecipient<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9).
Z
zengyawen 已提交
6835

A
Annie_wang 已提交
6836
removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean
Z
zengyawen 已提交
6837 6838 6839

Removes the callback used to receive death notifications of the remote object.

A
annie_wangli 已提交
6840
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6841

A
Annie_wang 已提交
6842
**Parameters**
A
Annie_wang 已提交
6843

A
Annie_wang 已提交
6844 6845 6846 6847
  | Name   | Type                             | Mandatory| Description                             |
  | --------- | --------------------------------- | ---- | --------------------------------- |
  | recipient | [DeathRecipient](#deathrecipient) | Yes  | Callback to remove.               |
  | flags     | number                            | Yes  | Flag of the death notification. This parameter is reserved. It is set to **0**.|
Z
zengyawen 已提交
6848

A
Annie_wang 已提交
6849
**Return value**
A
Annie_wang 已提交
6850

A
Annie_wang 已提交
6851 6852
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
6853
  | boolean | Returns **true** if the callback is removed; returns **false** otherwise.|
Z
zengyawen 已提交
6854

A
Annie_wang 已提交
6855
**Example**
A
annie_wangli 已提交
6856

A
Annie_wang 已提交
6857
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6858

A
Annie_wang 已提交
6859
  ```ts
A
Annie_wang 已提交
6860 6861 6862
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
6877
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6878
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
6879
  };
A
Annie_wang 已提交
6880
  
A
Annie_wang 已提交
6881 6882 6883 6884 6885 6886
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
6887
  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.
A
Annie_wang 已提交
6888 6889

  ```ts
Z
zengyawen 已提交
6890 6891
  class MyDeathRecipient {
      onRemoteDied() {
A
Annie_wang 已提交
6892
          console.log("server died");
Z
zengyawen 已提交
6893 6894 6895 6896 6897 6898 6899
      }
  }
  let deathRecipient = new MyDeathRecipient();
  proxy.addDeathRecippient(deathRecipient, 0);
  proxy.removeDeathRecipient(deathRecipient, 0);
  ```

A
Annie_wang 已提交
6900 6901 6902 6903
### getDescriptor<sup>9+</sup>

getDescriptor(): string

A
Annie_wang 已提交
6904
Obtains the interface descriptor (which is a string) of this proxy object.
A
Annie_wang 已提交
6905 6906 6907 6908 6909

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
6910 6911 6912
  | Type  | Description            |
  | ------ | ---------------- |
  | string | Interface descriptor obtained.|
A
Annie_wang 已提交
6913

A
Annie_wang 已提交
6914
**Error codes**
A
Annie_wang 已提交
6915 6916 6917

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
6918
  | ID| Error Message|
A
Annie_wang 已提交
6919 6920 6921
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
  | 1900007  | communication failed              |
A
Annie_wang 已提交
6922 6923 6924

**Example**

A
Annie_wang 已提交
6925
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6926

A
Annie_wang 已提交
6927
  ```ts
A
Annie_wang 已提交
6928 6929 6930
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
A
Annie_wang 已提交
6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
6946
      "abilityName": "com.ohos.server.EntryAbility",
A
Annie_wang 已提交
6947
  };
A
Annie_wang 已提交
6948 6949 6950 6951 6952
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
6953
  ```
A
Annie_wang 已提交
6954
  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.
A
Annie_wang 已提交
6955 6956

  ```ts
A
Annie_wang 已提交
6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968
  try {
      let descriptor = proxy.getDescriptor();
      console.log("RpcClient: descriptor is " + descriptor);
  } catch(error) {
      console.info("rpc get interface descriptor fail, errorCode " + error.code);
      console.info("rpc get interface descriptor fail, errorMessage " + error.message);
  }
  ```

### getInterfaceDescriptor<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9).
Z
zengyawen 已提交
6969 6970 6971 6972 6973

getInterfaceDescriptor(): string

Obtains the interface descriptor of this proxy object.

A
annie_wangli 已提交
6974
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
6975

A
Annie_wang 已提交
6976
**Return value**
A
Annie_wang 已提交
6977

A
Annie_wang 已提交
6978 6979 6980
  | Type  | Description              |
  | ------ | ------------------ |
  | string | Interface descriptor obtained.|
Z
zengyawen 已提交
6981

A
Annie_wang 已提交
6982
**Example**
A
annie_wangli 已提交
6983

A
Annie_wang 已提交
6984
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
6985

A
Annie_wang 已提交
6986
  ```ts
A
Annie_wang 已提交
6987 6988 6989
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
7004
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
7005
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
7006
  };
A
Annie_wang 已提交
7007
  
A
Annie_wang 已提交
7008 7009 7010 7011 7012 7013
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
  ```

A
Annie_wang 已提交
7014
  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.
A
Annie_wang 已提交
7015 7016

  ```ts
Z
zengyawen 已提交
7017 7018 7019 7020 7021 7022 7023 7024 7025 7026
  let descriptor = proxy.getInterfaceDescriptor();
  console.log("RpcClient: descriptor is " + descriptor);
  ```

### isObjectDead

isObjectDead(): boolean

Checks whether the **RemoteObject** is dead.

A
annie_wangli 已提交
7027
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7028

A
Annie_wang 已提交
7029
**Return value**
A
Annie_wang 已提交
7030

A
Annie_wang 已提交
7031 7032
  | Type   | Description                                             |
  | ------- | ------------------------------------------------- |
A
Annie_wang 已提交
7033
  | boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.|
Z
zengyawen 已提交
7034

A
Annie_wang 已提交
7035
**Example**
A
annie_wangli 已提交
7036

A
Annie_wang 已提交
7037
  Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context).
A
Annie_wang 已提交
7038

A
Annie_wang 已提交
7039
  ```ts
A
Annie_wang 已提交
7040 7041 7042
  // Import @ohos.ability.featureAbility only for the application developed based on the FA model.
  // import FA from "@ohos.ability.featureAbility";
  
Z
zengyawen 已提交
7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056
  let proxy;
  let connect = {
      onConnect: function(elementName, remoteProxy) {
          console.log("RpcClient: js onConnect called.");
          proxy = remoteProxy;
      },
      onDisconnect: function(elementName) {
          console.log("RpcClient: onDisconnect");
      },
      onFailed: function() {
          console.log("RpcClient: onFailed");
      }
  };
  let want = {
A
annie_wangli 已提交
7057
      "bundleName": "com.ohos.server",
A
Annie_wang 已提交
7058
      "abilityName": "com.ohos.server.EntryAbility",
Z
zengyawen 已提交
7059
  };
A
Annie_wang 已提交
7060 7061 7062 7063 7064
  
  // Use this method to connect to the ability for the FA model.
  // FA.connectAbility(want,connect);
  
  globalThis.context.connectServiceExtensionAbility(want, connect);
A
Annie_wang 已提交
7065 7066
  ```

A
Annie_wang 已提交
7067
  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.
A
Annie_wang 已提交
7068 7069

  ```ts
Z
zengyawen 已提交
7070 7071 7072 7073 7074 7075
  let isDead = proxy.isObjectDead();
  console.log("RpcClient: isObjectDead is " + isDead);
  ```

## MessageOption

A
Annie_wang 已提交
7076 7077 7078 7079
Provides common message options (flag and wait time). Use the specified flag to construct the **MessageOption** object.

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7080 7081
  | Name         | Value  | Description                                                       |
  | ------------- | ---- | ----------------------------------------------------------- |
A
Annie_wang 已提交
7082 7083
  | TF_SYNC       | 0    | Synchronous call.                                             |
  | TF_ASYNC      | 1    | Asynchronous call.                                             |
A
Annie_wang 已提交
7084
  | TF_ACCEPT_FDS | 0x10 | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.|
A
Annie_wang 已提交
7085
  | TF_WAIT_TIME  | 8    | Default waiting time, in seconds.                                    |
A
Annie_wang 已提交
7086 7087 7088 7089 7090 7091 7092


### constructor<sup>9+</sup>

constructor(async?: boolean);

A constructor used to create a **MessageOption** object.
Z
zengyawen 已提交
7093

A
annie_wangli 已提交
7094
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7095

A
Annie_wang 已提交
7096 7097
**Parameters**

A
Annie_wang 已提交
7098 7099 7100
  | Name   | Type  | Mandatory| Description                                  |
  | --------- | ------ | ---- | -------------------------------------- |
  | syncFlags | number | No  | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.|
Z
zengyawen 已提交
7101 7102


A
Annie_wang 已提交
7103 7104 7105 7106 7107 7108 7109 7110 7111 7112
**Example**

  ```ts
  class TestRemoteObject extends rpc.MessageOption {
    constructor(async) {
        super(async);
    }
  }
  ```

Z
zengyawen 已提交
7113 7114
### constructor

A
Annie_wang 已提交
7115
constructor(syncFlags?: number, waitTime?: number)
Z
zengyawen 已提交
7116

A
annie_wangli 已提交
7117 7118 7119
A constructor used to create a **MessageOption** object.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7120

A
Annie_wang 已提交
7121
**Parameters**
A
Annie_wang 已提交
7122

A
Annie_wang 已提交
7123 7124 7125 7126
  | Name   | Type  | Mandatory| Description                                         |
  | --------- | ------ | ---- | --------------------------------------------- |
  | syncFlags | number | No  | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.       |
  | waitTime  | number | No  | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.|
A
Annie_wang 已提交
7127

A
Annie_wang 已提交
7128
**Example**
A
Annie_wang 已提交
7129

A
Annie_wang 已提交
7130 7131 7132 7133 7134 7135 7136
  ```ts
  class TestRemoteObject extends rpc.MessageOption {
    constructor(syncFlags,waitTime) {
        super(syncFlags,waitTime);
    }
  }
  ```
A
Annie_wang 已提交
7137 7138 7139 7140 7141 7142 7143 7144 7145 7146
### isAsync<sup>9+</sup>

isAsync(): boolean;

Checks whether **SendMessageRequest** is called synchronously or asynchronously.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
7147 7148
  | Type   | Description                                    |
  | ------- | ---------------------------------------- |
A
Annie_wang 已提交
7149
  | boolean | Returns **true** if **SendMessageRequest** is called synchronously; returns **false** if **SendMessageRequest** is called asynchronously.|
A
Annie_wang 已提交
7150

A
Annie_wang 已提交
7151 7152 7153 7154 7155 7156
**Example**

  ```ts
  let option = new rpc.MessageOption();
  let isAsync = option.isAsync();
  ```
A
Annie_wang 已提交
7157 7158 7159 7160 7161 7162 7163 7164

### setAsync<sup>9+</sup>

setAsync(async: boolean): void;

Sets whether **SendMessageRequest** is called synchronously or asynchronously.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7165

A
Annie_wang 已提交
7166 7167 7168 7169 7170 7171 7172
**Example**

  ```ts
  let option = new rpc.MessageOption();
  let setAsync = option.setAsync(true);
  console.log("Set synchronization flag");
  ```
Z
zengyawen 已提交
7173 7174 7175 7176 7177

### getFlags

getFlags(): number

A
annie_wangli 已提交
7178
Obtains the call flag, which can be synchronous or asynchronous.
Z
zengyawen 已提交
7179

A
annie_wangli 已提交
7180
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7181

A
Annie_wang 已提交
7182
**Return value**
A
Annie_wang 已提交
7183

A
Annie_wang 已提交
7184 7185 7186
  | Type  | Description                                |
  | ------ | ------------------------------------ |
  | number | Call mode obtained.|
Z
zengyawen 已提交
7187

A
Annie_wang 已提交
7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203
**Example**

  ```ts
  try {
      let option = new rpc.MessageOption();
      console.info("create object successfully.");
      let flog = option.getFlags();
      console.info("run getFlags success, flog is " + flog);
      option.setFlags(1)
      console.info("run setFlags success");
      let flog2 = option.getFlags();
      console.info("run getFlags success, flog2 is " + flog2);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7204 7205 7206 7207 7208

### setFlags

setFlags(flags: number): void

A
annie_wangli 已提交
7209 7210 7211
Sets the call flag, which can be synchronous or asynchronous.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7212

A
Annie_wang 已提交
7213
**Parameters**
A
Annie_wang 已提交
7214

A
Annie_wang 已提交
7215 7216 7217
  | Name| Type  | Mandatory| Description                    |
  | ------ | ------ | ---- | ------------------------ |
  | flags  | number | Yes  | Call flag to set.|
Z
zengyawen 已提交
7218

A
Annie_wang 已提交
7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231
**Example**

  ```ts
  try {
      let option = new rpc.MessageOption();
      option.setFlags(1)
      console.info("run setFlags success");
      let flog = option.getFlags();
      console.info("run getFlags success, flog is " + flog);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7232 7233 7234 7235 7236 7237 7238

### getWaitTime

getWaitTime(): number

Obtains the maximum wait time for this RPC call.

A
annie_wangli 已提交
7239 7240
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7241
**Return value**
A
Annie_wang 已提交
7242

A
Annie_wang 已提交
7243 7244 7245
  | Type  | Description             |
  | ------ | ----------------- |
  | number | Maximum wait time obtained.|
Z
zengyawen 已提交
7246

A
Annie_wang 已提交
7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260
**Example**

  ```ts
  try {
      let option = new rpc.MessageOption();
      let time = option.getWaitTime();
      console.info("run getWaitTime success");
      option.setWaitTime(16);
      let time2 = option.getWaitTime();
      console.info("run getWaitTime success, time is " + time);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7261 7262 7263 7264 7265 7266 7267

### setWaitTime

setWaitTime(waitTime: number): void

Sets the maximum wait time for this RPC call.

A
annie_wangli 已提交
7268 7269
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7270
**Parameters**
A
Annie_wang 已提交
7271

A
Annie_wang 已提交
7272 7273 7274
  | Name  | Type  | Mandatory| Description                 |
  | -------- | ------ | ---- | --------------------- |
  | waitTime | number | Yes  | Maximum wait time to set.|
Z
zengyawen 已提交
7275

A
Annie_wang 已提交
7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287
**Example**

  ```ts
  try {
      let option = new rpc.MessageOption();
      option.setWaitTime(16);
      let time = option.getWaitTime();
      console.info("run getWaitTime success, time is " + time);
  } catch (error) {
      console.info("error " + error);
  }
  ```
Z
zengyawen 已提交
7288 7289 7290

## IPCSkeleton

A
annie_wangli 已提交
7291
Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device.
Z
zengyawen 已提交
7292 7293 7294 7295 7296

### getContextObject

static getContextObject(): IRemoteObject

A
Annie_wang 已提交
7297
Obtains the system capability manager. This API is a static method.
Z
zengyawen 已提交
7298

A
annie_wangli 已提交
7299
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7300

A
Annie_wang 已提交
7301
**Return value**
A
Annie_wang 已提交
7302

A
Annie_wang 已提交
7303 7304 7305
  | Type                           | Description                |
  | ------------------------------- | -------------------- |
  | [IRemoteObject](#iremoteobject) | System capability manager obtained.|
Z
zengyawen 已提交
7306

A
Annie_wang 已提交
7307
**Example**
A
annie_wangli 已提交
7308

A
Annie_wang 已提交
7309
  ```ts
Z
zengyawen 已提交
7310 7311 7312 7313 7314 7315 7316 7317
  let samgr = rpc.IPCSkeleton.getContextObject();
  console.log("RpcServer: getContextObject result: " + samgr);
  ```

### getCallingPid

static getCallingPid(): number

A
Annie_wang 已提交
7318
Obtains the PID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the PID of the process will be returned.
Z
zengyawen 已提交
7319

A
annie_wangli 已提交
7320
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7321

A
Annie_wang 已提交
7322
**Return value**
A
Annie_wang 已提交
7323

A
Annie_wang 已提交
7324 7325 7326
  | Type  | Description             |
  | ------ | ----------------- |
  | number | PID of the caller.|
Z
zengyawen 已提交
7327

A
Annie_wang 已提交
7328
**Example**
A
annie_wangli 已提交
7329

A
Annie_wang 已提交
7330
  ```ts
Z
zengyawen 已提交
7331
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7332
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7333 7334 7335 7336
          let callerPid = rpc.IPCSkeleton.getCallingPid();
          console.log("RpcServer: getCallingPid result: " + callerPid);
          return true;
      }
A
Annie_wang 已提交
7337
 }
Z
zengyawen 已提交
7338 7339 7340 7341 7342 7343
  ```

### getCallingUid

static getCallingUid(): number

A
Annie_wang 已提交
7344
Obtains the UID of the caller. This API is a static method, which is invoked by the **RemoteObject** object in the **onRemoteRequest** method. If this method is not invoked in the IPC context (**onRemoteRequest**), the UID of the process will be returned.
Z
zengyawen 已提交
7345

A
annie_wangli 已提交
7346
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7347

A
Annie_wang 已提交
7348
**Return value**
A
Annie_wang 已提交
7349

A
Annie_wang 已提交
7350 7351 7352
  | Type  | Description             |
  | ------ | ----------------- |
  | number | UID of the caller.|
Z
zengyawen 已提交
7353

A
Annie_wang 已提交
7354
**Example**
A
annie_wangli 已提交
7355

A
Annie_wang 已提交
7356
  ```ts
Z
zengyawen 已提交
7357
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7358
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7359 7360 7361 7362 7363 7364 7365
          let callerUid = rpc.IPCSkeleton.getCallingUid();
          console.log("RpcServer: getCallingUid result: " + callerUid);
          return true;
      }
  }
  ```

A
Annie_wang 已提交
7366
### getCallingTokenId<sup>8+</sup>
A
annie_wangli 已提交
7367 7368 7369 7370 7371 7372 7373

static getCallingTokenId(): number;

Obtains the caller's token ID, which is used to verify the caller identity.

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7374
**Return value**
A
Annie_wang 已提交
7375

A
Annie_wang 已提交
7376 7377 7378
   | Type  | Description                 |
   | ------ | --------------------- |
   | number | Token ID of the caller obtained.|
A
Annie_wang 已提交
7379

A
Annie_wang 已提交
7380
**Example**
A
annie_wangli 已提交
7381

A
Annie_wang 已提交
7382
  ```ts
A
annie_wangli 已提交
7383
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7384
      onRemoteMessageRequest(code, data, reply, option) {
A
annie_wangli 已提交
7385 7386 7387 7388 7389 7390 7391
          let callerTokenId = rpc.IPCSkeleton.getCallingTokenId();
          console.log("RpcServer: getCallingTokenId result: " + callerTokenId);
          return true;
      }
  }
  ```

Z
zengyawen 已提交
7392

A
Annie_wang 已提交
7393
### getCallingDeviceID
Z
zengyawen 已提交
7394 7395 7396

static getCallingDeviceID(): string

A
Annie_wang 已提交
7397
Obtains the ID of the device hosting the caller's process. This API is a static method.
Z
zengyawen 已提交
7398

A
annie_wangli 已提交
7399
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7400

A
Annie_wang 已提交
7401
**Return value**
A
Annie_wang 已提交
7402

A
Annie_wang 已提交
7403 7404 7405
  | Type  | Description                        |
  | ------ | ---------------------------- |
  | string | Device ID obtained.|
Z
zengyawen 已提交
7406

A
Annie_wang 已提交
7407
**Example**
A
annie_wangli 已提交
7408

A
Annie_wang 已提交
7409
  ```ts
Z
zengyawen 已提交
7410
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7411
      onRemoteMessageRequest(code, data, reply, option) {
A
Annie_wang 已提交
7412
          let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID();
Z
zengyawen 已提交
7413 7414 7415 7416 7417 7418 7419 7420 7421 7422
          console.log("RpcServer: callerDeviceID is: " + callerDeviceID);
          return true;
      }
  }
  ```

### getLocalDeviceID

static getLocalDeviceID(): string

A
Annie_wang 已提交
7423
Obtains the local device ID. This API is a static method.
Z
zengyawen 已提交
7424

A
annie_wangli 已提交
7425
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7426

A
Annie_wang 已提交
7427
**Return value**
A
Annie_wang 已提交
7428

A
Annie_wang 已提交
7429 7430 7431
  | Type  | Description              |
  | ------ | ------------------ |
  | string | Local device ID obtained.|
Z
zengyawen 已提交
7432

A
Annie_wang 已提交
7433
**Example**
A
annie_wangli 已提交
7434

A
Annie_wang 已提交
7435
  ```ts
Z
zengyawen 已提交
7436
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7437
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448
          let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID();
          console.log("RpcServer: localDeviceID is: " + localDeviceID);
          return true;
      }
  }
  ```

### isLocalCalling

static isLocalCalling(): boolean

A
Annie_wang 已提交
7449
Checks whether the remote process is a process of the local device. This API is a static method.
Z
zengyawen 已提交
7450

A
annie_wangli 已提交
7451
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7452

A
Annie_wang 已提交
7453
**Return value**
A
Annie_wang 已提交
7454

A
Annie_wang 已提交
7455 7456
  | Type   | Description                                              |
  | ------- | -------------------------------------------------- |
A
Annie_wang 已提交
7457
  | boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.|
Z
zengyawen 已提交
7458

A
Annie_wang 已提交
7459
**Example**
A
annie_wangli 已提交
7460

A
Annie_wang 已提交
7461
  ```ts
Z
zengyawen 已提交
7462
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7463
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7464 7465 7466 7467 7468 7469 7470
          let isLocalCalling = rpc.IPCSkeleton.isLocalCalling();
          console.log("RpcServer: isLocalCalling is: " + isLocalCalling);
          return true;
      }
  }
  ```

A
Annie_wang 已提交
7471
### flushCmdBuffer<sup>9+</sup>
Z
zengyawen 已提交
7472

A
Annie_wang 已提交
7473
static flushCmdBuffer(object: IRemoteObject): void
Z
zengyawen 已提交
7474

A
Annie_wang 已提交
7475
Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed.
Z
zengyawen 已提交
7476

A
annie_wangli 已提交
7477 7478
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7479
**Parameters**
A
Annie_wang 已提交
7480

A
Annie_wang 已提交
7481 7482 7483
  | Name| Type                           | Mandatory| Description               |
  | ------ | ------------------------------- | ---- | ------------------- |
  | object | [IRemoteObject](#iremoteobject) | Yes  | **RemoteProxy** specified. |
A
Annie_wang 已提交
7484 7485 7486 7487


**Example**

A
Annie_wang 已提交
7488
  ```ts
A
Annie_wang 已提交
7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let remoteObject = new TestRemoteObject("aaa");
  try {
      rpc.IPCSkeleton.flushCmdBuffer(remoteObject);
  } catch(error) {
      console.info("proxy set calling identity fail, errorCode " + error.code);
      console.info("proxy set calling identity fail, errorMessage " + error.message);
  }
  ```

### flushCommands<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [flushCmdBuffer](#flushcmdbuffer9).

static flushCommands(object: IRemoteObject): number
Z
zengyawen 已提交
7508

A
Annie_wang 已提交
7509
Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed.
A
Annie_wang 已提交
7510 7511 7512 7513 7514

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
7515 7516 7517
  | Name| Type                           | Mandatory| Description               |
  | ------ | ------------------------------- | ---- | ------------------- |
  | object | [IRemoteObject](#iremoteobject) | Yes  | **RemoteProxy** specified. |
Z
zengyawen 已提交
7518

A
Annie_wang 已提交
7519
**Return value**
A
Annie_wang 已提交
7520

A
Annie_wang 已提交
7521 7522 7523
  | 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.|
Z
zengyawen 已提交
7524

A
Annie_wang 已提交
7525
**Example**
A
annie_wangli 已提交
7526

A
Annie_wang 已提交
7527
  ```ts
A
Annie_wang 已提交
7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
  }
  let remoteObject = new TestRemoteObject("aaa");
Z
zengyawen 已提交
7548 7549 7550 7551 7552 7553
  let ret = rpc.IPCSkeleton.flushCommands(remoteObject);
  console.log("RpcServer: flushCommands result: " + ret);
  ```

### resetCallingIdentity

A
Annie_wang 已提交
7554 7555
static resetCallingIdentity(): string

A
Annie_wang 已提交
7556
Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication.
A
Annie_wang 已提交
7557 7558 7559 7560 7561

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
7562 7563 7564
  | Type  | Description                                |
  | ------ | ------------------------------------ |
  | string | String containing the UID and PID of the remote user.|
A
Annie_wang 已提交
7565 7566 7567

**Example**

A
Annie_wang 已提交
7568
  ```ts
A
Annie_wang 已提交
7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581
  class Stub extends rpc.RemoteObject {
      onRemoteMessageRequest(code, data, reply, option) {
          let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
          console.log("RpcServer: callingIdentity is: " + callingIdentity);
          return true;
      }
  }
  ```


### restoreCallingIdentity<sup>9+</sup>

static restoreCallingIdentity(identity: string): void
Z
zengyawen 已提交
7582

A
Annie_wang 已提交
7583
Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication.
Z
zengyawen 已提交
7584

A
annie_wangli 已提交
7585
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7586

A
Annie_wang 已提交
7587
**Parameters**
A
Annie_wang 已提交
7588

A
Annie_wang 已提交
7589 7590 7591
  | Name  | Type  | Mandatory| Description                                                              |
  | -------- | ------ | ---- | ------------------------------------------------------------------ |
  | identity | string | Yes  | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.|
Z
zengyawen 已提交
7592

A
Annie_wang 已提交
7593
**Example**
A
annie_wangli 已提交
7594

A
Annie_wang 已提交
7595
  ```ts
Z
zengyawen 已提交
7596
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7597 7598 7599 7600 7601 7602 7603 7604
      onRemoteMessageRequest(code, data, reply, option) {
          let callingIdentity = null;
          try {
              callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
              console.log("RpcServer: callingIdentity is: " + callingIdentity);
          } finally {
              rpc.IPCSkeleton.restoreCallingIdentity("callingIdentity ");
          }
Z
zengyawen 已提交
7605 7606 7607 7608 7609
          return true;
      }
  }
  ```

A
Annie_wang 已提交
7610 7611 7612
### setCallingIdentity<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [restoreCallingIdentity](#restorecallingidentity9).
Z
zengyawen 已提交
7613

A
Annie_wang 已提交
7614
static setCallingIdentity(identity: string): boolean
Z
zengyawen 已提交
7615

A
Annie_wang 已提交
7616
Sets the UID and PID of the remote user. This API is a static method. It is usually called when the UID and PID of the remote user are required. The UID and PID of the remote user are returned by **resetCallingIdentity**.
Z
zengyawen 已提交
7617

A
annie_wangli 已提交
7618 7619
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7620
**Parameters**
A
Annie_wang 已提交
7621

A
Annie_wang 已提交
7622 7623 7624
  | Name  | Type  | Mandatory| Description                                                              |
  | -------- | ------ | ---- | ------------------------------------------------------------------ |
  | identity | string | Yes  | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.|
Z
zengyawen 已提交
7625

A
Annie_wang 已提交
7626
**Return value**
A
Annie_wang 已提交
7627

A
Annie_wang 已提交
7628 7629
  | Type   | Description                            |
  | ------- | ---------------------------------|
A
Annie_wang 已提交
7630
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
7631

A
Annie_wang 已提交
7632
**Example**
A
annie_wangli 已提交
7633

A
Annie_wang 已提交
7634
  ```ts
Z
zengyawen 已提交
7635
  class Stub extends rpc.RemoteObject {
A
Annie_wang 已提交
7636
      onRemoteMessageRequest(code, data, reply, option) {
Z
zengyawen 已提交
7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657
          let callingIdentity = null;
          try {
              callingIdentity = rpc.IPCSkeleton.resetCallingIdentity();
              console.log("RpcServer: callingIdentity is: " + callingIdentity);
          } finally {
              let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity ");
              console.log("RpcServer: setCallingIdentity is: " + ret);
          }
          return true;
      }
  }
  ```

## RemoteObject

Provides methods to implement **RemoteObject**. The service provider must inherit from this class.

### constructor

constructor(descriptor: string)

A
annie_wangli 已提交
7658 7659 7660
A constructor used to create a **RemoteObject** object.

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7661

A
Annie_wang 已提交
7662
**Parameters**
A
Annie_wang 已提交
7663

A
Annie_wang 已提交
7664 7665 7666
  | Name    | Type  | Mandatory| Description        |
  | ---------- | ------ | ---- | ------------ |
  | descriptor | string | Yes  | Interface descriptor.|
Z
zengyawen 已提交
7667 7668


A
Annie_wang 已提交
7669
### sendRequest<sup>(deprecated)</sup>
Z
zengyawen 已提交
7670

A
Annie_wang 已提交
7671
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
Z
zengyawen 已提交
7672

A
Annie_wang 已提交
7673
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
A
Annie_wang 已提交
7674

A
Annie_wang 已提交
7675
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.
A
annie_wangli 已提交
7676 7677

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7678

A
Annie_wang 已提交
7679
**Parameters**
A
Annie_wang 已提交
7680

A
Annie_wang 已提交
7681 7682 7683
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
7684 7685
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
7686
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
Z
zengyawen 已提交
7687

A
Annie_wang 已提交
7688
**Return value**
A
Annie_wang 已提交
7689

A
Annie_wang 已提交
7690 7691
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
7692
  | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.|
Z
zengyawen 已提交
7693

A
Annie_wang 已提交
7694
**Example**
A
annie_wangli 已提交
7695

A
Annie_wang 已提交
7696
  ```ts
A
Annie_wang 已提交
7697 7698 7699 7700 7701
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
7702 7703 7704 7705
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
7706 7707 7708 7709 7710 7711 7712 7713 7714
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7715 7716 7717 7718 7719 7720 7721
  }
  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");
A
Annie_wang 已提交
7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732
  let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option);
  if (ret) {
      console.log("sendRequest got result");
      let msg = reply.readString();
      console.log("RPCTest: reply msg: " + msg);
  } else {
      console.log("RPCTest: sendRequest failed");
  }
  console.log("RPCTest: sendRequest ends, reclaim parcel");
  data.reclaim();
  reply.reclaim();
Z
zengyawen 已提交
7733 7734
  ```

A
Annie_wang 已提交
7735 7736
### sendRequest<sup>8+(deprecated)</sup>

A
Annie_wang 已提交
7737
>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
A
Annie_wang 已提交
7738

A
Annie_wang 已提交
7739
sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise&lt;SendRequestResult&gt;
A
Annie_wang 已提交
7740

A
Annie_wang 已提交
7741
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.
A
Annie_wang 已提交
7742 7743 7744 7745

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**
A
Annie_wang 已提交
7746

A
Annie_wang 已提交
7747 7748 7749
  | Name | Type                                     | Mandatory| Description                                                                                  |
  | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
7750 7751
  | data    | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
A
Annie_wang 已提交
7752
  | options | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
7753 7754

**Return value**
A
Annie_wang 已提交
7755

A
Annie_wang 已提交
7756 7757 7758
  | Type                            | Description                                         |
  | -------------------------------- | --------------------------------------------- |
  | Promise&lt;SendRequestResult&gt; | Promise used to return the **sendRequestResult** object.|
A
Annie_wang 已提交
7759 7760

**Example**
A
annie_wangli 已提交
7761

A
Annie_wang 已提交
7762
  ```ts
A
Annie_wang 已提交
7763 7764 7765 7766 7767
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
7768 7769 7770 7771
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
7772 7773 7774 7775 7776 7777 7778 7779 7780
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806
  }
  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");
  testRemoteObject.sendRequest(1, data, reply, option)
      .then(function(result) {
          if (result.errCode === 0) {
              console.log("sendRequest got result");
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
              console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
          }
      }).catch(function(e) {
          console.log("RPCTest: sendRequest got exception: " + e.message);
      }).finally (() => {
          console.log("RPCTest: sendRequest ends, reclaim parcel");
          data.reclaim();
          reply.reclaim();
      });
  ```

A
Annie_wang 已提交
7807
### sendMessageRequest<sup>9+</sup>
A
Annie_wang 已提交
7808

A
Annie_wang 已提交
7809
sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise&lt;RequestResult&gt;
A
Annie_wang 已提交
7810

A
Annie_wang 已提交
7811
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.
A
Annie_wang 已提交
7812 7813 7814 7815

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**
A
Annie_wang 已提交
7816

A
Annie_wang 已提交
7817 7818 7819
  | Name | Type                                | Mandatory| Description                                                                                  |
  | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code    | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
7820 7821
  | data    | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
  | reply   | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
A
Annie_wang 已提交
7822
  | options | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
A
Annie_wang 已提交
7823 7824

**Return value**
A
Annie_wang 已提交
7825

A
Annie_wang 已提交
7826 7827 7828
  | Type                        | Description                                         |
  | ---------------------------- | --------------------------------------------- |
  | Promise&lt;RequestResult&gt; | Promise used to return the **sendRequestResult** object.|
A
Annie_wang 已提交
7829 7830 7831

**Example**

A
Annie_wang 已提交
7832
  ```ts
A
Annie_wang 已提交
7833 7834 7835 7836 7837 7838 7839
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  let option = new rpc.MessageOption();
A
Annie_wang 已提交
7840 7841
  let data = rpc.MessageSequence.create();
  let reply = rpc.MessageSequence.create();
A
Annie_wang 已提交
7842 7843
  data.writeInt(1);
  data.writeString("hello");
A
Annie_wang 已提交
7844
  testRemoteObject.sendMessageRequest(1, data, reply, option)
A
Annie_wang 已提交
7845 7846
      .then(function(result) {
          if (result.errCode === 0) {
A
Annie_wang 已提交
7847
              console.log("sendMessageRequest got result");
A
Annie_wang 已提交
7848 7849 7850 7851
              result.reply.readException();
              let msg = result.reply.readString();
              console.log("RPCTest: reply msg: " + msg);
          } else {
A
Annie_wang 已提交
7852
              console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode);
A
Annie_wang 已提交
7853 7854
          }
      }).catch(function(e) {
A
Annie_wang 已提交
7855
          console.log("RPCTest: sendMessageRequest got exception: " + e.message);
A
Annie_wang 已提交
7856
      }).finally (() => {
A
Annie_wang 已提交
7857
          console.log("RPCTest: sendMessageRequest ends, reclaim parcel");
A
Annie_wang 已提交
7858 7859 7860 7861
          data.reclaim();
          reply.reclaim();
      });
  ```
Z
zengyawen 已提交
7862

A
Annie_wang 已提交
7863 7864 7865 7866
### sendMessageRequest<sup>9+</sup>

sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback&lt;RequestResult&gt;): void

A
Annie_wang 已提交
7867
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.
A
Annie_wang 已提交
7868 7869 7870 7871 7872

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
7873 7874 7875
  | Name       | Type                                | Mandatory| Description                                                                                  |
  | ------------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- |
  | code          | number                               | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
A
Annie_wang 已提交
7876 7877
  | data          | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object holding the data to send.                                           |
  | reply         | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that receives the response.                                                   |
A
Annie_wang 已提交
7878 7879
  | options       | [MessageOption](#messageoption)      | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | AsyncCallback | AsyncCallback&lt;RequestResult&gt;   | Yes  | Callback for receiving the sending result.                                                                  |
A
Annie_wang 已提交
7880 7881 7882

**Example**

A
Annie_wang 已提交
7883
  ```ts
A
Annie_wang 已提交
7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
  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, sendRequestCallback);
  ```

### sendRequest<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9).
Z
zengyawen 已提交
7914 7915 7916

sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback&lt;SendRequestResult&gt;): void

A
Annie_wang 已提交
7917
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.
A
annie_wangli 已提交
7918 7919

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
7920

A
Annie_wang 已提交
7921
**Parameters**
A
Annie_wang 已提交
7922

A
Annie_wang 已提交
7923 7924 7925 7926 7927 7928 7929
  | Name       | Type                                     | Mandatory| Description                                                                                  |
  | ------------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- |
  | code          | number                                    | Yes  | Message code called by the request, which is determined by the client and server. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.|
  | data          | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object holding the data to send.                                             |
  | reply         | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that receives the response.                                                     |
  | options       | [MessageOption](#messageoption)           | Yes  | Request sending mode, which can be synchronous (default) or asynchronous.                                                  |
  | AsyncCallback | AsyncCallback&lt;SendRequestResult&gt;    | Yes  | Callback for receiving the sending result.                                                                  |
Z
zengyawen 已提交
7930

A
Annie_wang 已提交
7931
**Example**
A
annie_wangli 已提交
7932

A
Annie_wang 已提交
7933
  ```ts
A
Annie_wang 已提交
7934 7935 7936 7937 7938
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
7939 7940 7941 7942
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
7943 7944 7945 7946 7947 7948 7949 7950 7951
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974
  }
  function sendRequestCallback(result) {
      if (result.errCode === 0) {
          console.log("sendRequest got result");
          result.reply.readException();
          let msg = result.reply.readString();
          console.log("RPCTest: reply msg: " + msg);
      } else {
          console.log("RPCTest: sendRequest failed, errCode: " + result.errCode);
      }
      console.log("RPCTest: sendRequest ends, reclaim parcel");
      result.data.reclaim();
      result.reply.reclaim();
  }
  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");
  testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback);
  ```

A
Annie_wang 已提交
7975 7976
### onRemoteRequest<sup>8+(deprecated)</sup>

A
Annie_wang 已提交
7977
>This API is no longer maintained since API version 9. You are advised to use [onRemoteMessageRequest](#onremotemessagerequest9).
Z
zengyawen 已提交
7978

A
Annie_wang 已提交
7979
onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean
A
Annie_wang 已提交
7980

A
Annie_wang 已提交
7981
Provides a response to **sendMessageRequest()**. The server processes the request and returns a response in this API.
Z
zengyawen 已提交
7982

A
annie_wangli 已提交
7983 7984
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
7985
**Parameters**
A
Annie_wang 已提交
7986

A
Annie_wang 已提交
7987 7988 7989
  | Name| Type                                     | Mandatory| Description                                   |
  | ------ | ----------------------------------------- | ---- | --------------------------------------- |
  | code   | number                                    | Yes  | Service request code sent by the remote end.                 |
A
Annie_wang 已提交
7990 7991
  | data   | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object that holds the parameters called by the client.|
  | reply  | [MessageParcel](#messageparceldeprecated) | Yes  | **MessageParcel** object carrying the result.          |
A
Annie_wang 已提交
7992
  | option | [MessageOption](#messageoption)           | Yes  | Whether the operation is synchronous or asynchronous.               |
Z
zengyawen 已提交
7993

A
Annie_wang 已提交
7994
**Return value**
A
Annie_wang 已提交
7995

A
Annie_wang 已提交
7996 7997
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
7998
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
7999

A
Annie_wang 已提交
8000
**Example**
A
annie_wangli 已提交
8001

A
Annie_wang 已提交
8002
  ```ts
A
Annie_wang 已提交
8003 8004 8005 8006 8007
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8008 8009 8010 8011
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8012 8013 8014 8015 8016 8017 8018 8019 8020
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
              console.log("RpcServer: onRemoteRequest called");
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```
A
Annie_wang 已提交
8032

A
Annie_wang 已提交
8033 8034 8035
### onRemoteMessageRequest<sup>9+</sup>

onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean>
A
Annie_wang 已提交
8036 8037

> **NOTE**<br/>
A
Annie_wang 已提交
8038 8039 8040
>
>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing.
>* If both onRemoteRequest() and onRemoteMessageRequest() are overloaded, only the onRemoteMessageRequest() takes effect.
A
Annie_wang 已提交
8041

A
Annie_wang 已提交
8042
Provides a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API.
A
Annie_wang 已提交
8043 8044 8045 8046 8047

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8048 8049 8050
  | Name| Type                                | Mandatory| Description                                     |
  | ------ | ------------------------------------ | ---- | ----------------------------------------- |
  | code   | number                               | Yes  | Service request code sent by the remote end.                   |
A
Annie_wang 已提交
8051 8052
  | data   | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object that holds the parameters called by the client.|
  | reply  | [MessageSequence](#messagesequence9) | Yes  | **MessageSequence** object to which the result is written.          |
A
Annie_wang 已提交
8053
  | option | [MessageOption](#messageoption)      | Yes  | Whether the operation is synchronous or asynchronous.                 |
A
Annie_wang 已提交
8054 8055 8056

**Return value**

A
Annie_wang 已提交
8057 8058
  | Type             | Description                                                                                           |
  | ----------------- | ----------------------------------------------------------------------------------------------- |
A
Annie_wang 已提交
8059 8060
  | boolean           | Returns a Boolean value if the request is processed synchronously in **onRemoteMessageRequest**. The value **true** means the operation is successful; the value **false** means the opposite.|
  | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**.                                |
A
Annie_wang 已提交
8061

A
Annie_wang 已提交
8062
**Example**: Overload **onRemoteMessageRequest** to process requests synchronously.
A
Annie_wang 已提交
8063

A
Annie_wang 已提交
8064
  ```ts
A
Annie_wang 已提交
8065 8066 8067 8068
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8069 8070

      onRemoteMessageRequest(code, data, reply, option) {
A
Annie_wang 已提交
8071
          if (code === 1) {
A
Annie_wang 已提交
8072
              console.log("RpcServer: sync onRemoteMessageRequest is called");
A
Annie_wang 已提交
8073 8074 8075 8076 8077 8078 8079 8080
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
  }
  ```
A
Annie_wang 已提交
8081 8082

  **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously.
Z
zengyawen 已提交
8083

A
Annie_wang 已提交
8084
  ```ts
A
Annie_wang 已提交
8085 8086 8087 8088
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8089 8090

      async onRemoteMessageRequest(code, data, reply, option) {
A
Annie_wang 已提交
8091
          if (code === 1) {
A
Annie_wang 已提交
8092
              console.log("RpcServer: async onRemoteMessageRequest is called");
A
Annie_wang 已提交
8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
          await new Promise((resolve) => {
            setTimeout(resolve, 100);
          })
          return true;
      }
  }
  ```
A
Annie_wang 已提交
8104 8105

**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously.
Z
zengyawen 已提交
8106

A
Annie_wang 已提交
8107
  ```ts
A
Annie_wang 已提交
8108 8109 8110 8111
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8112

A
Annie_wang 已提交
8113 8114
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
A
Annie_wang 已提交
8115
              console.log("RpcServer: sync onRemoteMessageRequest is called");
A
Annie_wang 已提交
8116 8117 8118 8119 8120 8121
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
A
Annie_wang 已提交
8122 8123
      // Only onRemoteMessageRequest is executed.
      onRemoteMessageRequest(code, data, reply, option) {
A
Annie_wang 已提交
8124
          if (code === 1) {
A
Annie_wang 已提交
8125
              console.log("RpcServer: async onRemoteMessageRequest is called");
A
Annie_wang 已提交
8126 8127 8128 8129
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
A
Annie_wang 已提交
8130
         
A
Annie_wang 已提交
8131 8132 8133 8134
          return true;
      }
  }
  ```
A
Annie_wang 已提交
8135 8136

  **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously.
A
Annie_wang 已提交
8137

A
Annie_wang 已提交
8138
  ```ts
A
Annie_wang 已提交
8139 8140 8141 8142
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8143

A
Annie_wang 已提交
8144 8145
      onRemoteRequest(code, data, reply, option) {
          if (code === 1) {
A
Annie_wang 已提交
8146
              console.log("RpcServer: sync onRemoteRequest is called");
A
Annie_wang 已提交
8147 8148 8149 8150 8151 8152
              return true;
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
      }
A
Annie_wang 已提交
8153 8154
      // Only onRemoteMessageRequest is executed.
      async onRemoteMessageRequest(code, data, reply, option) {
A
Annie_wang 已提交
8155
          if (code === 1) {
A
Annie_wang 已提交
8156
              console.log("RpcServer: async onRemoteMessageRequest is called");
A
Annie_wang 已提交
8157 8158 8159 8160
          } else {
              console.log("RpcServer: unknown code: " + code);
              return false;
          }
A
Annie_wang 已提交
8161
         await new Promise((resolve) => {
A
Annie_wang 已提交
8162 8163 8164 8165 8166 8167
            setTimeout(resolve, 100);
          })
          return true;
      }
  }
  ```
A
Annie_wang 已提交
8168

Z
zengyawen 已提交
8169 8170 8171 8172
### getCallingUid

getCallingUid(): number

A
annie_wangli 已提交
8173
Obtains the UID of the remote process.
Z
zengyawen 已提交
8174

A
annie_wangli 已提交
8175 8176
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8177
**Return value**
A
Annie_wang 已提交
8178 8179 8180
  | Type  | Description                   |
  | ------ | ----------------------- |
  | number | UID of the remote process obtained.|
Z
zengyawen 已提交
8181

A
Annie_wang 已提交
8182 8183
**Example**

A
Annie_wang 已提交
8184
  ```ts
A
Annie_wang 已提交
8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid());
  ```

### getCallingPid

getCallingPid(): number

Obtains the PID of the remote process.

**System capability**: SystemCapability.Communication.IPC.Core

**Return value**

A
Annie_wang 已提交
8204 8205 8206
  | Type  | Description                   |
  | ------ | ----------------------- |
  | number | PID of the remote process obtained.|
A
Annie_wang 已提交
8207 8208 8209

**Example**

A
Annie_wang 已提交
8210
  ```ts
A
Annie_wang 已提交
8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid());
  ```

### getLocalInterface<sup>9+</sup>

getLocalInterface(descriptor: string): IRemoteBroker

A
Annie_wang 已提交
8224
Obtains the interface descriptor.
A
Annie_wang 已提交
8225 8226 8227 8228 8229

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8230 8231 8232
  | Name    | Type  | Mandatory| Description                |
  | ---------- | ------ | ---- | -------------------- |
  | descriptor | string | Yes  | Interface descriptor.|
A
Annie_wang 已提交
8233 8234 8235

**Return value**

A
Annie_wang 已提交
8236 8237 8238
  | Type         | Description                                         |
  | ------------- | --------------------------------------------- |
  | IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.|
A
Annie_wang 已提交
8239 8240 8241 8242


**Example**

A
Annie_wang 已提交
8243
  ```ts
A
Annie_wang 已提交
8244 8245 8246 8247 8248 8249 8250 8251 8252
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8253 8254 8255 8256 8257 8258
      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.
      }
A
Annie_wang 已提交
8259 8260 8261 8262 8263 8264 8265 8266
      isObjectDead(): boolean {
          return false;
      }
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  try {
      let broker = testRemoteObject.getLocalInterface("testObject");
  } catch(error) {
A
Annie_wang 已提交
8267 8268
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
A
Annie_wang 已提交
8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283
  }
  ```

### queryLocalInterface<sup>(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9).

queryLocalInterface(descriptor: string): IRemoteBroker

Checks whether the remote object corresponding to the specified interface token exists.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8284 8285 8286
  | Name    | Type  | Mandatory| Description                  |
  | ---------- | ------ | ---- | ---------------------- |
  | descriptor | string | Yes  | Interface descriptor.|
A
Annie_wang 已提交
8287 8288 8289

**Return value**

A
Annie_wang 已提交
8290 8291 8292
  | Type         | Description                                                              |
  | ------------- | ------------------------------------------------------------------ |
  | IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.|
Z
zengyawen 已提交
8293

A
Annie_wang 已提交
8294
**Example**
A
annie_wangli 已提交
8295

A
Annie_wang 已提交
8296
  ```ts
A
Annie_wang 已提交
8297 8298 8299 8300 8301
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8302 8303 8304 8305
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8306 8307 8308 8309 8310 8311 8312 8313 8314
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8315 8316
  }
  let testRemoteObject = new TestRemoteObject("testObject");
A
Annie_wang 已提交
8317
  let broker = testRemoteObject.queryLocalInterface("testObject");
Z
zengyawen 已提交
8318 8319
  ```

A
Annie_wang 已提交
8320
### getDescriptor<sup>9+</sup>
Z
zengyawen 已提交
8321

A
Annie_wang 已提交
8322
getDescriptor(): string
Z
zengyawen 已提交
8323

A
Annie_wang 已提交
8324
Obtains the interface descriptor of this object. The interface descriptor is a string.
Z
zengyawen 已提交
8325

A
annie_wangli 已提交
8326 8327
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8328
**Return value**
A
Annie_wang 已提交
8329

A
Annie_wang 已提交
8330 8331 8332
  | Type  | Description            |
  | ------ | ---------------- |
  | string | Interface descriptor obtained.|
A
Annie_wang 已提交
8333

A
Annie_wang 已提交
8334
**Error codes**
Z
zengyawen 已提交
8335

A
Annie_wang 已提交
8336 8337
For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8338
  | ID| Error Message|
A
Annie_wang 已提交
8339 8340
  | -------- | -------- |
  | 1900008  | proxy or remote object is invalid |
Z
zengyawen 已提交
8341

A
Annie_wang 已提交
8342
**Example**
A
annie_wangli 已提交
8343

A
Annie_wang 已提交
8344
  ```ts
A
Annie_wang 已提交
8345 8346 8347 8348 8349
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8350 8351 8352 8353
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8354 8355 8356 8357 8358 8359
      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.
      }
A
Annie_wang 已提交
8360 8361 8362
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8363 8364
  }
  let testRemoteObject = new TestRemoteObject("testObject");
A
Annie_wang 已提交
8365
  console.log("RpcServer: descriptor is: " + descriptor);
A
Annie_wang 已提交
8366 8367 8368
  try {
      let descriptor = testRemoteObject.getDescriptor();
  } catch(error) {
A
Annie_wang 已提交
8369 8370
      console.info("rpc get local interface fail, errorCode " + error.code);
      console.info("rpc get local interface fail, errorMessage " + error.message);
A
Annie_wang 已提交
8371
  }
Z
zengyawen 已提交
8372 8373
  ```

A
Annie_wang 已提交
8374
### getInterfaceDescriptor<sup>(deprecated)</sup>
Z
zengyawen 已提交
8375

A
Annie_wang 已提交
8376
>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9).
Z
zengyawen 已提交
8377

A
Annie_wang 已提交
8378
getInterfaceDescriptor(): string
A
annie_wangli 已提交
8379

A
Annie_wang 已提交
8380
Obtains the interface descriptor.
A
Annie_wang 已提交
8381

A
Annie_wang 已提交
8382
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8383

A
Annie_wang 已提交
8384
**Return value**
A
Annie_wang 已提交
8385

A
Annie_wang 已提交
8386 8387 8388
  | Type  | Description            |
  | ------ | ---------------- |
  | string | Interface descriptor obtained.|
Z
zengyawen 已提交
8389

A
Annie_wang 已提交
8390
**Example**
A
annie_wangli 已提交
8391

A
Annie_wang 已提交
8392
  ```ts
A
Annie_wang 已提交
8393 8394 8395 8396 8397
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8398 8399 8400 8401
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
      }
A
Annie_wang 已提交
8402 8403 8404 8405 8406 8407 8408 8409 8410
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
Z
zengyawen 已提交
8411 8412
  }
  let testRemoteObject = new TestRemoteObject("testObject");
A
Annie_wang 已提交
8413 8414
  let descriptor = testRemoteObject.getInterfaceDescriptor();
  console.log("RpcServer: descriptor is: " + descriptor);
Z
zengyawen 已提交
8415 8416
  ```

A
Annie_wang 已提交
8417
### modifyLocalInterface<sup>9+</sup>
Z
zengyawen 已提交
8418

A
Annie_wang 已提交
8419
modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void
Z
zengyawen 已提交
8420

A
Annie_wang 已提交
8421
Binds an interface descriptor to an **IRemoteBroker** object.
Z
zengyawen 已提交
8422

A
annie_wangli 已提交
8423 8424
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8425
**Parameters**
A
Annie_wang 已提交
8426

A
Annie_wang 已提交
8427 8428 8429 8430
  | Name        | Type         | Mandatory| Description                                 |
  | -------------- | ------------- | ---- | ------------------------------------- |
  | localInterface | IRemoteBroker | Yes  | **IRemoteBroker** object.  |
  | descriptor     | string        | Yes  | Interface descriptor.|
Z
zengyawen 已提交
8431

A
Annie_wang 已提交
8432
**Example**
A
annie_wangli 已提交
8433

A
Annie_wang 已提交
8434
  ```ts
A
Annie_wang 已提交
8435 8436 8437 8438 8439
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8440 8441 8442
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
A
Annie_wang 已提交
8443 8444 8445
          try {
              this.modifyLocalInterface(this, descriptor);
          } catch(error) {
A
Annie_wang 已提交
8446 8447
              console.info(" rpc attach local interface fail, errorCode " + error.code);
              console.info(" rpc attach local interface fail, errorMessage " + error.message);
A
Annie_wang 已提交
8448
          }
Z
zengyawen 已提交
8449
      }
A
Annie_wang 已提交
8450 8451 8452 8453 8454 8455
      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.
      }
A
Annie_wang 已提交
8456 8457 8458
      isObjectDead(): boolean {
          return false;
      }
A
Annie_wang 已提交
8459 8460 8461
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
8462 8463 8464 8465
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  ```

A
Annie_wang 已提交
8466
### attachLocalInterface<sup>(deprecated)</sup>
Z
zengyawen 已提交
8467

A
Annie_wang 已提交
8468
>This API is no longer maintained since API version 9. You are advised to use [modifyLocalInterface](#modifylocalinterface9).
Z
zengyawen 已提交
8469 8470 8471 8472 8473

attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void

Binds an interface descriptor to an **IRemoteBroker** object.

A
annie_wangli 已提交
8474 8475
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8476
**Parameters**
A
Annie_wang 已提交
8477

A
Annie_wang 已提交
8478 8479 8480 8481
  | Name        | Type         | Mandatory| Description                                 |
  | -------------- | ------------- | ---- | ------------------------------------- |
  | localInterface | IRemoteBroker | Yes  | **IRemoteBroker** object.  |
  | descriptor     | string        | Yes  | Interface descriptor.|
Z
zengyawen 已提交
8482

A
Annie_wang 已提交
8483
**Example**
A
annie_wangli 已提交
8484

A
Annie_wang 已提交
8485
  ```ts
A
Annie_wang 已提交
8486 8487 8488 8489 8490
  class MyDeathRecipient {
      onRemoteDied() {
          console.log("server died");
      }
  }
Z
zengyawen 已提交
8491 8492 8493 8494 8495
  class TestRemoteObject extends rpc.RemoteObject {
      constructor(descriptor) {
          super(descriptor);
          this.attachLocalInterface(this, descriptor);
      }
A
Annie_wang 已提交
8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507
      addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean {
          return true;
      }
      isObjectDead(): boolean {
          return false;
      }
      asObject(): rpc.IRemoteObject {
          return this;
      }
Z
zengyawen 已提交
8508 8509 8510 8511 8512 8513 8514 8515
  }
  let testRemoteObject = new TestRemoteObject("testObject");
  ```

## Ashmem<sup>8+</sup>

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.

A
annie_wangli 已提交
8516
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8517

A
Annie_wang 已提交
8518 8519
The table below describes the protection types of the mapped memory.

A
Annie_wang 已提交
8520 8521 8522 8523 8524 8525
  | Name      | Value | Description              |
  | ---------- | --- | ------------------ |
  | PROT_EXEC  | 4   | The mapped memory is executable.  |
  | PROT_NONE  | 0   | The mapped memory is inaccessible.|
  | PROT_READ  | 1   | The mapped memory is readable.    |
  | PROT_WRITE | 2   | The mapped memory is writeable.    |
Z
zengyawen 已提交
8526

A
Annie_wang 已提交
8527
### create<sup>9+</sup>
Z
zengyawen 已提交
8528

A
Annie_wang 已提交
8529
static create(name: string, size: number): Ashmem
Z
zengyawen 已提交
8530

A
Annie_wang 已提交
8531
Creates an **Ashmem** object with the specified name and size. This API is a static method.
Z
zengyawen 已提交
8532

A
annie_wangli 已提交
8533 8534
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8535
**Parameters**
A
Annie_wang 已提交
8536

A
Annie_wang 已提交
8537 8538 8539 8540
  | 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.|
Z
zengyawen 已提交
8541

A
Annie_wang 已提交
8542
**Return value**
A
Annie_wang 已提交
8543

A
Annie_wang 已提交
8544 8545 8546
  | Type  | Description                                          |
  | ------ | ---------------------------------------------- |
  | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.|
Z
zengyawen 已提交
8547

A
Annie_wang 已提交
8548 8549
**Example**

A
Annie_wang 已提交
8550
  ```ts
A
Annie_wang 已提交
8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567
  let ashmem;
  try {
      ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  } catch(error) {
      console.info("Rpc creat ashmem fail, errorCode " + error.code);
      console.info("Rpc creat ashmem  fail, errorMessage " + error.message);
  }
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size);
  ```

### createAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [create](#create9).

static createAshmem(name: string, size: number): Ashmem

A
Annie_wang 已提交
8568
Creates an **Ashmem** object with the specified name and size. This API is a static method.
A
Annie_wang 已提交
8569 8570 8571 8572 8573

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8574 8575 8576 8577
  | 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.|
A
Annie_wang 已提交
8578 8579 8580

**Return value**

A
Annie_wang 已提交
8581 8582 8583
  | Type  | Description                                          |
  | ------ | ---------------------------------------------- |
  | Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.|
A
Annie_wang 已提交
8584

A
Annie_wang 已提交
8585
**Example**
A
annie_wangli 已提交
8586

A
Annie_wang 已提交
8587
  ```ts
Z
zengyawen 已提交
8588 8589 8590 8591 8592
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size);
  ```

A
Annie_wang 已提交
8593
### create<sup>9+</sup>
Z
zengyawen 已提交
8594

A
Annie_wang 已提交
8595
static create(ashmem: Ashmem): Ashmem
Z
zengyawen 已提交
8596

A
Annie_wang 已提交
8597
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.
A
annie_wangli 已提交
8598 8599

**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8600

A
Annie_wang 已提交
8601
**Parameters**
A
Annie_wang 已提交
8602

A
Annie_wang 已提交
8603 8604 8605
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | ashmem | Ashmem | Yes  | Existing **Ashmem** object.|
Z
zengyawen 已提交
8606

A
Annie_wang 已提交
8607
**Return value**
A
Annie_wang 已提交
8608

A
Annie_wang 已提交
8609 8610 8611
  | Type  | Description                  |
  | ------ | ---------------------- |
  | Ashmem | **Ashmem** object created.|
Z
zengyawen 已提交
8612 8613


A
Annie_wang 已提交
8614 8615
**Example**

A
Annie_wang 已提交
8616
  ```ts
A
Annie_wang 已提交
8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634
  let ashmem2;
  try {
      let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
      let ashmem2 = rpc.Ashmem.create(ashmem);
  } catch(error) {
      console.info("Rpc creat ashmem from existing fail, errorCode " + error.code);
      console.info("Rpc creat ashmem from existing  fail, errorMessage " + error.message);
  }
  let size = ashmem2.getAshmemSize();
  console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size);
  ```

### createAshmemFromExisting<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [create](#create9).

static createAshmemFromExisting(ashmem: Ashmem): Ashmem

A
Annie_wang 已提交
8635
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.
A
Annie_wang 已提交
8636 8637 8638 8639 8640

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8641 8642 8643
  | Name| Type  | Mandatory| Description                |
  | ------ | ------ | ---- | -------------------- |
  | ashmem | Ashmem | Yes  | Existing **Ashmem** object.|
A
Annie_wang 已提交
8644 8645 8646

**Return value**

A
Annie_wang 已提交
8647 8648 8649
  | Type  | Description                  |
  | ------ | ---------------------- |
  | Ashmem | **Ashmem** object created.|
A
Annie_wang 已提交
8650

A
Annie_wang 已提交
8651
**Example**
A
annie_wangli 已提交
8652

A
Annie_wang 已提交
8653
  ```ts
Z
zengyawen 已提交
8654 8655 8656 8657 8658 8659
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem);
  let size = ashmem2.getAshmemSize();
  console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size);
  ```

A
annie_wangli 已提交
8660
### closeAshmem<sup>8+</sup>
Z
zengyawen 已提交
8661 8662 8663 8664 8665

closeAshmem(): void

Closes this **Ashmem** object.

A
annie_wangli 已提交
8666
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8667

A
Annie_wang 已提交
8668
**Example**
A
annie_wangli 已提交
8669

A
Annie_wang 已提交
8670
  ```ts
A
Annie_wang 已提交
8671
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
Z
zengyawen 已提交
8672 8673 8674
  ashmem.closeAshmem();
  ```

A
annie_wangli 已提交
8675
### unmapAshmem<sup>8+</sup>
Z
zengyawen 已提交
8676 8677 8678 8679 8680

unmapAshmem(): void

Deletes the mappings for the specified address range of this **Ashmem** object.

A
annie_wangli 已提交
8681
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8682

A
Annie_wang 已提交
8683
**Example**
A
annie_wangli 已提交
8684

A
Annie_wang 已提交
8685
  ```ts
A
Annie_wang 已提交
8686
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
Z
zengyawen 已提交
8687 8688 8689
  ashmem.unmapAshmem();
  ```

A
annie_wangli 已提交
8690
### getAshmemSize<sup>8+</sup>
Z
zengyawen 已提交
8691 8692 8693 8694 8695

getAshmemSize(): number

Obtains the memory size of this **Ashmem** object.

A
annie_wangli 已提交
8696
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8697

A
Annie_wang 已提交
8698
**Return value**
A
Annie_wang 已提交
8699

A
Annie_wang 已提交
8700 8701 8702
  | Type  | Description                      |
  | ------ | -------------------------- |
  | number | **Ashmem** size obtained.|
Z
zengyawen 已提交
8703

A
Annie_wang 已提交
8704
**Example**
A
annie_wangli 已提交
8705

A
Annie_wang 已提交
8706
  ```ts
Z
zengyawen 已提交
8707 8708 8709 8710 8711
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let size = ashmem.getAshmemSize();
  console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size);
  ```

A
Annie_wang 已提交
8712 8713 8714 8715 8716 8717 8718 8719 8720 8721
### mapTypedAshmem<sup>9+</sup>

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**

A
Annie_wang 已提交
8722 8723 8724
  | Name | Type  | Mandatory| Description                          |
  | ------- | ------ | ---- | ------------------------------ |
  | mapType | number | Yes  | Protection level of the memory region to which the shared file is mapped.|
A
Annie_wang 已提交
8725

A
Annie_wang 已提交
8726
**Error codes**
A
Annie_wang 已提交
8727 8728 8729

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8730
  | ID| Error Message|
A
Annie_wang 已提交
8731 8732
  | -------- | -------- |
  | 1900001  | call mmap function failed |
A
Annie_wang 已提交
8733 8734 8735

**Example**

A
Annie_wang 已提交
8736
  ```ts
A
Annie_wang 已提交
8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE);
  } catch(error) {
      console.info("Rpc map ashmem fail, errorCode " + error.code);
      console.info("Rpc map ashmem fail, errorMessage " + error.message);
  }
  ```

### mapAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [mapTypedAshmem](#maptypedashmem9).
Z
zengyawen 已提交
8749 8750 8751 8752 8753

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.

A
annie_wangli 已提交
8754
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8755

A
Annie_wang 已提交
8756
**Parameters**
A
Annie_wang 已提交
8757

A
Annie_wang 已提交
8758 8759 8760
  | Name | Type  | Mandatory| Description                          |
  | ------- | ------ | ---- | ------------------------------ |
  | mapType | number | Yes  | Protection level of the memory region to which the shared file is mapped.|
Z
zengyawen 已提交
8761

A
Annie_wang 已提交
8762
**Return value**
A
Annie_wang 已提交
8763

A
Annie_wang 已提交
8764 8765
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
8766
  | boolean | Returns **true** if the mapping is created; returns **false** otherwise.|
Z
zengyawen 已提交
8767

A
Annie_wang 已提交
8768
**Example**
A
annie_wangli 已提交
8769

A
Annie_wang 已提交
8770
  ```ts
Z
zengyawen 已提交
8771
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
A
Annie_wang 已提交
8772
  let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE);
Z
zengyawen 已提交
8773 8774 8775
  console.log("RpcTest: map ashmem result is  : " + mapReadAndWrite);
  ```

A
Annie_wang 已提交
8776 8777 8778 8779 8780 8781 8782 8783
### mapReadWriteAshmem<sup>9+</sup>

mapReadWriteAshmem(): void

Maps the shared file to the readable and writable virtual address space of the process.

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8784
**Error codes**
A
Annie_wang 已提交
8785 8786 8787

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8788
  | ID| Error Message|
A
Annie_wang 已提交
8789 8790
  | -------- | -------- |
  | 1900001  | call mmap function failed |
A
Annie_wang 已提交
8791 8792 8793

**Example**

A
Annie_wang 已提交
8794
  ```ts
A
Annie_wang 已提交
8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapReadWriteAshmem();
  } catch(error) {
      console.info("Rpc map read and write ashmem fail, errorCode " + error.code);
      console.info("Rpc map read and write ashmem fail, errorMessage " + error.message);
  }
  ```

### mapReadAndWriteAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [mapReadWriteAshmem](#mapreadwriteashmem9).
Z
zengyawen 已提交
8807 8808 8809 8810 8811

mapReadAndWriteAshmem(): boolean

Maps the shared file to the readable and writable virtual address space of the process.

A
annie_wangli 已提交
8812
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8813

A
Annie_wang 已提交
8814
**Return value**
A
Annie_wang 已提交
8815

A
Annie_wang 已提交
8816 8817
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
8818
  | boolean | Returns **true** if the mapping is created; returns **false** otherwise.|
Z
zengyawen 已提交
8819

A
Annie_wang 已提交
8820
**Example**
A
annie_wangli 已提交
8821

A
Annie_wang 已提交
8822
  ```ts
Z
zengyawen 已提交
8823 8824 8825 8826 8827
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.log("RpcTest: map ashmem result is  : " + mapResult);
  ```

A
Annie_wang 已提交
8828 8829 8830 8831 8832 8833 8834 8835
### mapReadonlyAshmem<sup>9+</sup>

mapReadonlyAshmem(): void

Maps the shared file to the read-only virtual address space of the process.

**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
8836
**Error codes**
A
Annie_wang 已提交
8837 8838 8839

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8840
  | ID| Error Message|
A
Annie_wang 已提交
8841 8842
  | -------- | -------- |
  | 1900001  | call mmap function failed |
A
Annie_wang 已提交
8843 8844 8845

**Example**

A
Annie_wang 已提交
8846
  ```ts
A
Annie_wang 已提交
8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.mapReadonlyAshmem();
  } catch(error) {
      console.info("Rpc map read and write ashmem fail, errorCode " + error.code);
      console.info("Rpc map read and write ashmem fail, errorMessage " + error.message);
  }
  ```

### mapReadOnlyAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [mapReadonlyAshmem](#mapreadonlyashmem9).
Z
zengyawen 已提交
8859 8860 8861 8862 8863

mapReadOnlyAshmem(): boolean

Maps the shared file to the read-only virtual address space of the process.

A
annie_wangli 已提交
8864
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8865

A
Annie_wang 已提交
8866
**Return value**
A
Annie_wang 已提交
8867

A
Annie_wang 已提交
8868 8869
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
8870
  | boolean | Returns **true** if the mapping is created; returns **false** otherwise.|
Z
zengyawen 已提交
8871

A
Annie_wang 已提交
8872
**Example**
A
annie_wangli 已提交
8873

A
Annie_wang 已提交
8874
  ```ts
Z
zengyawen 已提交
8875 8876 8877 8878 8879
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
  let mapResult = ashmem.mapReadOnlyAshmem();
  console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult);
  ```

A
Annie_wang 已提交
8880 8881 8882 8883 8884 8885 8886 8887 8888 8889
### setProtectionType<sup>9+</sup>

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**

A
Annie_wang 已提交
8890 8891 8892
  | Name        | Type  | Mandatory| Description              |
  | -------------- | ------ | ---- | ------------------ |
  | protectionType | number | Yes  | Protection type to set.|
A
Annie_wang 已提交
8893

A
Annie_wang 已提交
8894
**Error codes**
A
Annie_wang 已提交
8895 8896 8897

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8898
  | ID| Error Message|
A
Annie_wang 已提交
8899 8900
  | -------- | -------- |
  | 1900002  | call os ioctl function failed |
A
Annie_wang 已提交
8901 8902 8903

**Example**

A
Annie_wang 已提交
8904
  ```ts
A
Annie_wang 已提交
8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  try {
      ashmem.setProtection(ashmem.PROT_READ);
  } catch(error) {
      console.info("Rpc set protection type fail, errorCode " + error.code);
      console.info("Rpc set protection type fail, errorMessage " + error.message);
  }
  ```

### setProtection<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [setProtectionType](#setprotectiontype9).
Z
zengyawen 已提交
8917 8918 8919 8920 8921

setProtection(protectionType: number): boolean

Sets the protection level of the memory region to which the shared file is mapped.

A
annie_wangli 已提交
8922
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8923

A
Annie_wang 已提交
8924
**Parameters**
A
Annie_wang 已提交
8925

A
Annie_wang 已提交
8926 8927 8928
  | Name        | Type  | Mandatory| Description              |
  | -------------- | ------ | ---- | ------------------ |
  | protectionType | number | Yes  | Protection type to set.|
Z
zengyawen 已提交
8929

A
Annie_wang 已提交
8930
**Return value**
A
Annie_wang 已提交
8931

A
Annie_wang 已提交
8932 8933
  | Type   | Description                            |
  | ------- | -------------------------------- |
A
Annie_wang 已提交
8934
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
8935

A
Annie_wang 已提交
8936
**Example**
A
annie_wangli 已提交
8937

A
Annie_wang 已提交
8938
  ```ts
Z
zengyawen 已提交
8939
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
A
Annie_wang 已提交
8940
  let result = ashmem.setProtection(ashmem.PROT_READ);
Z
zengyawen 已提交
8941 8942 8943
  console.log("RpcTest: Ashmem setProtection result is : " + result);
  ```

A
Annie_wang 已提交
8944 8945 8946 8947 8948 8949 8950 8951 8952 8953
### writeAshmem<sup>9+</sup>

writeAshmem(buf: number[], size: number, offset: number): void

Writes data to the shared file associated with this **Ashmem** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
8954 8955 8956 8957 8958
  | 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.|
A
Annie_wang 已提交
8959

A
Annie_wang 已提交
8960
**Error codes**
A
Annie_wang 已提交
8961 8962 8963

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
8964
  | ID| Error Message|
A
Annie_wang 已提交
8965 8966
  | -------- | -------- |
  | 1900003  | write to ashmem failed |
A
Annie_wang 已提交
8967 8968 8969

**Example**

A
Annie_wang 已提交
8970
  ```ts
A
Annie_wang 已提交
8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  ashmem.mapReadWriteAshmem();
  var ByteArrayVar = [1, 2, 3, 4, 5];
  try {
      ashmem.writeAshmem(ByteArrayVar, 5, 0);
  } catch(error) {
      console.info("Rpc write to ashmem fail, errorCode " + error.code);
      console.info("Rpc write to ashmem fail, errorMessage " + error.message);
  }
  ```

### writeToAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [writeAshmem](#writeashmem9).
Z
zengyawen 已提交
8985 8986 8987 8988 8989

writeToAshmem(buf: number[], size: number, offset: number): boolean

Writes data to the shared file associated with this **Ashmem** object.

A
annie_wangli 已提交
8990
**System capability**: SystemCapability.Communication.IPC.Core
Z
zengyawen 已提交
8991

A
Annie_wang 已提交
8992
**Parameters**
A
Annie_wang 已提交
8993

A
Annie_wang 已提交
8994 8995 8996 8997 8998
  | 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.|
Z
zengyawen 已提交
8999

A
Annie_wang 已提交
9000
**Return value**
A
Annie_wang 已提交
9001

A
Annie_wang 已提交
9002 9003
  | Type   | Description                                                                         |
  | ------- | ----------------------------------------------------------------------------- |
A
Annie_wang 已提交
9004
  | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.|
Z
zengyawen 已提交
9005

A
Annie_wang 已提交
9006
**Example**
A
annie_wangli 已提交
9007

A
Annie_wang 已提交
9008
  ```ts
Z
zengyawen 已提交
9009
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
A
Annie_wang 已提交
9010 9011 9012
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.info("RpcTest map ashmem result is " + mapResult);
  var ByteArrayVar = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
9013 9014 9015 9016
  let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
  console.log("RpcTest: write to Ashmem result is  : " + writeResult);
  ```

A
Annie_wang 已提交
9017
### readAshmem<sup>9+</sup>
Z
zengyawen 已提交
9018

A
Annie_wang 已提交
9019
readAshmem(size: number, offset: number): number[]
Z
zengyawen 已提交
9020 9021 9022

Reads data from the shared file associated with this **Ashmem** object.

A
annie_wangli 已提交
9023 9024
**System capability**: SystemCapability.Communication.IPC.Core

A
Annie_wang 已提交
9025
**Parameters**
A
Annie_wang 已提交
9026

A
Annie_wang 已提交
9027 9028 9029 9030
  | 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.|
Z
zengyawen 已提交
9031

A
Annie_wang 已提交
9032
**Return value**
A
Annie_wang 已提交
9033

A
Annie_wang 已提交
9034 9035 9036
  | Type    | Description            |
  | -------- | ---------------- |
  | number[] | Data read.|
Z
zengyawen 已提交
9037

A
Annie_wang 已提交
9038
**Error codes**
A
Annie_wang 已提交
9039 9040 9041

For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md).

A
Annie_wang 已提交
9042
  | ID| Error Message|
A
Annie_wang 已提交
9043
  | -------- | -------- |
A
Annie_wang 已提交
9044
  | 1900004  | read from ashmem failed |
Z
zengyawen 已提交
9045

A
Annie_wang 已提交
9046
**Example**
A
annie_wangli 已提交
9047

A
Annie_wang 已提交
9048
  ```ts
A
Annie_wang 已提交
9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073
  let ashmem = rpc.Ashmem.create("ashmem", 1024*1024);
  ashmem.mapReadWriteAshmem();
  var ByteArrayVar = [1, 2, 3, 4, 5];
  ashmem.writeAshmem(ByteArrayVar, 5, 0);
  try {
      let readResult = ashmem.readAshmem(5, 0);
      console.log("RpcTest: read from Ashmem result is  : " + readResult);
  } catch(error) {
      console.info("Rpc read from ashmem fail, errorCode " + error.code);
      console.info("Rpc read from ashmem fail, errorMessage " + error.message);
  }
  ```

### readFromAshmem<sup>8+(deprecated)</sup>

>This API is no longer maintained since API version 9. You are advised to use [readAshmem](#readashmem9).

readFromAshmem(size: number, offset: number): number[]

Reads data from the shared file associated with this **Ashmem** object.

**System capability**: SystemCapability.Communication.IPC.Core

**Parameters**

A
Annie_wang 已提交
9074 9075 9076 9077
  | 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.|
A
Annie_wang 已提交
9078 9079 9080

**Return value**

A
Annie_wang 已提交
9081 9082 9083
  | Type    | Description            |
  | -------- | ---------------- |
  | number[] | Data read.|
A
Annie_wang 已提交
9084 9085 9086

**Example**

A
Annie_wang 已提交
9087
 ``` ts
Z
zengyawen 已提交
9088
  let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024);
A
Annie_wang 已提交
9089 9090 9091
  let mapResult = ashmem.mapReadAndWriteAshmem();
  console.info("RpcTest map ashmem result is " + mapResult);
  var ByteArrayVar = [1, 2, 3, 4, 5];
Z
zengyawen 已提交
9092 9093 9094 9095
  let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0);
  console.log("RpcTest: write to Ashmem result is  : " + writeResult);
  let readResult = ashmem.readFromAshmem(5, 0);
  console.log("RpcTest: read to Ashmem result is  : " + readResult);
A
Annie_wang 已提交
9096
 ```
A
Annie_wang 已提交
9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129

## Obtaining the Context

**Example**

 ```ts
  import Ability from '@ohos.app.ability.UIAbility';
  export default class MainAbility extends Ability {
    onCreate(want, launchParam) {
        console.log("[Demo] MainAbility onCreate");
        globalThis.context = this.context;
    }
    onDestroy() {
        console.log("[Demo] MainAbility onDestroy");
    }
    onWindowStageCreate(windowStage) {
        // Main window is created, set main page for this ability
        console.log("[Demo] MainAbility onWindowStageCreate");
    }
    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        console.log("[Demo] MainAbility onWindowStageDestroy");
    }
    onForeground() {
        // Ability has brought to foreground
        console.log("[Demo] MainAbility onForeground");
    }
    onBackground() {
        // Ability has back to background
        console.log("[Demo] MainAbility onBackground");
    }
  };
 ```