1# @ohos.rpc (RPC) 2<!--Kit: IPC Kit--> 3<!--Subsystem: Communication--> 4<!--Owner: @xdx19211@luodonghui0157--> 5<!--SE: @zhaopeng_gitee--> 6<!--TSE: @maxiaorong2--> 7 8The **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. 9 10> **NOTE** 11> 12> - 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. 13> 14> - This module supports return of error codes since API version 9. 15 16## Modules to Import 17 18``` 19import { rpc } from '@kit.IPCKit'; 20``` 21 22## ErrorCode<sup>9+</sup> 23 24The APIs of this module return exceptions since API version 9. The following table lists the error codes. 25 26**System capability**: SystemCapability.Communication.IPC.Core 27 28 | Name | Value | Description | 29 | ------------------------------------- | ------- | --------------------------------------------- | 30 | CHECK_PARAM_ERROR | 401 | Parameter check failed. | 31 | OS_MMAP_ERROR | 1900001 | Failed to call mmap. | 32 | OS_IOCTL_ERROR | 1900002 | Failed to call **ioctl** with the shared memory file descriptor.| 33 | WRITE_TO_ASHMEM_ERROR | 1900003 | Failed to write data to the shared memory. | 34 | READ_FROM_ASHMEM_ERROR | 1900004 | Failed to read data from the shared memory. | 35 | ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | This operation is allowed only on the proxy object. | 36 | ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | This operation is allowed only on the remote object. | 37 | COMMUNICATION_ERROR | 1900007 | Failed to communicate with the remote object over IPC. | 38 | PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | Invalid proxy or remote object. | 39 | WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | Failed to write data to MessageSequence. | 40 | READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | Failed to read data from MessageSequence. | 41 | PARCEL_MEMORY_ALLOC_ERROR | 1900011 | Failed to allocate memory during serialization. | 42 | CALL_JS_METHOD_ERROR | 1900012 | Failed to invoke the JS callback. | 43 | OS_DUP_ERROR | 1900013 | Failed to call dup. | 44 45 46## TypeCode<sup>12+</sup> 47 48Since API version 12, [writeArrayBuffer](#writearraybuffer12) and [readArrayBuffer](#readarraybuffer12) are added to pass ArrayBuffer data. The specific TypedArray type is determined by the **TypeCode** defined as follows: 49 50**System capability**: SystemCapability.Communication.IPC.Core 51 52 | Name | Value | Description | 53 | ---------------------------- | ------ | -------------------------------------------- | 54 | INT8_ARRAY | 0 | The TypedArray type is **INT8_ARRAY**. | 55 | UINT8_ARRAY | 1 | The TypedArray type is **UINT8_ARRAY**. | 56 | INT16_ARRAY | 2 | The TypedArray type is **INT16_ARRAY**. | 57 | UINT16_ARRAY | 3 | The TypedArray type is **UINT16_ARRAY**. | 58 | INT32_ARRAY | 4 | The TypedArray type is **INT32_ARRAY**. | 59 | UINT32_ARRAY | 5 | The TypedArray type is **UINT32_ARRAY**. | 60 | FLOAT32_ARRAY | 6 | The TypedArray type is **FLOAT32_ARRAY**. | 61 | FLOAT64_ARRAY | 7 | The TypedArray type is **FLOAT64_ARRAY**. | 62 | BIGINT64_ARRAY | 8 | The TypedArray type is **BIGINT64_ARRAY**. | 63 | BIGUINT64_ARRAY | 9 | The TypedArray type is **BIGUINT64_ARRAY**. | 64 65 66## MessageSequence<sup>9+</sup> 67 68 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. 69 70### create 71 72 static create(): MessageSequence 73 74 Creates a **MessageSequence** object. This API is a static method. 75 76**System capability**: SystemCapability.Communication.IPC.Core 77 78**Return value** 79 80| Type | Description | 81| --------------- | ------------------------------- | 82| [MessageSequence](#messagesequence9) | **MessageSequence** object created.| 83 84**Example** 85 86 ```ts 87 import { hilog } from '@kit.PerformanceAnalysisKit'; 88 89 let data = rpc.MessageSequence.create(); 90 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 91 92 // When the MessageSequence object is no longer used, the service calls the reclaim method to release resources. 93 data.reclaim(); 94 ``` 95 96### reclaim 97 98reclaim(): void 99 100Reclaims the **MessageSequence** object that is no longer used. 101 102**System capability**: SystemCapability.Communication.IPC.Core 103 104**Example** 105 106 ```ts 107 let reply = rpc.MessageSequence.create(); 108 reply.reclaim(); 109 ``` 110 111### writeRemoteObject 112 113writeRemoteObject(object: IRemoteObject): void 114 115Serializes the remote object and writes it to the [MessageSequence](#messagesequence9) object. 116 117**System capability**: SystemCapability.Communication.IPC.Core 118 119**Parameters** 120 121 | Name| Type | Mandatory| Description | 122 | ------ | ------------------------------- | ---- | ----------------------------------------- | 123 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 124 125**Error codes** 126 127For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 128 129 | ID| Error Message| 130 | -------- | -------- | 131 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 132 | 1900008 | The proxy or remote object is invalid. | 133 | 1900009 | Failed to write data to the message sequence. | 134 135**Example** 136 137 ```ts 138 import { hilog } from '@kit.PerformanceAnalysisKit'; 139 import { BusinessError } from '@kit.BasicServicesKit'; 140 141 class TestRemoteObject extends rpc.RemoteObject { 142 constructor(descriptor: string) { 143 super(descriptor); 144 } 145 } 146 let data = rpc.MessageSequence.create(); 147 let testRemoteObject = new TestRemoteObject("testObject"); 148 try { 149 data.writeRemoteObject(testRemoteObject); 150 } catch (error) { 151 let e: BusinessError = error as BusinessError; 152 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 153 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 154 } 155 ``` 156 157### readRemoteObject 158 159readRemoteObject(): IRemoteObject 160 161Reads 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. 162 163**System capability**: SystemCapability.Communication.IPC.Core 164 165**Return value** 166 167 | Type | Description | 168 | ------------------------------- | ------------------ | 169 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 170 171**Error codes** 172 173For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 174 175 | ID| Error Message| 176 | -------- | -------- | 177 | 1900008 | The proxy or remote object is invalid. | 178 | 1900010 | Failed to read data from the message sequence. | 179 180**Example** 181 182 ```ts 183 import { hilog } from '@kit.PerformanceAnalysisKit'; 184 import { BusinessError } from '@kit.BasicServicesKit'; 185 186 class TestRemoteObject extends rpc.RemoteObject { 187 constructor(descriptor: string) { 188 super(descriptor); 189 } 190 } 191 let data = rpc.MessageSequence.create(); 192 let testRemoteObject = new TestRemoteObject("testObject"); 193 try { 194 data.writeRemoteObject(testRemoteObject); 195 let proxy = data.readRemoteObject(); 196 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 197 } catch (error) { 198 let e: BusinessError = error as BusinessError; 199 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 200 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 201 } 202 ``` 203 204### writeInterfaceToken 205 206writeInterfaceToken(token: string): void 207 208Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 209 210**System capability**: SystemCapability.Communication.IPC.Core 211 212**Parameters** 213 214 | Name| Type | Mandatory| Description | 215 | ------ | ------ | ---- | ------------------ | 216 | token | string | Yes | Interface token to write.| 217 218**Error codes** 219 220For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 221 222 | ID| Error Message| 223 | -------- | -------- | 224 | 401 | Parameter error. Possible causes:<br> 1.The number of parameters is incorrect;<br> 2.The parameter type does not match;<br> 3.The string length exceeds 40960 bytes;<br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 225 | 1900009 | Failed to write data to the message sequence. | 226 227**Example** 228 229 ```ts 230 import { hilog } from '@kit.PerformanceAnalysisKit'; 231 import { BusinessError } from '@kit.BasicServicesKit'; 232 233 let data = rpc.MessageSequence.create(); 234 try { 235 data.writeInterfaceToken("aaa"); 236 } catch (error) { 237 let e: BusinessError = error as BusinessError; 238 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 239 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 240 } 241 ``` 242 243### readInterfaceToken 244 245readInterfaceToken(): string 246 247Reads 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. 248 249**System capability**: SystemCapability.Communication.IPC.Core 250 251**Return value** 252 253 | Type | Description | 254 | ------ | ------------------------ | 255 | string | Interface token obtained.| 256 257**Error codes** 258 259For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 260 261 | ID| Error Message| 262 | -------- | -------- | 263 | 1900010 | Failed to read data from the message sequence. | 264 265**Example** 266 267```ts 268import { hilog } from '@kit.PerformanceAnalysisKit'; 269import { BusinessError } from '@kit.BasicServicesKit'; 270 271class Stub extends rpc.RemoteObject { 272 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 273 try { 274 let interfaceToken = data.readInterfaceToken(); 275 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 276 } catch (error) { 277 let e: BusinessError = error as BusinessError; 278 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 279 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 280 } 281 return true; 282 } 283} 284``` 285 286### getSize 287 288getSize(): number 289 290Obtains the data size of this **MessageSequence** object. 291 292**System capability**: SystemCapability.Communication.IPC.Core 293 294**Return value** 295 296 | Type | Description | 297 | ------ | ----------------------------------------------- | 298 | number | Size of the **MessageSequence** instance obtained, in bytes.| 299 300**Example** 301 302 ```ts 303 import { hilog } from '@kit.PerformanceAnalysisKit'; 304 305 let data = rpc.MessageSequence.create(); 306 let size = data.getSize(); 307 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 308 ``` 309 310### getCapacity 311 312getCapacity(): number 313 314Obtains the capacity of this **MessageSequence** object. 315 316**System capability**: SystemCapability.Communication.IPC.Core 317 318**Return value** 319 320 | Type | Description | 321 | ------ | ----- | 322 | number | Capacity of the obtained **MessageSequence** object, in bytes.| 323 324**Example** 325 326 ```ts 327 import { hilog } from '@kit.PerformanceAnalysisKit'; 328 329 let data = rpc.MessageSequence.create(); 330 let result = data.getCapacity(); 331 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 332 ``` 333 334### setSize 335 336setSize(size: number): void 337 338Sets the size of the data contained in this **MessageSequence** object. 339 340**System capability**: SystemCapability.Communication.IPC.Core 341 342**Parameters** 343 344 | Name| Type | Mandatory| Description | 345 | ------ | ------ | ---- | ------ | 346 | size | number | Yes | Data size to set, in bytes.| 347 348**Error codes** 349 350For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 351 352 | ID| Error Message| 353 | -------- | -------- | 354 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 355 | 1900009 | Failed to write data to the message sequence. | 356 357**Example** 358 359 ```ts 360 import { hilog } from '@kit.PerformanceAnalysisKit'; 361 import { BusinessError } from '@kit.BasicServicesKit'; 362 363 let data = rpc.MessageSequence.create(); 364 data.writeString('Hello World'); 365 try { 366 data.setSize(16); 367 } catch (error) { 368 let e: BusinessError = error as BusinessError; 369 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 370 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 371 } 372 ``` 373 374### setCapacity 375 376setCapacity(size: number): void 377 378Sets the storage capacity of this **MessageSequence** object. 379 380**System capability**: SystemCapability.Communication.IPC.Core 381 382**Parameters** 383 384 | Name| Type | Mandatory| Description | 385 | ------ | ------ | ---- | --------------------------------------------- | 386 | size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 387 388**Error codes** 389 390For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 391 392 | ID| Error Message| 393 | -------- | -------- | 394 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 395 | 1900009 | Failed to write data to the message sequence. | 396 | 1900011 | Memory allocation failed. | 397 398**Example** 399 400 ```ts 401 import { hilog } from '@kit.PerformanceAnalysisKit'; 402 import { BusinessError } from '@kit.BasicServicesKit'; 403 404 let data = rpc.MessageSequence.create(); 405 try { 406 data.setCapacity(100); 407 } catch (error) { 408 let e: BusinessError = error as BusinessError; 409 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 410 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 411 } 412 ``` 413 414### getWritableBytes 415 416getWritableBytes(): number 417 418Obtains the writable capacity (in bytes) of this **MessageSequence** object. 419 420**System capability**: SystemCapability.Communication.IPC.Core 421 422**Return value** 423 424 | Type | Description | 425 | ------ | ------ | 426 | number | Writable capacity of the **MessageSequence** instance, in bytes.| 427 428**Example** 429 430```ts 431import { hilog } from '@kit.PerformanceAnalysisKit'; 432 433class Stub extends rpc.RemoteObject { 434 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 435 let getWritableBytes = data.getWritableBytes(); 436 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 437 return true; 438 } 439} 440``` 441 442### getReadableBytes 443 444getReadableBytes(): number 445 446Obtains the readable capacity of this **MessageSequence** object. 447 448**System capability**: SystemCapability.Communication.IPC.Core 449 450**Return value** 451 452 | Type | Description | 453 | ------ | ------- | 454 | number | Readable capacity of the **MessageSequence** instance, in bytes.| 455 456**Example** 457 458```ts 459import { hilog } from '@kit.PerformanceAnalysisKit'; 460 461class Stub extends rpc.RemoteObject { 462 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 463 let result = data.getReadableBytes(); 464 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 465 return true; 466 } 467} 468``` 469 470### getReadPosition 471 472getReadPosition(): number 473 474Obtains the read position of this **MessageSequence** object. 475 476**System capability**: SystemCapability.Communication.IPC.Core 477 478**Return value** 479 480 | Type | Description | 481 | ------ | ------ | 482 | number | Read position obtained.| 483 484**Example** 485 486 ```ts 487 import { hilog } from '@kit.PerformanceAnalysisKit'; 488 489 let data = rpc.MessageSequence.create(); 490 let readPos = data.getReadPosition(); 491 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 492 ``` 493 494### getWritePosition 495 496getWritePosition(): number 497 498Obtains the write position of this **MessageSequence** object. 499 500**System capability**: SystemCapability.Communication.IPC.Core 501 502**Return value** 503 504 | Type | Description | 505 | ------ | ----- | 506 | number | Write position obtained.| 507 508**Example** 509 510 ```ts 511 import { hilog } from '@kit.PerformanceAnalysisKit'; 512 513 let data = rpc.MessageSequence.create(); 514 data.writeInt(10); 515 let bwPos = data.getWritePosition(); 516 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 517 ``` 518 519### rewindRead 520 521rewindRead(pos: number): void 522 523Moves the read pointer to the specified position. 524 525**System capability**: SystemCapability.Communication.IPC.Core 526 527**Parameters** 528 529 | Name| Type | Mandatory| Description | 530 | ------ | ------ | ---- | ------- | 531 | pos | number | Yes | Position from which data is to read.| 532 533**Error codes** 534 535For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 536 537 | ID| Error Message| 538 | -------- | -------- | 539 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 540 | 1900010 | Failed to read data from the message sequence. | 541 542**Example** 543 544 ```ts 545 import { hilog } from '@kit.PerformanceAnalysisKit'; 546 import { BusinessError } from '@kit.BasicServicesKit'; 547 548 let data = rpc.MessageSequence.create(); 549 data.writeInt(12); 550 data.writeString("sequence"); 551 let number = data.readInt(); 552 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 553 try { 554 data.rewindRead(0); 555 } catch (error) { 556 let e: BusinessError = error as BusinessError; 557 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 558 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 559 } 560 let number2 = data.readInt(); 561 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 562 ``` 563 564### rewindWrite 565 566rewindWrite(pos: number): void 567 568Moves the write pointer to the specified position. 569 570**System capability**: SystemCapability.Communication.IPC.Core 571 572**Parameters** 573 574 | Name| Type | Mandatory| Description | 575 | ------ | ------ | ---- | ----- | 576 | pos | number | Yes | Position from which data is to write.| 577 578**Error codes** 579 580For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 581 582 | ID| Error Message| 583 | -------- | -------- | 584 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 585 | 1900009 | Failed to write data to the message sequence. | 586 587**Example** 588 589 ```ts 590 import { hilog } from '@kit.PerformanceAnalysisKit'; 591 import { BusinessError } from '@kit.BasicServicesKit'; 592 593 let data = rpc.MessageSequence.create(); 594 data.writeInt(4); 595 try { 596 data.rewindWrite(0); 597 } catch (error) { 598 let e: BusinessError = error as BusinessError; 599 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 600 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 601 } 602 data.writeInt(5); 603 let number = data.readInt(); 604 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 605 ``` 606 607### writeByte 608 609writeByte(val: number): void 610 611Writes a byte value to this **MessageSequence** object. 612 613**System capability**: SystemCapability.Communication.IPC.Core 614 615**Parameters** 616 617 | Name| Type | Mandatory| Description | 618 | ------ | ------ | ---- | ----- | 619 | val | number | Yes | Byte value to write.| 620 621**Error codes** 622 623For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 624 625 | ID| Error Message| 626 | -------- | ------- | 627 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 628 | 1900009 | Failed to write data to the message sequence. | 629 630**Example** 631 632 ```ts 633 import { hilog } from '@kit.PerformanceAnalysisKit'; 634 import { BusinessError } from '@kit.BasicServicesKit'; 635 636 let data = rpc.MessageSequence.create(); 637 try { 638 data.writeByte(2); 639 } catch (error) { 640 let e: BusinessError = error as BusinessError; 641 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 642 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 643 } 644 ``` 645 646### readByte 647 648readByte(): number 649 650Reads the byte value from this **MessageSequence** object. 651 652**System capability**: SystemCapability.Communication.IPC.Core 653 654**Return value** 655 656 | Type | Description | 657 | ------ | ----- | 658 | number | Byte value read.| 659 660**Error codes** 661 662For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 663 664 | ID| Error Message| 665 | ------- | -------- | 666 | 1900010 | Failed to read data from the message sequence. | 667 668**Example** 669 670 ```ts 671 import { hilog } from '@kit.PerformanceAnalysisKit'; 672 import { BusinessError } from '@kit.BasicServicesKit'; 673 674 let data = rpc.MessageSequence.create(); 675 try { 676 data.writeByte(2); 677 } catch (error) { 678 let e: BusinessError = error as BusinessError; 679 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 680 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 681 } 682 try { 683 let ret = data.readByte(); 684 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 685 } catch (error) { 686 let e: BusinessError = error as BusinessError; 687 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 688 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 689 } 690 ``` 691 692### writeShort 693 694writeShort(val: number): void 695 696Writes a short integer to this **MessageSequence** object. 697 698**System capability**: SystemCapability.Communication.IPC.Core 699 700**Parameters** 701 702 | Name| Type | Mandatory| Description| 703 | ------ | ------ | --- | --- | 704 | val | number | Yes | Short integer to write.| 705 706**Error codes** 707 708For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 709 710 | ID| Error Message| 711 | -------- | -------- | 712 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 713 | 1900009 | Failed to write data to the message sequence. | 714 715**Example** 716 717 ```ts 718 import { hilog } from '@kit.PerformanceAnalysisKit'; 719 import { BusinessError } from '@kit.BasicServicesKit'; 720 721 let data = rpc.MessageSequence.create(); 722 try { 723 data.writeShort(8); 724 } catch (error) { 725 let e: BusinessError = error as BusinessError; 726 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 727 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 728 } 729 ``` 730 731### readShort 732 733readShort(): number 734 735Reads the short integer from this **MessageSequence** object. 736 737**System capability**: SystemCapability.Communication.IPC.Core 738 739**Return value** 740 741 | Type | Description | 742 | ------ | -------------- | 743 | number | Short integer read.| 744 745**Error codes** 746 747For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 748 749 | ID| Error Message| 750 | -------- | -------- | 751 | 1900010 | Failed to read data from the message sequence. | 752 753**Example** 754 755 ```ts 756 import { hilog } from '@kit.PerformanceAnalysisKit'; 757 import { BusinessError } from '@kit.BasicServicesKit'; 758 759 let data = rpc.MessageSequence.create(); 760 try { 761 data.writeShort(8); 762 } catch (error) { 763 let e: BusinessError = error as BusinessError; 764 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 765 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 766 } 767 try { 768 let ret = data.readShort(); 769 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 770 } catch (error) { 771 let e: BusinessError = error as BusinessError; 772 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 773 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 774 } 775 ``` 776 777### writeInt 778 779writeInt(val: number): void 780 781Writes an integer to this **MessageSequence** object. 782 783**System capability**: SystemCapability.Communication.IPC.Core 784 785**Parameters** 786 787 | Name| Type | Mandatory| Description | 788 | ------ | ------ | ---- | ---------------- | 789 | val | number | Yes | Integer to write.| 790 791**Error codes** 792 793For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 794 795 | ID| Error Message| 796 | -------- | -------- | 797 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 798 | 1900009 | Failed to write data to the message sequence. | 799 800**Example** 801 802 ```ts 803 import { hilog } from '@kit.PerformanceAnalysisKit'; 804 import { BusinessError } from '@kit.BasicServicesKit'; 805 806 let data = rpc.MessageSequence.create(); 807 try { 808 data.writeInt(10); 809 } catch (error) { 810 let e: BusinessError = error as BusinessError; 811 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 812 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 813 } 814 ``` 815 816### readInt 817 818readInt(): number 819 820Reads the integer from this **MessageSequence** object. 821 822**System capability**: SystemCapability.Communication.IPC.Core 823 824**Return value** 825 826 | Type | Description | 827 | ------ | ------------ | 828 | number | Integer read.| 829 830**Error codes** 831 832For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 833 834 | ID| Error Message| 835 | -------- | -------- | 836 | 1900010 | Failed to read data from the message sequence. | 837 838**Example** 839 840 ```ts 841 import { hilog } from '@kit.PerformanceAnalysisKit'; 842 import { BusinessError } from '@kit.BasicServicesKit'; 843 844 let data = rpc.MessageSequence.create(); 845 try { 846 data.writeInt(10); 847 } catch (error) { 848 let e: BusinessError = error as BusinessError; 849 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 850 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 851 } 852 try { 853 let ret = data.readInt(); 854 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 855 } catch (error) { 856 let e: BusinessError = error as BusinessError; 857 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 858 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 859 } 860 ``` 861 862### writeLong 863 864writeLong(val: number): void 865 866Writes a long integer to this **MessageSequence** object. 867 868**System capability**: SystemCapability.Communication.IPC.Core 869 870**Parameters** 871 872 | Name| Type | Mandatory| Description | 873 | ------ | ------ | ---- | ---------------- | 874 | val | number | Yes | Long integer to write.| 875 876**Error codes** 877 878For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 879 880 | ID| Error Message| 881 | -------- | -------- | 882 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 883 | 1900009 | Failed to write data to the message sequence. | 884 885**Example** 886 887 ```ts 888 import { hilog } from '@kit.PerformanceAnalysisKit'; 889 import { BusinessError } from '@kit.BasicServicesKit'; 890 891 let data = rpc.MessageSequence.create(); 892 try { 893 data.writeLong(10000); 894 } catch (error) { 895 let e: BusinessError = error as BusinessError; 896 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 897 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 898 } 899 ``` 900 901### readLong 902 903readLong(): number 904 905Reads the long integer from this **MessageSequence** object. 906 907**System capability**: SystemCapability.Communication.IPC.Core 908 909**Return value** 910 911 | Type | Description | 912 | ------ | -------------- | 913 | number | Long integer read.| 914 915**Error codes** 916 917For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 918 919 | ID| Error Message| 920 | -------- | -------- | 921 | 1900010 | Failed to read data from the message sequence. | 922 923**Example** 924 925 ```ts 926 import { hilog } from '@kit.PerformanceAnalysisKit'; 927 import { BusinessError } from '@kit.BasicServicesKit'; 928 929 let data = rpc.MessageSequence.create(); 930 try { 931 data.writeLong(10000); 932 } catch (error) { 933 let e: BusinessError = error as BusinessError; 934 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 935 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 936 } 937 try { 938 let ret = data.readLong(); 939 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 940 } catch (error) { 941 let e: BusinessError = error as BusinessError; 942 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 943 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 944 } 945 ``` 946 947### writeFloat 948 949writeFloat(val: number): void 950 951Writes a double value to this **MessageSequence** object. 952 953**System capability**: SystemCapability.Communication.IPC.Core 954 955**Parameters** 956 957 | Name| Type | Mandatory| Description | 958 | ------ | ------ | ---- | ----- | 959 | val | number | Yes | Double value to write.| 960 961**Error codes** 962 963For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 964 965 | ID| Error Message| 966 | -------- | -------- | 967 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 968 | 1900009 | Failed to write data to the message sequence. | 969 970**Example** 971 972 ```ts 973 import { hilog } from '@kit.PerformanceAnalysisKit'; 974 import { BusinessError } from '@kit.BasicServicesKit'; 975 976 let data = rpc.MessageSequence.create(); 977 try { 978 data.writeFloat(1.2); 979 } catch (error) { 980 let e: BusinessError = error as BusinessError; 981 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 982 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 983 } 984 ``` 985 986### readFloat 987 988readFloat(): number 989 990Reads the double value from this **MessageSequence** object. 991 992**System capability**: SystemCapability.Communication.IPC.Core 993 994**Return value** 995 996 | Type | Description | 997 | ------ | ------------ | 998 | number | Double value read.| 999 1000**Error codes** 1001 1002For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1003 1004 | ID| Error Message| 1005 | -------- | -------- | 1006 | 1900010 | Failed to read data from the message sequence. | 1007 1008**Example** 1009 1010 ```ts 1011 import { hilog } from '@kit.PerformanceAnalysisKit'; 1012 import { BusinessError } from '@kit.BasicServicesKit'; 1013 1014 let data = rpc.MessageSequence.create(); 1015 try { 1016 data.writeFloat(1.2); 1017 } catch (error) { 1018 let e: BusinessError = error as BusinessError; 1019 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1020 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1021 } 1022 try { 1023 let ret = data.readFloat(); 1024 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1025 } catch (error) { 1026 let e: BusinessError = error as BusinessError; 1027 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1028 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1029 } 1030 ``` 1031 1032### writeDouble 1033 1034writeDouble(val: number): void 1035 1036Writes a double value to this **MessageSequence** object. 1037 1038**System capability**: SystemCapability.Communication.IPC.Core 1039 1040**Parameters** 1041 1042 | Name| Type | Mandatory| Description | 1043 | ------ | ------ | ---- | ---------------------- | 1044 | val | number | Yes | Double value to write.| 1045 1046**Error codes** 1047 1048For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1049 1050 | ID| Error Message| 1051 | -------- | -------- | 1052 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1053 | 1900009 | Failed to write data to the message sequence. | 1054 1055**Example** 1056 1057 ```ts 1058 import { hilog } from '@kit.PerformanceAnalysisKit'; 1059 import { BusinessError } from '@kit.BasicServicesKit'; 1060 1061 let data = rpc.MessageSequence.create(); 1062 try { 1063 data.writeDouble(10.2); 1064 } catch (error) { 1065 let e: BusinessError = error as BusinessError; 1066 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1067 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1068 } 1069 ``` 1070 1071### readDouble 1072 1073readDouble(): number 1074 1075Reads the double value from this **MessageSequence** object. 1076 1077**System capability**: SystemCapability.Communication.IPC.Core 1078 1079**Return value** 1080 1081 | Type | Description | 1082 | ------ | ------------------ | 1083 | number | Double value read.| 1084 1085**Error codes** 1086 1087For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1088 1089 | ID| Error Message| 1090 | -------- | -------- | 1091 | 1900010 | Failed to read data from the message sequence. | 1092 1093**Example** 1094 1095 ```ts 1096 import { hilog } from '@kit.PerformanceAnalysisKit'; 1097 import { BusinessError } from '@kit.BasicServicesKit'; 1098 1099 let data = rpc.MessageSequence.create(); 1100 try { 1101 data.writeDouble(10.2); 1102 } catch (error) { 1103 let e: BusinessError = error as BusinessError; 1104 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1105 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1106 } 1107 try { 1108 let ret = data.readDouble(); 1109 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1110 } catch (error) { 1111 let e: BusinessError = error as BusinessError; 1112 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1113 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1114 } 1115 ``` 1116 1117### writeBoolean 1118 1119writeBoolean(val: boolean): void 1120 1121Writes a Boolean value to this **MessageSequence** object. 1122 1123**System capability**: SystemCapability.Communication.IPC.Core 1124 1125**Parameters** 1126 1127 | Name| Type | Mandatory| Description | 1128 | ------ | ------- | ---- | ---------------- | 1129 | val | boolean | Yes | Boolean value to write.| 1130 1131**Error codes** 1132 1133For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1134 1135 | ID| Error Message| 1136 | -------- | -------- | 1137 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1138 | 1900009 | Failed to write data to the message sequence. | 1139 1140**Example** 1141 1142 ```ts 1143 import { hilog } from '@kit.PerformanceAnalysisKit'; 1144 import { BusinessError } from '@kit.BasicServicesKit'; 1145 1146 let data = rpc.MessageSequence.create(); 1147 try { 1148 data.writeBoolean(false); 1149 } catch (error) { 1150 let e: BusinessError = error as BusinessError; 1151 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1152 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1153 } 1154 ``` 1155 1156### readBoolean 1157 1158readBoolean(): boolean 1159 1160Reads the Boolean value from this **MessageSequence** object. 1161 1162**System capability**: SystemCapability.Communication.IPC.Core 1163 1164**Return value** 1165 1166 | Type | Description | 1167 | ------- | -------------------- | 1168 | boolean | Boolean value read.| 1169 1170**Error codes** 1171 1172For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1173 1174 | ID| Error Message| 1175 | -------- | -------- | 1176 | 1900010 | Failed to read data from the message sequence. | 1177 1178**Example** 1179 1180 ```ts 1181 import { hilog } from '@kit.PerformanceAnalysisKit'; 1182 import { BusinessError } from '@kit.BasicServicesKit'; 1183 1184 let data = rpc.MessageSequence.create(); 1185 try { 1186 data.writeBoolean(false); 1187 } catch (error) { 1188 let e: BusinessError = error as BusinessError; 1189 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1190 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1191 } 1192 try { 1193 let ret = data.readBoolean(); 1194 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1195 } catch (error) { 1196 let e: BusinessError = error as BusinessError; 1197 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1198 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1199 } 1200 ``` 1201 1202### writeChar 1203 1204writeChar(val: number): void 1205 1206Writes a character to this **MessageSequence** object. 1207 1208**System capability**: SystemCapability.Communication.IPC.Core 1209 1210**Parameters** 1211 1212 | Name| Type | Mandatory| Description | 1213 | ------ | ------ | ---- | -------------------- | 1214 | val | number | Yes | **Char** value to write.| 1215 1216**Error codes** 1217 1218For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1219 1220 | ID| Error Message| 1221 | -------- | -------- | 1222 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1223 | 1900009 | Failed to write data to the message sequence. | 1224 1225**Example** 1226 1227 ```ts 1228 import { hilog } from '@kit.PerformanceAnalysisKit'; 1229 import { BusinessError } from '@kit.BasicServicesKit'; 1230 1231 let data = rpc.MessageSequence.create(); 1232 try { 1233 data.writeChar(97); 1234 } catch (error) { 1235 let e: BusinessError = error as BusinessError; 1236 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1237 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1238 } 1239 ``` 1240 1241### readChar 1242 1243readChar(): number 1244 1245Reads the character from this **MessageSequence** object. 1246 1247**System capability**: SystemCapability.Communication.IPC.Core 1248 1249**Return value** 1250 1251 | Type | Description| 1252 | ------ | ---- | 1253 | number | **Char** value read.| 1254 1255**Error codes** 1256 1257For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1258 1259 | ID| Error Message| 1260 | -------- | -------- | 1261 | 1900010 | Failed to read data from the message sequence. | 1262 1263**Example** 1264 1265 ```ts 1266 import { hilog } from '@kit.PerformanceAnalysisKit'; 1267 import { BusinessError } from '@kit.BasicServicesKit'; 1268 1269 let data = rpc.MessageSequence.create(); 1270 try { 1271 data.writeChar(97); 1272 } catch (error) { 1273 let e: BusinessError = error as BusinessError; 1274 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1275 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1276 } 1277 try { 1278 let ret = data.readChar(); 1279 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1280 } catch (error) { 1281 let e: BusinessError = error as BusinessError; 1282 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1283 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1284 } 1285 ``` 1286 1287### writeString 1288 1289writeString(val: string): void 1290 1291Writes a string to this **MessageSequence** object. 1292 1293**System capability**: SystemCapability.Communication.IPC.Core 1294 1295**Parameters** 1296 1297 | Name| Type | Mandatory| Description | 1298 | ------ | ------ | ---- | ----------------------------------------- | 1299 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1300 1301**Error codes** 1302 1303For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1304 1305 | ID| Error Message| 1306 | -------- | -------- | 1307 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 1308 | 1900009 | Failed to write data to the message sequence. | 1309 1310**Example** 1311 1312 ```ts 1313 import { hilog } from '@kit.PerformanceAnalysisKit'; 1314 import { BusinessError } from '@kit.BasicServicesKit'; 1315 1316 let data = rpc.MessageSequence.create(); 1317 try { 1318 data.writeString('abc'); 1319 } catch (error) { 1320 let e: BusinessError = error as BusinessError; 1321 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1322 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1323 } 1324 ``` 1325 1326### readString 1327 1328readString(): string 1329 1330Reads the string from this **MessageSequence** object. 1331 1332**System capability**: SystemCapability.Communication.IPC.Core 1333 1334**Return value** 1335 1336 | Type | Description | 1337 | ------ | -------------- | 1338 | string | String read.| 1339 1340**Error codes** 1341 1342For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1343 1344 | ID| Error Message| 1345 | -------- | -------- | 1346 | 1900010 | Failed to read data from the message sequence. | 1347 1348**Example** 1349 1350 ```ts 1351 import { hilog } from '@kit.PerformanceAnalysisKit'; 1352 import { BusinessError } from '@kit.BasicServicesKit'; 1353 1354 let data = rpc.MessageSequence.create(); 1355 try { 1356 data.writeString('abc'); 1357 } catch (error) { 1358 let e: BusinessError = error as BusinessError; 1359 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1360 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1361 } 1362 try { 1363 let ret = data.readString(); 1364 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1365 } catch (error) { 1366 let e: BusinessError = error as BusinessError; 1367 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1368 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1369 } 1370 ``` 1371 1372### writeParcelable 1373 1374writeParcelable(val: Parcelable): void 1375 1376Writes a **Parcelable** object to this **MessageSequence** object. 1377 1378**System capability**: SystemCapability.Communication.IPC.Core 1379 1380**Parameters** 1381 1382| Name| Type| Mandatory| Description| 1383| ------ | --------- | ---- | ------ | 1384| val | [Parcelable](#parcelable9) | Yes | **Parcelable** object to write.| 1385 1386**Error codes** 1387 1388For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1389 1390 | ID| Error Message| 1391 | -------- | -------- | 1392 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1393 | 1900009 | Failed to write data to the message sequence. | 1394 1395**Example** 1396 1397 ```ts 1398 import { hilog } from '@kit.PerformanceAnalysisKit'; 1399 import { BusinessError } from '@kit.BasicServicesKit'; 1400 1401 class MyParcelable implements rpc.Parcelable { 1402 num: number = 0; 1403 str: string = ''; 1404 constructor( num: number, str: string) { 1405 this.num = num; 1406 this.str = str; 1407 } 1408 marshalling(messageSequence: rpc.MessageSequence): boolean { 1409 messageSequence.writeInt(this.num); 1410 messageSequence.writeString(this.str); 1411 return true; 1412 } 1413 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1414 this.num = messageSequence.readInt(); 1415 this.str = messageSequence.readString(); 1416 return true; 1417 } 1418 } 1419 let parcelable = new MyParcelable(1, "aaa"); 1420 let data = rpc.MessageSequence.create(); 1421 try { 1422 data.writeParcelable(parcelable); 1423 } catch (error) { 1424 let e: BusinessError = error as BusinessError; 1425 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1426 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1427 } 1428 ``` 1429 1430### readParcelable 1431 1432readParcelable(dataIn: Parcelable): void 1433 1434Reads the **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1435 1436**System capability**: SystemCapability.Communication.IPC.Core 1437 1438**Parameters** 1439 1440| Name| Type | Mandatory| Description | 1441| ------ | -------------------------- | ---- | ----------------------------------------- | 1442| dataIn | [Parcelable](#parcelable9) | Yes | **Parcelable** object to read.| 1443 1444**Error codes** 1445 1446For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1447 1448 | ID| Error Message| 1449 | -------- | -------- | 1450 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect. | 1451 | 1900010 | Failed to read data from the message sequence. | 1452 | 1900012 | Failed to call the JS callback function. | 1453 1454**Example** 1455 1456 ```ts 1457 import { hilog } from '@kit.PerformanceAnalysisKit'; 1458 import { BusinessError } from '@kit.BasicServicesKit'; 1459 1460 class MyParcelable implements rpc.Parcelable { 1461 num: number = 0; 1462 str: string = ''; 1463 constructor(num: number, str: string) { 1464 this.num = num; 1465 this.str = str; 1466 } 1467 marshalling(messageSequence: rpc.MessageSequence): boolean { 1468 messageSequence.writeInt(this.num); 1469 messageSequence.writeString(this.str); 1470 return true; 1471 } 1472 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1473 this.num = messageSequence.readInt(); 1474 this.str = messageSequence.readString(); 1475 return true; 1476 } 1477 } 1478 let parcelable = new MyParcelable(1, "aaa"); 1479 let data = rpc.MessageSequence.create(); 1480 data.writeParcelable(parcelable); 1481 let ret = new MyParcelable(0, ""); 1482 try { 1483 data.readParcelable(ret); 1484 }catch (error) { 1485 let e: BusinessError = error as BusinessError; 1486 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1487 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1488 } 1489 ``` 1490 1491### writeByteArray 1492 1493writeByteArray(byteArray: number[]): void 1494 1495Writes a byte array to this **MessageSequence** object. 1496 1497**System capability**: SystemCapability.Communication.IPC.Core 1498 1499**Parameters** 1500 1501 | Name | Type | Mandatory| Description | 1502 | --------- | -------- | ---- | ------------------ | 1503 | byteArray | number[] | Yes | Byte array to write.| 1504 1505**Error codes** 1506 1507For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1508 1509 | ID| Error Message| 1510 | -------- | -------- | 1511 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. <br> 5.The type of the element in the array is incorrect. | 1512 | 1900009 | Failed to write data to the message sequence. | 1513 1514**Example** 1515 1516 ```ts 1517 import { hilog } from '@kit.PerformanceAnalysisKit'; 1518 import { BusinessError } from '@kit.BasicServicesKit'; 1519 1520 let data = rpc.MessageSequence.create(); 1521 let ByteArrayVar = [1, 2, 3, 4, 5]; 1522 try { 1523 data.writeByteArray(ByteArrayVar); 1524 } catch (error) { 1525 let e: BusinessError = error as BusinessError; 1526 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1527 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1528 } 1529 ``` 1530 1531### readByteArray 1532 1533readByteArray(dataIn: number[]): void 1534 1535Reads the byte array from this **MessageSequence** object. 1536 1537**System capability**: SystemCapability.Communication.IPC.Core 1538 1539**Parameters** 1540 1541 | Name| Type | Mandatory| Description | 1542 | ------ | -------- | ---- | ------------------ | 1543 | dataIn | number[] | Yes | Byte array to read.| 1544 1545**Error codes** 1546 1547For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1548 1549 | ID| Error Message| 1550 | -------- | -------- | 1551 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1552 | 1900010 | Failed to read data from the message sequence. | 1553 1554**Example** 1555 1556 ```ts 1557 import { hilog } from '@kit.PerformanceAnalysisKit'; 1558 import { BusinessError } from '@kit.BasicServicesKit'; 1559 1560 let data = rpc.MessageSequence.create(); 1561 let ByteArrayVar = [1, 2, 3, 4, 5]; 1562 try { 1563 data.writeByteArray(ByteArrayVar); 1564 } catch (error) { 1565 let e: BusinessError = error as BusinessError; 1566 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1567 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1568 } 1569 try { 1570 let array: Array<number> = new Array(5); 1571 data.readByteArray(array); 1572 } catch (error) { 1573 let e: BusinessError = error as BusinessError; 1574 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1575 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1576 } 1577 ``` 1578 1579### readByteArray 1580 1581readByteArray(): number[] 1582 1583Reads the byte array from this **MessageSequence** object. 1584 1585**System capability**: SystemCapability.Communication.IPC.Core 1586 1587**Return value** 1588 1589 | Type | Description | 1590 | -------- | -------------- | 1591 | number[] | Byte array read.| 1592 1593**Error codes** 1594 1595For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1596 1597 | ID| Error Message| 1598 | -------- | -------- | 1599 | 1900010 | Failed to read data from the message sequence. | 1600 1601**Example** 1602 1603 ```ts 1604 import { hilog } from '@kit.PerformanceAnalysisKit'; 1605 import { BusinessError } from '@kit.BasicServicesKit'; 1606 1607 let data = rpc.MessageSequence.create(); 1608 let byteArrayVar = [1, 2, 3, 4, 5]; 1609 try { 1610 data.writeByteArray(byteArrayVar); 1611 } catch (error) { 1612 let e: BusinessError = error as BusinessError; 1613 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1614 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1615 } 1616 try { 1617 let array = data.readByteArray(); 1618 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1619 } catch (error) { 1620 let e: BusinessError = error as BusinessError; 1621 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1622 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1623 } 1624 ``` 1625 1626### writeShortArray 1627 1628writeShortArray(shortArray: number[]): void 1629 1630Writes a short array to this **MessageSequence** object. 1631 1632**System capability**: SystemCapability.Communication.IPC.Core 1633 1634**Parameters** 1635 1636 | Name | Type | Mandatory| Description | 1637 | ---------- | -------- | ---- | -------------------- | 1638 | shortArray | number[] | Yes | Short array to write.| 1639 1640**Error codes** 1641 1642For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1643 1644 | ID| Error Message| 1645 | -------- | -------- | 1646 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1647 | 1900009 | Failed to write data to the message sequence. | 1648 1649**Example** 1650 1651 ```ts 1652 import { hilog } from '@kit.PerformanceAnalysisKit'; 1653 import { BusinessError } from '@kit.BasicServicesKit'; 1654 1655 let data = rpc.MessageSequence.create(); 1656 try { 1657 data.writeShortArray([11, 12, 13]); 1658 } catch (error) { 1659 let e: BusinessError = error as BusinessError; 1660 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1661 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1662 } 1663 ``` 1664 1665### readShortArray 1666 1667readShortArray(dataIn: number[]): void 1668 1669Reads the short array from this **MessageSequence** object. 1670 1671**System capability**: SystemCapability.Communication.IPC.Core 1672 1673**Parameters** 1674 1675 | Name| Type | Mandatory| Description | 1676 | ------ | -------- | ---- | -------------------- | 1677 | dataIn | number[] | Yes | Short array to read.| 1678 1679**Error codes** 1680 1681For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1682 1683 | ID| Error Message| 1684 | -------- | -------- | 1685 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1686 | 1900010 | Failed to read data from the message sequence. | 1687 1688**Example** 1689 1690 ```ts 1691 import { hilog } from '@kit.PerformanceAnalysisKit'; 1692 import { BusinessError } from '@kit.BasicServicesKit'; 1693 1694 let data = rpc.MessageSequence.create(); 1695 try { 1696 data.writeShortArray([11, 12, 13]); 1697 } catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1700 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1701 } 1702 try { 1703 let array: Array<number> = new Array(3); 1704 data.readShortArray(array); 1705 } catch (error) { 1706 let e: BusinessError = error as BusinessError; 1707 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1708 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1709 } 1710 ``` 1711 1712### readShortArray 1713 1714readShortArray(): number[] 1715 1716Reads the short array from this **MessageSequence** object. 1717 1718**System capability**: SystemCapability.Communication.IPC.Core 1719 1720**Return value** 1721 1722 | Type | Description | 1723 | -------- | ---------------- | 1724 | number[] | Short array read.| 1725 1726**Error codes** 1727 1728For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1729 1730 | ID| Error Message| 1731 | -------- | -------- | 1732 | 1900010 | Failed to read data from the message sequence. | 1733 1734**Example** 1735 1736 ```ts 1737 import { hilog } from '@kit.PerformanceAnalysisKit'; 1738 import { BusinessError } from '@kit.BasicServicesKit'; 1739 1740 let data = rpc.MessageSequence.create(); 1741 try { 1742 data.writeShortArray([11, 12, 13]); 1743 } catch (error) { 1744 let e: BusinessError = error as BusinessError; 1745 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1746 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1747 } 1748 try { 1749 let array = data.readShortArray(); 1750 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1751 } catch (error) { 1752 let e: BusinessError = error as BusinessError; 1753 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1754 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1755 } 1756 ``` 1757 1758### writeIntArray 1759 1760writeIntArray(intArray: number[]): void 1761 1762Writes an integer array to this **MessageSequence** object. 1763 1764**System capability**: SystemCapability.Communication.IPC.Core 1765 1766**Parameters** 1767 1768 | Name | Type | Mandatory| Description | 1769 | -------- | -------- | ---- | ------------------ | 1770 | intArray | number[] | Yes | Integer array to write.| 1771 1772**Error codes** 1773 1774For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1775 1776 | ID| Error Message| 1777 | -------- | -------- | 1778 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1779 | 1900009 | Failed to write data to the message sequence. | 1780 1781**Example** 1782 1783 ```ts 1784 import { hilog } from '@kit.PerformanceAnalysisKit'; 1785 import { BusinessError } from '@kit.BasicServicesKit'; 1786 1787 let data = rpc.MessageSequence.create(); 1788 try { 1789 data.writeIntArray([100, 111, 112]); 1790 } catch (error) { 1791 let e: BusinessError = error as BusinessError; 1792 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1793 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1794 } 1795 ``` 1796 1797### readIntArray 1798 1799readIntArray(dataIn: number[]): void 1800 1801Reads the integer array from this **MessageSequence** object. 1802 1803**System capability**: SystemCapability.Communication.IPC.Core 1804 1805**Parameters** 1806 1807 | Name| Type | Mandatory| Description | 1808 | ------ | -------- | ---- | ------------------ | 1809 | dataIn | number[] | Yes | Integer array to read.| 1810 1811**Error codes** 1812 1813For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1814 1815 | ID| Error Message| 1816 | -------- | -------- | 1817 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1818 | 1900010 | Failed to read data from the message sequence. | 1819 1820**Example** 1821 1822 ```ts 1823 import { hilog } from '@kit.PerformanceAnalysisKit'; 1824 import { BusinessError } from '@kit.BasicServicesKit'; 1825 1826 let data = rpc.MessageSequence.create(); 1827 try { 1828 data.writeIntArray([100, 111, 112]); 1829 } catch (error) { 1830 let e: BusinessError = error as BusinessError; 1831 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1832 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1833 } 1834 let array: Array<number> = new Array(3); 1835 try { 1836 data.readIntArray(array); 1837 } catch (error) { 1838 let e: BusinessError = error as BusinessError; 1839 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1840 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1841 } 1842 ``` 1843 1844### readIntArray 1845 1846readIntArray(): number[] 1847 1848Reads the integer array from this **MessageSequence** object. 1849 1850**System capability**: SystemCapability.Communication.IPC.Core 1851 1852**Return value** 1853 1854 | Type | Description | 1855 | -------- | -------------- | 1856 | number[] | Integer array read.| 1857 1858**Error codes** 1859 1860For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1861 1862 | ID| Error Message| 1863 | -------- | -------- | 1864 | 1900010 | Failed to read data from the message sequence. | 1865 1866**Example** 1867 1868 ```ts 1869 import { hilog } from '@kit.PerformanceAnalysisKit'; 1870 import { BusinessError } from '@kit.BasicServicesKit'; 1871 1872 let data = rpc.MessageSequence.create(); 1873 try { 1874 data.writeIntArray([100, 111, 112]); 1875 } catch (error) { 1876 let e: BusinessError = error as BusinessError; 1877 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1878 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1879 } 1880 try { 1881 let array = data.readIntArray(); 1882 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1883 } catch (error) { 1884 let e: BusinessError = error as BusinessError; 1885 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1886 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1887 } 1888 ``` 1889 1890### writeLongArray 1891 1892writeLongArray(longArray: number[]): void 1893 1894Writes a long array to this **MessageSequence** object. 1895 1896**System capability**: SystemCapability.Communication.IPC.Core 1897 1898**Parameters** 1899 1900 | Name | Type | Mandatory| Description | 1901 | --------- | -------- | ---- | -------------------- | 1902 | longArray | number[] | Yes | Long array to write.| 1903 1904**Error codes** 1905 1906For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1907 1908 | ID| Error Message| 1909 | -------- | -------- | 1910 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1911 | 1900009 | Failed to write data to the message sequence. | 1912 1913**Example** 1914 1915 ```ts 1916 import { hilog } from '@kit.PerformanceAnalysisKit'; 1917 import { BusinessError } from '@kit.BasicServicesKit'; 1918 1919 let data = rpc.MessageSequence.create(); 1920 try { 1921 data.writeLongArray([1111, 1112, 1113]); 1922 }catch (error){ 1923 let e: BusinessError = error as BusinessError; 1924 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1925 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1926 } 1927 ``` 1928 1929### readLongArray 1930 1931readLongArray(dataIn: number[]): void 1932 1933Reads the long array from this **MessageSequence** object. 1934 1935**System capability**: SystemCapability.Communication.IPC.Core 1936 1937**Parameters** 1938 1939 | Name| Type | Mandatory| Description | 1940 | ------ | -------- | ---- | -------------------- | 1941 | dataIn | number[] | Yes | Long array to read.| 1942 1943**Error codes** 1944 1945For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1946 1947 | ID| Error Message| 1948 | -------- | -------- | 1949 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1950 | 1900010 | Failed to read data from the message sequence. | 1951 1952**Example** 1953 1954 ```ts 1955 import { hilog } from '@kit.PerformanceAnalysisKit'; 1956 import { BusinessError } from '@kit.BasicServicesKit'; 1957 1958 let data = rpc.MessageSequence.create(); 1959 try { 1960 data.writeLongArray([1111, 1112, 1113]); 1961 } catch (error) { 1962 let e: BusinessError = error as BusinessError; 1963 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1964 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1965 } 1966 let array: Array<number> = new Array(3); 1967 try { 1968 data.readLongArray(array); 1969 } catch (error) { 1970 let e: BusinessError = error as BusinessError; 1971 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1972 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1973 } 1974 ``` 1975 1976### readLongArray 1977 1978readLongArray(): number[] 1979 1980Reads the long integer array from this **MessageSequence** object. 1981 1982**System capability**: SystemCapability.Communication.IPC.Core 1983 1984**Return value** 1985 1986 | Type | Description | 1987 | -------- | ---------------- | 1988 | number[] | Long array read.| 1989 1990**Error codes** 1991 1992For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1993 1994 | ID| Error Message| 1995 | -------- | -------- | 1996 | 1900010 | Failed to read data from the message sequence. | 1997 1998**Example** 1999 2000 ```ts 2001 import { hilog } from '@kit.PerformanceAnalysisKit'; 2002 import { BusinessError } from '@kit.BasicServicesKit'; 2003 2004 let data = rpc.MessageSequence.create(); 2005 try { 2006 data.writeLongArray([1111, 1112, 1113]); 2007 } catch (error) { 2008 let e: BusinessError = error as BusinessError; 2009 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 2010 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2011 } 2012 try { 2013 let array = data.readLongArray(); 2014 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2015 } catch (error) { 2016 let e: BusinessError = error as BusinessError; 2017 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2018 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2019 } 2020 ``` 2021 2022### writeFloatArray 2023 2024writeFloatArray(floatArray: number[]): void 2025 2026Writes a double array to this **MessageSequence** object. 2027 2028**System capability**: SystemCapability.Communication.IPC.Core 2029 2030**Parameters** 2031 2032 | Name | Type | Mandatory| Description | 2033 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2034 | floatArray | number[] | Yes | Double array to write. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type.| 2035 2036**Error codes** 2037 2038For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2039 2040 | ID| Error Message| 2041 | -------- | -------- | 2042 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 2043 | 1900009 | Failed to write data to the message sequence. | 2044 2045**Example** 2046 2047 ```ts 2048 import { hilog } from '@kit.PerformanceAnalysisKit'; 2049 import { BusinessError } from '@kit.BasicServicesKit'; 2050 2051 let data = rpc.MessageSequence.create(); 2052 try { 2053 data.writeFloatArray([1.2, 1.3, 1.4]); 2054 } catch (error) { 2055 let e: BusinessError = error as BusinessError; 2056 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2057 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2058 } 2059 ``` 2060 2061### readFloatArray 2062 2063readFloatArray(dataIn: number[]): void 2064 2065Reads the double array from this **MessageSequence** object. 2066 2067**System capability**: SystemCapability.Communication.IPC.Core 2068 2069**Parameters** 2070 2071 | Name| Type | Mandatory| Description | 2072 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2073 | dataIn | number[] | Yes | Double array to read. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type.| 2074 2075**Error codes** 2076 2077For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2078 2079 | ID| Error Message| 2080 | -------- | -------- | 2081 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2082 | 1900010 | Failed to read data from the message sequence. | 2083 2084**Example** 2085 2086 ```ts 2087 import { hilog } from '@kit.PerformanceAnalysisKit'; 2088 import { BusinessError } from '@kit.BasicServicesKit'; 2089 2090 let data = rpc.MessageSequence.create(); 2091 try { 2092 data.writeFloatArray([1.2, 1.3, 1.4]); 2093 }catch (error){ 2094 let e: BusinessError = error as BusinessError; 2095 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2096 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2097 } 2098 let array: Array<number> = new Array(3); 2099 try { 2100 data.readFloatArray(array); 2101 } catch (error) { 2102 let e: BusinessError = error as BusinessError; 2103 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2104 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2105 } 2106 ``` 2107 2108### readFloatArray 2109 2110readFloatArray(): number[] 2111 2112Reads the double array from this **MessageSequence** object. 2113 2114**System capability**: SystemCapability.Communication.IPC.Core 2115 2116**Return value** 2117 2118 | Type | Description | 2119 | -------- | -------------- | 2120 | number[] | Double array read.| 2121 2122**Error codes** 2123 2124For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2125 2126 | ID| Error Message| 2127 | -------- | -------- | 2128 | 1900010 | Failed to read data from the message sequence. | 2129 2130**Example** 2131 2132 ```ts 2133 import { hilog } from '@kit.PerformanceAnalysisKit'; 2134 import { BusinessError } from '@kit.BasicServicesKit'; 2135 2136 let data = rpc.MessageSequence.create(); 2137 try { 2138 data.writeFloatArray([1.2, 1.3, 1.4]); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2142 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2143 } 2144 try { 2145 let array = data.readFloatArray(); 2146 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2147 } catch (error) { 2148 let e: BusinessError = error as BusinessError; 2149 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2150 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2151 } 2152 ``` 2153 2154### writeDoubleArray 2155 2156writeDoubleArray(doubleArray: number[]): void 2157 2158Writes a double array to this **MessageSequence** object. 2159 2160**System capability**: SystemCapability.Communication.IPC.Core 2161 2162**Parameters** 2163 2164 | Name | Type | Mandatory| Description | 2165 | ----------- | -------- | ---- | ------------------------ | 2166 | doubleArray | number[] | Yes | Double array to write.| 2167 2168**Error codes** 2169 2170For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2171 2172 | ID| Error Message| 2173 | -------- | -------- | 2174 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 2175 | 1900009 | Failed to write data to the message sequence. | 2176 2177**Example** 2178 2179 ```ts 2180 import { hilog } from '@kit.PerformanceAnalysisKit'; 2181 import { BusinessError } from '@kit.BasicServicesKit'; 2182 2183 let data = rpc.MessageSequence.create(); 2184 try { 2185 data.writeDoubleArray([11.1, 12.2, 13.3]); 2186 } catch (error) { 2187 let e: BusinessError = error as BusinessError; 2188 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2189 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2190 } 2191 ``` 2192 2193### readDoubleArray 2194 2195readDoubleArray(dataIn: number[]): void 2196 2197Reads the double array from this **MessageSequence** object. 2198 2199**System capability**: SystemCapability.Communication.IPC.Core 2200 2201**Parameters** 2202 2203 | Name| Type | Mandatory| Description | 2204 | ------ | -------- | ---- | ------------------------ | 2205 | dataIn | number[] | Yes | Double array to read.| 2206 2207**Error codes** 2208 2209For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2210 2211 | ID| Error Message| 2212 | -------- | -------- | 2213 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2214 | 1900010 | Failed to read data from the message sequence. | 2215 2216**Example** 2217 2218 ```ts 2219 import { hilog } from '@kit.PerformanceAnalysisKit'; 2220 import { BusinessError } from '@kit.BasicServicesKit'; 2221 2222 let data = rpc.MessageSequence.create(); 2223 try { 2224 data.writeDoubleArray([11.1, 12.2, 13.3]); 2225 } catch (error) { 2226 let e: BusinessError = error as BusinessError; 2227 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2228 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2229 } 2230 let array: Array<number> = new Array(3); 2231 try { 2232 data.readDoubleArray(array); 2233 } catch (error) { 2234 let e: BusinessError = error as BusinessError; 2235 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2236 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2237 } 2238 ``` 2239 2240### readDoubleArray 2241 2242readDoubleArray(): number[] 2243 2244Reads the double array from this **MessageSequence** object. 2245 2246**System capability**: SystemCapability.Communication.IPC.Core 2247 2248**Return value** 2249 2250 | Type | Description | 2251 | -------- | -------------------- | 2252 | number[] | Double array read.| 2253 2254**Error codes** 2255 2256For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2257 2258 | ID| Error Message| 2259 | -------- | -------- | 2260 | 1900010 | Failed to read data from the message sequence. | 2261 2262**Example** 2263 2264 ```ts 2265 import { hilog } from '@kit.PerformanceAnalysisKit'; 2266 import { BusinessError } from '@kit.BasicServicesKit'; 2267 2268 let data = rpc.MessageSequence.create(); 2269 try { 2270 data.writeDoubleArray([11.1, 12.2, 13.3]); 2271 } catch (error) { 2272 let e: BusinessError = error as BusinessError; 2273 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2274 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2275 } 2276 try { 2277 let array = data.readDoubleArray(); 2278 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2279 } catch (error) { 2280 let e: BusinessError = error as BusinessError; 2281 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2282 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2283 } 2284 ``` 2285 2286### writeBooleanArray 2287 2288writeBooleanArray(booleanArray: boolean[]): void 2289 2290Writes a Boolean array to this **MessageSequence** object. 2291 2292**System capability**: SystemCapability.Communication.IPC.Core 2293 2294**Parameters** 2295 2296 | Name | Type | Mandatory| Description | 2297 | ------------ | --------- | ---- | ------------------ | 2298 | booleanArray | boolean[] | Yes | Boolean array to write.| 2299 2300**Error codes** 2301 2302For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2303 2304 | ID| Error Message| 2305 | -------- | -------- | 2306 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2307 | 1900009 | Failed to write data to the message sequence. | 2308 2309**Example** 2310 2311 ```ts 2312 import { hilog } from '@kit.PerformanceAnalysisKit'; 2313 import { BusinessError } from '@kit.BasicServicesKit'; 2314 2315 let data = rpc.MessageSequence.create(); 2316 try { 2317 data.writeBooleanArray([false, true, false]); 2318 } catch (error) { 2319 let e: BusinessError = error as BusinessError; 2320 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2321 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2322 } 2323 ``` 2324 2325### readBooleanArray 2326 2327readBooleanArray(dataIn: boolean[]): void 2328 2329Reads the Boolean array from this **MessageSequence** object. 2330 2331**System capability**: SystemCapability.Communication.IPC.Core 2332 2333**Parameters** 2334 2335 | Name| Type | Mandatory| Description | 2336 | ------ | --------- | ---- | ------------------ | 2337 | dataIn | boolean[] | Yes | Boolean array to read.| 2338 2339**Error codes** 2340 2341For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2342 2343 | ID| Error Message| 2344 | -------- | -------- | 2345 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2346 | 1900010 | Failed to read data from the message sequence. | 2347 2348**Example** 2349 2350 ```ts 2351 import { hilog } from '@kit.PerformanceAnalysisKit'; 2352 import { BusinessError } from '@kit.BasicServicesKit'; 2353 2354 let data = rpc.MessageSequence.create(); 2355 try { 2356 data.writeBooleanArray([false, true, false]); 2357 } catch (error) { 2358 let e: BusinessError = error as BusinessError; 2359 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2360 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2361 } 2362 let array: Array<boolean> = new Array(3); 2363 try { 2364 data.readBooleanArray(array); 2365 } catch (error) { 2366 let e: BusinessError = error as BusinessError; 2367 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2368 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2369 } 2370 ``` 2371 2372### readBooleanArray 2373 2374readBooleanArray(): boolean[] 2375 2376Reads the Boolean array from this **MessageSequence** object. 2377 2378**System capability**: SystemCapability.Communication.IPC.Core 2379 2380**Return value** 2381 2382 | Type | Description | 2383 | --------- | -------------- | 2384 | boolean[] | Boolean array read.| 2385 2386**Error codes** 2387 2388For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2389 2390 | ID| Error Message| 2391 | -------- | -------- | 2392 | 1900010 | Failed to read data from the message sequence. | 2393 2394**Example** 2395 2396 ```ts 2397 import { hilog } from '@kit.PerformanceAnalysisKit'; 2398 import { BusinessError } from '@kit.BasicServicesKit'; 2399 2400 let data = rpc.MessageSequence.create(); 2401 try { 2402 data.writeBooleanArray([false, true, false]); 2403 } catch (error) { 2404 let e: BusinessError = error as BusinessError; 2405 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2406 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2407 } 2408 try { 2409 let array = data.readBooleanArray(); 2410 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2411 } catch (error) { 2412 let e: BusinessError = error as BusinessError; 2413 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2414 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2415 } 2416 ``` 2417 2418### writeCharArray 2419 2420writeCharArray(charArray: number[]): void 2421 2422Writes a character array to this **MessageSequence** object. 2423 2424**System capability**: SystemCapability.Communication.IPC.Core 2425 2426**Parameters** 2427 2428 | Name | Type | Mandatory| Description | 2429 | --------- | -------- | ---- | ---------------------- | 2430 | charArray | number[] | Yes | Character array to write.| 2431 2432**Error codes** 2433 2434For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2435 2436 | ID| Error Message| 2437 | -------- | -------- | 2438 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2439 | 1900009 | Failed to write data to the message sequence. | 2440 2441**Example** 2442 2443 ```ts 2444 import { hilog } from '@kit.PerformanceAnalysisKit'; 2445 import { BusinessError } from '@kit.BasicServicesKit'; 2446 2447 let data = rpc.MessageSequence.create(); 2448 try { 2449 data.writeCharArray([97, 98, 88]); 2450 } catch (error) { 2451 let e: BusinessError = error as BusinessError; 2452 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2453 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2454 } 2455 ``` 2456 2457### readCharArray 2458 2459readCharArray(dataIn: number[]): void 2460 2461Reads the character array from this **MessageSequence** object. 2462 2463**System capability**: SystemCapability.Communication.IPC.Core 2464 2465**Parameters** 2466 2467 | Name| Type | Mandatory| Description | 2468 | ------ | -------- | ---- | ---------------------- | 2469 | dataIn | number[] | Yes | Character array to read.| 2470 2471**Error codes** 2472 2473For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2474 2475 | ID| Error Message| 2476 | -------- | -------- | 2477 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2478 | 1900010 | Failed to read data from the message sequence. | 2479 2480**Example** 2481 2482 ```ts 2483 import { hilog } from '@kit.PerformanceAnalysisKit'; 2484 import { BusinessError } from '@kit.BasicServicesKit'; 2485 2486 let data = rpc.MessageSequence.create(); 2487 try { 2488 data.writeCharArray([97, 98, 88]); 2489 } catch (error) { 2490 let e: BusinessError = error as BusinessError; 2491 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2492 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2493 } 2494 let array: Array<number> = new Array(3); 2495 try { 2496 data.readCharArray(array); 2497 } catch (error) { 2498 let e: BusinessError = error as BusinessError; 2499 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2500 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2501 } 2502 ``` 2503 2504### readCharArray 2505 2506readCharArray(): number[] 2507 2508Reads the character array from this **MessageSequence** object. 2509 2510**System capability**: SystemCapability.Communication.IPC.Core 2511 2512**Return value** 2513 2514 | Type | Description | 2515 | -------- | ------------------ | 2516 | number[] | Character array read.| 2517 2518**Error codes** 2519 2520For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2521 2522 | ID| Error Message| 2523 | -------- | -------- | 2524 | 1900010 | Failed to read data from the message sequence. | 2525 2526**Example** 2527 2528 ```ts 2529 import { hilog } from '@kit.PerformanceAnalysisKit'; 2530 import { BusinessError } from '@kit.BasicServicesKit'; 2531 2532 let data = rpc.MessageSequence.create(); 2533 try { 2534 data.writeCharArray([97, 98, 88]); 2535 } catch (error) { 2536 let e: BusinessError = error as BusinessError; 2537 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2538 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2539 } 2540 try { 2541 let array = data.readCharArray(); 2542 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2543 } catch (error) { 2544 let e: BusinessError = error as BusinessError; 2545 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2546 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2547 } 2548 ``` 2549 2550### writeStringArray 2551 2552writeStringArray(stringArray: string[]): void 2553 2554Writes a string array to this **MessageSequence** object. 2555 2556**System capability**: SystemCapability.Communication.IPC.Core 2557 2558**Parameters** 2559 2560 | Name | Type | Mandatory| Description | 2561 | ----------- | -------- | ---- | ------------------------------------------------------- | 2562 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2563 2564**Error codes** 2565 2566For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2567 2568 | ID| Error Message| 2569 | -------- | -------- | 2570 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The string length exceeds 40960 bytes; <br> 5.The number of bytes copied to the buffer is different from the length of the obtained string. | 2571 | 1900009 | Failed to write data to the message sequence. | 2572 2573**Example** 2574 2575 ```ts 2576 import { hilog } from '@kit.PerformanceAnalysisKit'; 2577 import { BusinessError } from '@kit.BasicServicesKit'; 2578 2579 let data = rpc.MessageSequence.create(); 2580 try { 2581 data.writeStringArray(["abc", "def"]); 2582 } catch (error) { 2583 let e: BusinessError = error as BusinessError; 2584 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2585 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2586 } 2587 ``` 2588 2589### readStringArray 2590 2591readStringArray(dataIn: string[]): void 2592 2593Reads the string array from this **MessageSequence** object. 2594 2595**System capability**: SystemCapability.Communication.IPC.Core 2596 2597**Parameters** 2598 2599 | Name| Type | Mandatory| Description | 2600 | ------ | -------- | ---- | -------------------- | 2601 | dataIn | string[] | Yes | String array to read.| 2602 2603**Error codes** 2604 2605For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2606 2607 | ID| Error Message| 2608 | -------- | -------- | 2609 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2610 | 1900010 | Failed to read data from the message sequence. | 2611 2612**Example** 2613 2614 ```ts 2615 import { hilog } from '@kit.PerformanceAnalysisKit'; 2616 import { BusinessError } from '@kit.BasicServicesKit'; 2617 2618 let data = rpc.MessageSequence.create(); 2619 try { 2620 data.writeStringArray(["abc", "def"]); 2621 } catch (error) { 2622 let e: BusinessError = error as BusinessError; 2623 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2624 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2625 } 2626 let array: Array<string> = new Array(2); 2627 try { 2628 data.readStringArray(array); 2629 } catch (error) { 2630 let e: BusinessError = error as BusinessError; 2631 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2632 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2633 } 2634 ``` 2635 2636### readStringArray 2637 2638readStringArray(): string[] 2639 2640Reads the string array from this **MessageSequence** object. 2641 2642**System capability**: SystemCapability.Communication.IPC.Core 2643 2644**Return value** 2645 2646 | Type | Description | 2647 | -------- | ---------------- | 2648 | string[] | String array read.| 2649 2650**Error codes** 2651 2652For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2653 2654 | ID| Error Message| 2655 | -------- | -------- | 2656 | 1900010 | Failed to read data from the message sequence. | 2657 2658**Example** 2659 2660 ```ts 2661 import { hilog } from '@kit.PerformanceAnalysisKit'; 2662 import { BusinessError } from '@kit.BasicServicesKit'; 2663 2664 let data = rpc.MessageSequence.create(); 2665 try { 2666 data.writeStringArray(["abc", "def"]); 2667 } catch (error) { 2668 let e: BusinessError = error as BusinessError; 2669 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2670 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2671 } 2672 try { 2673 let array = data.readStringArray(); 2674 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2675 } catch (error) { 2676 let e: BusinessError = error as BusinessError; 2677 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2678 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2679 } 2680 ``` 2681 2682### writeNoException 2683 2684writeNoException(): void 2685 2686Writes information to this **MessageSequence** object indicating that no exception occurred. 2687 2688**System capability**: SystemCapability.Communication.IPC.Core 2689 2690**Error codes** 2691 2692For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2693 2694 | ID| Error Message| 2695 | -------- | -------- | 2696 | 1900009 | Failed to write data to the message sequence. | 2697 2698**Example** 2699 2700 ```ts 2701 import { hilog } from '@kit.PerformanceAnalysisKit'; 2702 import { BusinessError } from '@kit.BasicServicesKit'; 2703 2704 class TestRemoteObject extends rpc.RemoteObject { 2705 constructor(descriptor: string) { 2706 super(descriptor); 2707 } 2708 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2709 if (code === 1) { 2710 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2711 try { 2712 reply.writeNoException(); 2713 } catch (error) { 2714 let e: BusinessError = error as BusinessError; 2715 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2716 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2717 } 2718 return true; 2719 } else { 2720 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2721 return false; 2722 } 2723 } 2724 } 2725 ``` 2726 2727### readException 2728 2729readException(): void 2730 2731Reads the exception information from this **MessageSequence** object. 2732 2733**System capability**: SystemCapability.Communication.IPC.Core 2734 2735**Error codes** 2736 2737For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2738 2739 | ID| Error Message| 2740 | -------- | -------- | 2741 | 1900010 | Failed to read data from the message sequence. | 2742 2743**Example** 2744 2745>**NOTE** 2746> 2747>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2748 2749 <!--code_no_check--> 2750 ```ts 2751 // If the FA model is used, import featureAbility from @kit.AbilityKit. 2752 // import { featureAbility } from '@kit.AbilityKit'; 2753 import { Want, common } from '@kit.AbilityKit'; 2754 import { hilog } from '@kit.PerformanceAnalysisKit'; 2755 2756 let proxy: rpc.IRemoteObject | undefined; 2757 let connect: common.ConnectOptions = { 2758 onConnect: (elementName, remoteProxy) => { 2759 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2760 proxy = remoteProxy; 2761 }, 2762 onDisconnect: (elementName) => { 2763 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2764 }, 2765 onFailed: () => { 2766 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2767 } 2768 }; 2769 let want: Want = { 2770 bundleName: "com.ohos.server", 2771 abilityName: "com.ohos.server.EntryAbility", 2772 }; 2773 2774 // Use this method to connect to the ability for the FA model. 2775 // FA.connectAbility(want,connect); 2776 2777 // Save the connection ID, which will be used for the subsequent service disconnection. 2778 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 2779 // Save the connection ID, which will be used for the subsequent service disconnection. 2780 let connectionId = context.connectServiceExtensionAbility(want, connect); 2781 ``` 2782 2783 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. 2784 2785 ```ts 2786 import { BusinessError } from '@kit.BasicServicesKit'; 2787 import { hilog } from '@kit.PerformanceAnalysisKit'; 2788 2789 let option = new rpc.MessageOption(); 2790 let data = rpc.MessageSequence.create(); 2791 let reply = rpc.MessageSequence.create(); 2792 data.writeNoException(); 2793 data.writeInt(6); 2794 if (proxy != undefined) { 2795 proxy.sendMessageRequest(1, data, reply, option) 2796 .then((result: rpc.RequestResult) => { 2797 if (result.errCode === 0) { 2798 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2799 try { 2800 result.reply.readException(); 2801 } catch (error) { 2802 let e: BusinessError = error as BusinessError; 2803 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2804 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2805 } 2806 let num = result.reply.readInt(); 2807 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2808 } else { 2809 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2810 } 2811 }).catch((e: Error) => { 2812 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2813 }).finally (() => { 2814 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2815 data.reclaim(); 2816 reply.reclaim(); 2817 }); 2818 } 2819 ``` 2820 2821### writeParcelableArray 2822 2823writeParcelableArray(parcelableArray: Parcelable[]): void 2824 2825Writes the **Parcelable** array to this **MessageSequence** object. 2826 2827**System capability**: SystemCapability.Communication.IPC.Core 2828 2829**Parameters** 2830 2831| Name | Type | Mandatory| Description | 2832| --------------- | ------------ | ---- | -------------------------- | 2833| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to write.| 2834 2835**Error codes** 2836 2837For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2838 2839 | ID| Error Message| 2840 | -------- | -------- | 2841 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2842 | 1900009 | Failed to write data to the message sequence. | 2843 2844**Example** 2845 2846 ```ts 2847 import { hilog } from '@kit.PerformanceAnalysisKit'; 2848 import { BusinessError } from '@kit.BasicServicesKit'; 2849 2850 class MyParcelable implements rpc.Parcelable { 2851 num: number = 0; 2852 str: string = ''; 2853 constructor(num: number, str: string) { 2854 this.num = num; 2855 this.str = str; 2856 } 2857 marshalling(messageSequence: rpc.MessageSequence): boolean { 2858 messageSequence.writeInt(this.num); 2859 messageSequence.writeString(this.str); 2860 return true; 2861 } 2862 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2863 this.num = messageSequence.readInt(); 2864 this.str = messageSequence.readString(); 2865 return true; 2866 } 2867 } 2868 let parcelable = new MyParcelable(1, "aaa"); 2869 let parcelable2 = new MyParcelable(2, "bbb"); 2870 let parcelable3 = new MyParcelable(3, "ccc"); 2871 let a = [parcelable, parcelable2, parcelable3]; 2872 let data = rpc.MessageSequence.create(); 2873 try { 2874 data.writeParcelableArray(a); 2875 } catch (error) { 2876 let e: BusinessError = error as BusinessError; 2877 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2878 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2879 } 2880 ``` 2881 2882### readParcelableArray 2883 2884readParcelableArray(parcelableArray: Parcelable[]): void 2885 2886Reads the **Parcelable** array from this **MessageSequence** object. 2887 2888**System capability**: SystemCapability.Communication.IPC.Core 2889 2890**Parameters** 2891 2892| Name | Type | Mandatory| Description | 2893| --------------- | ------------ | ---- | -------------------------- | 2894| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to read.| 2895 2896**Error codes** 2897 2898For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2899 2900 | ID| Error Message| 2901 | -------- | -------- | 2902 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The length of the array passed when reading is not equal to the length passed when writing to the array; <br> 5.The element does not exist in the array. | 2903 | 1900010 | Failed to read data from the message sequence. | 2904 | 1900012 | Failed to call the JS callback function. | 2905 2906**Example** 2907 2908 ```ts 2909 import { hilog } from '@kit.PerformanceAnalysisKit'; 2910 import { BusinessError } from '@kit.BasicServicesKit'; 2911 2912 class MyParcelable implements rpc.Parcelable { 2913 num: number = 0; 2914 str: string = ''; 2915 constructor(num: number, str: string) { 2916 this.num = num; 2917 this.str = str; 2918 } 2919 marshalling(messageSequence: rpc.MessageSequence): boolean { 2920 messageSequence.writeInt(this.num); 2921 messageSequence.writeString(this.str); 2922 return true; 2923 } 2924 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2925 this.num = messageSequence.readInt(); 2926 this.str = messageSequence.readString(); 2927 return true; 2928 } 2929 } 2930 let parcelable = new MyParcelable(1, "aaa"); 2931 let parcelable2 = new MyParcelable(2, "bbb"); 2932 let parcelable3 = new MyParcelable(3, "ccc"); 2933 let a = [parcelable, parcelable2, parcelable3]; 2934 let data = rpc.MessageSequence.create(); 2935 data.writeParcelableArray(a); 2936 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2937 try { 2938 data.readParcelableArray(b); 2939 } catch (error) { 2940 let e: BusinessError = error as BusinessError; 2941 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2942 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2943 } 2944 ``` 2945 2946### writeRemoteObjectArray 2947 2948writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2949 2950Writes an **IRemoteObject** array to this **MessageSequence** object. 2951 2952**System capability**: SystemCapability.Communication.IPC.Core 2953 2954**Parameters** 2955 2956| Name | Type | Mandatory| Description | 2957| ----------- | --------------- | ---- | ---------------------------------------------- | 2958| objectArray | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to write.| 2959 2960**Error codes** 2961 2962For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2963 2964 | ID| Error Message| 2965 | -------- | -------- | 2966 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The obtained remoteObject is null. | 2967 | 1900009 | Failed to write data to the message sequence. | 2968 2969**Example** 2970 2971 ```ts 2972 import { hilog } from '@kit.PerformanceAnalysisKit'; 2973 import { BusinessError } from '@kit.BasicServicesKit'; 2974 2975 class TestRemoteObject extends rpc.RemoteObject { 2976 constructor(descriptor: string) { 2977 super(descriptor); 2978 this.modifyLocalInterface(this, descriptor); 2979 } 2980 2981 asObject(): rpc.IRemoteObject { 2982 return this; 2983 } 2984 } 2985 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2986 let data = rpc.MessageSequence.create(); 2987 try { 2988 data.writeRemoteObjectArray(a); 2989 } catch (error) { 2990 let e: BusinessError = error as BusinessError; 2991 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2992 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2993 } 2994 ``` 2995 2996### readRemoteObjectArray 2997 2998readRemoteObjectArray(objects: IRemoteObject[]): void 2999 3000Reads the **IRemoteObject** array from this **MessageSequence** object. 3001 3002**System capability**: SystemCapability.Communication.IPC.Core 3003 3004**Parameters** 3005 3006| Name | Type | Mandatory| Description | 3007| ------- | --------------- | ---- | ---------------------------------------------- | 3008| objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 3009 3010**Error codes** 3011 3012For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3013 3014 | ID| Error Message| 3015 | -------- | -------- | 3016 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The length of the array passed when reading is not equal to the length passed when writing to the array. | 3017 | 1900010 | Failed to read data from the message sequence. | 3018 3019**Example** 3020 3021 ```ts 3022 import { hilog } from '@kit.PerformanceAnalysisKit'; 3023 import { BusinessError } from '@kit.BasicServicesKit'; 3024 3025 class TestRemoteObject extends rpc.RemoteObject { 3026 constructor(descriptor: string) { 3027 super(descriptor); 3028 this.modifyLocalInterface(this, descriptor); 3029 } 3030 3031 asObject(): rpc.IRemoteObject { 3032 return this; 3033 } 3034 } 3035 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3036 let data = rpc.MessageSequence.create(); 3037 data.writeRemoteObjectArray(a); 3038 let b: Array<rpc.IRemoteObject> = new Array(3); 3039 try { 3040 data.readRemoteObjectArray(b); 3041 } catch (error) { 3042 let e: BusinessError = error as BusinessError; 3043 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3044 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3045 } 3046 ``` 3047 3048### readRemoteObjectArray 3049 3050readRemoteObjectArray(): IRemoteObject[] 3051 3052Reads the **IRemoteObject** array from this **MessageSequence** object. 3053 3054**System capability**: SystemCapability.Communication.IPC.Core 3055 3056**Return value** 3057 3058| Type | Description | 3059| --------------- | --------------------------- | 3060| [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 3061 3062**Error codes** 3063 3064For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3065 3066 | ID| Error Message| 3067 | -------- | -------- | 3068 | 1900010 | Failed to read data from the message sequence. | 3069 3070**Example** 3071 3072 ```ts 3073 import { hilog } from '@kit.PerformanceAnalysisKit'; 3074 import { BusinessError } from '@kit.BasicServicesKit'; 3075 3076 class TestRemoteObject extends rpc.RemoteObject { 3077 constructor(descriptor: string) { 3078 super(descriptor); 3079 this.modifyLocalInterface(this, descriptor); 3080 } 3081 3082 asObject(): rpc.IRemoteObject { 3083 return this; 3084 } 3085 } 3086 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3087 let data = rpc.MessageSequence.create(); 3088 data.writeRemoteObjectArray(a); 3089 try { 3090 let b = data.readRemoteObjectArray(); 3091 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3092 } catch (error) { 3093 let e: BusinessError = error as BusinessError; 3094 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3095 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3096 } 3097 ``` 3098 3099### closeFileDescriptor 3100 3101static closeFileDescriptor(fd: number): void 3102 3103Closes a file descriptor. This API is a static method. 3104 3105**System capability**: SystemCapability.Communication.IPC.Core 3106 3107**Parameters** 3108 3109 | Name| Type | Mandatory| Description | 3110 | ------ | ------ | ---- | -------------------- | 3111 | fd | number | Yes | File descriptor to close.| 3112 3113**Error codes** 3114 3115For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3116 3117 | ID| Error Message| 3118 | -------- | -------- | 3119 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3120 3121**Example** 3122 3123 ```ts 3124 import { fileIo } from '@kit.CoreFileKit'; 3125 import { hilog } from '@kit.PerformanceAnalysisKit'; 3126 import { BusinessError } from '@kit.BasicServicesKit'; 3127 3128 let filePath = "path/to/file"; 3129 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3130 try { 3131 rpc.MessageSequence.closeFileDescriptor(file.fd); 3132 } catch (error) { 3133 let e: BusinessError = error as BusinessError; 3134 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3135 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3136 } 3137 ``` 3138 3139### dupFileDescriptor 3140 3141static dupFileDescriptor(fd: number): number 3142 3143Duplicates a file descriptor. This API is a static method. 3144 3145**System capability**: SystemCapability.Communication.IPC.Core 3146 3147**Parameters** 3148 3149 | Name| Type | Mandatory| Description | 3150 | ------ | ------ | ---- | ------------------------ | 3151 | fd | number | Yes | File descriptor to duplicate.| 3152 3153**Return value** 3154 3155 | Type | Description | 3156 | ------ | -------------------- | 3157 | number | New file descriptor.| 3158 3159**Error codes** 3160 3161For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3162 3163 | ID| Error Message| 3164 | -------- | -------- | 3165 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3166 | 1900013 | Failed to call dup. | 3167 3168**Example** 3169 3170 ```ts 3171 import { fileIo } from '@kit.CoreFileKit'; 3172 import { hilog } from '@kit.PerformanceAnalysisKit'; 3173 import { BusinessError } from '@kit.BasicServicesKit'; 3174 3175 let filePath = "path/to/file"; 3176 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3177 try { 3178 rpc.MessageSequence.dupFileDescriptor(file.fd); 3179 } catch (error) { 3180 let e: BusinessError = error as BusinessError; 3181 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3182 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3183 } 3184 ``` 3185 3186### containFileDescriptors 3187 3188containFileDescriptors(): boolean 3189 3190Checks whether this **MessageSequence** object contains file descriptors. 3191 3192**System capability**: SystemCapability.Communication.IPC.Core 3193 3194**Return value** 3195 3196 | Type | Description | 3197 | ------- | -------------------------------------------------------------------- | 3198 | boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 3199 3200**Example** 3201 3202 ```ts 3203 import { fileIo } from '@kit.CoreFileKit'; 3204 import { hilog } from '@kit.PerformanceAnalysisKit'; 3205 import { BusinessError } from '@kit.BasicServicesKit'; 3206 3207 let sequence = new rpc.MessageSequence(); 3208 let filePath = "path/to/file"; 3209 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3210 try { 3211 sequence.writeFileDescriptor(file.fd); 3212 } catch (error) { 3213 let e: BusinessError = error as BusinessError; 3214 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3215 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3216 } 3217 try { 3218 let containFD = sequence.containFileDescriptors(); 3219 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3220 } catch (error) { 3221 let e: BusinessError = error as BusinessError; 3222 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3223 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3224 } 3225 ``` 3226 3227### writeFileDescriptor 3228 3229writeFileDescriptor(fd: number): void 3230 3231Writes a file descriptor to this **MessageSequence** object. 3232 3233**System capability**: SystemCapability.Communication.IPC.Core 3234 3235**Parameters** 3236 3237 | Name| Type | Mandatory| Description | 3238 | ------ | ------ | ---- | ------------ | 3239 | fd | number | Yes | File descriptor to write.| 3240 3241**Error codes** 3242 3243For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3244 3245 | ID| Error Message| 3246 | -------- | -------- | 3247 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3248 | 1900009 | Failed to write data to the message sequence. | 3249 3250**Example** 3251 3252 ```ts 3253 import { fileIo } from '@kit.CoreFileKit'; 3254 import { hilog } from '@kit.PerformanceAnalysisKit'; 3255 import { BusinessError } from '@kit.BasicServicesKit'; 3256 3257 let sequence = new rpc.MessageSequence(); 3258 let filePath = "path/to/file"; 3259 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3260 try { 3261 sequence.writeFileDescriptor(file.fd); 3262 } catch (error) { 3263 let e: BusinessError = error as BusinessError; 3264 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3265 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3266 } 3267 ``` 3268 3269### readFileDescriptor 3270 3271readFileDescriptor(): number 3272 3273Reads the file descriptor from this **MessageSequence** object. 3274 3275**System capability**: SystemCapability.Communication.IPC.Core 3276 3277**Return value** 3278 3279 | Type | Description | 3280 | ------ | ---------------- | 3281 | number | File descriptor read.| 3282 3283**Error codes** 3284 3285For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3286 3287 | ID| Error Message| 3288 | -------- | -------- | 3289 | 1900010 | Failed to read data from the message sequence. | 3290 3291**Example** 3292 3293 ```ts 3294 import { fileIo } from '@kit.CoreFileKit'; 3295 import { hilog } from '@kit.PerformanceAnalysisKit'; 3296 import { BusinessError } from '@kit.BasicServicesKit'; 3297 3298 let sequence = new rpc.MessageSequence(); 3299 let filePath = "path/to/file"; 3300 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3301 try { 3302 sequence.writeFileDescriptor(file.fd); 3303 } catch (error) { 3304 let e: BusinessError = error as BusinessError; 3305 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3306 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3307 } 3308 try { 3309 let readFD = sequence.readFileDescriptor(); 3310 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3311 } catch (error) { 3312 let e: BusinessError = error as BusinessError; 3313 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3314 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3315 } 3316 ``` 3317 3318### writeAshmem 3319 3320writeAshmem(ashmem: Ashmem): void 3321 3322Writes an anonymous shared object to this **MessageSequence** object. 3323 3324**System capability**: SystemCapability.Communication.IPC.Core 3325 3326**Parameters** 3327 3328| Name| Type | Mandatory| Description | 3329| ------ | ------ | ---- | ------------------------------------- | 3330| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 3331 3332**Error codes** 3333 3334For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3335 3336 | ID| Error Message| 3337 | -------- | ------- | 3338 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter is not an instance of the Ashmem object. | 3339 | 1900009 | Failed to write data to the message sequence. | 3340 3341**Example** 3342 3343 ```ts 3344 import { hilog } from '@kit.PerformanceAnalysisKit'; 3345 import { BusinessError } from '@kit.BasicServicesKit'; 3346 3347 let sequence = new rpc.MessageSequence(); 3348 let ashmem: rpc.Ashmem | undefined = undefined; 3349 try { 3350 ashmem = rpc.Ashmem.create("ashmem", 1024); 3351 try { 3352 sequence.writeAshmem(ashmem); 3353 } catch (error) { 3354 let e: BusinessError = error as BusinessError; 3355 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3356 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3357 } 3358 } catch (error) { 3359 let e: BusinessError = error as BusinessError; 3360 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3361 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3362 } 3363 ``` 3364 3365### readAshmem 3366 3367readAshmem(): Ashmem 3368 3369Reads the anonymous shared object from this **MessageSequence** object. 3370 3371**System capability**: SystemCapability.Communication.IPC.Core 3372 3373**Return value** 3374 3375| Type | Description | 3376| ------ | ------------------ | 3377| [Ashmem](#ashmem8) | Anonymous share object obtained.| 3378 3379**Error codes** 3380 3381For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3382 3383 | ID| Error Message| 3384 | -------- | -------- | 3385 | 1900010 | Failed to read data from the message sequence. | 3386 3387**Example** 3388 3389 ```ts 3390 import { hilog } from '@kit.PerformanceAnalysisKit'; 3391 import { BusinessError } from '@kit.BasicServicesKit'; 3392 3393 let sequence = new rpc.MessageSequence(); 3394 let ashmem: rpc.Ashmem | undefined = undefined; 3395 try { 3396 ashmem = rpc.Ashmem.create("ashmem", 1024); 3397 try { 3398 sequence.writeAshmem(ashmem); 3399 } catch (error) { 3400 let e: BusinessError = error as BusinessError; 3401 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3402 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3403 } 3404 } catch (error) { 3405 let e: BusinessError = error as BusinessError; 3406 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3407 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3408 } 3409 try { 3410 sequence.readAshmem(); 3411 } catch (error) { 3412 let e: BusinessError = error as BusinessError; 3413 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3414 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3415 } 3416 ``` 3417 3418### getRawDataCapacity 3419 3420getRawDataCapacity(): number 3421 3422Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3423 3424**System capability**: SystemCapability.Communication.IPC.Core 3425 3426**Return value** 3427 3428 | Type | Description | 3429 | ------ | ------------------------------------------------------------ | 3430 | number | Maximum amount of raw data that **MessageSequence** can hold, that is, 128 MB.| 3431 3432**Example** 3433 3434 ```ts 3435 import { hilog } from '@kit.PerformanceAnalysisKit'; 3436 3437 let sequence = new rpc.MessageSequence(); 3438 let result = sequence.getRawDataCapacity(); 3439 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3440 ``` 3441 3442### writeRawData<sup>(deprecated)</sup> 3443 3444writeRawData(rawData: number[], size: number): void 3445 3446Writes raw data to this **MessageSequence** object. 3447 3448> **NOTE** 3449> 3450> This API is deprecated since API version 11. Use [writeRawDataBuffer](#writerawdatabuffer11) instead. 3451> 3452> This API cannot be called for multiple times in one parcel communication. 3453> When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration. 3454 3455**System capability**: SystemCapability.Communication.IPC.Core 3456 3457**Parameters** 3458 3459 | Name | Type | Mandatory| Description | 3460 | ------- | -------- | ---- | ---------------------------------- | 3461 | rawData | number[] | Yes | Raw data to write. The size cannot exceed 128 MB.| 3462 | size | number | Yes | Size of the raw data, in bytes.| 3463 3464**Error codes** 3465 3466For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3467 3468 | ID| Error Message| 3469 | -------- | -------- | 3470 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The transferred size cannot be obtained; <br> 5.The transferred size is less than or equal to 0;<br> 6.The element does not exist in the array; <br> 7.Failed to obtain typedArray information; <br> 8.The array is not of type int32; <br> 9.The length of typedarray is smaller than the size of the original data sent. | 3471 | 1900009 | Failed to write data to the message sequence. | 3472 3473**Example** 3474 3475 ```ts 3476 import { hilog } from '@kit.PerformanceAnalysisKit'; 3477 import { BusinessError } from '@kit.BasicServicesKit'; 3478 3479 let sequence = new rpc.MessageSequence(); 3480 let arr = [1, 2, 3, 4, 5]; 3481 try { 3482 sequence.writeRawData(arr, arr.length); 3483 } catch (error) { 3484 let e: BusinessError = error as BusinessError; 3485 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3486 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3487 } 3488 ``` 3489 3490### writeRawDataBuffer<sup>11+</sup> 3491 3492writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3493 3494Writes raw data to this **MessageSequence** object. 3495 3496> **NOTE** 3497> 3498> This API cannot be called for multiple times in one parcel communication. 3499> When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration. 3500 3501**System capability**: SystemCapability.Communication.IPC.Core 3502 3503**Parameters** 3504 3505 | Name | Type | Mandatory| Description | 3506 | ------- | ----------- | ---- | ------------------------------------ | 3507 | rawData | ArrayBuffer | Yes | Raw data to write. The size cannot exceed 128 MB.| 3508 | size | number | Yes | Size of the raw data, in bytes. | 3509 3510**Error codes** 3511 3512For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3513 3514 | ID| Error Message| 3515 | -------- | -------- | 3516 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain arrayBuffer information; <br> 4.The transferred size cannot be obtained; <br> 5.The transferred size is less than or equal to 0; <br> 6.The transferred size is greater than the byte length of ArrayBuffer. | 3517 | 1900009 | Failed to write data to the message sequence. | 3518 3519**Example** 3520 3521 ```ts 3522 import { hilog } from '@kit.PerformanceAnalysisKit'; 3523 import { BusinessError } from '@kit.BasicServicesKit'; 3524 3525 let buffer = new ArrayBuffer(64 * 1024); 3526 let int32View = new Int32Array(buffer); 3527 for (let i = 0; i < int32View.length; i++) { 3528 int32View[i] = i * 2 + 1; 3529 } 3530 let size = buffer.byteLength; 3531 let sequence = new rpc.MessageSequence(); 3532 try { 3533 sequence.writeRawDataBuffer(buffer, size); 3534 } catch (error) { 3535 let e: BusinessError = error as BusinessError; 3536 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3537 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3538 } 3539 ``` 3540 3541### readRawData<sup>(deprecated)</sup> 3542 3543readRawData(size: number): number[] 3544 3545Reads raw data from this **MessageSequence** object. 3546 3547> **NOTE** 3548> 3549> This API is deprecated since API version 11. Use [readRawDataBuffer](#readrawdatabuffer11) instead. 3550 3551 3552**System capability**: SystemCapability.Communication.IPC.Core 3553 3554**Parameters** 3555 3556 | Name| Type | Mandatory| Description | 3557 | ------ | ------ | ---- | ------------------------ | 3558 | size | number | Yes | Size of the raw data to read.| 3559 3560**Return value** 3561 3562 | Type | Description | 3563 | -------- | ------------------------------ | 3564 | number[] | Raw data obtained, in bytes.| 3565 3566**Error codes** 3567 3568For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3569 3570 | ID| Error Message| 3571 | -------- | -------- | 3572 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3573 | 1900010 | Failed to read data from the message sequence. | 3574 3575**Example** 3576 3577 ```ts 3578 import { hilog } from '@kit.PerformanceAnalysisKit'; 3579 import { BusinessError } from '@kit.BasicServicesKit'; 3580 3581 let sequence = new rpc.MessageSequence(); 3582 let arr = [1, 2, 3, 4, 5]; 3583 try { 3584 sequence.writeRawData(arr, arr.length); 3585 } catch (error) { 3586 let e: BusinessError = error as BusinessError; 3587 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3588 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3589 } 3590 try { 3591 let result = sequence.readRawData(5); 3592 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3593 } catch (error) { 3594 let e: BusinessError = error as BusinessError; 3595 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3596 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3597 } 3598 ``` 3599 3600### readRawDataBuffer<sup>11+</sup> 3601 3602readRawDataBuffer(size: number): ArrayBuffer 3603 3604Reads raw data from this **MessageSequence** object. 3605 3606**System capability**: SystemCapability.Communication.IPC.Core 3607 3608**Parameters** 3609 3610 | Name| Type | Mandatory| Description | 3611 | ------ | ------ | ---- | ------------------------ | 3612 | size | number | Yes | Size of the raw data to read.| 3613 3614**Return value** 3615 3616 | Type | Description | 3617 | -------- | ------------------------------ | 3618 | ArrayBuffer | Raw data obtained, in bytes.| 3619 3620**Error codes** 3621 3622For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3623 3624 | ID| Error Message| 3625 | -------- | -------- | 3626 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3627 | 1900010 | Failed to read data from the message sequence. | 3628 3629**Example** 3630 3631 ```ts 3632 import { hilog } from '@kit.PerformanceAnalysisKit'; 3633 import { BusinessError } from '@kit.BasicServicesKit'; 3634 3635 let buffer = new ArrayBuffer(64 * 1024); 3636 let int32View = new Int32Array(buffer); 3637 for (let i = 0; i < int32View.length; i++) { 3638 int32View[i] = i * 2 + 1; 3639 } 3640 let size = buffer.byteLength; 3641 let sequence = new rpc.MessageSequence(); 3642 try { 3643 sequence.writeRawDataBuffer(buffer, size); 3644 } catch (error) { 3645 let e: BusinessError = error as BusinessError; 3646 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3647 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3648 } 3649 try { 3650 let result = sequence.readRawDataBuffer(size); 3651 let readInt32View = new Int32Array(result); 3652 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3653 } catch (error) { 3654 let e: BusinessError = error as BusinessError; 3655 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3656 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3657 } 3658 ``` 3659 3660### writeArrayBuffer<sup>12+</sup> 3661 3662writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3663 3664Writes data of the ArrayBuffer type to this **MessageSequence** object. 3665 3666**System capability**: SystemCapability.Communication.IPC.Core 3667 3668**Parameters** 3669 3670 | Name | Type | Mandatory| Description | 3671 | --------- | ------------------------- | ---- | --------------------------- | 3672 | buf | ArrayBuffer | Yes | Data to write. | 3673 | typeCode | [TypeCode](#typecode12) | Yes | TypedArray type of the ArrayBuffer data.<br>The underlying write mode is determined based on the enum value of **TypeCode** passed by the service.| 3674 3675**Error codes** 3676 3677For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3678 3679 | ID| Error Message| 3680 | -------- | -------- | 3681 | 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The obtained value of typeCode is incorrect; <br> 5.Failed to obtain arrayBuffer information. | 3682 | 1900009 | Failed to write data to the message sequence. | 3683 3684**Example** 3685 3686 ```ts 3687 // In this example, the value of TypeCode is Int16Array. 3688 import { hilog } from '@kit.PerformanceAnalysisKit'; 3689 import { BusinessError } from '@kit.BasicServicesKit'; 3690 3691 const data = rpc.MessageSequence.create(); 3692 3693 let buffer = new ArrayBuffer(10); 3694 let int16View = new Int16Array(buffer); 3695 for (let i = 0; i < int16View.length; i++) { 3696 int16View[i] = i * 2 + 1; 3697 } 3698 3699 try { 3700 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3701 } catch (error) { 3702 let e: BusinessError = error as BusinessError; 3703 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorCode ' + e.code); 3704 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorMessage ' + e.message); 3705 } 3706 ``` 3707 3708### readArrayBuffer<sup>12+</sup> 3709 3710readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3711 3712Reads data of the ArrayBuffer type from this **MessageSequence**. 3713 3714**System capability**: SystemCapability.Communication.IPC.Core 3715 3716**Parameters** 3717 3718 | Name | Type | Mandatory| Description | 3719 | -------- | ----------------------- | ---- | ------------------------| 3720 | typeCode | [TypeCode](#typecode12) | Yes | TypedArray type of the ArrayBuffer data.<br>The underlying read mode is determined based on the enum value of **TypeCode** passed by the service. | 3721 3722**Return value** 3723 3724 | Type | Description | 3725 | -------- | -------------------------------------------- | 3726 | ArrayBuffer | Data of the ArrayBuffer type read, in bytes.| 3727 3728**Error codes** 3729 3730For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3731 3732 | ID| Error Message| 3733 | -------- | -------- | 3734 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The obtained value of typeCode is incorrect; | 3735 | 1900010 | Failed to read data from the message sequence. | 3736 3737**Example** 3738 3739 ```ts 3740 // In this example, the value of TypeCode is Int16Array. 3741 import { hilog } from '@kit.PerformanceAnalysisKit'; 3742 import { BusinessError } from '@kit.BasicServicesKit'; 3743 3744 const data = rpc.MessageSequence.create(); 3745 3746 let buffer = new ArrayBuffer(10); 3747 let int16View = new Int16Array(buffer); 3748 for (let i = 0; i < int16View.length; i++) { 3749 int16View[i] = i * 2 + 1; 3750 } 3751 3752 try { 3753 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3754 } catch (error) { 3755 let e: BusinessError = error as BusinessError; 3756 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorCode ' + e.code); 3757 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffer fail, errorMessage ' + e.message); 3758 } 3759 try { 3760 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3761 let readInt16View = new Int16Array(result); 3762 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3763 } catch (error) { 3764 let e: BusinessError = error as BusinessError; 3765 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3766 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3767 } 3768 ``` 3769 3770## MessageParcel<sup>(deprecated)</sup> 3771 3772Provides 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. 3773 3774> **NOTE** 3775> 3776> This API is deprecated since API version 9. Use [MessageSequence](#messagesequence9) instead. 3777 3778### create 3779 3780static create(): MessageParcel 3781 3782Creates a **MessageParcel** object. This method is a static method. 3783 3784**System capability**: SystemCapability.Communication.IPC.Core 3785 3786**Return value** 3787 3788 | Type | Description | 3789 | ------------- | ----------------------------- | 3790 | [MessageParcel](#messageparceldeprecated) | **MessageParcel** object created.| 3791 3792**Example** 3793 3794 ```ts 3795 import { hilog } from '@kit.PerformanceAnalysisKit'; 3796 3797 let data = rpc.MessageParcel.create(); 3798 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3799 3800 // When the MessageParcel object is no longer used, the service calls the reclaim method to release resources. 3801 data.reclaim(); 3802 ``` 3803 3804### reclaim 3805 3806reclaim(): void 3807 3808Reclaims the **MessageParcel** object that is no longer used. 3809 3810**System capability**: SystemCapability.Communication.IPC.Core 3811 3812**Example** 3813 3814 ```ts 3815 let reply = rpc.MessageParcel.create(); 3816 reply.reclaim(); 3817 ``` 3818 3819### writeRemoteObject 3820 3821writeRemoteObject(object: IRemoteObject): boolean 3822 3823Serializes a remote object and writes it to this **MessageParcel** object. 3824 3825**System capability**: SystemCapability.Communication.IPC.Core 3826 3827**Parameters** 3828 3829 | Name| Type | Mandatory| Description | 3830 | ------ | ------------------------------- | ---- | --------------------------------------- | 3831 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3832 3833**Return value** 3834 3835 | Type | Description | 3836 | ------- | ----------------------------------------- | 3837 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3838 3839**Example** 3840 3841 ```ts 3842 import { hilog } from '@kit.PerformanceAnalysisKit'; 3843 3844 class MyDeathRecipient implements rpc.DeathRecipient { 3845 onRemoteDied() { 3846 hilog.info(0x0000, 'testTag', 'server died'); 3847 } 3848 } 3849 class TestRemoteObject extends rpc.RemoteObject { 3850 constructor(descriptor: string) { 3851 super(descriptor); 3852 } 3853 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3854 return true; 3855 } 3856 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3857 return true; 3858 } 3859 isObjectDead(): boolean { 3860 return false; 3861 } 3862 } 3863 let data = rpc.MessageParcel.create(); 3864 let testRemoteObject = new TestRemoteObject("testObject"); 3865 data.writeRemoteObject(testRemoteObject); 3866 ``` 3867 3868### readRemoteObject 3869 3870readRemoteObject(): IRemoteObject 3871 3872Reads 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. 3873 3874**System capability**: SystemCapability.Communication.IPC.Core 3875 3876**Return value** 3877 3878 | Type | Description | 3879 | ------------------------------- | ------------------ | 3880 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 3881 3882**Example** 3883 3884 ```ts 3885 import { hilog } from '@kit.PerformanceAnalysisKit'; 3886 3887 class MyDeathRecipient implements rpc.DeathRecipient { 3888 onRemoteDied() { 3889 hilog.info(0x0000, 'testTag', 'server died'); 3890 } 3891 } 3892 class TestRemoteObject extends rpc.RemoteObject { 3893 constructor(descriptor: string) { 3894 super(descriptor); 3895 } 3896 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3897 return true; 3898 } 3899 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3900 return true; 3901 } 3902 isObjectDead(): boolean { 3903 return false; 3904 } 3905 } 3906 let data = rpc.MessageParcel.create(); 3907 let testRemoteObject = new TestRemoteObject("testObject"); 3908 data.writeRemoteObject(testRemoteObject); 3909 let proxy = data.readRemoteObject(); 3910 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3911 ``` 3912 3913### writeInterfaceToken 3914 3915writeInterfaceToken(token: string): boolean 3916 3917Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3918 3919**System capability**: SystemCapability.Communication.IPC.Core 3920 3921**Parameters** 3922 3923 | Name| Type | Mandatory| Description | 3924 | ------ | ------ | ---- | ------------------ | 3925 | token | string | Yes | Interface token to write.| 3926 3927**Return value** 3928 3929 | Type | Description | 3930 | ------- | ----------------------------------------- | 3931 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3932 3933**Example** 3934 3935 ```ts 3936 import { hilog } from '@kit.PerformanceAnalysisKit'; 3937 3938 let data = rpc.MessageParcel.create(); 3939 let result = data.writeInterfaceToken("aaa"); 3940 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3941 ``` 3942 3943### readInterfaceToken 3944 3945readInterfaceToken(): string 3946 3947Reads 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. 3948 3949**System capability**: SystemCapability.Communication.IPC.Core 3950 3951**Return value** 3952 3953 | Type | Description | 3954 | ------ | ------------------------ | 3955 | string | Interface token obtained.| 3956 3957**Example** 3958 3959 ```ts 3960 import { hilog } from '@kit.PerformanceAnalysisKit'; 3961 3962 class Stub extends rpc.RemoteObject { 3963 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3964 let interfaceToken = data.readInterfaceToken(); 3965 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3966 return true; 3967 } 3968 } 3969 ``` 3970 3971### getSize 3972 3973getSize(): number 3974 3975Obtains the data size of this **MessageParcel** object. 3976 3977**System capability**: SystemCapability.Communication.IPC.Core 3978 3979**Return value** 3980 3981 | Type | Description | 3982 | ------ | --------------------------------------------- | 3983 | number | Size of the **MessageParcel** object obtained, in bytes.| 3984 3985**Example** 3986 3987 ```ts 3988 import { hilog } from '@kit.PerformanceAnalysisKit'; 3989 3990 let data = rpc.MessageParcel.create(); 3991 let size = data.getSize(); 3992 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3993 ``` 3994 3995### getCapacity 3996 3997getCapacity(): number 3998 3999Obtains the capacity of this **MessageParcel** object. 4000 4001**System capability**: SystemCapability.Communication.IPC.Core 4002 4003**Return value** 4004 4005 | Type | Description | 4006 | ------ | --------------------------------------------- | 4007 | number | **MessageParcel** capacity obtained, in bytes.| 4008 4009**Example** 4010 4011 ```ts 4012 import { hilog } from '@kit.PerformanceAnalysisKit'; 4013 4014 let data = rpc.MessageParcel.create(); 4015 let result = data.getCapacity(); 4016 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 4017 ``` 4018 4019### setSize 4020 4021setSize(size: number): boolean 4022 4023Sets the size of data contained in this **MessageParcel** object. 4024 4025**System capability**: SystemCapability.Communication.IPC.Core 4026 4027**Parameters** 4028 4029 | Name| Type | Mandatory| Description | 4030 | ------ | ------ | ---- | ------------------------------------------- | 4031 | size | number | Yes | Data size to set, in bytes.| 4032 4033**Return value** 4034 4035 | Type | Description | 4036 | ------- | --------------------------------- | 4037 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4038 4039**Example** 4040 4041 ```ts 4042 import { hilog } from '@kit.PerformanceAnalysisKit'; 4043 4044 let data = rpc.MessageParcel.create(); 4045 let setSize = data.setSize(16); 4046 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4047 ``` 4048 4049### setCapacity 4050 4051setCapacity(size: number): boolean 4052 4053Sets the storage capacity of this **MessageParcel** object. 4054 4055**System capability**: SystemCapability.Communication.IPC.Core 4056 4057**Parameters** 4058 4059 | Name| Type | Mandatory| Description | 4060 | ------ | ------ | ---- | ------------------------------------------- | 4061 | size | number | Yes | Storage capacity to set, in bytes.| 4062 4063**Return value** 4064 4065 | Type | Description | 4066 | ------- | --------------------------------- | 4067 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4068 4069**Example** 4070 4071 ```ts 4072 import { hilog } from '@kit.PerformanceAnalysisKit'; 4073 4074 let data = rpc.MessageParcel.create(); 4075 let result = data.setCapacity(100); 4076 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4077 ``` 4078 4079### getWritableBytes 4080 4081getWritableBytes(): number 4082 4083Obtains the writable capacity of this **MessageParcel** object. 4084 4085**System capability**: SystemCapability.Communication.IPC.Core 4086 4087**Return value** 4088 4089 | Type | Description | 4090 | ------ | --------------------------------------------------- | 4091 | number | **MessageParcel** writable capacity obtained, in bytes.| 4092 4093**Example** 4094 4095 ```ts 4096 import { hilog } from '@kit.PerformanceAnalysisKit'; 4097 4098 class Stub extends rpc.RemoteObject { 4099 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4100 let getWritableBytes = data.getWritableBytes(); 4101 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4102 return true; 4103 } 4104 } 4105 ``` 4106 4107### getReadableBytes 4108 4109getReadableBytes(): number 4110 4111Obtains the readable capacity of this **MessageParcel** object. 4112 4113**System capability**: SystemCapability.Communication.IPC.Core 4114 4115**Return value** 4116 4117 | Type | Description | 4118 | ------ | --------------------------------------------------- | 4119 | number | **MessageParcel** object readable capacity, in bytes.| 4120 4121**Example** 4122 4123 ```ts 4124 import { hilog } from '@kit.PerformanceAnalysisKit'; 4125 4126 class Stub extends rpc.RemoteObject { 4127 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4128 let result = data.getReadableBytes(); 4129 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4130 return true; 4131 } 4132 } 4133 ``` 4134 4135### getReadPosition 4136 4137getReadPosition(): number 4138 4139Obtains the read position of this **MessageParcel** object. 4140 4141**System capability**: SystemCapability.Communication.IPC.Core 4142 4143**Return value** 4144 4145 | Type | Description | 4146 | ------ | --------------------------------------- | 4147 | number | Current read position of the **MessageParcel** object.| 4148 4149**Example** 4150 4151 ```ts 4152 import { hilog } from '@kit.PerformanceAnalysisKit'; 4153 4154 let data = rpc.MessageParcel.create(); 4155 let readPos = data.getReadPosition(); 4156 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4157 ``` 4158 4159### getWritePosition 4160 4161getWritePosition(): number 4162 4163Obtains the write position of this **MessageParcel** object. 4164 4165**System capability**: SystemCapability.Communication.IPC.Core 4166 4167**Return value** 4168 4169 | Type | Description | 4170 | ------ | --------------------------------------- | 4171 | number | Current write position of the **MessageParcel** object.| 4172 4173**Example** 4174 4175 ```ts 4176 import { hilog } from '@kit.PerformanceAnalysisKit'; 4177 4178 let data = rpc.MessageParcel.create(); 4179 data.writeInt(10); 4180 let bwPos = data.getWritePosition(); 4181 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4182 ``` 4183 4184### rewindRead 4185 4186rewindRead(pos: number): boolean 4187 4188Moves the read pointer to the specified position. 4189 4190**System capability**: SystemCapability.Communication.IPC.Core 4191 4192**Parameters** 4193 4194 | Name| Type | Mandatory| Description | 4195 | ------ | ------ | ---- | ------------------------ | 4196 | pos | number | Yes | Position from which data is to read.| 4197 4198**Return value** 4199 4200 | Type | Description | 4201 | ------- | ------------------------------------------------- | 4202 | boolean | Returns **true** if the read position changes; returns **false** otherwise.| 4203 4204**Example** 4205 4206 ```ts 4207 import { hilog } from '@kit.PerformanceAnalysisKit'; 4208 4209 let data = rpc.MessageParcel.create(); 4210 data.writeInt(12); 4211 data.writeString("parcel"); 4212 let number = data.readInt(); 4213 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4214 data.rewindRead(0); 4215 let number2 = data.readInt(); 4216 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4217 ``` 4218 4219### rewindWrite 4220 4221rewindWrite(pos: number): boolean 4222 4223Moves the write pointer to the specified position. 4224 4225**System capability**: SystemCapability.Communication.IPC.Core 4226 4227**Parameters** 4228 4229 | Name| Type | Mandatory| Description | 4230 | ------ | ------ | ---- | ------------------------ | 4231 | pos | number | Yes | Position from which data is to write.| 4232 4233**Return value** 4234 4235 | Type | Description | 4236 | ------- | --------------------------------------------- | 4237 | boolean | Returns **true** if the write position changes; returns **false** otherwise.| 4238 4239**Example** 4240 4241 ```ts 4242 import { hilog } from '@kit.PerformanceAnalysisKit'; 4243 4244 let data = rpc.MessageParcel.create(); 4245 data.writeInt(4); 4246 data.rewindWrite(0); 4247 data.writeInt(5); 4248 let number = data.readInt(); 4249 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4250 ``` 4251 4252### writeByte 4253 4254writeByte(val: number): boolean 4255 4256Writes a Byte value to this **MessageParcel** object. 4257 4258**System capability**: SystemCapability.Communication.IPC.Core 4259 4260**Parameters** 4261 4262 | Name| Type | Mandatory| Description | 4263 | ------ | ------ | ---- | ---------------- | 4264 | val | number | Yes | Byte value to write.| 4265 4266**Return value** 4267 4268 | Type | Description | 4269 | ------- | ----------------------------- | 4270 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4271 4272**Example** 4273 4274 ```ts 4275 import { hilog } from '@kit.PerformanceAnalysisKit'; 4276 4277 let data = rpc.MessageParcel.create(); 4278 let result = data.writeByte(2); 4279 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4280 ``` 4281 4282### readByte 4283 4284readByte(): number 4285 4286Reads the Byte value from this **MessageParcel** object. 4287 4288**System capability**: SystemCapability.Communication.IPC.Core 4289 4290**Return value** 4291 4292 | Type | Description | 4293 | ------ | ------------ | 4294 | number | Byte value read.| 4295 4296**Example** 4297 4298 ```ts 4299 import { hilog } from '@kit.PerformanceAnalysisKit'; 4300 4301 let data = rpc.MessageParcel.create(); 4302 let result = data.writeByte(2); 4303 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4304 let ret = data.readByte(); 4305 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4306 ``` 4307 4308### writeShort 4309 4310writeShort(val: number): boolean 4311 4312Writes a short int value to this **MessageParcel** object. 4313 4314**System capability**: SystemCapability.Communication.IPC.Core 4315 4316**Parameters** 4317 4318 | Name| Type | Mandatory| Description | 4319 | ------ | ------ | ---- | ------------------ | 4320 | val | number | Yes | Short integer to write.| 4321 4322**Return value** 4323 4324 | Type | Description | 4325 | ------- | ----------------------------- | 4326 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4327 4328**Example** 4329 4330 ```ts 4331 import { hilog } from '@kit.PerformanceAnalysisKit'; 4332 4333 let data = rpc.MessageParcel.create(); 4334 let result = data.writeShort(8); 4335 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4336 ``` 4337 4338### readShort 4339 4340readShort(): number 4341 4342Reads the short int value from this **MessageParcel** object. 4343 4344**System capability**: SystemCapability.Communication.IPC.Core 4345 4346**Return value** 4347 4348 | Type | Description | 4349 | ------ | -------------- | 4350 | number | Short integer read.| 4351 4352**Example** 4353 4354 ```ts 4355 import { hilog } from '@kit.PerformanceAnalysisKit'; 4356 4357 let data = rpc.MessageParcel.create(); 4358 let result = data.writeShort(8); 4359 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4360 let ret = data.readShort(); 4361 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4362 ``` 4363 4364### writeInt 4365 4366writeInt(val: number): boolean 4367 4368Writes an int value to this **MessageParcel** object. 4369 4370**System capability**: SystemCapability.Communication.IPC.Core 4371 4372**Parameters** 4373 4374 | Name| Type | Mandatory| Description | 4375 | ------ | ------ | ---- | ---------------- | 4376 | val | number | Yes | Integer to write.| 4377 4378**Return value** 4379 4380 | Type | Description | 4381 | ------- | ----------------------------- | 4382 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4383 4384**Example** 4385 4386 ```ts 4387 import { hilog } from '@kit.PerformanceAnalysisKit'; 4388 4389 let data = rpc.MessageParcel.create(); 4390 let result = data.writeInt(10); 4391 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4392 ``` 4393 4394### readInt 4395 4396readInt(): number 4397 4398Reads the int value from this **MessageParcel** object. 4399 4400**System capability**: SystemCapability.Communication.IPC.Core 4401 4402**Return value** 4403 4404 | Type | Description | 4405 | ------ | ------------ | 4406 | number | Integer read.| 4407 4408**Example** 4409 4410 ```ts 4411 import { hilog } from '@kit.PerformanceAnalysisKit'; 4412 4413 let data = rpc.MessageParcel.create(); 4414 let result = data.writeInt(10); 4415 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4416 let ret = data.readInt(); 4417 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4418 ``` 4419 4420### writeLong 4421 4422writeLong(val: number): boolean 4423 4424Writes a long int value to this **MessageParcel** object. 4425 4426**System capability**: SystemCapability.Communication.IPC.Core 4427 4428**Parameters** 4429 4430 | Name| Type | Mandatory| Description | 4431 | ------ | ------ | ---- | ---------------- | 4432 | val | number | Yes | Long int value to write.| 4433 4434**Return value** 4435 4436 | Type | Description | 4437 | ------- | --------------------------------- | 4438 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4439 4440**Example** 4441 4442 ```ts 4443 import { hilog } from '@kit.PerformanceAnalysisKit'; 4444 4445 let data = rpc.MessageParcel.create(); 4446 let result = data.writeLong(10000); 4447 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4448 ``` 4449 4450### readLong 4451 4452readLong(): number 4453 4454Reads the long int value from this **MessageParcel** object. 4455 4456**System capability**: SystemCapability.Communication.IPC.Core 4457 4458**Return value** 4459 4460 | Type | Description | 4461 | ------ | -------------- | 4462 | number | Long integer read.| 4463 4464**Example** 4465 4466 ```ts 4467 import { hilog } from '@kit.PerformanceAnalysisKit'; 4468 4469 let data = rpc.MessageParcel.create(); 4470 let result = data.writeLong(10000); 4471 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4472 let ret = data.readLong(); 4473 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4474 ``` 4475 4476### writeFloat 4477 4478writeFloat(val: number): boolean 4479 4480Writes a double value to this **MessageParcel** object. 4481 4482**System capability**: SystemCapability.Communication.IPC.Core 4483 4484**Parameters** 4485 4486 | Name| Type | Mandatory| Description | 4487 | ------ | ------ | ---- | ---------------- | 4488 | val | number | Yes | Double value to write.| 4489 4490**Return value** 4491 4492 | Type | Description | 4493 | ------- | --------------------------------- | 4494 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4495 4496**Example** 4497 4498 ```ts 4499 import { hilog } from '@kit.PerformanceAnalysisKit'; 4500 4501 let data = rpc.MessageParcel.create(); 4502 let result = data.writeFloat(1.2); 4503 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4504 ``` 4505 4506### readFloat 4507 4508readFloat(): number 4509 4510Reads the double value from this **MessageParcel** object. 4511 4512**System capability**: SystemCapability.Communication.IPC.Core 4513 4514**Return value** 4515 4516 | Type | Description | 4517 | ------ | ------------ | 4518 | number | Double value read.| 4519 4520**Example** 4521 4522 ```ts 4523 import { hilog } from '@kit.PerformanceAnalysisKit'; 4524 4525 let data = rpc.MessageParcel.create(); 4526 let result = data.writeFloat(1.2); 4527 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4528 let ret = data.readFloat(); 4529 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4530 ``` 4531 4532### writeDouble 4533 4534writeDouble(val: number): boolean 4535 4536Writes a double value to this **MessageParcel** object. 4537 4538**System capability**: SystemCapability.Communication.IPC.Core 4539 4540**Parameters** 4541 4542 | Name| Type | Mandatory| Description | 4543 | ------ | ------ | ---- | ---------------------- | 4544 | val | number | Yes | Double value to write.| 4545 4546**Return value** 4547 4548 | Type | Description | 4549 | ------- | --------------------------------- | 4550 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4551 4552**Example** 4553 4554 ```ts 4555 import { hilog } from '@kit.PerformanceAnalysisKit'; 4556 4557 let data = rpc.MessageParcel.create(); 4558 let result = data.writeDouble(10.2); 4559 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4560 ``` 4561 4562### readDouble 4563 4564readDouble(): number 4565 4566Reads the double value from this **MessageParcel** object. 4567 4568**System capability**: SystemCapability.Communication.IPC.Core 4569 4570**Return value** 4571 4572 | Type | Description | 4573 | ------ | ------------------ | 4574 | number | Double value read.| 4575 4576**Example** 4577 4578 ```ts 4579 import { hilog } from '@kit.PerformanceAnalysisKit'; 4580 4581 let data = rpc.MessageParcel.create(); 4582 let result = data.writeDouble(10.2); 4583 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4584 let ret = data.readDouble(); 4585 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4586 ``` 4587 4588### writeBoolean 4589 4590writeBoolean(val: boolean): boolean 4591 4592Writes a Boolean value to this **MessageParcel** object. 4593 4594**System capability**: SystemCapability.Communication.IPC.Core 4595 4596**Parameters** 4597 4598 | Name| Type | Mandatory| Description | 4599 | ------ | ------- | ---- | ---------------- | 4600 | val | boolean | Yes | Boolean value to write.| 4601 4602**Return value** 4603 4604 | Type | Description | 4605 | ------- | --------------------------------- | 4606 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4607 4608**Example** 4609 4610 ```ts 4611 import { hilog } from '@kit.PerformanceAnalysisKit'; 4612 4613 let data = rpc.MessageParcel.create(); 4614 let result = data.writeBoolean(false); 4615 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4616 ``` 4617 4618### readBoolean 4619 4620readBoolean(): boolean 4621 4622Reads the Boolean value from this **MessageParcel** object. 4623 4624**System capability**: SystemCapability.Communication.IPC.Core 4625 4626**Return value** 4627 4628 | Type | Description | 4629 | ------- | -------------------- | 4630 | boolean | Boolean value read.| 4631 4632**Example** 4633 4634 ```ts 4635 import { hilog } from '@kit.PerformanceAnalysisKit'; 4636 4637 let data = rpc.MessageParcel.create(); 4638 let result = data.writeBoolean(false); 4639 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4640 let ret = data.readBoolean(); 4641 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4642 ``` 4643 4644### writeChar 4645 4646writeChar(val: number): boolean 4647 4648Writes a single character value to this **MessageParcel** object. 4649 4650**System capability**: SystemCapability.Communication.IPC.Core 4651 4652**Parameters** 4653 4654 | Name| Type | Mandatory| Description | 4655 | ------ | ------ | ---- | -------------------- | 4656 | val | number | Yes | **Char** value to write.| 4657 4658**Return value** 4659 4660 | Type | Description | 4661 | ------- | ----------------------------- | 4662 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4663 4664**Example** 4665 4666 ```ts 4667 import { hilog } from '@kit.PerformanceAnalysisKit'; 4668 4669 let data = rpc.MessageParcel.create(); 4670 let result = data.writeChar(97); 4671 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4672 ``` 4673 4674### readChar 4675 4676readChar(): number 4677 4678Reads the single character value from this **MessageParcel** object. 4679 4680**System capability**: SystemCapability.Communication.IPC.Core 4681 4682**Return value** 4683 4684 | Type | Description | 4685 | ------ | ---------------- | 4686 | number | **Char** value read.| 4687 4688**Example** 4689 4690 ```ts 4691 import { hilog } from '@kit.PerformanceAnalysisKit'; 4692 4693 let data = rpc.MessageParcel.create(); 4694 let result = data.writeChar(97); 4695 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4696 let ret = data.readChar(); 4697 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4698 ``` 4699 4700### writeString 4701 4702writeString(val: string): boolean 4703 4704Writes a string to this **MessageParcel** object. 4705 4706**System capability**: SystemCapability.Communication.IPC.Core 4707 4708**Parameters** 4709 4710 | Name| Type | Mandatory| Description | 4711 | ------ | ------ | ---- | ----------------------------------------- | 4712 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 4713 4714**Return value** 4715 4716 | Type | Description | 4717 | ------- | --------------------------------- | 4718 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4719 4720**Example** 4721 4722 ```ts 4723 import { hilog } from '@kit.PerformanceAnalysisKit'; 4724 4725 let data = rpc.MessageParcel.create(); 4726 let result = data.writeString('abc'); 4727 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4728 ``` 4729 4730### readString 4731 4732readString(): string 4733 4734Reads the string from this **MessageParcel** object. 4735 4736**System capability**: SystemCapability.Communication.IPC.Core 4737 4738**Return value** 4739 4740 | Type | Description | 4741 | ------ | -------------- | 4742 | string | String read.| 4743 4744**Example** 4745 4746 ```ts 4747 import { hilog } from '@kit.PerformanceAnalysisKit'; 4748 4749 let data = rpc.MessageParcel.create(); 4750 let result = data.writeString('abc'); 4751 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4752 let ret = data.readString(); 4753 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4754 ``` 4755 4756### writeSequenceable 4757 4758writeSequenceable(val: Sequenceable): boolean 4759 4760Writes a **Sequenceable** object to this **MessageParcel** object. 4761 4762**System capability**: SystemCapability.Communication.IPC.Core 4763 4764**Parameters** 4765 4766 | Name| Type | Mandatory| Description | 4767 | ------ | ----------------------------- | ---- | -------------------- | 4768 | val | [Sequenceable](#sequenceabledeprecated) | Yes | **Sequenceable** object to write.| 4769 4770**Return value** 4771 4772 | Type | Description | 4773 | ------- | -------------------------------- | 4774 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4775 4776**Example** 4777 4778 ```ts 4779 import { hilog } from '@kit.PerformanceAnalysisKit'; 4780 4781 class MySequenceable implements rpc.Sequenceable { 4782 num: number = 0; 4783 str: string = ''; 4784 constructor(num: number, str: string) { 4785 this.num = num; 4786 this.str = str; 4787 } 4788 marshalling(messageParcel: rpc.MessageParcel): boolean { 4789 messageParcel.writeInt(this.num); 4790 messageParcel.writeString(this.str); 4791 return true; 4792 } 4793 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4794 this.num = messageParcel.readInt(); 4795 this.str = messageParcel.readString(); 4796 return true; 4797 } 4798 } 4799 let sequenceable = new MySequenceable(1, "aaa"); 4800 let data = rpc.MessageParcel.create(); 4801 let result = data.writeSequenceable(sequenceable); 4802 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4803 ``` 4804 4805### readSequenceable 4806 4807readSequenceable(dataIn: Sequenceable): boolean 4808 4809Reads member variables from this **MessageParcel** object. 4810 4811**System capability**: SystemCapability.Communication.IPC.Core 4812 4813**Parameters** 4814 4815 | Name| Type | Mandatory | Description | 4816 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4817 | dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4818 4819**Return value** 4820 4821 | Type | Description | 4822 | ------- | ---------------------------------------- | 4823 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4824 4825**Example** 4826 4827 ```ts 4828 import { hilog } from '@kit.PerformanceAnalysisKit'; 4829 4830 class MySequenceable implements rpc.Sequenceable { 4831 num: number = 0; 4832 str: string = ''; 4833 constructor(num: number, str: string) { 4834 this.num = num; 4835 this.str = str; 4836 } 4837 marshalling(messageParcel: rpc.MessageParcel): boolean { 4838 messageParcel.writeInt(this.num); 4839 messageParcel.writeString(this.str); 4840 return true; 4841 } 4842 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4843 this.num = messageParcel.readInt(); 4844 this.str = messageParcel.readString(); 4845 return true; 4846 } 4847 } 4848 let sequenceable = new MySequenceable(1, "aaa"); 4849 let data = rpc.MessageParcel.create(); 4850 let result = data.writeSequenceable(sequenceable); 4851 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4852 let ret = new MySequenceable(0, ""); 4853 let result2 = data.readSequenceable(ret); 4854 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4855 ``` 4856 4857### writeByteArray 4858 4859writeByteArray(byteArray: number[]): boolean 4860 4861Writes a byte array to this **MessageParcel** object. 4862 4863**System capability**: SystemCapability.Communication.IPC.Core 4864 4865**Parameters** 4866 4867 | Name | Type | Mandatory| Description | 4868 | --------- | -------- | ---- | ------------------ | 4869 | byteArray | number[] | Yes | Byte array to write.| 4870 4871**Return value** 4872 4873 | Type | Description | 4874 | ------- | -------------------------------- | 4875 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4876 4877**Example** 4878 4879 ```ts 4880 import { hilog } from '@kit.PerformanceAnalysisKit'; 4881 4882 let data = rpc.MessageParcel.create(); 4883 let ByteArrayVar = [1, 2, 3, 4, 5]; 4884 let result = data.writeByteArray(ByteArrayVar); 4885 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4886 ``` 4887 4888### readByteArray 4889 4890readByteArray(dataIn: number[]): void 4891 4892Reads the byte array from this **MessageParcel** object. 4893 4894**System capability**: SystemCapability.Communication.IPC.Core 4895 4896**Parameters** 4897 4898 | Name| Type | Mandatory| Description | 4899 | ------ | -------- | ---- | ------------------ | 4900 | dataIn | number[] | Yes | Byte array to read.| 4901 4902**Example** 4903 4904 ```ts 4905 import { hilog } from '@kit.PerformanceAnalysisKit'; 4906 4907 let data = rpc.MessageParcel.create(); 4908 let ByteArrayVar = [1, 2, 3, 4, 5]; 4909 let result = data.writeByteArray(ByteArrayVar); 4910 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4911 let array: Array<number> = new Array(5); 4912 data.readByteArray(array); 4913 ``` 4914 4915### readByteArray 4916 4917readByteArray(): number[] 4918 4919Reads the byte array from this **MessageParcel** object. 4920 4921**System capability**: SystemCapability.Communication.IPC.Core 4922 4923**Return value** 4924 4925 | Type | Description | 4926 | -------- | -------------- | 4927 | number[] | Byte array read.| 4928 4929**Example** 4930 4931 ```ts 4932 import { hilog } from '@kit.PerformanceAnalysisKit'; 4933 4934 let data = rpc.MessageParcel.create(); 4935 let ByteArrayVar = [1, 2, 3, 4, 5]; 4936 let result = data.writeByteArray(ByteArrayVar); 4937 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4938 let array = data.readByteArray(); 4939 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4940 ``` 4941 4942### writeShortArray 4943 4944writeShortArray(shortArray: number[]): boolean 4945 4946Writes a short array to this **MessageParcel** object. 4947 4948**System capability**: SystemCapability.Communication.IPC.Core 4949 4950**Parameters** 4951 4952 | Name | Type | Mandatory| Description | 4953 | ---------- | -------- | ---- | -------------------- | 4954 | shortArray | number[] | Yes | Short array to write.| 4955 4956**Return value** 4957 4958 | Type | Description | 4959 | ------- | -------------------------------- | 4960 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4961 4962**Example** 4963 4964 ```ts 4965 import { hilog } from '@kit.PerformanceAnalysisKit'; 4966 4967 let data = rpc.MessageParcel.create(); 4968 let result = data.writeShortArray([11, 12, 13]); 4969 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4970 ``` 4971 4972### readShortArray 4973 4974readShortArray(dataIn: number[]): void 4975 4976Reads the short array from this **MessageParcel** object. 4977 4978**System capability**: SystemCapability.Communication.IPC.Core 4979 4980**Parameters** 4981 4982 | Name| Type | Mandatory| Description | 4983 | ------ | -------- | ---- | -------------------- | 4984 | dataIn | number[] | Yes | Short array to read.| 4985 4986**Example** 4987 4988 ```ts 4989 import { hilog } from '@kit.PerformanceAnalysisKit'; 4990 4991 let data = rpc.MessageParcel.create(); 4992 let result = data.writeShortArray([11, 12, 13]); 4993 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4994 let array: Array<number> = new Array(3); 4995 data.readShortArray(array); 4996 ``` 4997 4998### readShortArray 4999 5000readShortArray(): number[] 5001 5002Reads the short array from this **MessageParcel** object. 5003 5004**System capability**: SystemCapability.Communication.IPC.Core 5005 5006**Return value** 5007 5008 | Type | Description | 5009 | -------- | ---------------- | 5010 | number[] | Short array read.| 5011 5012**Example** 5013 5014 ```ts 5015 import { hilog } from '@kit.PerformanceAnalysisKit'; 5016 5017 let data = rpc.MessageParcel.create(); 5018 let result = data.writeShortArray([11, 12, 13]); 5019 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 5020 let array = data.readShortArray(); 5021 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 5022 ``` 5023 5024### writeIntArray 5025 5026writeIntArray(intArray: number[]): boolean 5027 5028Writes an integer array to this **MessageParcel** object. 5029 5030**System capability**: SystemCapability.Communication.IPC.Core 5031 5032**Parameters** 5033 5034 | Name | Type | Mandatory| Description | 5035 | -------- | -------- | ---- | ------------------ | 5036 | intArray | number[] | Yes | Integer array to write.| 5037 5038**Return value** 5039 5040 | Type | Description | 5041 | ------- | -------------------------------- | 5042 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5043 5044**Example** 5045 5046 ```ts 5047 import { hilog } from '@kit.PerformanceAnalysisKit'; 5048 5049 let data = rpc.MessageParcel.create(); 5050 let result = data.writeIntArray([100, 111, 112]); 5051 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5052 ``` 5053 5054### readIntArray 5055 5056readIntArray(dataIn: number[]): void 5057 5058Reads the integer array from this **MessageParcel** object. 5059 5060**System capability**: SystemCapability.Communication.IPC.Core 5061 5062**Parameters** 5063 5064 | Name| Type | Mandatory| Description | 5065 | ------ | -------- | ---- | ------------------ | 5066 | dataIn | number[] | Yes | Integer array to read.| 5067 5068**Example** 5069 5070 ```ts 5071 import { hilog } from '@kit.PerformanceAnalysisKit'; 5072 5073 let data = rpc.MessageParcel.create(); 5074 let result = data.writeIntArray([100, 111, 112]); 5075 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5076 let array: Array<number> = new Array(3); 5077 data.readIntArray(array); 5078 ``` 5079 5080### readIntArray 5081 5082readIntArray(): number[] 5083 5084Reads the integer array from this **MessageParcel** object. 5085 5086**System capability**: SystemCapability.Communication.IPC.Core 5087 5088**Return value** 5089 5090 | Type | Description | 5091 | -------- | -------------- | 5092 | number[] | Integer array read.| 5093 5094**Example** 5095 5096 ```ts 5097 import { hilog } from '@kit.PerformanceAnalysisKit'; 5098 5099 let data = rpc.MessageParcel.create(); 5100 let result = data.writeIntArray([100, 111, 112]); 5101 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5102 let array = data.readIntArray(); 5103 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5104 ``` 5105 5106### writeLongArray 5107 5108writeLongArray(longArray: number[]): boolean 5109 5110Writes a long array to this **MessageParcel** object. 5111 5112**System capability**: SystemCapability.Communication.IPC.Core 5113 5114**Parameters** 5115 5116 | Name | Type | Mandatory| Description | 5117 | --------- | -------- | ---- | -------------------- | 5118 | longArray | number[] | Yes | Long array to write.| 5119 5120**Return value** 5121 5122 | Type | Description | 5123 | ------- | ----------------------------- | 5124 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5125 5126**Example** 5127 5128 ```ts 5129 import { hilog } from '@kit.PerformanceAnalysisKit'; 5130 5131 let data = rpc.MessageParcel.create(); 5132 let result = data.writeLongArray([1111, 1112, 1113]); 5133 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5134 ``` 5135 5136### readLongArray 5137 5138readLongArray(dataIn: number[]): void 5139 5140Reads the long array from this **MessageParcel** object. 5141 5142**System capability**: SystemCapability.Communication.IPC.Core 5143 5144**Parameters** 5145 5146 | Name| Type | Mandatory| Description | 5147 | ------ | -------- | ---- | -------------------- | 5148 | dataIn | number[] | Yes | Long array to read.| 5149 5150**Example** 5151 5152 ```ts 5153 import { hilog } from '@kit.PerformanceAnalysisKit'; 5154 5155 let data = rpc.MessageParcel.create(); 5156 let result = data.writeLongArray([1111, 1112, 1113]); 5157 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5158 let array: Array<number> = new Array(3); 5159 data.readLongArray(array); 5160 ``` 5161 5162### readLongArray 5163 5164readLongArray(): number[] 5165 5166Reads the long array from this **MessageParcel** object. 5167 5168**System capability**: SystemCapability.Communication.IPC.Core 5169 5170**Return value** 5171 5172 | Type | Description | 5173 | -------- | ---------------- | 5174 | number[] | Long array read.| 5175 5176**Example** 5177 5178 ```ts 5179 import { hilog } from '@kit.PerformanceAnalysisKit'; 5180 5181 let data = rpc.MessageParcel.create(); 5182 let result = data.writeLongArray([1111, 1112, 1113]); 5183 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5184 let array = data.readLongArray(); 5185 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5186 ``` 5187 5188### writeFloatArray 5189 5190writeFloatArray(floatArray: number[]): boolean 5191 5192Writes a double array to this **MessageParcel** object. 5193 5194**System capability**: SystemCapability.Communication.IPC.Core 5195 5196**Parameters** 5197 5198 | Name| Type| Mandatory| Description | 5199 | ---------- | -------- | ---- | --- | 5200 | floatArray | number[] | Yes | Double array to write. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type.| 5201 5202**Return value** 5203 5204 | Type | Description | 5205 | ------- | -------------------------------- | 5206 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5207 5208**Example** 5209 5210 ```ts 5211 import { hilog } from '@kit.PerformanceAnalysisKit'; 5212 5213 let data = rpc.MessageParcel.create(); 5214 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5215 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5216 ``` 5217 5218### readFloatArray 5219 5220readFloatArray(dataIn: number[]): void 5221 5222Reads the double array from this **MessageParcel** object. 5223 5224**System capability**: SystemCapability.Communication.IPC.Core 5225 5226**Parameters** 5227 5228 | Name| Type | Mandatory| Description | 5229 | ------ | -------- | ---- | ------ | 5230 | dataIn | number[] | Yes | Double array to read. The system processes float data as that of the double type. Therefore, the total number of bytes occupied by a float array must be calculated as the double type.| 5231 5232**Example** 5233 5234 ```ts 5235 import { hilog } from '@kit.PerformanceAnalysisKit'; 5236 5237 let data = rpc.MessageParcel.create(); 5238 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5239 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5240 let array: Array<number> = new Array(3); 5241 data.readFloatArray(array); 5242 ``` 5243 5244### readFloatArray 5245 5246readFloatArray(): number[] 5247 5248Reads the double array from this **MessageParcel** object. 5249 5250**System capability**: SystemCapability.Communication.IPC.Core 5251 5252**Return value** 5253 5254 | Type | Description | 5255 | -------- | -------------- | 5256 | number[] | Double array read.| 5257 5258**Example** 5259 5260 ```ts 5261 import { hilog } from '@kit.PerformanceAnalysisKit'; 5262 5263 let data = rpc.MessageParcel.create(); 5264 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5265 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5266 let array = data.readFloatArray(); 5267 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5268 ``` 5269 5270### writeDoubleArray 5271 5272writeDoubleArray(doubleArray: number[]): boolean 5273 5274Writes a double array to this **MessageParcel** object. 5275 5276**System capability**: SystemCapability.Communication.IPC.Core 5277 5278**Parameters** 5279 5280 | Name | Type | Mandatory| Description | 5281 | ----------- | -------- | ---- | ------------------------ | 5282 | doubleArray | number[] | Yes | Double array to write.| 5283 5284**Return value** 5285 5286 | Type | Description | 5287 | ------- | -------------------------------- | 5288 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5289 5290**Example** 5291 5292 ```ts 5293 import { hilog } from '@kit.PerformanceAnalysisKit'; 5294 5295 let data = rpc.MessageParcel.create(); 5296 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5297 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5298 ``` 5299 5300### readDoubleArray 5301 5302readDoubleArray(dataIn: number[]): void 5303 5304Reads the double array from this **MessageParcel** object. 5305 5306**System capability**: SystemCapability.Communication.IPC.Core 5307 5308**Parameters** 5309 5310 | Name| Type | Mandatory| Description | 5311 | ------ | -------- | ---- | ------------------------ | 5312 | dataIn | number[] | Yes | Double array to read.| 5313 5314**Example** 5315 5316 ```ts 5317 import { hilog } from '@kit.PerformanceAnalysisKit'; 5318 5319 let data = rpc.MessageParcel.create(); 5320 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5321 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5322 let array: Array<number> = new Array(3); 5323 data.readDoubleArray(array); 5324 ``` 5325 5326### readDoubleArray 5327 5328readDoubleArray(): number[] 5329 5330Reads the double array from this **MessageParcel** object. 5331 5332**System capability**: SystemCapability.Communication.IPC.Core 5333 5334**Return value** 5335 5336 | Type | Description | 5337 | -------- | -------------------- | 5338 | number[] | Double array read.| 5339 5340**Example** 5341 5342 ```ts 5343 import { hilog } from '@kit.PerformanceAnalysisKit'; 5344 5345 let data = rpc.MessageParcel.create(); 5346 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5347 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5348 let array = data.readDoubleArray(); 5349 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5350 ``` 5351 5352### writeBooleanArray 5353 5354writeBooleanArray(booleanArray: boolean[]): boolean 5355 5356Writes a Boolean array to this **MessageParcel** object. 5357 5358**System capability**: SystemCapability.Communication.IPC.Core 5359 5360**Parameters** 5361 5362 | Name | Type | Mandatory| Description | 5363 | ------------ | --------- | ---- | ------------------ | 5364 | booleanArray | boolean[] | Yes | Boolean array to write.| 5365 5366**Return value** 5367 5368 | Type | Description | 5369 | ------- | -------------------------------- | 5370 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5371 5372**Example** 5373 5374 ```ts 5375 import { hilog } from '@kit.PerformanceAnalysisKit'; 5376 5377 let data = rpc.MessageParcel.create(); 5378 let result = data.writeBooleanArray([false, true, false]); 5379 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5380 ``` 5381 5382### readBooleanArray 5383 5384readBooleanArray(dataIn: boolean[]): void 5385 5386Reads the Boolean array from this **MessageParcel** object. 5387 5388**System capability**: SystemCapability.Communication.IPC.Core 5389 5390**Parameters** 5391 5392 | Name| Type | Mandatory| Description | 5393 | ------ | --------- | ---- | ------------------ | 5394 | dataIn | boolean[] | Yes | Boolean array to read.| 5395 5396**Example** 5397 5398 ```ts 5399 import { hilog } from '@kit.PerformanceAnalysisKit'; 5400 5401 let data = rpc.MessageParcel.create(); 5402 let result = data.writeBooleanArray([false, true, false]); 5403 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5404 let array: Array<boolean> = new Array(3); 5405 data.readBooleanArray(array); 5406 ``` 5407 5408### readBooleanArray 5409 5410readBooleanArray(): boolean[] 5411 5412Reads the Boolean array from this **MessageParcel** object. 5413 5414**System capability**: SystemCapability.Communication.IPC.Core 5415 5416**Return value** 5417 5418 | Type | Description | 5419 | --------- | -------------- | 5420 | boolean[] | Boolean array read.| 5421 5422**Example** 5423 5424 ```ts 5425 import { hilog } from '@kit.PerformanceAnalysisKit'; 5426 5427 let data = rpc.MessageParcel.create(); 5428 let result = data.writeBooleanArray([false, true, false]); 5429 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5430 let array = data.readBooleanArray(); 5431 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5432 ``` 5433 5434### writeCharArray 5435 5436writeCharArray(charArray: number[]): boolean 5437 5438Writes a single character array to this **MessageParcel** object. 5439 5440**System capability**: SystemCapability.Communication.IPC.Core 5441 5442**Parameters** 5443 5444 | Name | Type | Mandatory| Description | 5445 | --------- | -------- | ---- | ---------------------- | 5446 | charArray | number[] | Yes | Character array to write.| 5447 5448**Return value** 5449 5450 | Type | Description | 5451 | ------- | -------------------------------- | 5452 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5453 5454**Example** 5455 5456 ```ts 5457 import { hilog } from '@kit.PerformanceAnalysisKit'; 5458 5459 let data = rpc.MessageParcel.create(); 5460 let result = data.writeCharArray([97, 98, 88]); 5461 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5462 ``` 5463 5464### readCharArray 5465 5466readCharArray(dataIn: number[]): void 5467 5468Reads the single character array from this **MessageParcel** object. 5469 5470**System capability**: SystemCapability.Communication.IPC.Core 5471 5472**Parameters** 5473 5474 | Name| Type | Mandatory| Description | 5475 | ------ | -------- | ---- | ---------------------- | 5476 | dataIn | number[] | Yes | Character array to read.| 5477 5478**Example** 5479 5480 ```ts 5481 import { hilog } from '@kit.PerformanceAnalysisKit'; 5482 5483 let data = rpc.MessageParcel.create(); 5484 let result = data.writeCharArray([97, 98, 99]); 5485 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5486 let array: Array<number> = new Array(3); 5487 data.readCharArray(array); 5488 ``` 5489 5490### readCharArray 5491 5492readCharArray(): number[] 5493 5494Reads the character array from this **MessageParcel** object. 5495 5496**System capability**: SystemCapability.Communication.IPC.Core 5497 5498**Return value** 5499 5500 | Type | Description | 5501 | -------- | ------------------ | 5502 | number[] | Character array read.| 5503 5504**Example** 5505 5506 ```ts 5507 import { hilog } from '@kit.PerformanceAnalysisKit'; 5508 5509 let data = rpc.MessageParcel.create(); 5510 let result = data.writeCharArray([97, 98, 99]); 5511 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5512 let array = data.readCharArray(); 5513 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5514 ``` 5515 5516### writeStringArray 5517 5518writeStringArray(stringArray: string[]): boolean 5519 5520Writes a string array to this **MessageParcel** object. 5521 5522**System capability**: SystemCapability.Communication.IPC.Core 5523 5524**Parameters** 5525 5526 | Name | Type | Mandatory| Description | 5527 | ----------- | -------- | ---- | ---------------- | 5528 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 5529 5530**Return value** 5531 5532 | Type | Description| 5533 | ------- | -------------------------------- | 5534 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5535 5536**Example** 5537 5538 ```ts 5539 import { hilog } from '@kit.PerformanceAnalysisKit'; 5540 5541 let data = rpc.MessageParcel.create(); 5542 let result = data.writeStringArray(["abc", "def"]); 5543 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5544 ``` 5545 5546### readStringArray 5547 5548readStringArray(dataIn: string[]): void 5549 5550Reads the string array from this **MessageParcel** object. 5551 5552**System capability**: SystemCapability.Communication.IPC.Core 5553 5554**Parameters** 5555 5556 | Name| Type | Mandatory| Description | 5557 | ------ | -------- | ---- | -------------------- | 5558 | dataIn | string[] | Yes | String array to read.| 5559 5560**Example** 5561 5562 ```ts 5563 import { hilog } from '@kit.PerformanceAnalysisKit'; 5564 5565 let data = rpc.MessageParcel.create(); 5566 let result = data.writeStringArray(["abc", "def"]); 5567 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5568 let array: Array<string> = new Array(2); 5569 data.readStringArray(array); 5570 ``` 5571 5572### readStringArray 5573 5574readStringArray(): string[] 5575 5576Reads the string array from this **MessageParcel** object. 5577 5578**System capability**: SystemCapability.Communication.IPC.Core 5579 5580**Return value** 5581 5582 | Type | Description | 5583 | -------- | ---------------- | 5584 | string[] | String array read.| 5585 5586**Example** 5587 5588 ```ts 5589 import { hilog } from '@kit.PerformanceAnalysisKit'; 5590 5591 let data = rpc.MessageParcel.create(); 5592 let result = data.writeStringArray(["abc", "def"]); 5593 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5594 let array = data.readStringArray(); 5595 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5596 ``` 5597 5598### writeNoException<sup>8+</sup> 5599 5600writeNoException(): void 5601 5602Writes information to this **MessageParcel** object indicating that no exception occurred. 5603 5604**System capability**: SystemCapability.Communication.IPC.Core 5605 5606**Example** 5607 5608 ```ts 5609 import { hilog } from '@kit.PerformanceAnalysisKit'; 5610 5611 class MyDeathRecipient implements rpc.DeathRecipient { 5612 onRemoteDied() { 5613 hilog.info(0x0000, 'testTag', 'server died'); 5614 } 5615 } 5616 class TestRemoteObject extends rpc.RemoteObject { 5617 constructor(descriptor: string) { 5618 super(descriptor); 5619 } 5620 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5621 return true; 5622 } 5623 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5624 return true; 5625 } 5626 isObjectDead(): boolean { 5627 return false; 5628 } 5629 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5630 if (code === 1) { 5631 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5632 reply.writeNoException(); 5633 return true; 5634 } else { 5635 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5636 return false; 5637 } 5638 } 5639 } 5640 ``` 5641 5642### readException<sup>8+</sup> 5643 5644readException(): void 5645 5646Reads the exception information from this **MessageParcel** object. 5647 5648**System capability**: SystemCapability.Communication.IPC.Core 5649 5650**Example** 5651 5652>**NOTE** 5653> 5654>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 5655 5656 <!--code_no_check--> 5657 ```ts 5658 // If the FA model is used, import featureAbility from @kit.AbilityKit. 5659 // import { featureAbility } from '@kit.AbilityKit'; 5660 import { Want, common } from '@kit.AbilityKit'; 5661 import { hilog } from '@kit.PerformanceAnalysisKit'; 5662 5663 let proxy: rpc.IRemoteObject | undefined; 5664 let connect: common.ConnectOptions = { 5665 onConnect: (elementName, remoteProxy) => { 5666 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5667 proxy = remoteProxy; 5668 }, 5669 onDisconnect: (elementName) => { 5670 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5671 }, 5672 onFailed: () => { 5673 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5674 } 5675 }; 5676 let want: Want = { 5677 bundleName: "com.ohos.server", 5678 abilityName: "com.ohos.server.EntryAbility", 5679 }; 5680 5681 // Use this method to connect to the ability for the FA model. 5682 // FA.connectAbility(want,connect); 5683 5684 // Save the connection ID, which will be used for the subsequent service disconnection. 5685 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 5686 // Save the connection ID, which will be used for the subsequent service disconnection. 5687 let connectionId = context.connectServiceExtensionAbility(want, connect); 5688 ``` 5689 5690 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 5691 5692 ```ts 5693 import { hilog } from '@kit.PerformanceAnalysisKit'; 5694 5695 let option = new rpc.MessageOption(); 5696 let data = rpc.MessageParcel.create(); 5697 let reply = rpc.MessageParcel.create(); 5698 data.writeNoException(); 5699 data.writeString('hello'); 5700 if (proxy != undefined) { 5701 let a = proxy.sendRequest(1, data, reply, option) as Object; 5702 let b = a as Promise<rpc.SendRequestResult>; 5703 b.then((result: rpc.SendRequestResult) => { 5704 if (result.errCode === 0) { 5705 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5706 result.reply.readException(); 5707 let msg = result.reply.readString(); 5708 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5709 } else { 5710 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5711 } 5712 }).catch((e: Error) => { 5713 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5714 }).finally (() => { 5715 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5716 data.reclaim(); 5717 reply.reclaim(); 5718 }); 5719 } 5720 ``` 5721 5722### writeSequenceableArray 5723 5724writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5725 5726Writes a **Sequenceable** array to this **MessageParcel** object. 5727 5728**System capability**: SystemCapability.Communication.IPC.Core 5729 5730**Parameters** 5731 5732| Name | Type | Mandatory| Description | 5733| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5734| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | **Sequenceable** array to write.| 5735 5736**Return value** 5737 5738 | Type | Description | 5739 | ------- | -------------------------------- | 5740 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5741 5742**Example** 5743 5744 ```ts 5745 import { hilog } from '@kit.PerformanceAnalysisKit'; 5746 5747 class MySequenceable implements rpc.Sequenceable { 5748 num: number = 0; 5749 str: string = ''; 5750 constructor(num: number, str: string) { 5751 this.num = num; 5752 this.str = str; 5753 } 5754 marshalling(messageParcel: rpc.MessageParcel): boolean { 5755 messageParcel.writeInt(this.num); 5756 messageParcel.writeString(this.str); 5757 return true; 5758 } 5759 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5760 this.num = messageParcel.readInt(); 5761 this.str = messageParcel.readString(); 5762 return true; 5763 } 5764 } 5765 let sequenceable = new MySequenceable(1, "aaa"); 5766 let sequenceable2 = new MySequenceable(2, "bbb"); 5767 let sequenceable3 = new MySequenceable(3, "ccc"); 5768 let a = [sequenceable, sequenceable2, sequenceable3]; 5769 let data = rpc.MessageParcel.create(); 5770 let result = data.writeSequenceableArray(a); 5771 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5772 ``` 5773 5774### readSequenceableArray<sup>8+</sup> 5775 5776readSequenceableArray(sequenceableArray: Sequenceable[]): void 5777 5778Reads the **Sequenceable** array from this **MessageParcel** object. 5779 5780**System capability**: SystemCapability.Communication.IPC.Core 5781 5782**Parameters** 5783 5784| Name | Type | Mandatory| Description | 5785| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5786| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | **Sequenceable** array to read.| 5787 5788**Example** 5789 5790 ```ts 5791 import { hilog } from '@kit.PerformanceAnalysisKit'; 5792 5793 class MySequenceable implements rpc.Sequenceable { 5794 num: number = 0; 5795 str: string = ''; 5796 constructor(num: number, str: string) { 5797 this.num = num; 5798 this.str = str; 5799 } 5800 marshalling(messageParcel: rpc.MessageParcel): boolean { 5801 messageParcel.writeInt(this.num); 5802 messageParcel.writeString(this.str); 5803 return true; 5804 } 5805 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5806 this.num = messageParcel.readInt(); 5807 this.str = messageParcel.readString(); 5808 return true; 5809 } 5810 } 5811 let sequenceable = new MySequenceable(1, "aaa"); 5812 let sequenceable2 = new MySequenceable(2, "bbb"); 5813 let sequenceable3 = new MySequenceable(3, "ccc"); 5814 let a = [sequenceable, sequenceable2, sequenceable3]; 5815 let data = rpc.MessageParcel.create(); 5816 let result = data.writeSequenceableArray(a); 5817 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5818 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5819 data.readSequenceableArray(b); 5820 ``` 5821 5822### writeRemoteObjectArray<sup>8+</sup> 5823 5824writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5825 5826Writes an **IRemoteObject** array to this **MessageParcel** object. 5827 5828**System capability**: SystemCapability.Communication.IPC.Core 5829 5830**Parameters** 5831 5832 | Name | Type | Mandatory| Description | 5833 | ----------- | --------------- | ---- | ----- | 5834 | objectArray | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to write.| 5835 5836**Return value** 5837 5838 | Type | Description | 5839 | ------- | -------------------------------- | 5840 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5841 5842**Example** 5843 5844 ```ts 5845 import { hilog } from '@kit.PerformanceAnalysisKit'; 5846 5847 class MyDeathRecipient implements rpc.DeathRecipient { 5848 onRemoteDied() { 5849 hilog.info(0x0000, 'testTag', 'server died'); 5850 } 5851 } 5852 class TestRemoteObject extends rpc.RemoteObject { 5853 constructor(descriptor: string) { 5854 super(descriptor); 5855 this.attachLocalInterface(this, descriptor); 5856 } 5857 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5858 return true; 5859 } 5860 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5861 return true; 5862 } 5863 isObjectDead(): boolean { 5864 return false; 5865 } 5866 asObject(): rpc.IRemoteObject { 5867 return this; 5868 } 5869 } 5870 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5871 let data = rpc.MessageParcel.create(); 5872 let result = data.writeRemoteObjectArray(a); 5873 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5874 ``` 5875 5876### readRemoteObjectArray<sup>8+</sup> 5877 5878readRemoteObjectArray(objects: IRemoteObject[]): void 5879 5880Reads the **IRemoteObject** array from this **MessageParcel** object. 5881 5882**System capability**: SystemCapability.Communication.IPC.Core 5883 5884**Parameters** 5885 5886 | Name | Type | Mandatory| Description | 5887 | ------- | --------------- | ---- | --------- | 5888 | objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 5889 5890**Example** 5891 5892 ```ts 5893 import { hilog } from '@kit.PerformanceAnalysisKit'; 5894 5895 class MyDeathRecipient implements rpc.DeathRecipient { 5896 onRemoteDied() { 5897 hilog.info(0x0000, 'testTag', 'server died'); 5898 } 5899 } 5900 class TestRemoteObject extends rpc.RemoteObject { 5901 constructor(descriptor: string) { 5902 super(descriptor); 5903 this.attachLocalInterface(this, descriptor); 5904 } 5905 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5906 return true; 5907 } 5908 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5909 return true; 5910 } 5911 isObjectDead(): boolean { 5912 return false; 5913 } 5914 asObject(): rpc.IRemoteObject { 5915 return this; 5916 } 5917 } 5918 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5919 let data = rpc.MessageParcel.create(); 5920 data.writeRemoteObjectArray(a); 5921 let b: Array<rpc.IRemoteObject> = new Array(3); 5922 data.readRemoteObjectArray(b); 5923 ``` 5924 5925### readRemoteObjectArray<sup>8+</sup> 5926 5927readRemoteObjectArray(): IRemoteObject[] 5928 5929Reads the **IRemoteObject** array from this **MessageParcel** object. 5930 5931**System capability**: SystemCapability.Communication.IPC.Core 5932 5933**Return value** 5934 5935 | Type | Description | 5936 | --------------- | --------------------------- | 5937 | [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 5938 5939**Example** 5940 5941 ```ts 5942 import { hilog } from '@kit.PerformanceAnalysisKit'; 5943 5944 class MyDeathRecipient implements rpc.DeathRecipient { 5945 onRemoteDied() { 5946 hilog.info(0x0000, 'testTag', 'server died'); 5947 } 5948 } 5949 class TestRemoteObject extends rpc.RemoteObject { 5950 constructor(descriptor: string) { 5951 super(descriptor); 5952 this.attachLocalInterface(this, descriptor); 5953 } 5954 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5955 return true; 5956 } 5957 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5958 return true; 5959 } 5960 isObjectDead(): boolean { 5961 return false; 5962 } 5963 asObject(): rpc.IRemoteObject { 5964 return this; 5965 } 5966 } 5967 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5968 let data = rpc.MessageParcel.create(); 5969 let result = data.writeRemoteObjectArray(a); 5970 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5971 let b = data.readRemoteObjectArray(); 5972 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5973 ``` 5974 5975### closeFileDescriptor<sup>8+</sup> 5976 5977static closeFileDescriptor(fd: number): void 5978 5979Closes a file descriptor. This API is a static method. 5980 5981**System capability**: SystemCapability.Communication.IPC.Core 5982 5983**Parameters** 5984 5985 | Name| Type | Mandatory| Description | 5986 | ------ | ------ | ---- | -------------------- | 5987 | fd | number | Yes | File descriptor to close.| 5988 5989**Example** 5990 5991 ```ts 5992 import { fileIo } from '@kit.CoreFileKit'; 5993 5994 let filePath = "path/to/file"; 5995 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5996 rpc.MessageParcel.closeFileDescriptor(file.fd); 5997 ``` 5998 5999### dupFileDescriptor<sup>8+</sup> 6000 6001static dupFileDescriptor(fd: number) :number 6002 6003Duplicates a file descriptor. This API is a static method. 6004 6005**System capability**: SystemCapability.Communication.IPC.Core 6006 6007**Parameters** 6008 6009 | Name| Type | Mandatory| Description | 6010 | ------ | ------ | ---- | ------------------------ | 6011 | fd | number | Yes | File descriptor to duplicate.| 6012 6013**Return value** 6014 6015 | Type | Description | 6016 | ------ | -------------------- | 6017 | number | New file descriptor.| 6018 6019**Example** 6020 6021 ```ts 6022 import { fileIo } from '@kit.CoreFileKit'; 6023 6024 let filePath = "path/to/file"; 6025 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6026 rpc.MessageParcel.dupFileDescriptor(file.fd); 6027 ``` 6028 6029### containFileDescriptors<sup>8+</sup> 6030 6031containFileDescriptors(): boolean 6032 6033Checks whether this **MessageParcel** object contains file descriptors. 6034 6035**System capability**: SystemCapability.Communication.IPC.Core 6036 6037**Return value** 6038 6039 | Type | Description | 6040 | ------- | --------------------------------------------- | 6041 | boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 6042 6043**Example** 6044 6045 ```ts 6046 import { fileIo } from '@kit.CoreFileKit'; 6047 import { hilog } from '@kit.PerformanceAnalysisKit'; 6048 6049 let parcel = new rpc.MessageParcel(); 6050 let filePath = "path/to/file"; 6051 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6052 let writeResult = parcel.writeFileDescriptor(file.fd); 6053 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6054 let containFD = parcel.containFileDescriptors(); 6055 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6056 ``` 6057 6058### writeFileDescriptor<sup>8+</sup> 6059 6060writeFileDescriptor(fd: number): boolean 6061 6062Writes a file descriptor to this **MessageParcel** object. 6063 6064**System capability**: SystemCapability.Communication.IPC.Core 6065 6066**Parameters** 6067 6068 | Name| Type | Mandatory| Description | 6069 | ------ | ------ | ---- | ------------ | 6070 | fd | number | Yes | File descriptor to write.| 6071 6072**Return value** 6073 6074 | Type | Description | 6075 | ------- | -------------------------------- | 6076 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6077 6078**Example** 6079 6080 ```ts 6081 import { fileIo } from '@kit.CoreFileKit'; 6082 import { hilog } from '@kit.PerformanceAnalysisKit'; 6083 6084 let parcel = new rpc.MessageParcel(); 6085 let filePath = "path/to/file"; 6086 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6087 let writeResult = parcel.writeFileDescriptor(file.fd); 6088 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6089 ``` 6090 6091### readFileDescriptor<sup>8+</sup> 6092 6093readFileDescriptor(): number 6094 6095Reads the file descriptor from this **MessageParcel** object. 6096 6097**System capability**: SystemCapability.Communication.IPC.Core 6098 6099**Return value** 6100 6101 | Type | Description | 6102 | ------ | ---------------- | 6103 | number | File descriptor read.| 6104 6105**Example** 6106 6107 ```ts 6108 import { fileIo } from '@kit.CoreFileKit'; 6109 import { hilog } from '@kit.PerformanceAnalysisKit'; 6110 6111 let parcel = new rpc.MessageParcel(); 6112 let filePath = "path/to/file"; 6113 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6114 parcel.writeFileDescriptor(file.fd); 6115 let readFD = parcel.readFileDescriptor(); 6116 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6117 ``` 6118 6119### writeAshmem<sup>8+</sup> 6120 6121writeAshmem(ashmem: Ashmem): boolean 6122 6123Writes an anonymous shared object to this **MessageParcel** object. 6124 6125**System capability**: SystemCapability.Communication.IPC.Core 6126 6127**Parameters** 6128 6129| Name| Type | Mandatory| Description | 6130| ------ | ------ | ---- | ----------------------------------- | 6131| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 6132 6133**Return value** 6134 6135 | Type | Description | 6136 | ------- | -------------------------------- | 6137 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6138 6139**Example** 6140 6141 ```ts 6142 import { hilog } from '@kit.PerformanceAnalysisKit'; 6143 6144 let parcel = new rpc.MessageParcel(); 6145 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6146 let isWriteSuccess = parcel.writeAshmem(ashmem); 6147 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6148 ``` 6149 6150### readAshmem<sup>8+</sup> 6151 6152readAshmem(): Ashmem 6153 6154Reads the anonymous shared object from this **MessageParcel** object. 6155 6156**System capability**: SystemCapability.Communication.IPC.Core 6157 6158**Return value** 6159 6160| Type | Description | 6161| ------ | ------------------ | 6162| [Ashmem](#ashmem8) | Anonymous share object obtained.| 6163 6164**Example** 6165 6166 ```ts 6167 import { hilog } from '@kit.PerformanceAnalysisKit'; 6168 6169 let parcel = new rpc.MessageParcel(); 6170 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6171 let isWriteSuccess = parcel.writeAshmem(ashmem); 6172 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6173 let readAshmem = parcel.readAshmem(); 6174 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6175 ``` 6176 6177### getRawDataCapacity<sup>8+</sup> 6178 6179getRawDataCapacity(): number 6180 6181Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 6182 6183**System capability**: SystemCapability.Communication.IPC.Core 6184 6185**Return value** 6186 6187 | Type | Description | 6188 | ------ | ---------------------------------------------------------- | 6189 | number | Maximum amount of raw data that **MessageParcel** can hold, that is, 128 MB.| 6190 6191**Example** 6192 6193 ```ts 6194 import { hilog } from '@kit.PerformanceAnalysisKit'; 6195 6196 let parcel = new rpc.MessageParcel(); 6197 let result = parcel.getRawDataCapacity(); 6198 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6199 ``` 6200 6201### writeRawData<sup>8+</sup> 6202 6203writeRawData(rawData: number[], size: number): boolean 6204 6205Writes raw data to this **MessageParcel** object. 6206 6207**System capability**: SystemCapability.Communication.IPC.Core 6208 6209**Parameters** 6210 6211 | Name | Type | Mandatory| Description | 6212 | ------- | -------- | ---- | ---------------------------------- | 6213 | rawData | number[] | Yes | Raw data to write. | 6214 | size | number | Yes | Size of the raw data, in bytes.| 6215 6216**Return value** 6217 6218 | Type | Description | 6219 | ------- | -------------------------------- | 6220 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6221 6222**Example** 6223 6224 ```ts 6225 import { hilog } from '@kit.PerformanceAnalysisKit'; 6226 6227 let parcel = new rpc.MessageParcel(); 6228 let arr = [1, 2, 3, 4, 5]; 6229 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6230 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6231 ``` 6232 6233### readRawData<sup>8+</sup> 6234 6235readRawData(size: number): number[] 6236 6237Reads raw data from this **MessageParcel** object. 6238 6239**System capability**: SystemCapability.Communication.IPC.Core 6240 6241**Parameters** 6242 6243 | Name| Type | Mandatory| Description | 6244 | ------ | ------ | ---- | ------------------------ | 6245 | size | number | Yes | Size of the raw data to read.| 6246 6247**Return value** 6248 6249 | Type | Description | 6250 | -------- | ------------------------------ | 6251 | number[] | Raw data obtained, in bytes.| 6252 6253**Example** 6254 6255 ```ts 6256 import { hilog } from '@kit.PerformanceAnalysisKit'; 6257 6258 let parcel = new rpc.MessageParcel(); 6259 let arr = [1, 2, 3, 4, 5]; 6260 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6261 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6262 let result = parcel.readRawData(5); 6263 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6264 ``` 6265 6266## Parcelable<sup>9+</sup> 6267 6268Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 6269 6270### marshalling 6271 6272marshalling(dataOut: MessageSequence): boolean 6273 6274Marshals this **Parcelable** object into a **MessageSequence** object. 6275 6276**System capability**: SystemCapability.Communication.IPC.Core 6277 6278**Parameters** 6279 6280| Name | Type | Mandatory| Description | 6281| ------- | --------------- | ---- | ------------------------------------------- | 6282| dataOut |[MessageSequence](#messagesequence9)| Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 6283 6284**Return value** 6285 6286 | Type | Description | 6287 | ------- | -------------------------------- | 6288 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6289 6290**Example** 6291 6292 ```ts 6293 import { hilog } from '@kit.PerformanceAnalysisKit'; 6294 6295 class MyParcelable implements rpc.Parcelable { 6296 num: number = 0; 6297 str: string = ''; 6298 constructor(num: number, str: string) { 6299 this.num = num; 6300 this.str = str; 6301 } 6302 marshalling(messageSequence: rpc.MessageSequence): boolean { 6303 messageSequence.writeInt(this.num); 6304 messageSequence.writeString(this.str); 6305 return true; 6306 } 6307 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6308 this.num = messageSequence.readInt(); 6309 this.str = messageSequence.readString(); 6310 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6311 return true; 6312 } 6313 } 6314 let parcelable = new MyParcelable(1, "aaa"); 6315 let data = rpc.MessageSequence.create(); 6316 data.writeParcelable(parcelable); 6317 let ret = new MyParcelable(0, ""); 6318 data.readParcelable(ret); 6319 ``` 6320 6321### unmarshalling 6322 6323unmarshalling(dataIn: MessageSequence): boolean 6324 6325Unmarshals this **Parcelable** object from a **MessageSequence** object. 6326 6327**System capability**: SystemCapability.Communication.IPC.Core 6328 6329**Parameters** 6330 6331| Name| Type | Mandatory| Description | 6332| ------ | --------------- | ---- | ----------------------------------------------- | 6333| dataIn | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 6334 6335**Return value** 6336 6337 | Type | Description | 6338 | ------- | ---------------------------------------- | 6339 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6340 6341**Example** 6342 6343 ```ts 6344 import { hilog } from '@kit.PerformanceAnalysisKit'; 6345 6346 class MyParcelable implements rpc.Parcelable { 6347 num: number = 0; 6348 str: string = ''; 6349 constructor(num: number, str: string) { 6350 this.num = num; 6351 this.str = str; 6352 } 6353 marshalling(messageSequence: rpc.MessageSequence): boolean { 6354 messageSequence.writeInt(this.num); 6355 messageSequence.writeString(this.str); 6356 return true; 6357 } 6358 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6359 this.num = messageSequence.readInt(); 6360 this.str = messageSequence.readString(); 6361 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6362 return true; 6363 } 6364 } 6365 let parcelable = new MyParcelable(1, "aaa"); 6366 let data = rpc.MessageSequence.create(); 6367 data.writeParcelable(parcelable); 6368 let ret = new MyParcelable(0, ""); 6369 data.readParcelable(ret); 6370 ``` 6371 6372## Sequenceable<sup>(deprecated)</sup> 6373 6374Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 6375 6376> **NOTE** 6377> 6378> This API is deprecated since API version 9. Use [Parcelable](#parcelable9) instead. 6379 6380### marshalling 6381 6382marshalling(dataOut: MessageParcel): boolean 6383 6384Marshals the sequenceable object into a **MessageParcel** object. 6385 6386**System capability**: SystemCapability.Communication.IPC.Core 6387 6388**Parameters** 6389 6390 | Name | Type | Mandatory| Description | 6391 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6392 | dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 6393 6394**Return value** 6395 6396 | Type | Description | 6397 | ------- | -------------------------------- | 6398 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6399 6400**Example** 6401 6402 ```ts 6403 import { hilog } from '@kit.PerformanceAnalysisKit'; 6404 6405 class MySequenceable implements rpc.Sequenceable { 6406 num: number = 0; 6407 str: string = ''; 6408 constructor(num: number, str: string) { 6409 this.num = num; 6410 this.str = str; 6411 } 6412 marshalling(messageParcel: rpc.MessageParcel): boolean { 6413 messageParcel.writeInt(this.num); 6414 messageParcel.writeString(this.str); 6415 return true; 6416 } 6417 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6418 this.num = messageParcel.readInt(); 6419 this.str = messageParcel.readString(); 6420 return true; 6421 } 6422 } 6423 let sequenceable = new MySequenceable(1, "aaa"); 6424 let data = rpc.MessageParcel.create(); 6425 let result = data.writeSequenceable(sequenceable); 6426 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6427 let ret = new MySequenceable(0, ""); 6428 let result2 = data.readSequenceable(ret); 6429 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6430 ``` 6431 6432### unmarshalling 6433 6434unmarshalling(dataIn: MessageParcel): boolean 6435 6436Unmarshals this sequenceable object from a **MessageParcel** object. 6437 6438**System capability**: SystemCapability.Communication.IPC.Core 6439 6440**Parameters** 6441 6442 | Name| Type | Mandatory| Description | 6443 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6444 | dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 6445 6446**Return value** 6447 6448 | Type | Description | 6449 | ------- | ---------------------------------------- | 6450 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6451 6452**Example** 6453 6454 ```ts 6455 import { hilog } from '@kit.PerformanceAnalysisKit'; 6456 6457 class MySequenceable implements rpc.Sequenceable { 6458 num: number = 0; 6459 str: string = ''; 6460 constructor(num: number, str: string) { 6461 this.num = num; 6462 this.str = str; 6463 } 6464 marshalling(messageParcel: rpc.MessageParcel): boolean { 6465 messageParcel.writeInt(this.num); 6466 messageParcel.writeString(this.str); 6467 return true; 6468 } 6469 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6470 this.num = messageParcel.readInt(); 6471 this.str = messageParcel.readString(); 6472 return true; 6473 } 6474 } 6475 let sequenceable = new MySequenceable(1, "aaa"); 6476 let data = rpc.MessageParcel.create(); 6477 let result = data.writeSequenceable(sequenceable); 6478 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6479 let ret = new MySequenceable(0, ""); 6480 let result2 = data.readSequenceable(ret); 6481 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6482 ``` 6483 6484## IRemoteBroker 6485 6486Represents the holder of a remote proxy object. It is used to obtain a proxy object. 6487 6488### asObject 6489 6490asObject(): IRemoteObject 6491 6492Obtains a proxy or remote object. This API must be implemented by its derived classes. 6493 6494**System capability**: SystemCapability.Communication.IPC.Core 6495 6496**Return value** 6497 6498 | Type | Description | 6499 | ----- | ----- | 6500 | [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.| 6501 6502**Example** 6503 6504 ```ts 6505 class TestAbility extends rpc.RemoteObject { 6506 asObject() { 6507 return this; 6508 } 6509 } 6510 let remoteObject = new TestAbility("testObject").asObject(); 6511 ``` 6512 6513**Example** 6514 6515>**NOTE** 6516> 6517>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 6518 6519 <!--code_no_check--> 6520 ```ts 6521 // If the FA model is used, import featureAbility from @kit.AbilityKit. 6522 // import { featureAbility } from '@kit.AbilityKit'; 6523 import { Want, common } from '@kit.AbilityKit'; 6524 import { hilog } from '@kit.PerformanceAnalysisKit'; 6525 6526 let proxy: rpc.IRemoteObject | undefined; 6527 let connect: common.ConnectOptions = { 6528 onConnect: (elementName, remoteProxy) => { 6529 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6530 proxy = remoteProxy; 6531 }, 6532 onDisconnect: (elementName) => { 6533 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6534 }, 6535 onFailed: () => { 6536 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6537 } 6538 }; 6539 let want: Want = { 6540 bundleName: "com.ohos.server", 6541 abilityName: "com.ohos.server.EntryAbility", 6542 }; 6543 6544 // Use this method to connect to the ability for the FA model. 6545 // FA.connectAbility(want,connect); 6546 6547 // Save the connection ID, which will be used for the subsequent service disconnection. 6548 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 6549 // Save the connection ID, which will be used for the subsequent service disconnection. 6550 let connectionId = context.connectServiceExtensionAbility(want, connect); 6551 ``` 6552 6553 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. 6554 6555 ```ts 6556 class TestProxy { 6557 remote: rpc.IRemoteObject; 6558 constructor(remote: rpc.IRemoteObject) { 6559 this.remote = remote; 6560 } 6561 asObject() { 6562 return this.remote; 6563 } 6564 } 6565 if (proxy != undefined) { 6566 let iRemoteObject = new TestProxy(proxy).asObject(); 6567 } 6568 ``` 6569 6570## DeathRecipient 6571 6572Subscribes 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. 6573 6574### onRemoteDied 6575 6576onRemoteDied(): void 6577 6578Called to perform subsequent operations when a death notification of the remote object is received. 6579 6580**System capability**: SystemCapability.Communication.IPC.Core 6581 6582**Example** 6583 6584 ```ts 6585 import { hilog } from '@kit.PerformanceAnalysisKit'; 6586 6587 class MyDeathRecipient implements rpc.DeathRecipient { 6588 onRemoteDied() { 6589 hilog.info(0x0000, 'testTag', 'server died'); 6590 } 6591 } 6592 ``` 6593 6594## RequestResult<sup>9+</sup> 6595 6596Defines the response to the request. 6597 6598**System capability**: SystemCapability.Communication.IPC.Core 6599 6600| Name | Type | Readable| Writable| Description | 6601| ------- | --------------- | ---- | ---- |-------------------------------------- | 6602| errCode | number | Yes | No | Error code. | 6603| code | number | Yes | No | Message code. | 6604| data | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object sent to the remote process.| 6605| reply | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object returned by the remote process. | 6606 6607## SendRequestResult<sup>(deprecated)</sup> 6608 6609Defines the response to the request. 6610 6611> **NOTE** 6612> 6613> This API is supported since API version 8 and deprecated since API version 9. Use [RequestResult](#requestresult9) instead. 6614 6615**System capability**: SystemCapability.Communication.IPC.Core 6616 6617 | Name | Type | Readable| Writable| Description | 6618 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6619 | errCode | number | Yes | No | Error code. | 6620 | code | number | Yes | No | Message code. | 6621 | data | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object sent to the remote process.| 6622 | reply | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object returned by the remote process. | 6623 6624## IRemoteObject 6625 6626Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 6627 6628### getLocalInterface<sup>9+</sup> 6629 6630getLocalInterface(descriptor: string): IRemoteBroker 6631 6632Obtains the string of the interface descriptor. 6633 6634**System capability**: SystemCapability.Communication.IPC.Core 6635 6636**Parameters** 6637 6638 | Name | Type | Mandatory| Description | 6639 | ---------- | ------ | ---- | -------------------- | 6640 | descriptor | string | Yes | Interface descriptor.| 6641 6642**Return value** 6643 6644| Type | Description | 6645| ------------- | --------------------------------------------- | 6646| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6647 6648**Error codes** 6649 6650For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6651 6652 | ID| Error Message| 6653 | -------- | -------- | 6654 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 6655 6656### queryLocalInterface<sup>(deprecated)</sup> 6657 6658queryLocalInterface(descriptor: string): IRemoteBroker 6659 6660Obtains the string of the interface descriptor. 6661 6662> **NOTE** 6663> 6664> This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9) instead. 6665 6666**System capability**: SystemCapability.Communication.IPC.Core 6667 6668**Parameters** 6669 6670 | Name | Type | Mandatory| Description | 6671 | ---------- | ------ | ---- | -------------------- | 6672 | descriptor | string | Yes | Interface descriptor.| 6673 6674**Return value** 6675 6676| Type | Description | 6677| ------------- | --------------------------------------------- | 6678| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6679 6680### sendRequest<sup>(deprecated)</sup> 6681 6682sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6683 6684Sends 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. 6685 6686> **NOTE** 6687> 6688> This API is deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9) instead. 6689 6690**System capability**: SystemCapability.Communication.IPC.Core 6691 6692**Parameters** 6693 6694 | Name | Type | Mandatory| Description | 6695 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6696 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6697 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6698 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6699 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6700 6701**Return value** 6702 6703 | Type | Description | 6704 | ------- | -------------------------------- | 6705 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6706 6707### sendMessageRequest<sup>9+</sup> 6708 6709sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6710 6711Sends 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. 6712 6713**System capability**: SystemCapability.Communication.IPC.Core 6714 6715**Parameters** 6716 6717 | Name | Type | Mandatory| Description | 6718 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6719 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6720 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6721 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6722 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6723 6724**Return value** 6725 6726 | Type | Description | 6727 | ---------------------------- | ----------------------------------------- | 6728 | Promise<[RequestResult](#requestresult9)> | Promise used to return a **requestResult** instance.| 6729 6730**Error codes** 6731 6732For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6733 6734 | ID| Error Message| 6735 | -------- | -------- | 6736 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 6737 6738### sendRequest<sup>(deprecated)</sup> 6739 6740sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6741 6742Sends 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. 6743 6744> **NOTE** 6745> 6746> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9) instead. 6747 6748**System capability**: SystemCapability.Communication.IPC.Core 6749 6750**Parameters** 6751 6752 | Name | Type | Mandatory| Description | 6753 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6754 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6755 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6756 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6757 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6758 6759**Return value** 6760 6761| Type | Description | 6762| ------------------------------------------------------------ | --------------------------------------------- | 6763| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 6764 6765### sendMessageRequest<sup>9+</sup> 6766 6767sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6768 6769Sends 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. 6770 6771**System capability**: SystemCapability.Communication.IPC.Core 6772 6773**Parameters** 6774 6775 | Name | Type | Mandatory| Description | 6776 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6777 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6778 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6779 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6780 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6781 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 6782 6783**Error codes** 6784 6785For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6786 6787 | ID| Error Message| 6788 | -------- | -------- | 6789 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 6790 6791### sendRequest<sup>(deprecated)</sup> 6792 6793sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6794 6795Sends 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. 6796 6797> **NOTE** 6798> 6799> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-1) instead. 6800 6801**System capability**: SystemCapability.Communication.IPC.Core 6802 6803**Parameters** 6804 6805| Name | Type | Mandatory| Description | 6806| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6807| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 6808| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6809| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6810| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6811| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 6812 6813### registerDeathRecipient<sup>9+</sup> 6814 6815registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6816 6817Registers 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. 6818 6819**System capability**: SystemCapability.Communication.IPC.Core 6820 6821**Parameters** 6822 6823 | Name | Type | Mandatory| Description | 6824 | --------- | --------------------------------- | ---- | -------------- | 6825 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6826 | flags | number | Yes | Flag of the death notification.| 6827 6828**Error codes** 6829 6830For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6831 6832 | ID| Error Message| 6833 | -------- | -------- | 6834 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 6835 | 1900005 | Operation allowed only for the proxy object. | 6836 | 1900008 | The proxy or remote object is invalid. | 6837 6838### addDeathRecipient<sup>(deprecated)</sup> 6839 6840addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6841 6842Adds 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. 6843 6844> **NOTE** 6845> 6846> This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9) instead. 6847 6848**System capability**: SystemCapability.Communication.IPC.Core 6849 6850**Parameters** 6851 6852 | Name | Type | Mandatory| Description | 6853 | --------- | --------------------------------- | ---- | -------------- | 6854 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6855 | flags | number | Yes | Flag of the death notification.| 6856 6857**Return value** 6858 6859 | Type | Description | 6860 | ------- | ---------------------------------------- | 6861 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6862 6863### unregisterDeathRecipient<sup>9+</sup> 6864 6865unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6866 6867Unregisters from the callback used to receive death notifications of the remote object. 6868 6869**System capability**: SystemCapability.Communication.IPC.Core 6870 6871**Parameters** 6872 6873 | Name | Type | Mandatory| Description | 6874 | --------- | --------------------------------- | ---- | -------------- | 6875 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6876 | flags | number | Yes | Flag of the death notification.| 6877 6878**Error codes** 6879 6880For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6881 6882 | ID| Error Message| 6883 | -------- | -------- | 6884 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 6885 | 1900005 | Operation allowed only for the proxy object. | 6886 | 1900008 | The proxy or remote object is invalid. | 6887 6888### removeDeathRecipient<sup>(deprecated)</sup> 6889 6890removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6891 6892Removes the callback used to receive death notifications of the remote object. 6893 6894> **NOTE** 6895> 6896> This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9) instead. 6897 6898**System capability**: SystemCapability.Communication.IPC.Core 6899 6900**Parameters** 6901 6902 | Name | Type | Mandatory| Description | 6903 | --------- | --------------------------------- | ---- | -------------- | 6904 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6905 | flags | number | Yes | Flag of the death notification.| 6906 6907**Return value** 6908 6909 | Type | Description | 6910 | ------- | -----------------------------------------| 6911 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6912 6913### getDescriptor<sup>9+</sup> 6914 6915getDescriptor(): string 6916 6917Obtains the interface descriptor (which is a string) of this object. 6918 6919**System capability**: SystemCapability.Communication.IPC.Core 6920 6921**Return value** 6922 6923 | Type | Description | 6924 | ------ | ---------------- | 6925 | string | Interface descriptor obtained.| 6926 6927**Error codes** 6928 6929For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6930 6931 | ID| Error Message| 6932 | -------- | -------- | 6933 | 1900008 | The proxy or remote object is invalid. | 6934 6935### getInterfaceDescriptor<sup>(deprecated)</sup> 6936 6937getInterfaceDescriptor(): string 6938 6939Obtains the interface descriptor (which is a string) of this object. 6940 6941> **NOTE** 6942> 6943> This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9) instead. 6944 6945**System capability**: SystemCapability.Communication.IPC.Core 6946 6947**Return value** 6948 6949 | Type | Description | 6950 | ------ | ---------------- | 6951 | string | Interface descriptor obtained.| 6952 6953### isObjectDead 6954 6955isObjectDead(): boolean 6956 6957Checks whether this object is dead. 6958 6959**System capability**: SystemCapability.Communication.IPC.Core 6960 6961**Return value** 6962 6963 | Type | Description | 6964 | ------- | ---------------------------------- | 6965 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6966 6967## RemoteProxy 6968 6969Provides APIs to implement **IRemoteObject**. 6970 6971### Properties 6972 6973**System capability**: SystemCapability.Communication.IPC.Core 6974 6975 | Name | Type | Readable | Writable| Description | 6976 | --------------------- | -------| ------|------|------------------------------------------ | 6977 | PING_TRANSACTION | number | Yes | No | Internal instruction code used to test whether the IPC service is normal. | 6978 | DUMP_TRANSACTION | number | Yes | No | Internal instruction code used to obtain IPC service status information. | 6979 | INTERFACE_TRANSACTION | number | Yes | No | Internal instruction code used to obtain the remote interface token. | 6980 | MIN_TRANSACTION_ID | number | Yes | No | Minimum valid instruction code. | 6981 | MAX_TRANSACTION_ID | number | Yes | No | Maximum valid instruction code. | 6982 6983 6984### sendRequest<sup>(deprecated)</sup> 6985 6986sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6987 6988Sends 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. 6989 6990> **NOTE** 6991> 6992> This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 6993 6994**System capability**: SystemCapability.Communication.IPC.Core 6995 6996**Parameters** 6997 6998 | Name | Type | Mandatory| Description | 6999 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7000 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7001 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7002 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7003 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7004 7005**Return value** 7006 7007 | Type | Description | 7008 | ------- | ---------------------------------| 7009 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 7010 7011**Example** 7012 7013>**NOTE** 7014> 7015>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7016 7017 <!--code_no_check--> 7018 ```ts 7019 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7020 // import { featureAbility } from '@kit.AbilityKit'; 7021 import { Want, common } from '@kit.AbilityKit'; 7022 import { hilog } from '@kit.PerformanceAnalysisKit'; 7023 7024 let proxy: rpc.IRemoteObject | undefined; 7025 let connect: common.ConnectOptions = { 7026 onConnect: (elementName, remoteProxy) => { 7027 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7028 proxy = remoteProxy; 7029 }, 7030 onDisconnect: (elementName) => { 7031 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7032 }, 7033 onFailed: () => { 7034 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7035 } 7036 }; 7037 let want: Want = { 7038 bundleName: "com.ohos.server", 7039 abilityName: "com.ohos.server.EntryAbility", 7040 }; 7041 7042 // Use this method to connect to the ability for the FA model. 7043 // FA.connectAbility(want,connect); 7044 7045 // Save the connection ID, which will be used for the subsequent service disconnection. 7046 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7047 // Save the connection ID, which will be used for the subsequent service disconnection. 7048 let connectionId = context.connectServiceExtensionAbility(want, connect); 7049 ``` 7050 7051 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7052 7053 ```ts 7054 import { hilog } from '@kit.PerformanceAnalysisKit'; 7055 7056 let option = new rpc.MessageOption(); 7057 let data = rpc.MessageParcel.create(); 7058 let reply = rpc.MessageParcel.create(); 7059 data.writeInt(1); 7060 data.writeString("hello"); 7061 if (proxy != undefined) { 7062 let ret: boolean = proxy.sendRequest(1, data, reply, option); 7063 if (ret) { 7064 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7065 let msg = reply.readString(); 7066 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7067 } else { 7068 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 7069 } 7070 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7071 data.reclaim(); 7072 reply.reclaim(); 7073 } 7074 ``` 7075 7076### sendMessageRequest<sup>9+</sup> 7077 7078sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7079 7080Sends 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. 7081 7082**System capability**: SystemCapability.Communication.IPC.Core 7083 7084**Parameters** 7085 7086 | Name | Type | Mandatory| Description | 7087 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7088 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7089 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7090 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7091 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7092 7093**Return value** 7094 7095 | Type | Description | 7096 | ---------------------------- | ----------------------------------------- | 7097 | Promise<[RequestResult](#requestresult9)> | Promise used to return a **requestResult** instance.| 7098 7099 7100**Error codes** 7101 7102For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7103 7104 | ID| Error Message| 7105 | -------- | -------- | 7106 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 7107 7108**Example** 7109 7110>**NOTE** 7111> 7112>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7113 7114 <!--code_no_check--> 7115 ```ts 7116 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7117 // import { featureAbility } from '@kit.AbilityKit'; 7118 import { Want, common } from '@kit.AbilityKit'; 7119 import { hilog } from '@kit.PerformanceAnalysisKit'; 7120 7121 let proxy: rpc.IRemoteObject | undefined; 7122 let connect: common.ConnectOptions = { 7123 onConnect: (elementName, remoteProxy) => { 7124 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7125 proxy = remoteProxy; 7126 }, 7127 onDisconnect: (elementName) => { 7128 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7129 }, 7130 onFailed: () => { 7131 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7132 } 7133 }; 7134 let want: Want = { 7135 bundleName: "com.ohos.server", 7136 abilityName: "com.ohos.server.EntryAbility", 7137 }; 7138 7139 // Use this method to connect to the ability for the FA model. 7140 // FA.connectAbility(want,connect); 7141 7142 // Save the connection ID, which will be used for the subsequent service disconnection. 7143 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7144 // Save the connection ID, which will be used for the subsequent service disconnection. 7145 let connectionId = context.connectServiceExtensionAbility(want, connect); 7146 ``` 7147 7148 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. 7149 7150 ```ts 7151 import { hilog } from '@kit.PerformanceAnalysisKit'; 7152 7153 let option = new rpc.MessageOption(); 7154 let data = rpc.MessageSequence.create(); 7155 let reply = rpc.MessageSequence.create(); 7156 data.writeInt(1); 7157 data.writeString("hello"); 7158 if (proxy != undefined) { 7159 proxy.sendMessageRequest(1, data, reply, option) 7160 .then((result: rpc.RequestResult) => { 7161 if (result.errCode === 0) { 7162 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7163 let num = result.reply.readInt(); 7164 let msg = result.reply.readString(); 7165 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7166 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7167 } else { 7168 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7169 } 7170 }).catch((e: Error) => { 7171 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7172 }).finally (() => { 7173 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7174 data.reclaim(); 7175 reply.reclaim(); 7176 }); 7177 } 7178 ``` 7179 7180### sendRequest<sup>(deprecated)</sup> 7181 7182sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7183 7184Sends 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. 7185 7186> **NOTE** 7187> 7188> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 7189 7190**System capability**: SystemCapability.Communication.IPC.Core 7191 7192**Parameters** 7193 7194 | Name | Type | Mandatory| Description | 7195 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7196 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7197 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7198 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7199 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7200 7201**Return value** 7202 7203| Type | Description | 7204| ------------------------------------------------------------ | --------------------------------------------- | 7205| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 7206 7207**Example** 7208 7209>**NOTE** 7210> 7211>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7212 7213 <!--code_no_check--> 7214 ```ts 7215 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7216 // import { featureAbility } from '@kit.AbilityKit'; 7217 import { Want, common } from '@kit.AbilityKit'; 7218 import { hilog } from '@kit.PerformanceAnalysisKit'; 7219 7220 let proxy: rpc.IRemoteObject | undefined; 7221 let connect: common.ConnectOptions = { 7222 onConnect: (elementName, remoteProxy) => { 7223 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7224 proxy = remoteProxy; 7225 }, 7226 onDisconnect: (elementName) => { 7227 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7228 }, 7229 onFailed: () => { 7230 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7231 } 7232 }; 7233 let want: Want = { 7234 bundleName: "com.ohos.server", 7235 abilityName: "com.ohos.server.EntryAbility", 7236 }; 7237 7238 // Use this method to connect to the ability for the FA model. 7239 // FA.connectAbility(want,connect); 7240 7241 // Save the connection ID, which will be used for the subsequent service disconnection. 7242 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7243 // Save the connection ID, which will be used for the subsequent service disconnection. 7244 let connectionId = context.connectServiceExtensionAbility(want, connect); 7245 ``` 7246 7247 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7248 7249 ```ts 7250 import { hilog } from '@kit.PerformanceAnalysisKit'; 7251 7252 let option = new rpc.MessageOption(); 7253 let data = rpc.MessageParcel.create(); 7254 let reply = rpc.MessageParcel.create(); 7255 data.writeInt(1); 7256 data.writeString("hello"); 7257 if (proxy != undefined) { 7258 let a = proxy.sendRequest(1, data, reply, option) as Object; 7259 let b = a as Promise<rpc.SendRequestResult>; 7260 b.then((result: rpc.SendRequestResult) => { 7261 if (result.errCode === 0) { 7262 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7263 let num = result.reply.readInt(); 7264 let msg = result.reply.readString(); 7265 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7266 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7267 } else { 7268 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7269 } 7270 }).catch((e: Error) => { 7271 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7272 }).finally (() => { 7273 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7274 data.reclaim(); 7275 reply.reclaim(); 7276 }); 7277 } 7278 ``` 7279 7280### sendMessageRequest<sup>9+</sup> 7281 7282sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7283 7284Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked at certain time after the response to **RequestResult** is returned, and the reply contains the returned information. 7285 7286**System capability**: SystemCapability.Communication.IPC.Core 7287 7288**Parameters** 7289 7290 | Name | Type | Mandatory| Description | 7291 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7292 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7293 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7294 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7295 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7296 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 7297 7298 7299**Error codes** 7300 7301For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7302 7303 | ID| Error Message| 7304 | -------- | -------- | 7305 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 7306 7307### sendRequest<sup>(deprecated)</sup> 7308 7309sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7310 7311Sends 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. 7312 7313> **NOTE** 7314> 7315> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-3) instead. 7316 7317**System capability**: SystemCapability.Communication.IPC.Core 7318 7319**Parameters** 7320 7321| Name | Type | Mandatory| Description | 7322| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7323| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 7324| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7325| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7326| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7327| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 7328 7329### getLocalInterface<sup>9+</sup> 7330 7331getLocalInterface(interface: string): IRemoteBroker 7332 7333Obtains the **LocalInterface** object of an interface token. 7334 7335**System capability**: SystemCapability.Communication.IPC.Core 7336 7337**Parameters** 7338 7339 | Name | Type | Mandatory| Description | 7340 | --------- | ------ | ---- | ---------------------- | 7341 | interface | string | Yes | Interface descriptor.| 7342 7343**Return value** 7344 7345| Type | Description | 7346| ------------------------------- | ------------------------------------------ | 7347| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7348 7349**Error codes** 7350 7351For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7352 7353 | ID| Error Message| 7354 | -------- | -------- | 7355 | 401 | check param failed | 7356 | 1900006 | Operation allowed only for the remote object. | 7357 7358**Example** 7359 7360>**NOTE** 7361> 7362>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7363 7364 <!--code_no_check--> 7365 ```ts 7366 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7367 // import { featureAbility } from '@kit.AbilityKit'; 7368 import { Want, common } from '@kit.AbilityKit'; 7369 import { hilog } from '@kit.PerformanceAnalysisKit'; 7370 7371 let proxy: rpc.IRemoteObject | undefined; 7372 let connect: common.ConnectOptions = { 7373 onConnect: (elementName, remoteProxy) => { 7374 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7375 proxy = remoteProxy; 7376 }, 7377 onDisconnect: (elementName) => { 7378 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7379 }, 7380 onFailed: () => { 7381 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7382 } 7383 }; 7384 let want: Want = { 7385 bundleName: "com.ohos.server", 7386 abilityName: "com.ohos.server.EntryAbility", 7387 }; 7388 7389 // Use this method to connect to the ability for the FA model. 7390 // FA.connectAbility(want,connect); 7391 7392 // Save the connection ID, which will be used for the subsequent service disconnection. 7393 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7394 // Save the connection ID, which will be used for the subsequent service disconnection. 7395 let connectionId = context.connectServiceExtensionAbility(want, connect); 7396 ``` 7397 7398 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. 7399 7400 ```ts 7401 import { hilog } from '@kit.PerformanceAnalysisKit'; 7402 import { BusinessError } from '@kit.BasicServicesKit'; 7403 7404 if (proxy != undefined) { 7405 try { 7406 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7407 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7408 } catch (error) { 7409 let e: BusinessError = error as BusinessError; 7410 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7411 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7412 } 7413 } 7414 ``` 7415 7416### queryLocalInterface<sup>(deprecated)</sup> 7417 7418queryLocalInterface(interface: string): IRemoteBroker 7419 7420Obtains the **LocalInterface** object of an interface token. 7421 7422> **NOTE** 7423> 7424> This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-1) instead. 7425 7426**System capability**: SystemCapability.Communication.IPC.Core 7427 7428**Parameters** 7429 7430 | Name | Type | Mandatory| Description | 7431 | --------- | ------ | ---- | ---------------------- | 7432 | interface | string | Yes | Interface descriptor.| 7433 7434**Return value** 7435 7436| Type | Description | 7437| ------------------------------- | ------------------------------------------ | 7438| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7439 7440**Example** 7441 7442>**NOTE** 7443> 7444>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7445 7446 <!--code_no_check--> 7447 ```ts 7448 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7449 // import { featureAbility } from '@kit.AbilityKit'; 7450 import { Want, common } from '@kit.AbilityKit'; 7451 import { hilog } from '@kit.PerformanceAnalysisKit'; 7452 7453 let proxy: rpc.IRemoteObject | undefined; 7454 let connect: common.ConnectOptions = { 7455 onConnect: (elementName, remoteProxy) => { 7456 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7457 proxy = remoteProxy; 7458 }, 7459 onDisconnect: (elementName) => { 7460 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7461 }, 7462 onFailed: () => { 7463 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7464 } 7465 }; 7466 let want: Want = { 7467 bundleName: "com.ohos.server", 7468 abilityName: "com.ohos.server.EntryAbility", 7469 }; 7470 7471 // Use this method to connect to the ability for the FA model. 7472 // FA.connectAbility(want,connect); 7473 7474 // Save the connection ID, which will be used for the subsequent service disconnection. 7475 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7476 // Save the connection ID, which will be used for the subsequent service disconnection. 7477 let connectionId = context.connectServiceExtensionAbility(want, connect); 7478 ``` 7479 7480 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. 7481 7482 ```ts 7483 import { hilog } from '@kit.PerformanceAnalysisKit'; 7484 7485 if (proxy != undefined) { 7486 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7487 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7488 } 7489 ``` 7490 7491### registerDeathRecipient<sup>9+</sup> 7492 7493registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7494 7495Registers 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. 7496 7497**System capability**: SystemCapability.Communication.IPC.Core 7498 7499**Parameters** 7500 7501 | Name | Type | Mandatory| Description | 7502 | --------- | --------------------------------- | ---- | -------------- | 7503 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 7504 | flags | number | Yes | Flag of the death notification.| 7505 7506**Error codes** 7507 7508For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7509 7510 | ID| Error Message| 7511 | -------- | -------- | 7512 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 7513 | 1900008 | The proxy or remote object is invalid. | 7514 7515**Example** 7516 7517>**NOTE** 7518> 7519>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7520 7521 <!--code_no_check--> 7522 ```ts 7523 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7524 // import { featureAbility } from '@kit.AbilityKit'; 7525 import { Want, common } from '@kit.AbilityKit'; 7526 import { hilog } from '@kit.PerformanceAnalysisKit'; 7527 7528 let proxy: rpc.IRemoteObject | undefined; 7529 let connect: common.ConnectOptions = { 7530 onConnect: (elementName, remoteProxy) => { 7531 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7532 proxy = remoteProxy; 7533 }, 7534 onDisconnect: (elementName) => { 7535 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7536 }, 7537 onFailed: () => { 7538 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7539 } 7540 }; 7541 let want: Want = { 7542 bundleName: "com.ohos.server", 7543 abilityName: "com.ohos.server.EntryAbility", 7544 }; 7545 7546 // Use this method to connect to the ability for the FA model. 7547 // FA.connectAbility(want,connect); 7548 7549 // Save the connection ID, which will be used for the subsequent service disconnection. 7550 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7551 // Save the connection ID, which will be used for the subsequent service disconnection. 7552 let connectionId = context.connectServiceExtensionAbility(want, connect); 7553 ``` 7554 7555 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. 7556 7557 ```ts 7558 import { hilog } from '@kit.PerformanceAnalysisKit'; 7559 import { BusinessError } from '@kit.BasicServicesKit'; 7560 7561 class MyDeathRecipient implements rpc.DeathRecipient { 7562 onRemoteDied() { 7563 hilog.info(0x0000, 'testTag', 'server died'); 7564 } 7565 } 7566 let deathRecipient = new MyDeathRecipient(); 7567 if (proxy != undefined) { 7568 try { 7569 proxy.registerDeathRecipient(deathRecipient, 0); 7570 } catch (error) { 7571 let e: BusinessError = error as BusinessError; 7572 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7573 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7574 } 7575 } 7576 ``` 7577 7578### addDeathRecipient<sup>(deprecated)</sup> 7579 7580addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7581 7582Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 7583 7584> **NOTE** 7585> 7586> This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9-1) instead. 7587 7588**System capability**: SystemCapability.Communication.IPC.Core 7589 7590**Parameters** 7591 7592 | Name | Type | Mandatory| Description | 7593 | --------- | --------------------------------- | ---- | --------------------------------- | 7594 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 7595 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7596 7597**Return value** 7598 7599 | Type | Description | 7600 | ------- | ---------------------------------------- | 7601 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 7602 7603**Example** 7604 7605>**NOTE** 7606> 7607>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7608 7609 <!--code_no_check--> 7610 ```ts 7611 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7612 // import { featureAbility } from '@kit.AbilityKit'; 7613 import { Want, common } from '@kit.AbilityKit'; 7614 import { hilog } from '@kit.PerformanceAnalysisKit'; 7615 7616 let proxy: rpc.IRemoteObject | undefined; 7617 let connect: common.ConnectOptions = { 7618 onConnect: (elementName, remoteProxy) => { 7619 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7620 proxy = remoteProxy; 7621 }, 7622 onDisconnect: (elementName) => { 7623 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7624 }, 7625 onFailed: () => { 7626 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7627 } 7628 }; 7629 let want: Want = { 7630 bundleName: "com.ohos.server", 7631 abilityName: "com.ohos.server.EntryAbility", 7632 }; 7633 7634 // Use this method to connect to the ability for the FA model. 7635 // FA.connectAbility(want,connect); 7636 7637 // Save the connection ID, which will be used for the subsequent service disconnection. 7638 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7639 // Save the connection ID, which will be used for the subsequent service disconnection. 7640 let connectionId = context.connectServiceExtensionAbility(want, connect); 7641 ``` 7642 7643 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecipient()** of the proxy object is called to add a callback for receiving the death notification of the remove object. 7644 7645 ```ts 7646 import { hilog } from '@kit.PerformanceAnalysisKit'; 7647 7648 class MyDeathRecipient implements rpc.DeathRecipient { 7649 onRemoteDied() { 7650 hilog.info(0x0000, 'testTag', 'server died'); 7651 } 7652 } 7653 let deathRecipient = new MyDeathRecipient(); 7654 if (proxy != undefined) { 7655 proxy.addDeathRecipient(deathRecipient, 0); 7656 } 7657 ``` 7658 7659### unregisterDeathRecipient<sup>9+</sup> 7660 7661unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7662 7663Unregisters from the callback used to receive death notifications of the remote object. 7664 7665**System capability**: SystemCapability.Communication.IPC.Core 7666 7667**Parameters** 7668 7669 | Name | Type | Mandatory| Description | 7670 | --------- | --------------------------------- | ---- | -------------- | 7671 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 7672 | flags | number | Yes | Flag of the death notification.| 7673 7674**Error codes** 7675 7676For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7677 7678 | ID| Error Message| 7679 | -------- | -------- | 7680 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 7681 | 1900008 | The proxy or remote object is invalid. | 7682 7683**Example** 7684 7685>**NOTE** 7686> 7687>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7688 7689 <!--code_no_check--> 7690 ```ts 7691 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7692 // import { featureAbility } from '@kit.AbilityKit'; 7693 import { Want, common } from '@kit.AbilityKit'; 7694 import { hilog } from '@kit.PerformanceAnalysisKit'; 7695 7696 let proxy: rpc.IRemoteObject | undefined; 7697 let connect: common.ConnectOptions = { 7698 onConnect: (elementName, remoteProxy) => { 7699 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7700 proxy = remoteProxy; 7701 }, 7702 onDisconnect: (elementName) => { 7703 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7704 }, 7705 onFailed: () => { 7706 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7707 } 7708 }; 7709 let want: Want = { 7710 bundleName: "com.ohos.server", 7711 abilityName: "com.ohos.server.EntryAbility", 7712 }; 7713 7714 // Use this method to connect to the ability for the FA model. 7715 // FA.connectAbility(want,connect); 7716 7717 // Save the connection ID, which will be used for the subsequent service disconnection. 7718 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7719 // Save the connection ID, which will be used for the subsequent service disconnection. 7720 let connectionId = context.connectServiceExtensionAbility(want, connect); 7721 ``` 7722 7723 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. 7724 7725 ```ts 7726 import { hilog } from '@kit.PerformanceAnalysisKit'; 7727 import { BusinessError } from '@kit.BasicServicesKit'; 7728 7729 class MyDeathRecipient implements rpc.DeathRecipient { 7730 onRemoteDied() { 7731 hilog.info(0x0000, 'testTag', 'server died'); 7732 } 7733 } 7734 let deathRecipient = new MyDeathRecipient(); 7735 if (proxy != undefined) { 7736 try { 7737 proxy.registerDeathRecipient(deathRecipient, 0); 7738 proxy.unregisterDeathRecipient(deathRecipient, 0); 7739 } catch (error) { 7740 let e: BusinessError = error as BusinessError; 7741 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7742 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7743 } 7744 } 7745 ``` 7746 7747### removeDeathRecipient<sup>(deprecated)</sup> 7748 7749removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7750 7751Removes the callback used to receive death notifications of the remote object. 7752 7753> **NOTE** 7754> 7755> This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9-1) instead. 7756 7757**System capability**: SystemCapability.Communication.IPC.Core 7758 7759**Parameters** 7760 7761 | Name | Type | Mandatory| Description | 7762 | --------- | --------------------------------- | ---- | --------------------------------- | 7763 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 7764 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7765 7766**Return value** 7767 7768 | Type | Description | 7769 | ------- | ---------------------------------------- | 7770 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 7771 7772**Example** 7773 7774>**NOTE** 7775> 7776>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7777 7778 <!--code_no_check--> 7779 ```ts 7780 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7781 // import { featureAbility } from '@kit.AbilityKit'; 7782 import { Want, common } from '@kit.AbilityKit'; 7783 import { hilog } from '@kit.PerformanceAnalysisKit'; 7784 7785 let proxy: rpc.IRemoteObject | undefined; 7786 let connect: common.ConnectOptions = { 7787 onConnect: (elementName, remoteProxy) => { 7788 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7789 proxy = remoteProxy; 7790 }, 7791 onDisconnect: (elementName) => { 7792 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7793 }, 7794 onFailed: () => { 7795 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7796 } 7797 }; 7798 let want: Want = { 7799 bundleName: "com.ohos.server", 7800 abilityName: "com.ohos.server.EntryAbility", 7801 }; 7802 7803 // Use this method to connect to the ability for the FA model. 7804 // FA.connectAbility(want,connect); 7805 7806 // Save the connection ID, which will be used for the subsequent service disconnection. 7807 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7808 // Save the connection ID, which will be used for the subsequent service disconnection. 7809 let connectionId = context.connectServiceExtensionAbility(want, connect); 7810 ``` 7811 7812 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. 7813 7814 ```ts 7815 import { hilog } from '@kit.PerformanceAnalysisKit'; 7816 7817 class MyDeathRecipient implements rpc.DeathRecipient { 7818 onRemoteDied() { 7819 hilog.info(0x0000, 'testTag', 'server died'); 7820 } 7821 } 7822 let deathRecipient = new MyDeathRecipient(); 7823 if (proxy != undefined) { 7824 proxy.addDeathRecipient(deathRecipient, 0); 7825 proxy.removeDeathRecipient(deathRecipient, 0); 7826 } 7827 ``` 7828 7829### getDescriptor<sup>9+</sup> 7830 7831getDescriptor(): string 7832 7833Obtains the interface descriptor (which is a string) of this object. 7834 7835**System capability**: SystemCapability.Communication.IPC.Core 7836 7837**Return value** 7838 7839 | Type | Description | 7840 | ------ | ---------------- | 7841 | string | Interface descriptor obtained.| 7842 7843**Error codes** 7844 7845For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7846 7847 | ID| Error Message| 7848 | -------- | -------- | 7849 | 1900007 | communication failed. | 7850 | 1900008 | The proxy or remote object is invalid. | 7851 7852**Example** 7853 7854>**NOTE** 7855> 7856>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7857 7858 <!--code_no_check--> 7859 ```ts 7860 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7861 // import { featureAbility } from '@kit.AbilityKit'; 7862 import { Want, common } from '@kit.AbilityKit'; 7863 import { hilog } from '@kit.PerformanceAnalysisKit'; 7864 7865 let proxy: rpc.IRemoteObject | undefined; 7866 let connect: common.ConnectOptions = { 7867 onConnect: (elementName, remoteProxy) => { 7868 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7869 proxy = remoteProxy; 7870 }, 7871 onDisconnect: (elementName) => { 7872 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7873 }, 7874 onFailed: () => { 7875 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7876 } 7877 }; 7878 let want: Want = { 7879 bundleName: "com.ohos.server", 7880 abilityName: "com.ohos.server.EntryAbility", 7881 }; 7882 7883 // Use this method to connect to the ability for the FA model. 7884 // FA.connectAbility(want,connect); 7885 7886 // Save the connection ID, which will be used for the subsequent service disconnection. 7887 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7888 // Save the connection ID, which will be used for the subsequent service disconnection. 7889 let connectionId = context.connectServiceExtensionAbility(want, connect); 7890 ``` 7891 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. 7892 7893 ```ts 7894 import { hilog } from '@kit.PerformanceAnalysisKit'; 7895 import { BusinessError } from '@kit.BasicServicesKit'; 7896 7897 if (proxy != undefined) { 7898 try { 7899 let descriptor: string = proxy.getDescriptor(); 7900 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7901 } catch (error) { 7902 let e: BusinessError = error as BusinessError; 7903 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7904 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7905 } 7906 } 7907 ``` 7908 7909### getInterfaceDescriptor<sup>(deprecated)</sup> 7910 7911getInterfaceDescriptor(): string 7912 7913Obtains the interface descriptor of this proxy object. 7914 7915> **NOTE** 7916> 7917> This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-1) instead. 7918 7919**System capability**: SystemCapability.Communication.IPC.Core 7920 7921**Return value** 7922 7923 | Type | Description | 7924 | ------ | ------------------ | 7925 | string | Interface descriptor obtained.| 7926 7927**Example** 7928 7929>**NOTE** 7930> 7931>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7932 7933 <!--code_no_check--> 7934 ```ts 7935 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7936 // import { featureAbility } from '@kit.AbilityKit'; 7937 import { Want, common } from '@kit.AbilityKit'; 7938 import { hilog } from '@kit.PerformanceAnalysisKit'; 7939 7940 let proxy: rpc.IRemoteObject | undefined; 7941 let connect: common.ConnectOptions = { 7942 onConnect: (elementName, remoteProxy) => { 7943 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7944 proxy = remoteProxy; 7945 }, 7946 onDisconnect: (elementName) => { 7947 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7948 }, 7949 onFailed: () => { 7950 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7951 } 7952 }; 7953 let want: Want = { 7954 bundleName: "com.ohos.server", 7955 abilityName: "com.ohos.server.EntryAbility", 7956 }; 7957 7958 // Use this method to connect to the ability for the FA model. 7959 // FA.connectAbility(want,connect); 7960 7961 // Save the connection ID, which will be used for the subsequent service disconnection. 7962 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 7963 // Save the connection ID, which will be used for the subsequent service disconnection. 7964 let connectionId = context.connectServiceExtensionAbility(want, connect); 7965 ``` 7966 7967 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. 7968 7969 ```ts 7970 import { hilog } from '@kit.PerformanceAnalysisKit'; 7971 7972 if (proxy != undefined) { 7973 let descriptor: string = proxy.getInterfaceDescriptor(); 7974 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7975 } 7976 ``` 7977 7978### isObjectDead 7979 7980isObjectDead(): boolean 7981 7982Checks whether the **RemoteObject** is dead. 7983 7984**System capability**: SystemCapability.Communication.IPC.Core 7985 7986**Return value** 7987 7988 | Type | Description | 7989 | ------- | ------------------------------------------------- | 7990 | boolean | Returns **true** if **RemoteObject** is dead; returns **false** otherwise.| 7991 7992**Example** 7993 7994>**NOTE** 7995> 7996>In the sample code provided in this topic, **this.context** is used to obtain **UIAbilityContext**, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 7997 7998 <!--code_no_check--> 7999 ```ts 8000 // If the FA model is used, import featureAbility from @kit.AbilityKit. 8001 // import { featureAbility } from '@kit.AbilityKit'; 8002 import { Want, common } from '@kit.AbilityKit'; 8003 import { hilog } from '@kit.PerformanceAnalysisKit'; 8004 8005 let proxy: rpc.IRemoteObject | undefined; 8006 let connect: common.ConnectOptions = { 8007 onConnect: (elementName, remoteProxy) => { 8008 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8009 proxy = remoteProxy; 8010 }, 8011 onDisconnect: (elementName) => { 8012 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8013 }, 8014 onFailed: () => { 8015 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8016 } 8017 }; 8018 let want: Want = { 8019 bundleName: "com.ohos.server", 8020 abilityName: "com.ohos.server.EntryAbility", 8021 }; 8022 8023 // Use this method to connect to the ability for the FA model. 8024 // FA.connectAbility(want,connect); 8025 8026 // Save the connection ID, which will be used for the subsequent service disconnection. 8027 let context: common.UIAbilityContext = this.getUIContext().getHostContext(); // UIAbilityContext 8028 // Save the connection ID, which will be used for the subsequent service disconnection. 8029 let connectionId = context.connectServiceExtensionAbility(want, connect); 8030 ``` 8031 8032 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. 8033 8034 ```ts 8035 import { hilog } from '@kit.PerformanceAnalysisKit'; 8036 8037 if (proxy != undefined) { 8038 let isDead: boolean = proxy.isObjectDead(); 8039 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8040 } 8041 ``` 8042 8043## MessageOption 8044 8045Defines the options used to construct the **MessageOption** object. 8046 8047### Properties 8048 8049**System capability**: SystemCapability.Communication.IPC.Core 8050 8051 | Name | Type | Readable | Writable | Description | 8052 | ------------- | ------ | ----- | ----- | ------------------------------------------------------------------------ | 8053 | TF_SYNC | number | Yes | No | Synchronous call. | 8054 | TF_ASYNC | number | Yes | No | Asynchronous call. | 8055 | TF_ACCEPT_FDS | number | Yes | No | Indication to **sendMessageRequest<sup>9+</sup>** for passing the file descriptor. | 8056 | TF_WAIT_TIME | number | Yes | Yes | RPC wait time, in seconds. This parameter cannot be used in IPC. The default waiting time is 8 seconds. You are advised not to change the waiting time.| 8057 8058### constructor<sup>9+</sup> 8059 8060constructor(async?: boolean) 8061 8062A constructor used to create a **MessageOption** object. 8063 8064**System capability**: SystemCapability.Communication.IPC.Core 8065 8066**Parameters** 8067 8068| Name| Type | Mandatory| Description | 8069| ------ | ------- | ---- | ------------------------------------------------------------ | 8070| async | boolean | No | Whether to execute the call asynchronously. The value **true** means to execute the call asynchronously; the value **false** means to execute the call synchronously. The default value is **synchronous**.| 8071 8072**Example** 8073 8074 ```ts 8075 class TestRemoteObject extends rpc.MessageOption { 8076 constructor(async: boolean) { 8077 super(async); 8078 } 8079 } 8080 ``` 8081 8082### constructor 8083 8084constructor(syncFlags?: number, waitTime?: number) 8085 8086A constructor used to create a **MessageOption** object. 8087 8088**System capability**: SystemCapability.Communication.IPC.Core 8089 8090**Parameters** 8091 8092 | Name | Type | Mandatory| Description | 8093 | --------- | ------ | ---- | --------------------------------------------- | 8094 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 8095 | waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 8096 8097**Example** 8098 8099 ```ts 8100 class TestRemoteObject extends rpc.MessageOption { 8101 constructor(syncFlags?: number,waitTime?: number) { 8102 super(syncFlags,waitTime); 8103 } 8104 } 8105 ``` 8106### isAsync<sup>9+</sup> 8107 8108isAsync(): boolean 8109 8110Checks whether **SendMessageRequest** is called synchronously or asynchronously. 8111 8112**System capability**: SystemCapability.Communication.IPC.Core 8113 8114**Return value** 8115 8116 | Type | Description | 8117 | ------- | ---------------------------------------- | 8118 | boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 8119 8120**Example** 8121 8122 ```ts 8123 let option = new rpc.MessageOption(); 8124 option.isAsync(); 8125 ``` 8126 8127### setAsync<sup>9+</sup> 8128 8129setAsync(async: boolean): void 8130 8131Sets whether **SendMessageRequest** is called synchronously or asynchronously. 8132 8133**System capability**: SystemCapability.Communication.IPC.Core 8134 8135**Parameters** 8136 8137| Name| Type | Mandatory| Description | 8138| ------ | ------- | ---- | ------------------------------------------------- | 8139| async | boolean | Yes | Whether to execute the call asynchronously. The value **true** means to execute the call asynchronously; the value **false** means to execute the call synchronously.| 8140 8141**Example** 8142 8143 ```ts 8144 import { hilog } from '@kit.PerformanceAnalysisKit'; 8145 8146 let option = new rpc.MessageOption(); 8147 option.setAsync(true); 8148 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8149 ``` 8150 8151### getFlags 8152 8153getFlags(): number 8154 8155Obtains the call flag, which can be synchronous or asynchronous. 8156 8157**System capability**: SystemCapability.Communication.IPC.Core 8158 8159**Return value** 8160 8161 | Type | Description | 8162 | ------ | ------------------------------------ | 8163 | number | Call mode obtained.| 8164 8165**Example** 8166 8167 ```ts 8168 import { hilog } from '@kit.PerformanceAnalysisKit'; 8169 8170 try { 8171 let option = new rpc.MessageOption(); 8172 hilog.info(0x0000, 'testTag', 'create object successfully'); 8173 let flog = option.getFlags(); 8174 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8175 option.setFlags(rpc.MessageOption.TF_ASYNC); 8176 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8177 let flog2 = option.getFlags(); 8178 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8179 } catch (error) { 8180 hilog.error(0x0000, 'testTag', 'error ' + error); 8181 } 8182 ``` 8183 8184### setFlags 8185 8186setFlags(flags: number): void 8187 8188Sets the call flag, which can be synchronous or asynchronous. 8189 8190**System capability**: SystemCapability.Communication.IPC.Core 8191 8192**Parameters** 8193 8194 | Name| Type | Mandatory| Description | 8195 | ------ | ------ | ---- | ------------------------ | 8196 | flags | number | Yes | Call flag to set.| 8197 8198**Example** 8199 8200 ```ts 8201 import { hilog } from '@kit.PerformanceAnalysisKit'; 8202 8203 try { 8204 let option = new rpc.MessageOption(); 8205 option.setFlags(rpc.MessageOption.TF_ASYNC); 8206 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8207 let flog = option.getFlags(); 8208 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8209 } catch (error) { 8210 hilog.error(0x0000, 'testTag', 'error ' + error); 8211 } 8212 ``` 8213 8214### getWaitTime 8215 8216getWaitTime(): number 8217 8218Obtains the maximum wait time for this RPC call. 8219 8220**System capability**: SystemCapability.Communication.IPC.Core 8221 8222**Return value** 8223 8224 | Type | Description | 8225 | ------ | ----------------- | 8226 | number | Maximum wait time obtained.| 8227 8228**Example** 8229 8230 ```ts 8231 import { hilog } from '@kit.PerformanceAnalysisKit'; 8232 8233 try { 8234 let option = new rpc.MessageOption(); 8235 let time = option.getWaitTime(); 8236 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8237 option.setWaitTime(16); 8238 let time2 = option.getWaitTime(); 8239 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8240 } catch (error) { 8241 hilog.error(0x0000, 'testTag', 'error ' + error); 8242 } 8243 ``` 8244 8245### setWaitTime 8246 8247setWaitTime(waitTime: number): void 8248 8249Sets the maximum wait time for this RPC call. 8250 8251**System capability**: SystemCapability.Communication.IPC.Core 8252 8253**Parameters** 8254 8255 | Name | Type | Mandatory| Description | 8256 | -------- | ------ | ---- | --------------------- | 8257 | waitTime | number | Yes | Maximum wait time to set. The upper limit is 3000 seconds.| 8258 8259**Example** 8260 8261 ```ts 8262 import { hilog } from '@kit.PerformanceAnalysisKit'; 8263 8264 try { 8265 let option = new rpc.MessageOption(); 8266 option.setWaitTime(16); 8267 let time = option.getWaitTime(); 8268 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8269 } catch (error) { 8270 hilog.error(0x0000, 'testTag', 'error ' + error); 8271 } 8272 ``` 8273 8274## IPCSkeleton 8275 8276Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 8277 8278### getContextObject 8279 8280static getContextObject(): IRemoteObject 8281 8282Obtains the system capability manager. This API is a static method. 8283 8284**System capability**: SystemCapability.Communication.IPC.Core 8285 8286**Return value** 8287 8288 | Type | Description | 8289 | ------------------------------- | -------------------- | 8290 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 8291 8292**Example** 8293 8294 ```ts 8295 import { hilog } from '@kit.PerformanceAnalysisKit'; 8296 8297 let samgr = rpc.IPCSkeleton.getContextObject(); 8298 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8299 ``` 8300 8301### getCallingPid 8302 8303static getCallingPid(): number 8304 8305Obtains 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. 8306 8307**System capability**: SystemCapability.Communication.IPC.Core 8308 8309**Return value** 8310 8311 | Type | Description | 8312 | ------ | ----------------- | 8313 | number | PID of the caller.| 8314 8315**Example** 8316 8317 ```ts 8318 import { hilog } from '@kit.PerformanceAnalysisKit'; 8319 8320 class Stub extends rpc.RemoteObject { 8321 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8322 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8323 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8324 return true; 8325 } 8326 } 8327 ``` 8328 8329### getCallingUid 8330 8331static getCallingUid(): number 8332 8333Obtains 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. 8334 8335**System capability**: SystemCapability.Communication.IPC.Core 8336 8337**Return value** 8338 8339 | Type | Description | 8340 | ------ | ----------------- | 8341 | number | UID of the caller.| 8342 8343**Example** 8344 8345 ```ts 8346 import { hilog } from '@kit.PerformanceAnalysisKit'; 8347 8348 class Stub extends rpc.RemoteObject { 8349 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8350 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8351 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8352 return true; 8353 } 8354 } 8355 ``` 8356 8357### getCallingTokenId<sup>8+</sup> 8358 8359static getCallingTokenId(): number 8360 8361Obtains the caller's token ID, which is used to verify the caller identity. 8362 8363**System capability**: SystemCapability.Communication.IPC.Core 8364 8365**Return value** 8366 8367 | Type | Description | 8368 | ------ | --------------------- | 8369 | number | Token ID of the caller obtained.| 8370 8371**Example** 8372 8373 ```ts 8374 import { hilog } from '@kit.PerformanceAnalysisKit'; 8375 8376 class Stub extends rpc.RemoteObject { 8377 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8378 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8379 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8380 return true; 8381 } 8382 } 8383 ``` 8384 8385### getCallingDeviceID 8386 8387static getCallingDeviceID(): string 8388 8389Obtains the ID of the device hosting the caller's process. This API is a static method. 8390 8391**System capability**: SystemCapability.Communication.IPC.Core 8392 8393**Return value** 8394 8395 | Type | Description | 8396 | ------ | ---------------------------- | 8397 | string | Device ID obtained.| 8398 8399**Example** 8400 8401 ```ts 8402 import { hilog } from '@kit.PerformanceAnalysisKit'; 8403 8404 class Stub extends rpc.RemoteObject { 8405 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8406 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8407 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8408 return true; 8409 } 8410 } 8411 ``` 8412 8413### getLocalDeviceID 8414 8415static getLocalDeviceID(): string 8416 8417Obtains the local device ID. This API is a static method. 8418 8419**System capability**: SystemCapability.Communication.IPC.Core 8420 8421**Return value** 8422 8423 | Type | Description | 8424 | ------ | ------------------ | 8425 | string | Local device ID obtained.| 8426 8427**Example** 8428 8429 ```ts 8430 import { hilog } from '@kit.PerformanceAnalysisKit'; 8431 8432 class Stub extends rpc.RemoteObject { 8433 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8434 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8435 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8436 return true; 8437 } 8438 } 8439 ``` 8440 8441### isLocalCalling 8442 8443static isLocalCalling(): boolean 8444 8445Checks whether the peer process is a process of the local device. This API is a static method. 8446 8447**System capability**: SystemCapability.Communication.IPC.Core 8448 8449**Return value** 8450 8451 | Type | Description | 8452 | ------- | -------------------------------------------------- | 8453 | boolean | Returns **true** if the local and peer processes are on the same device; returns **false** otherwise.| 8454 8455**Example** 8456 8457 ```ts 8458 import { hilog } from '@kit.PerformanceAnalysisKit'; 8459 8460 class Stub extends rpc.RemoteObject { 8461 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8462 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8463 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8464 return true; 8465 } 8466 } 8467 ``` 8468 8469### flushCmdBuffer<sup>9+</sup> 8470 8471static flushCmdBuffer(object: IRemoteObject): void 8472 8473Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8474 8475**System capability**: SystemCapability.Communication.IPC.Core 8476 8477**Parameters** 8478 8479 | Name| Type | Mandatory| Description | 8480 | ------ | ------------------------------- | ---- | ------------------- | 8481 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8482 8483**Error codes** 8484 8485For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8486 8487 | ID| Error Message| 8488 | -------- | -------- | 8489 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 8490 8491**Example** 8492 8493 ```ts 8494 import { hilog } from '@kit.PerformanceAnalysisKit'; 8495 import { BusinessError } from '@kit.BasicServicesKit'; 8496 8497 class TestRemoteObject extends rpc.RemoteObject { 8498 constructor(descriptor: string) { 8499 super(descriptor); 8500 } 8501 } 8502 let remoteObject = new TestRemoteObject("aaa"); 8503 try { 8504 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8505 } catch (error) { 8506 let e: BusinessError = error as BusinessError; 8507 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8508 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8509 } 8510 ``` 8511 8512### flushCommands<sup>(deprecated)</sup> 8513 8514static flushCommands(object: IRemoteObject): number 8515 8516Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8517 8518> **NOTE** 8519> 8520> This API is deprecated since API version 9. Use [flushCmdBuffer](#flushcmdbuffer9) instead. 8521 8522**System capability**: SystemCapability.Communication.IPC.Core 8523 8524**Parameters** 8525 8526 | Name| Type | Mandatory| Description | 8527 | ------ | ------------------------------- | ---- | ------------------- | 8528 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8529 8530**Return value** 8531 8532 | Type | Description | 8533 | ------ | --------------------------------------------------------------------------------- | 8534 | 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.| 8535 8536**Example** 8537 8538 ```ts 8539 import { hilog } from '@kit.PerformanceAnalysisKit'; 8540 8541 class MyDeathRecipient implements rpc.DeathRecipient { 8542 onRemoteDied() { 8543 hilog.info(0x0000, 'testTag', 'server died'); 8544 } 8545 } 8546 class TestRemoteObject extends rpc.RemoteObject { 8547 constructor(descriptor: string) { 8548 super(descriptor); 8549 } 8550 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8551 return true; 8552 } 8553 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8554 return true; 8555 } 8556 isObjectDead(): boolean { 8557 return false; 8558 } 8559 } 8560 let remoteObject = new TestRemoteObject("aaa"); 8561 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8562 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8563 ``` 8564 8565### resetCallingIdentity 8566 8567static resetCallingIdentity(): string 8568 8569Resets the UID and PID of the remote user to those of the local user. This API is a static method and is used in scenarios such as identity authentication. 8570 8571**System capability**: SystemCapability.Communication.IPC.Core 8572 8573**Return value** 8574 8575 | Type | Description | 8576 | ------ | ------------------------------------ | 8577 | string | String containing the UID and PID of the remote user.| 8578 8579**Example** 8580 8581 ```ts 8582 import { hilog } from '@kit.PerformanceAnalysisKit'; 8583 8584 class Stub extends rpc.RemoteObject { 8585 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8586 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8587 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8588 return true; 8589 } 8590 } 8591 ``` 8592 8593### restoreCallingIdentity<sup>9+</sup> 8594 8595static restoreCallingIdentity(identity: string): void 8596 8597Restores the UID and PID of the remote user. This API is a static method. It is usually called after **resetCallingIdentity**, and the UID and PID of the remote user returned by **resetCallingIdentity** are required. 8598 8599**System capability**: SystemCapability.Communication.IPC.Core 8600 8601**Parameters** 8602 8603 | Name | Type | Mandatory| Description | 8604 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8605 | identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8606 8607**Error codes** 8608 8609For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8610 8611 | ID| Error Message| 8612 | -------- | -------- | 8613 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 8614 8615**Example** 8616 8617 ```ts 8618 import { hilog } from '@kit.PerformanceAnalysisKit'; 8619 8620 class Stub extends rpc.RemoteObject { 8621 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8622 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8623 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8624 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8625 return true; 8626 } 8627 } 8628 ``` 8629 8630### setCallingIdentity<sup>(deprecated)</sup> 8631 8632static setCallingIdentity(identity: string): boolean 8633 8634Sets the UID and PID of the remote user. This API is a static method. It is usually called after **resetCallingIdentity**, and the UID and PID of the remote user returned by **resetCallingIdentity** are required. 8635 8636> **NOTE** 8637> 8638> This API is deprecated since API version 9. Use [restoreCallingIdentity](#restorecallingidentity9) instead. 8639 8640**System capability**: SystemCapability.Communication.IPC.Core 8641 8642**Parameters** 8643 8644 | Name | Type | Mandatory| Description | 8645 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8646 | identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8647 8648**Return value** 8649 8650 | Type | Description | 8651 | ------- | ---------------------------------| 8652 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8653 8654**Example** 8655 8656 ```ts 8657 import { hilog } from '@kit.PerformanceAnalysisKit'; 8658 8659 class Stub extends rpc.RemoteObject { 8660 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8661 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8662 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8663 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8664 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8665 return true; 8666 } 8667 } 8668 ``` 8669 8670## RemoteObject 8671 8672Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 8673 8674### constructor 8675 8676constructor(descriptor: string) 8677 8678A constructor used to create a **RemoteObject** object. 8679 8680**System capability**: SystemCapability.Communication.IPC.Core 8681 8682**Parameters** 8683 8684 | Name | Type | Mandatory| Description | 8685 | ---------- | ------ | ---- | ------------ | 8686 | descriptor | string | Yes | Interface descriptor.| 8687 8688**Example** 8689 8690 ```ts 8691 class TestRemoteObject extends rpc.RemoteObject { 8692 constructor(descriptor: string) { 8693 super(descriptor); 8694 } 8695 } 8696 ``` 8697### sendRequest<sup>(deprecated)</sup> 8698 8699sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8700 8701Sends 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. 8702 8703> **NOTE** 8704> 8705> This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8706 8707**System capability**: SystemCapability.Communication.IPC.Core 8708 8709**Parameters** 8710 8711 | Name | Type | Mandatory| Description | 8712 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8713 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8714 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8715 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8716 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8717 8718**Return value** 8719 8720 | Type | Description | 8721 | ------- | -------------------------------- | 8722 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 8723 8724**Example** 8725 8726 ```ts 8727 import { hilog } from '@kit.PerformanceAnalysisKit'; 8728 8729 class MyDeathRecipient implements rpc.DeathRecipient { 8730 onRemoteDied() { 8731 hilog.info(0x0000, 'testTag', 'server died'); 8732 } 8733 } 8734 class TestRemoteObject extends rpc.RemoteObject { 8735 constructor(descriptor: string) { 8736 super(descriptor); 8737 } 8738 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8739 return true; 8740 } 8741 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8742 return true; 8743 } 8744 isObjectDead(): boolean { 8745 return false; 8746 } 8747 } 8748 let testRemoteObject = new TestRemoteObject("testObject"); 8749 let option = new rpc.MessageOption(); 8750 let data = rpc.MessageParcel.create(); 8751 let reply = rpc.MessageParcel.create(); 8752 data.writeInt(1); 8753 data.writeString("hello"); 8754 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8755 if (ret) { 8756 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8757 let msg = reply.readString(); 8758 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8759 } else { 8760 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8761 } 8762 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8763 data.reclaim(); 8764 reply.reclaim(); 8765 ``` 8766 8767### sendMessageRequest<sup>9+</sup> 8768 8769sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8770 8771Sends 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. 8772 8773**System capability**: SystemCapability.Communication.IPC.Core 8774 8775**Parameters** 8776 8777 | Name | Type | Mandatory| Description | 8778 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8779 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8780 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8781 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8782 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8783 8784**Return value** 8785 8786| Type | Description | 8787| ----------------------------------------------- | ----------------------------------------- | 8788| Promise<[RequestResult](#requestresult9)> | Promise used to return a **RequestResult** instance.| 8789 8790 8791**Error codes** 8792 8793For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8794 8795 | ID| Error Message| 8796 | -------- | -------- | 8797 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 8798 8799**Example** 8800 8801 ```ts 8802 import { hilog } from '@kit.PerformanceAnalysisKit'; 8803 8804 class TestRemoteObject extends rpc.RemoteObject { 8805 constructor(descriptor: string) { 8806 super(descriptor); 8807 } 8808 } 8809 let testRemoteObject = new TestRemoteObject("testObject"); 8810 let option = new rpc.MessageOption(); 8811 let data = rpc.MessageSequence.create(); 8812 let reply = rpc.MessageSequence.create(); 8813 data.writeInt(1); 8814 data.writeString("hello"); 8815 testRemoteObject.sendMessageRequest(1, data, reply, option) 8816 .then((result: rpc.RequestResult) => { 8817 if (result.errCode === 0) { 8818 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8819 let num = result.reply.readInt(); 8820 let msg = result.reply.readString(); 8821 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8822 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8823 } else { 8824 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8825 } 8826 }).catch((e: Error) => { 8827 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8828 }).finally (() => { 8829 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8830 data.reclaim(); 8831 reply.reclaim(); 8832 }); 8833 ``` 8834 8835### sendRequest<sup>(deprecated)</sup> 8836 8837sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8838 8839Sends 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. 8840 8841> **NOTE** 8842> 8843> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8844 8845**System capability**: SystemCapability.Communication.IPC.Core 8846 8847**Parameters** 8848 8849 | Name | Type | Mandatory| Description | 8850 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8851 | code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8852 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8853 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8854 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8855 8856**Return value** 8857 8858| Type | Description | 8859| ------------------------------------------------------------ | --------------------------------------------- | 8860| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 8861 8862**Example** 8863 8864 ```ts 8865 import { hilog } from '@kit.PerformanceAnalysisKit'; 8866 8867 class MyDeathRecipient implements rpc.DeathRecipient { 8868 onRemoteDied() { 8869 hilog.info(0x0000, 'testTag', 'server died'); 8870 } 8871 } 8872 class TestRemoteObject extends rpc.RemoteObject { 8873 constructor(descriptor: string) { 8874 super(descriptor); 8875 } 8876 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8877 return true; 8878 } 8879 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8880 return true; 8881 } 8882 isObjectDead(): boolean { 8883 return false; 8884 } 8885 } 8886 let testRemoteObject = new TestRemoteObject("testObject"); 8887 let option = new rpc.MessageOption(); 8888 let data = rpc.MessageParcel.create(); 8889 let reply = rpc.MessageParcel.create(); 8890 data.writeInt(1); 8891 data.writeString("hello"); 8892 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8893 let b = a as Promise<rpc.SendRequestResult>; 8894 b.then((result: rpc.SendRequestResult) => { 8895 if (result.errCode === 0) { 8896 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8897 let num = result.reply.readInt(); 8898 let msg = result.reply.readString(); 8899 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8900 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8901 } else { 8902 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8903 } 8904 }).catch((e: Error) => { 8905 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8906 }).finally (() => { 8907 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8908 data.reclaim(); 8909 reply.reclaim(); 8910 }); 8911 ``` 8912 8913### sendMessageRequest<sup>9+</sup> 8914 8915sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8916 8917Sends 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. 8918 8919**System capability**: SystemCapability.Communication.IPC.Core 8920 8921**Parameters** 8922 8923| Name | Type | Mandatory| Description | 8924| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8925| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8926| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8927| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8928| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8929| callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 8930 8931 8932**Error codes** 8933 8934For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8935 8936 | ID| Error Message| 8937 | -------- | -------- | 8938 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 8939 8940### sendRequest<sup>(deprecated)</sup> 8941 8942sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8943 8944Sends 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. 8945 8946> **NOTE** 8947> 8948> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-5) instead. 8949 8950**System capability**: SystemCapability.Communication.IPC.Core 8951 8952**Parameters** 8953 8954| Name | Type | Mandatory| Description | 8955| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8956| code | number | Yes | Message code (1-16777215) called by the request, which is determined by the communication parties. If the method is generated by an IDL tool, the message code is automatically generated by the IDL tool.| 8957| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8958| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8959| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8960| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 8961 8962### onRemoteMessageRequest<sup>9+</sup> 8963 8964onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8965 8966> **NOTE** 8967> 8968>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 8969>* If both **onRemoteRequest()** and **onRemoteMessageRequest()** are overloaded, only the onRemoteMessageRequest() takes effect. 8970 8971Called to return a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 8972 8973**System capability**: SystemCapability.Communication.IPC.Core 8974 8975**Parameters** 8976 8977 | Name| Type | Mandatory| Description | 8978 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 8979 | code | number | Yes | Service request code sent by the remote end. | 8980 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 8981 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 8982 | options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8983 8984**Return value** 8985 8986 | Type | Description | 8987 | ----------------- | ----------------------------------------------------------------------------------------------- | 8988 | 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.| 8989 | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 8990 8991**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 8992 8993 ```ts 8994 import { hilog } from '@kit.PerformanceAnalysisKit'; 8995 8996 class TestRemoteObject extends rpc.RemoteObject { 8997 constructor(descriptor: string) { 8998 super(descriptor); 8999 } 9000 9001 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9002 if (code === 1) { 9003 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9004 return true; 9005 } else { 9006 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9007 return false; 9008 } 9009 } 9010 } 9011 ``` 9012 9013 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 9014 9015 ```ts 9016 import { hilog } from '@kit.PerformanceAnalysisKit'; 9017 9018 class TestRemoteObject extends rpc.RemoteObject { 9019 constructor(descriptor: string) { 9020 super(descriptor); 9021 } 9022 9023 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9024 if (code === 1) { 9025 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9026 } else { 9027 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9028 return false; 9029 } 9030 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9031 setTimeout(resolve, 100); 9032 }) 9033 return true; 9034 } 9035 } 9036 ``` 9037 9038**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 9039 9040 ```ts 9041 import { hilog } from '@kit.PerformanceAnalysisKit'; 9042 9043 class TestRemoteObject extends rpc.RemoteObject { 9044 constructor(descriptor: string) { 9045 super(descriptor); 9046 } 9047 9048 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9049 if (code === 1) { 9050 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9051 return true; 9052 } else { 9053 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9054 return false; 9055 } 9056 } 9057 // Only onRemoteMessageRequest is executed. 9058 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9059 if (code === 1) { 9060 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9061 } else { 9062 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9063 return false; 9064 } 9065 return true; 9066 } 9067 } 9068 ``` 9069 9070 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 9071 9072 ```ts 9073 import { hilog } from '@kit.PerformanceAnalysisKit'; 9074 class TestRemoteObject extends rpc.RemoteObject { 9075 constructor(descriptor: string) { 9076 super(descriptor); 9077 } 9078 9079 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9080 if (code === 1) { 9081 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9082 return true; 9083 } else { 9084 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9085 return false; 9086 } 9087 } 9088 // Only onRemoteMessageRequest is executed. 9089 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9090 if (code === 1) { 9091 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9092 } else { 9093 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9094 return false; 9095 } 9096 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9097 setTimeout(resolve, 100); 9098 }) 9099 return true; 9100 } 9101 } 9102 ``` 9103 9104### onRemoteRequest<sup>(deprecated)</sup> 9105 9106onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9107 9108Called to return a response to **sendRequest()**. The server processes the request and returns a response in this function. 9109 9110> **NOTE** 9111> 9112> This API is deprecated since API version 9. Use [onRemoteMessageRequest](#onremotemessagerequest9) instead. 9113 9114**System capability**: SystemCapability.Communication.IPC.Core 9115 9116**Parameters** 9117 9118 | Name| Type | Mandatory| Description | 9119 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9120 | code | number | Yes | Service request code sent by the remote end. | 9121 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 9122 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 9123 | options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 9124 9125**Return value** 9126 9127 | Type | Description | 9128 | ------- | -------------------------------- | 9129 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 9130 9131**Example** 9132 9133 ```ts 9134 import { hilog } from '@kit.PerformanceAnalysisKit'; 9135 9136 class MyDeathRecipient implements rpc.DeathRecipient { 9137 onRemoteDied() { 9138 hilog.info(0x0000, 'testTag', 'server died'); 9139 } 9140 } 9141 class TestRemoteObject extends rpc.RemoteObject { 9142 constructor(descriptor: string) { 9143 super(descriptor); 9144 } 9145 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9146 return true; 9147 } 9148 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9149 return true; 9150 } 9151 isObjectDead(): boolean { 9152 return false; 9153 } 9154 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9155 if (code === 1) { 9156 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9157 return true; 9158 } else { 9159 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9160 return false; 9161 } 9162 } 9163 } 9164 ``` 9165 9166### getCallingUid 9167 9168getCallingUid(): number 9169 9170Obtains the UID of the remote process. 9171 9172**System capability**: SystemCapability.Communication.IPC.Core 9173 9174**Return value** 9175 | Type | Description | 9176 | ------ | ----------------------- | 9177 | number | UID of the remote process obtained.| 9178 9179**Example** 9180 9181 ```ts 9182 import { hilog } from '@kit.PerformanceAnalysisKit'; 9183 9184 class TestRemoteObject extends rpc.RemoteObject { 9185 constructor(descriptor: string) { 9186 super(descriptor); 9187 } 9188 } 9189 let testRemoteObject = new TestRemoteObject("testObject"); 9190 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9191 ``` 9192 9193### getCallingPid 9194 9195getCallingPid(): number 9196 9197Obtains the PID of the remote process. 9198 9199**System capability**: SystemCapability.Communication.IPC.Core 9200 9201**Return value** 9202 9203 | Type | Description | 9204 | ------ | ----------------------- | 9205 | number | PID of the remote process obtained.| 9206 9207**Example** 9208 9209 ```ts 9210 import { hilog } from '@kit.PerformanceAnalysisKit'; 9211 9212 class TestRemoteObject extends rpc.RemoteObject { 9213 constructor(descriptor: string) { 9214 super(descriptor); 9215 } 9216 } 9217 let testRemoteObject = new TestRemoteObject("testObject"); 9218 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9219 ``` 9220 9221### getLocalInterface<sup>9+</sup> 9222 9223getLocalInterface(descriptor: string): IRemoteBroker 9224 9225Obtains the string of the interface descriptor. 9226 9227**System capability**: SystemCapability.Communication.IPC.Core 9228 9229**Parameters** 9230 9231 | Name | Type | Mandatory| Description | 9232 | ---------- | ------ | ---- | -------------------- | 9233 | descriptor | string | Yes | Interface descriptor.| 9234 9235**Return value** 9236 9237 | Type | Description | 9238 | ------------- | --------------------------------------------- | 9239 | [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 9240 9241**Error codes** 9242 9243For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9244 9245 | ID| Error Message| 9246 | -------- | -------- | 9247 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9248 9249**Example** 9250 9251 ```ts 9252 import { hilog } from '@kit.PerformanceAnalysisKit'; 9253 import { BusinessError } from '@kit.BasicServicesKit'; 9254 9255 class MyDeathRecipient implements rpc.DeathRecipient { 9256 onRemoteDied() { 9257 hilog.info(0x0000, 'testTag', 'server died'); 9258 } 9259 } 9260 class TestRemoteObject extends rpc.RemoteObject { 9261 constructor(descriptor: string) { 9262 super(descriptor); 9263 this.modifyLocalInterface(this, descriptor); 9264 } 9265 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9266 // Implement the method logic based on service requirements. 9267 } 9268 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9269 // Implement the method logic based on service requirements. 9270 } 9271 isObjectDead(): boolean { 9272 return false; 9273 } 9274 asObject(): rpc.IRemoteObject { 9275 return this; 9276 } 9277 } 9278 let testRemoteObject = new TestRemoteObject("testObject"); 9279 try { 9280 testRemoteObject.getLocalInterface("testObject"); 9281 } catch (error) { 9282 let e: BusinessError = error as BusinessError; 9283 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9284 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9285 } 9286 ``` 9287 9288### queryLocalInterface<sup>(deprecated)</sup> 9289 9290queryLocalInterface(descriptor: string): IRemoteBroker 9291 9292Checks whether the remote object corresponding to the specified interface token exists. 9293 9294> **NOTE** 9295> 9296> This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-2) instead. 9297 9298**System capability**: SystemCapability.Communication.IPC.Core 9299 9300**Parameters** 9301 9302 | Name | Type | Mandatory| Description | 9303 | ---------- | ------ | ---- | ---------------------- | 9304 | descriptor | string | Yes | Interface descriptor.| 9305 9306**Return value** 9307 9308 | Type | Description | 9309 | ------------- | ------------------------------------------------------------------ | 9310 | [IRemoteBroker](#iremotebroker) | Returns the remote object if a match is found; returns **Null** otherwise.| 9311 9312**Example** 9313 9314 ```ts 9315 import { hilog } from '@kit.PerformanceAnalysisKit'; 9316 9317 class MyDeathRecipient implements rpc.DeathRecipient { 9318 onRemoteDied() { 9319 hilog.info(0x0000, 'testTag', 'server died'); 9320 } 9321 } 9322 class TestRemoteObject extends rpc.RemoteObject { 9323 constructor(descriptor: string) { 9324 super(descriptor); 9325 this.attachLocalInterface(this, descriptor); 9326 } 9327 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9328 return true; 9329 } 9330 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9331 return true; 9332 } 9333 isObjectDead(): boolean { 9334 return false; 9335 } 9336 asObject(): rpc.IRemoteObject { 9337 return this; 9338 } 9339 } 9340 let testRemoteObject = new TestRemoteObject("testObject"); 9341 testRemoteObject.queryLocalInterface("testObject"); 9342 ``` 9343 9344### getDescriptor<sup>9+</sup> 9345 9346getDescriptor(): string 9347 9348Obtains the interface descriptor of this object. The interface descriptor is a string. 9349 9350**System capability**: SystemCapability.Communication.IPC.Core 9351 9352**Return value** 9353 9354 | Type | Description | 9355 | ------ | ---------------- | 9356 | string | Interface descriptor obtained.| 9357 9358**Error codes** 9359 9360For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9361 9362 | ID| Error Message| 9363 | -------- | -------- | 9364 | 1900008 | The proxy or remote object is invalid. | 9365 9366**Example** 9367 9368 ```ts 9369 import { hilog } from '@kit.PerformanceAnalysisKit'; 9370 import { BusinessError } from '@kit.BasicServicesKit'; 9371 9372 class MyDeathRecipient implements rpc.DeathRecipient { 9373 onRemoteDied() { 9374 hilog.info(0x0000, 'testTag', 'server died'); 9375 } 9376 } 9377 class TestRemoteObject extends rpc.RemoteObject { 9378 constructor(descriptor: string) { 9379 super(descriptor); 9380 } 9381 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9382 // Implement the method logic based on service requirements. 9383 } 9384 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9385 // Implement the method logic based on service requirements. 9386 } 9387 isObjectDead(): boolean { 9388 return false; 9389 } 9390 } 9391 let testRemoteObject = new TestRemoteObject("testObject"); 9392 try { 9393 let descriptor = testRemoteObject.getDescriptor(); 9394 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9395 } catch (error) { 9396 let e: BusinessError = error as BusinessError; 9397 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9398 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9399 } 9400 ``` 9401 9402### getInterfaceDescriptor<sup>(deprecated)</sup> 9403 9404getInterfaceDescriptor(): string 9405 9406Obtains the interface descriptor. 9407 9408> **NOTE** 9409> 9410> This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-2) instead. 9411 9412**System capability**: SystemCapability.Communication.IPC.Core 9413 9414**Return value** 9415 9416 | Type | Description | 9417 | ------ | ---------------- | 9418 | string | Interface descriptor obtained.| 9419 9420**Example** 9421 9422 ```ts 9423 import { hilog } from '@kit.PerformanceAnalysisKit'; 9424 9425 class MyDeathRecipient implements rpc.DeathRecipient { 9426 onRemoteDied() { 9427 hilog.info(0x0000, 'testTag', 'server died'); 9428 } 9429 } 9430 class TestRemoteObject extends rpc.RemoteObject { 9431 constructor(descriptor: string) { 9432 super(descriptor); 9433 } 9434 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9435 return true; 9436 } 9437 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9438 return true; 9439 } 9440 isObjectDead(): boolean { 9441 return false; 9442 } 9443 } 9444 let testRemoteObject = new TestRemoteObject("testObject"); 9445 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9446 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9447 ``` 9448 9449### modifyLocalInterface<sup>9+</sup> 9450 9451modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9452 9453Binds an interface descriptor to an **IRemoteBroker** object. 9454 9455**System capability**: SystemCapability.Communication.IPC.Core 9456 9457**Parameters** 9458 9459| Name | Type | Mandatory| Description | 9460| -------------- | ------------------------------- | ---- | ------------------------------------- | 9461| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9462| descriptor | string | Yes | Interface descriptor.| 9463 9464**Error codes** 9465 9466For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9467 9468 | ID| Error Message| 9469 | -------- | -------- | 9470 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9471 9472**Example** 9473 9474 ```ts 9475 import { hilog } from '@kit.PerformanceAnalysisKit'; 9476 import { BusinessError } from '@kit.BasicServicesKit'; 9477 9478 class MyDeathRecipient implements rpc.DeathRecipient { 9479 onRemoteDied() { 9480 hilog.info(0x0000, 'testTag', 'server died'); 9481 } 9482 } 9483 class TestRemoteObject extends rpc.RemoteObject { 9484 constructor(descriptor: string) { 9485 super(descriptor); 9486 try { 9487 this.modifyLocalInterface(this, descriptor); 9488 } catch (error) { 9489 let e: BusinessError = error as BusinessError; 9490 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9491 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9492 } 9493 } 9494 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9495 // Implement the method logic based on service requirements. 9496 } 9497 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9498 // Implement the method logic based on service requirements. 9499 } 9500 isObjectDead(): boolean { 9501 return false; 9502 } 9503 asObject(): rpc.IRemoteObject { 9504 return this; 9505 } 9506 } 9507 let testRemoteObject = new TestRemoteObject("testObject"); 9508 ``` 9509 9510### attachLocalInterface<sup>(deprecated)</sup> 9511 9512attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9513 9514Binds an interface descriptor to an **IRemoteBroker** object. 9515 9516> **NOTE** 9517> 9518> This API is deprecated since API version 9. Use [modifyLocalInterface](#modifylocalinterface9) instead. 9519 9520**System capability**: SystemCapability.Communication.IPC.Core 9521 9522**Parameters** 9523 9524| Name | Type | Mandatory| Description | 9525| -------------- | ------------------------------- | ---- | ------------------------------------- | 9526| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9527| descriptor | string | Yes | Interface descriptor.| 9528 9529**Example** 9530 9531 ```ts 9532 import { hilog } from '@kit.PerformanceAnalysisKit'; 9533 9534 class MyDeathRecipient implements rpc.DeathRecipient { 9535 onRemoteDied() { 9536 hilog.info(0x0000, 'testTag', 'server died'); 9537 } 9538 } 9539 class TestRemoteObject extends rpc.RemoteObject { 9540 constructor(descriptor: string) { 9541 super(descriptor); 9542 this.attachLocalInterface(this, descriptor); 9543 } 9544 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9545 return true; 9546 } 9547 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9548 return true; 9549 } 9550 isObjectDead(): boolean { 9551 return false; 9552 } 9553 asObject(): rpc.IRemoteObject { 9554 return this; 9555 } 9556 } 9557 let testRemoteObject = new TestRemoteObject("testObject"); 9558 ``` 9559 9560## Ashmem<sup>8+</sup> 9561 9562Provides 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. 9563The shared memory applies only to cross-process communication within the local device. 9564 9565### Properties 9566 9567**System capability**: SystemCapability.Communication.IPC.Core 9568 9569 | Name | Type | Readable | Writable | Description | 9570 | ---------- | ------ | ----- | ----- |----------------------------------------- | 9571 | PROT_EXEC | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is executable. | 9572 | PROT_NONE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory cannot be accessed.| 9573 | PROT_READ | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. | 9574 | PROT_WRITE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. | 9575 9576### create<sup>9+</sup> 9577 9578static create(name: string, size: number): Ashmem 9579 9580Creates an **Ashmem** object with the specified name and size. This API is a static method. 9581 9582**System capability**: SystemCapability.Communication.IPC.Core 9583 9584**Parameters** 9585 9586 | Name| Type | Mandatory| Description | 9587 | ------ | ------ | ---- | ---------------------------- | 9588 | name | string | Yes | Name of the **Ashmem** object to create. | 9589 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9590 9591**Return value** 9592 9593| Type | Description | 9594| ------------------ | ---------------------------------------------- | 9595| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9596 9597**Error codes** 9598 9599For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9600 9601 | ID| Error Message| 9602 | -------- | -------- | 9603 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The Ashmem name passed is empty; <br> 4.The Ashmem size passed is less than or equal to 0. | 9604 9605**Example** 9606 9607 ```ts 9608 import { hilog } from '@kit.PerformanceAnalysisKit'; 9609 import { BusinessError } from '@kit.BasicServicesKit'; 9610 9611 let ashmem: rpc.Ashmem | undefined = undefined; 9612 try { 9613 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9614 let size = ashmem.getAshmemSize(); 9615 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9616 } catch (error) { 9617 let e: BusinessError = error as BusinessError; 9618 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9619 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9620 } 9621 ``` 9622 9623### createAshmem<sup>(deprecated)</sup> 9624 9625static createAshmem(name: string, size: number): Ashmem 9626 9627Creates an **Ashmem** object with the specified name and size. This API is a static method. 9628 9629> **NOTE** 9630> 9631> This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9) instead. 9632 9633**System capability**: SystemCapability.Communication.IPC.Core 9634 9635**Parameters** 9636 9637 | Name| Type | Mandatory| Description | 9638 | ------ | ------ | ---- | ---------------------------- | 9639 | name | string | Yes | Name of the **Ashmem** object to create. | 9640 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9641 9642**Return value** 9643 9644| Type | Description | 9645| ------------------ | ---------------------------------------------- | 9646| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9647 9648**Example** 9649 9650 ```ts 9651 import { hilog } from '@kit.PerformanceAnalysisKit'; 9652 9653 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9654 let size = ashmem.getAshmemSize(); 9655 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9656 ``` 9657 9658### create<sup>9+</sup> 9659 9660static create(ashmem: Ashmem): Ashmem 9661 9662Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9663 9664**System capability**: SystemCapability.Communication.IPC.Core 9665 9666**Parameters** 9667 9668| Name| Type | Mandatory| Description | 9669| ------ | ------------------ | ---- | -------------------- | 9670| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9671 9672**Return value** 9673 9674| Type | Description | 9675| ------------------ | ---------------------- | 9676| [Ashmem](#ashmem8) | **Ashmem** object created.| 9677 9678**Error codes** 9679 9680For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9681 9682 | ID| Error Message| 9683 | -------- | -------- | 9684 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The passed parameter is not an Ahmem object; <br> 3.The ashmem instance for obtaining packaging is empty. | 9685 9686**Example** 9687 9688 ```ts 9689 import { hilog } from '@kit.PerformanceAnalysisKit'; 9690 import { BusinessError } from '@kit.BasicServicesKit'; 9691 9692 try { 9693 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9694 let ashmem2 = rpc.Ashmem.create(ashmem); 9695 let size = ashmem2.getAshmemSize(); 9696 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9697 } catch (error) { 9698 let e: BusinessError = error as BusinessError; 9699 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9700 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9701 } 9702 ``` 9703 9704### createAshmemFromExisting<sup>(deprecated)</sup> 9705 9706static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9707 9708Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9709 9710> **NOTE** 9711> 9712> This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9-1) instead. 9713 9714**System capability**: SystemCapability.Communication.IPC.Core 9715 9716**Parameters** 9717 9718| Name| Type | Mandatory| Description | 9719| ------ | ------------------ | ---- | -------------------- | 9720| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9721 9722**Return value** 9723 9724| Type | Description | 9725| ------------------ | ---------------------- | 9726| [Ashmem](#ashmem8) | **Ashmem** object created.| 9727 9728**Example** 9729 9730 ```ts 9731 import { hilog } from '@kit.PerformanceAnalysisKit'; 9732 9733 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9734 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9735 let size = ashmem2.getAshmemSize(); 9736 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9737 ``` 9738 9739### closeAshmem<sup>8+</sup> 9740 9741closeAshmem(): void 9742 9743Closes this **Ashmem** object. 9744 9745> **NOTE** 9746> 9747> Before closing the **Ashmem** object, you need to remove the address mapping. 9748 9749**System capability**: SystemCapability.Communication.IPC.Core 9750 9751**Example** 9752 9753 ```ts 9754 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9755 ashmem.closeAshmem(); 9756 ``` 9757 9758### unmapAshmem<sup>8+</sup> 9759 9760unmapAshmem(): void 9761 9762Deletes the mappings for the specified address range of this **Ashmem** object. 9763 9764**System capability**: SystemCapability.Communication.IPC.Core 9765 9766**Example** 9767 9768 ```ts 9769 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9770 ashmem.unmapAshmem(); 9771 ``` 9772 9773### getAshmemSize<sup>8+</sup> 9774 9775getAshmemSize(): number 9776 9777Obtains the memory size of this **Ashmem** object. 9778 9779**System capability**: SystemCapability.Communication.IPC.Core 9780 9781**Return value** 9782 9783 | Type | Description | 9784 | ------ | -------------------------- | 9785 | number | **Ashmem** size obtained.| 9786 9787**Example** 9788 9789 ```ts 9790 import { hilog } from '@kit.PerformanceAnalysisKit'; 9791 9792 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9793 let size = ashmem.getAshmemSize(); 9794 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9795 ``` 9796 9797### mapTypedAshmem<sup>9+</sup> 9798 9799mapTypedAshmem(mapType: number): void 9800 9801Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9802 9803**System capability**: SystemCapability.Communication.IPC.Core 9804 9805**Parameters** 9806 9807 | Name | Type | Mandatory| Description | 9808 | ------- | ------ | ---- | ------------------------------ | 9809 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9810 9811**Error codes** 9812 9813For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9814 9815 | ID| Error Message| 9816 | -------- | -------- | 9817 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The passed mapType exceeds the maximum protection level. | 9818 | 1900001 | Failed to call mmap. | 9819 9820**Example** 9821 9822 ```ts 9823 import { hilog } from '@kit.PerformanceAnalysisKit'; 9824 import { BusinessError } from '@kit.BasicServicesKit'; 9825 9826 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9827 try { 9828 ashmem.mapTypedAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 9829 } catch (error) { 9830 let e: BusinessError = error as BusinessError; 9831 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9832 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9833 } 9834 ``` 9835 9836### mapAshmem<sup>(deprecated)</sup> 9837 9838mapAshmem(mapType: number): boolean 9839 9840Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9841 9842> **NOTE** 9843> 9844> This API is supported since API version 8 and deprecated since API version 9. Use [mapTypedAshmem](#maptypedashmem9) instead. 9845 9846**System capability**: SystemCapability.Communication.IPC.Core 9847 9848**Parameters** 9849 9850 | Name | Type | Mandatory| Description | 9851 | ------- | ------ | ---- | ------------------------------ | 9852 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9853 9854**Return value** 9855 9856 | Type | Description | 9857 | ------- | -------------------------------- | 9858 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9859 9860**Example** 9861 9862 ```ts 9863 import { hilog } from '@kit.PerformanceAnalysisKit'; 9864 9865 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9866 let mapReadAndWrite = ashmem.mapAshmem(rpc.Ashmem.PROT_READ | rpc.Ashmem.PROT_WRITE); 9867 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9868 ``` 9869 9870### mapReadWriteAshmem<sup>9+</sup> 9871 9872mapReadWriteAshmem(): void 9873 9874Maps the shared file to the readable and writable virtual address space of the process. 9875 9876**System capability**: SystemCapability.Communication.IPC.Core 9877 9878**Error codes** 9879 9880For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9881 9882 | ID| Error Message| 9883 | -------- | -------- | 9884 | 1900001 | Failed to call mmap. | 9885 9886**Example** 9887 9888 ```ts 9889 import { hilog } from '@kit.PerformanceAnalysisKit'; 9890 import { BusinessError } from '@kit.BasicServicesKit'; 9891 9892 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9893 try { 9894 ashmem.mapReadWriteAshmem(); 9895 } catch (error) { 9896 let e: BusinessError = error as BusinessError; 9897 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9898 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9899 } 9900 ``` 9901 9902### mapReadAndWriteAshmem<sup>(deprecated)</sup> 9903 9904mapReadAndWriteAshmem(): boolean 9905 9906Maps the shared file to the readable and writable virtual address space of the process. 9907 9908> **NOTE** 9909> 9910> This API is supported since API version 8 and deprecated since API version 9. Use [mapReadWriteAshmem](#mapreadwriteashmem9) instead. 9911 9912**System capability**: SystemCapability.Communication.IPC.Core 9913 9914**Return value** 9915 9916 | Type | Description | 9917 | ------- | -------------------------------- | 9918 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9919 9920**Example** 9921 9922 ```ts 9923 import { hilog } from '@kit.PerformanceAnalysisKit'; 9924 9925 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9926 let mapResult = ashmem.mapReadAndWriteAshmem(); 9927 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9928 ``` 9929 9930### mapReadonlyAshmem<sup>9+</sup> 9931 9932mapReadonlyAshmem(): void 9933 9934Maps the shared file to the read-only virtual address space of the process. 9935 9936**System capability**: SystemCapability.Communication.IPC.Core 9937 9938**Error codes** 9939 9940For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9941 9942 | ID| Error Message| 9943 | -------- | -------- | 9944 | 1900001 | Failed to call mmap. | 9945 9946**Example** 9947 9948 ```ts 9949 import { hilog } from '@kit.PerformanceAnalysisKit'; 9950 import { BusinessError } from '@kit.BasicServicesKit'; 9951 9952 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9953 try { 9954 ashmem.mapReadonlyAshmem(); 9955 } catch (error) { 9956 let e: BusinessError = error as BusinessError; 9957 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9958 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9959 } 9960 ``` 9961 9962### mapReadOnlyAshmem<sup>(deprecated)</sup> 9963 9964mapReadOnlyAshmem(): boolean 9965 9966Maps the shared file to the read-only virtual address space of the process. 9967 9968> **NOTE** 9969> 9970> This API is supported since API version 8 and deprecated since API version 9. Use [mapReadonlyAshmem](#mapreadonlyashmem9) instead. 9971 9972**System capability**: SystemCapability.Communication.IPC.Core 9973 9974**Return value** 9975 9976 | Type | Description | 9977 | ------- | -------------------------------- | 9978 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9979 9980**Example** 9981 9982 ```ts 9983 import { hilog } from '@kit.PerformanceAnalysisKit'; 9984 9985 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9986 let mapResult = ashmem.mapReadOnlyAshmem(); 9987 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 9988 ``` 9989 9990### setProtectionType<sup>9+</sup> 9991 9992setProtectionType(protectionType: number): void 9993 9994Sets the protection level of the memory region to which the shared file is mapped. 9995 9996**System capability**: SystemCapability.Communication.IPC.Core 9997 9998**Parameters** 9999 10000 | Name | Type | Mandatory| Description | 10001 | -------------- | ------ | ---- | ------------------ | 10002 | protectionType | number | Yes | Protection type to set.| 10003 10004**Error codes** 10005 10006For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10007 10008 | ID| Error Message| 10009 | -------- | -------- | 10010 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10011 | 1900002 | Failed to call ioctl. | 10012 10013**Example** 10014 10015 ```ts 10016 import { hilog } from '@kit.PerformanceAnalysisKit'; 10017 import { BusinessError } from '@kit.BasicServicesKit'; 10018 10019 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10020 try { 10021 ashmem.setProtectionType(rpc.Ashmem.PROT_READ); 10022 } catch (error) { 10023 let e: BusinessError = error as BusinessError; 10024 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10025 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10026 } 10027 ``` 10028 10029### setProtection<sup>(deprecated)</sup> 10030 10031setProtection(protectionType: number): boolean 10032 10033Sets the protection level of the memory region to which the shared file is mapped. 10034 10035> **NOTE** 10036> 10037> This API is supported since API version 8 and deprecated since API version 9. Use [setProtectionType](#setprotectiontype9) instead. 10038 10039**System capability**: SystemCapability.Communication.IPC.Core 10040 10041**Parameters** 10042 10043 | Name | Type | Mandatory| Description | 10044 | -------------- | ------ | ---- | ------------------ | 10045 | protectionType | number | Yes | Protection type to set.| 10046 10047**Return value** 10048 10049 | Type | Description | 10050 | ------- | -------------------------------- | 10051 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 10052 10053**Example** 10054 10055 ```ts 10056 import { hilog } from '@kit.PerformanceAnalysisKit'; 10057 10058 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10059 let result = ashmem.setProtection(rpc.Ashmem.PROT_READ); 10060 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10061 ``` 10062 10063### writeDataToAshmem<sup>11+</sup> 10064 10065writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10066 10067Writes data to the shared file associated with this **Ashmem** object. 10068 10069> **NOTE** 10070> 10071> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10072 10073**System capability**: SystemCapability.Communication.IPC.Core 10074 10075**Parameters** 10076 10077 | Name| Type | Mandatory| Description | 10078 | ------ | -------- | ---- | -------------------------------------------------- | 10079 | buf | ArrayBuffer | Yes | Data to write. | 10080 | size | number | Yes | Size of the data to write. | 10081 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10082 10083**Error codes** 10084 10085For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10086 10087 | ID| Error Message| 10088 | -------- | -------- | 10089 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain arrayBuffer information. | 10090 | 1900003 | Failed to write data to the shared memory. | 10091 10092**Example** 10093 10094 ```ts 10095 import { hilog } from '@kit.PerformanceAnalysisKit'; 10096 import { BusinessError } from '@kit.BasicServicesKit'; 10097 10098 let buffer = new ArrayBuffer(1024); 10099 let int32View = new Int32Array(buffer); 10100 for (let i = 0; i < int32View.length; i++) { 10101 int32View[i] = i * 2 + 1; 10102 } 10103 let size = buffer.byteLength; 10104 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10105 ashmem.mapReadWriteAshmem(); 10106 try { 10107 ashmem.writeDataToAshmem(buffer, size, 0); 10108 } catch (error) { 10109 let e: BusinessError = error as BusinessError; 10110 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10111 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10112 } 10113 ``` 10114 10115### writeAshmem<sup>(deprecated)</sup> 10116 10117writeAshmem(buf: number[], size: number, offset: number): void 10118 10119Writes data to the shared file associated with this **Ashmem** object. 10120 10121> **NOTE** 10122> 10123> This API is supported since API version 9 and deprecated since API version 11. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10124> 10125> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10126 10127**System capability**: SystemCapability.Communication.IPC.Core 10128 10129**Parameters** 10130 10131 | Name| Type | Mandatory| Description | 10132 | ------ | -------- | ---- | -------------------------------------------------- | 10133 | buf | number[] | Yes | Data to write. | 10134 | size | number | Yes | Size of the data to write. | 10135 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10136 10137**Error codes** 10138 10139For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10140 10141 | ID| Error Message| 10142 | -------- | -------- | 10143 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The element does not exist in the array. | 10144 | 1900003 | Failed to write data to the shared memory. | 10145 10146**Example** 10147 10148 ```ts 10149 import { hilog } from '@kit.PerformanceAnalysisKit'; 10150 import { BusinessError } from '@kit.BasicServicesKit'; 10151 10152 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10153 ashmem.mapReadWriteAshmem(); 10154 let ByteArrayVar = [1, 2, 3, 4, 5]; 10155 try { 10156 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10157 } catch (error) { 10158 let e: BusinessError = error as BusinessError; 10159 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10160 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10161 } 10162 ``` 10163 10164### writeToAshmem<sup>(deprecated)</sup> 10165 10166writeToAshmem(buf: number[], size: number, offset: number): boolean 10167 10168Writes data to the shared file associated with this **Ashmem** object. 10169 10170> **NOTE** 10171> 10172> This API is supported since API version 8 and deprecated since API version 9. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10173> 10174> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10175 10176**System capability**: SystemCapability.Communication.IPC.Core 10177 10178**Parameters** 10179 10180 | Name| Type | Mandatory| Description | 10181 | ------ | -------- | ---- | -------------------------------------------------- | 10182 | buf | number[] | Yes | Data to write. | 10183 | size | number | Yes | Size of the data to write. | 10184 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10185 10186**Return value** 10187 10188 | Type | Description | 10189 | ------- | ----------------------------------------------------------------------------- | 10190 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 10191 10192**Example** 10193 10194 ```ts 10195 import { hilog } from '@kit.PerformanceAnalysisKit'; 10196 10197 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10198 let mapResult = ashmem.mapReadAndWriteAshmem(); 10199 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10200 let ByteArrayVar = [1, 2, 3, 4, 5]; 10201 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10202 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10203 ``` 10204 10205### readDataFromAshmem<sup>11+</sup> 10206 10207readDataFromAshmem(size: number, offset: number): ArrayBuffer 10208 10209Reads data from the shared file associated with this **Ashmem** object. 10210 10211> **NOTE** 10212> 10213> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10214 10215**System capability**: SystemCapability.Communication.IPC.Core 10216 10217**Parameters** 10218 10219 | Name| Type | Mandatory| Description | 10220 | ------ | ------ | ---- | -------------------------------------------------- | 10221 | size | number | Yes | Size of the data to read. | 10222 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10223 10224**Return value** 10225 10226 | Type | Description | 10227 | -------- | ---------------- | 10228 | ArrayBuffer | Data read.| 10229 10230**Error codes** 10231 10232For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10233 10234 | ID| Error Message| 10235 | -------- | -------- | 10236 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10237 | 1900004 | Failed to read data from the shared memory. | 10238 10239**Example** 10240 10241 ```ts 10242 import { hilog } from '@kit.PerformanceAnalysisKit'; 10243 import { BusinessError } from '@kit.BasicServicesKit'; 10244 10245 let buffer = new ArrayBuffer(1024); 10246 let int32View = new Int32Array(buffer); 10247 for (let i = 0; i < int32View.length; i++) { 10248 int32View[i] = i * 2 + 1; 10249 } 10250 let size = buffer.byteLength; 10251 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10252 ashmem.mapReadWriteAshmem(); 10253 try { 10254 ashmem.writeDataToAshmem(buffer, size, 0); 10255 } catch (error) { 10256 let e: BusinessError = error as BusinessError; 10257 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10258 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10259 } 10260 try { 10261 let readResult = ashmem.readDataFromAshmem(size, 0); 10262 let readInt32View = new Int32Array(readResult); 10263 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10264 } catch (error) { 10265 let e: BusinessError = error as BusinessError; 10266 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10267 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10268 } 10269 ``` 10270 10271### readAshmem<sup>(deprecated)</sup> 10272 10273readAshmem(size: number, offset: number): number[] 10274 10275Reads data from the shared file associated with this **Ashmem** object. 10276 10277> **NOTE** 10278> 10279> This API is supported since API version 9 and deprecated since API version 11. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10280> 10281> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10282 10283 10284**System capability**: SystemCapability.Communication.IPC.Core 10285 10286**Parameters** 10287 10288 | Name| Type | Mandatory| Description | 10289 | ------ | ------ | ---- | -------------------------------------------------- | 10290 | size | number | Yes | Size of the data to read. | 10291 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10292 10293**Return value** 10294 10295 | Type | Description | 10296 | -------- | ---------------- | 10297 | number[] | Data read.| 10298 10299**Error codes** 10300 10301For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10302 10303 | ID| Error Message| 10304 | -------- | -------- | 10305 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10306 | 1900004 | Failed to read data from the shared memory. | 10307 10308**Example** 10309 10310 ```ts 10311 import { hilog } from '@kit.PerformanceAnalysisKit'; 10312 import { BusinessError } from '@kit.BasicServicesKit'; 10313 10314 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10315 ashmem.mapReadWriteAshmem(); 10316 let ByteArrayVar = [1, 2, 3, 4, 5]; 10317 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10318 try { 10319 let readResult = ashmem.readAshmem(5, 0); 10320 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10321 } catch (error) { 10322 let e: BusinessError = error as BusinessError; 10323 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10324 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10325 } 10326 ``` 10327 10328### readFromAshmem<sup>(deprecated)</sup> 10329 10330readFromAshmem(size: number, offset: number): number[] 10331 10332Reads data from the shared file associated with this **Ashmem** object. 10333 10334> **NOTE** 10335> 10336> This API is supported since API version 8 and deprecated since API version 9. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10337> 10338> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10339 10340**System capability**: SystemCapability.Communication.IPC.Core 10341 10342**Parameters** 10343 10344 | Name| Type | Mandatory| Description | 10345 | ------ | ------ | ---- | -------------------------------------------------- | 10346 | size | number | Yes | Size of the data to read. | 10347 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10348 10349**Return value** 10350 10351 | Type | Description | 10352 | -------- | ---------------- | 10353 | number[] | Data read.| 10354 10355**Example** 10356 10357 ``` ts 10358 import { hilog } from '@kit.PerformanceAnalysisKit'; 10359 10360 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10361 let mapResult = ashmem.mapReadAndWriteAshmem(); 10362 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10363 let ByteArrayVar = [1, 2, 3, 4, 5]; 10364 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10365 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10366 let readResult = ashmem.readFromAshmem(5, 0); 10367 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10368 ``` 10369