1# @ohos.rpc (RPC) 2 3The **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. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> - This module supports return of error codes since API version 9. 10 11## Modules to Import 12 13``` 14import rpc from '@ohos.rpc'; 15``` 16 17## ErrorCode<sup>9+</sup> 18 19The APIs of this module return exceptions since API version 9. The following table lists the error codes. 20 21**System capability**: SystemCapability.Communication.IPC.Core 22 23| Name | Value | Description | 24| ------------------------------------- | ------- | --------------------------------------------- | 25| CHECK_PARAM_ERROR | 401 | Parameter check failed. | 26| OS_MMAP_ERROR | 1900001 | Failed to call mmap. | 27| OS_IOCTL_ERROR | 1900002 | Failed to call **ioctl** with the shared memory file descriptor.| 28| WRITE_TO_ASHMEM_ERROR | 1900003 | Failed to write data to the shared memory. | 29| READ_FROM_ASHMEM_ERROR | 1900004 | Failed to read data from the shared memory. | 30| ONLY_PROXY_OBJECT_PERMITTED_ERROR | 1900005 | This operation is allowed only on the proxy object. | 31| ONLY_REMOTE_OBJECT_PERMITTED_ERROR | 1900006 | This operation is allowed only on the remote object. | 32| COMMUNICATION_ERROR | 1900007 | Failed to communicate with the remote object over IPC. | 33| PROXY_OR_REMOTE_OBJECT_INVALID_ERROR | 1900008 | Invalid proxy or remote object. | 34| WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR | 1900009 | Failed to write data to MessageSequence. | 35| READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR | 1900010 | Failed to read data from MessageSequence. | 36| PARCEL_MEMORY_ALLOC_ERROR | 1900011 | Failed to allocate memory during serialization. | 37| CALL_JS_METHOD_ERROR | 1900012 | Failed to invoke the JS callback. | 38| OS_DUP_ERROR | 1900013 | Failed to call dup. | 39 40 41## MessageSequence<sup>9+</sup> 42 43Provides APIs for reading and writing data in specific format. 44During 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. 45 46### create 47 48 static create(): MessageSequence 49 50 Creates a **MessageSequence** object. This API is a static method. 51 52**System capability**: SystemCapability.Communication.IPC.Core 53 54**Return value** 55 56| Type | Description | 57| --------------- | ------------------------------- | 58| MessageSequence | **MessageSequence** object created.| 59 60**Example** 61 62 ```ts 63 let data = rpc.MessageSequence.create(); 64 console.log("RpcClient: data is " + data); 65 ``` 66 67### reclaim 68 69reclaim(): void 70 71Reclaims the **MessageSequence** object that is no longer used. 72 73**System capability**: SystemCapability.Communication.IPC.Core 74 75**Example** 76 77 ```ts 78 let reply = rpc.MessageSequence.create(); 79 reply.reclaim(); 80 ``` 81 82### writeRemoteObject 83 84writeRemoteObject(object: [IRemoteObject](#iremoteobject)): void 85 86Serializes a remote object and writes it to this **MessageSequence** object. 87 88**System capability**: SystemCapability.Communication.IPC.Core 89 90**Parameters** 91 92| Name| Type | Mandatory| Description | 93| ------ | ------------------------------- | ---- | ----------------------------------------- | 94| object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 95 96**Error codes** 97 98For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 99 100| ID| Error Message| 101| -------- | -------- | 102| 1900008 | proxy or remote object is invalid | 103| 1900009 | write data to message sequence failed | 104 105**Example** 106 107 ```ts 108 import { BusinessError } from '@ohos.base'; 109 110 class TestRemoteObject extends rpc.RemoteObject { 111 constructor(descriptor: string) { 112 super(descriptor); 113 } 114 } 115 let data = rpc.MessageSequence.create(); 116 let testRemoteObject = new TestRemoteObject("testObject"); 117 try { 118 data.writeRemoteObject(testRemoteObject); 119 } catch(error) { 120 let e: BusinessError = error as BusinessError; 121 console.info("Rpc write remote object fail, errorCode " + e.code); 122 console.info("Rpc write remote object fail, errorMessage " + e.message); 123 } 124 ``` 125 126### readRemoteObject 127 128readRemoteObject(): IRemoteObject 129 130Reads 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. 131 132**System capability**: SystemCapability.Communication.IPC.Core 133 134**Return value** 135 136| Type | Description | 137| ------------------------------- | ------------------ | 138| [IRemoteObject](#iremoteobject) | Remote object obtained.| 139 140**Error codes** 141 142For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 143 144| ID| Error Message| 145| -------- | -------- | 146| 1900008 | proxy or remote object is invalid | 147| 1900010 | read data from message sequence failed | 148 149**Example** 150 151 ```ts 152 import { BusinessError } from '@ohos.base'; 153 154 class TestRemoteObject extends rpc.RemoteObject { 155 constructor(descriptor: string) { 156 super(descriptor); 157 } 158 } 159 let data = rpc.MessageSequence.create(); 160 let testRemoteObject = new TestRemoteObject("testObject"); 161 try { 162 data.writeRemoteObject(testRemoteObject); 163 let proxy = data.readRemoteObject(); 164 console.log("RpcClient: readRemoteObject is " + proxy); 165 } catch(error) { 166 let e: BusinessError = error as BusinessError; 167 console.info("Rpc write remote object fail, errorCode " + e.code); 168 console.info("Rpc write remote object fail, errorMessage " + e.message); 169 } 170 ``` 171 172### writeInterfaceToken 173 174writeInterfaceToken(token: string): void 175 176Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 177 178**System capability**: SystemCapability.Communication.IPC.Core 179 180**Parameters** 181 182| Name| Type | Mandatory| Description | 183| ------ | ------ | ---- | ------------------ | 184| token | string | Yes | Interface token to write.| 185 186**Error codes** 187 188For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 189 190| ID| Error Message| 191| -------- | -------- | 192| 1900009 | write data to message sequence failed | 193 194**Example** 195 196 ```ts 197 import { BusinessError } from '@ohos.base'; 198 199 let data = rpc.MessageSequence.create(); 200 try { 201 data.writeInterfaceToken("aaa"); 202 } catch(error) { 203 let e: BusinessError = error as BusinessError; 204 console.info("rpc write interface fail, errorCode " + e.code); 205 console.info("rpc write interface fail, errorMessage " + e.message); 206 } 207 ``` 208 209### readInterfaceToken 210 211readInterfaceToken(): string 212 213Reads 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. 214 215**System capability**: SystemCapability.Communication.IPC.Core 216 217**Return value** 218 219| Type | Description | 220| ------ | ------------------------ | 221| string | Interface token obtained.| 222 223**Error codes** 224 225For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 226 227| ID| Error Message| 228| -------- | -------- | 229| 1900010 | read data from message sequence failed | 230 231**Example** 232 233```ts 234import { BusinessError } from '@ohos.base'; 235 236class Stub extends rpc.RemoteObject { 237 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 238 try { 239 let interfaceToken = data.readInterfaceToken(); 240 console.log("RpcServer: interfaceToken is " + interfaceToken); 241 } catch(error) { 242 let e: BusinessError = error as BusinessError; 243 console.info("RpcServer: read interfaceToken failed, errorCode " + e.code); 244 console.info("RpcServer: read interfaceToken failed, errorMessage " + e.message); 245 } 246 return true; 247 } 248} 249``` 250 251### getSize 252 253getSize(): number 254 255Obtains the data size of this **MessageSequence** object. 256 257**System capability**: SystemCapability.Communication.IPC.Core 258 259**Return value** 260 261| Type | Description | 262| ------ | ----------------------------------------------- | 263| number | Size of the **MessageSequence** instance obtained, in bytes.| 264 265**Example** 266 267 ```ts 268 let data = rpc.MessageSequence.create(); 269 let size = data.getSize(); 270 console.log("RpcClient: size is " + size); 271 ``` 272 273### getCapacity 274 275getCapacity(): number 276 277Obtains the capacity of this **MessageSequence** object. 278 279**System capability**: SystemCapability.Communication.IPC.Core 280 281**Return value** 282 283| Type | Description | 284| ------ | ----- | 285| number | **MessageSequence** capacity obtained, in bytes.| 286 287**Example** 288 289 ```ts 290 let data = rpc.MessageSequence.create(); 291 let result = data.getCapacity(); 292 console.log("RpcClient: capacity is " + result); 293 ``` 294 295### setSize 296 297setSize(size: number): void 298 299Sets the size of the data contained in this **MessageSequence** object. 300 301**System capability**: SystemCapability.Communication.IPC.Core 302 303**Parameters** 304 305| Name| Type | Mandatory| Description | 306| ------ | ------ | ---- | ------ | 307| size | number | Yes | Data size to set, in bytes.| 308 309**Example** 310 311 ```ts 312 import { BusinessError } from '@ohos.base'; 313 314 let data = rpc.MessageSequence.create(); 315 try { 316 data.setSize(16); 317 console.log("RpcClient: setSize is " + data.getSize()); 318 } catch(error) { 319 let e: BusinessError = error as BusinessError; 320 console.info("rpc set size of MessageSequence fail, errorCode " + e.code); 321 console.info("rpc set size of MessageSequence fail, errorMessage " + e.message); 322 } 323 ``` 324 325### setCapacity 326 327setCapacity(size: number): void 328 329Sets the storage capacity of this **MessageSequence** object. 330 331**System capability**: SystemCapability.Communication.IPC.Core 332 333**Parameters** 334 335| Name| Type | Mandatory| Description | 336| ------ | ------ | ---- | --------------------------------------------- | 337| size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 338 339**Error codes** 340 341For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 342 343| ID| Error Message| 344| -------- | -------- | 345| 1900011 | parcel memory alloc failed | 346 347**Example** 348 349 ```ts 350 import { BusinessError } from '@ohos.base'; 351 352 let data = rpc.MessageSequence.create(); 353 try { 354 data.setCapacity(100); 355 console.log("RpcClient: setCapacity is " + data.getCapacity()); 356 } catch(error) { 357 let e: BusinessError = error as BusinessError; 358 console.info("rpc memory alloc fail, errorCode " + e.code); 359 console.info("rpc memory alloc fail, errorMessage " + e.message); 360 } 361 ``` 362 363### getWritableBytes 364 365getWritableBytes(): number 366 367Obtains the writable capacity (in bytes) of this **MessageSequence** object. 368 369**System capability**: SystemCapability.Communication.IPC.Core 370 371**Return value** 372 373| Type | Description | 374| ------ | ------ | 375| number | Writable capacity of the **MessageSequence** instance, in bytes.| 376 377**Example** 378 379```ts 380class Stub extends rpc.RemoteObject { 381 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 382 let getWritableBytes = data.getWritableBytes(); 383 console.log("RpcServer: getWritableBytes is " + getWritableBytes); 384 return true; 385 } 386} 387``` 388 389### getReadableBytes 390 391getReadableBytes(): number 392 393Obtains the readable capacity of this **MessageSequence** object. 394 395**System capability**: SystemCapability.Communication.IPC.Core 396 397**Return value** 398 399| Type | Description | 400| ------ | ------- | 401| number | Readable capacity of the **MessageSequence** instance, in bytes.| 402 403**Example** 404 405```ts 406class Stub extends rpc.RemoteObject { 407 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 408 let result = data.getReadableBytes(); 409 console.log("RpcServer: getReadableBytes is " + result); 410 return true; 411 } 412} 413``` 414 415### getReadPosition 416 417getReadPosition(): number 418 419Obtains the read position of this **MessageSequence** object. 420 421**System capability**: SystemCapability.Communication.IPC.Core 422 423**Return value** 424 425| Type | Description | 426| ------ | ------ | 427| number | Read position obtained.| 428 429**Example** 430 431 ```ts 432 let data = rpc.MessageSequence.create(); 433 let readPos = data.getReadPosition(); 434 console.log("RpcClient: readPos is " + readPos); 435 ``` 436 437### getWritePosition 438 439getWritePosition(): number 440 441Obtains the write position of this **MessageSequence** object. 442 443**System capability**: SystemCapability.Communication.IPC.Core 444 445**Return value** 446 447| Type | Description | 448| ------ | ----- | 449| number | Write position obtained.| 450 451**Example** 452 453 ```ts 454 let data = rpc.MessageSequence.create(); 455 data.writeInt(10); 456 let bwPos = data.getWritePosition(); 457 console.log("RpcClient: bwPos is " + bwPos); 458 ``` 459 460### rewindRead 461 462rewindRead(pos: number): void 463 464Moves the read pointer to the specified position. 465 466**System capability**: SystemCapability.Communication.IPC.Core 467 468**Parameters** 469 470| Name| Type | Mandatory| Description | 471| ------ | ------ | ---- | ------- | 472| pos | number | Yes | Position from which data is to read.| 473 474**Example** 475 476 ```ts 477 import { BusinessError } from '@ohos.base'; 478 479 let data = rpc.MessageSequence.create(); 480 data.writeInt(12); 481 data.writeString("sequence"); 482 let number = data.readInt(); 483 console.log("RpcClient: number is " + number); 484 try { 485 data.rewindRead(0); 486 } catch(error) { 487 let e: BusinessError = error as BusinessError; 488 console.info("rpc rewind read data fail, errorCode " + e.code); 489 console.info("rpc rewind read data fail, errorMessage " + e.message); 490 } 491 let number2 = data.readInt(); 492 console.log("RpcClient: rewindRead is " + number2); 493 ``` 494 495### rewindWrite 496 497rewindWrite(pos: number): void 498 499Moves the write pointer to the specified position. 500 501**System capability**: SystemCapability.Communication.IPC.Core 502 503**Parameters** 504 505| Name| Type | Mandatory| Description | 506| ------ | ------ | ---- | ----- | 507| pos | number | Yes | Position from which data is to write.| 508 509**Example** 510 511 ```ts 512 import { BusinessError } from '@ohos.base'; 513 514 let data = rpc.MessageSequence.create(); 515 data.writeInt(4); 516 try { 517 data.rewindWrite(0); 518 } catch(error) { 519 let e: BusinessError = error as BusinessError; 520 console.info("rpc rewind read data fail, errorCode " + e.code); 521 console.info("rpc rewind read data fail, errorMessage " + e.message); 522 } 523 data.writeInt(5); 524 let number = data.readInt(); 525 console.log("RpcClient: rewindWrite is: " + number); 526 ``` 527 528### writeByte 529 530writeByte(val: number): void 531 532Writes a byte value to this **MessageSequence** object. 533 534**System capability**: SystemCapability.Communication.IPC.Core 535 536**Parameters** 537 538| Name| Type | Mandatory| Description | 539| ------ | ------ | ---- | ----- | 540| val | number | Yes | Byte value to write.| 541 542**Error codes** 543 544For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 545 546| ID| Error Message| 547| -------- | ------- | 548| 1900009 | write data to message sequence failed | 549 550**Example** 551 552 ```ts 553 import { BusinessError } from '@ohos.base'; 554 555 let data = rpc.MessageSequence.create(); 556 try { 557 data.writeByte(2); 558 } catch(error) { 559 let e: BusinessError = error as BusinessError; 560 console.info("rpc write byte fail, errorCode " + e.code); 561 console.info("rpc write byte fail, errorMessage" + e.message); 562 } 563 ``` 564 565### readByte 566 567readByte(): number 568 569Reads the byte value from this **MessageSequence** object. 570 571**System capability**: SystemCapability.Communication.IPC.Core 572 573**Return value** 574 575| Type | Description | 576| ------ | ----- | 577| number | Byte value read.| 578 579**Error codes** 580 581For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 582 583| ID| Error Message| 584| ------- | -------- | 585| 1900010 | read data from message sequence failed | 586 587**Example** 588 589 ```ts 590 import { BusinessError } from '@ohos.base'; 591 592 let data = rpc.MessageSequence.create(); 593 try { 594 data.writeByte(2); 595 } catch(error) { 596 let e: BusinessError = error as BusinessError; 597 console.info("rpc write byte fail, errorCode " + e.code); 598 console.info("rpc write byte fail, errorMessage" + e.message); 599 } 600 try { 601 let ret = data.readByte(); 602 console.log("RpcClient: readByte is: " + ret); 603 } catch(error) { 604 let e: BusinessError = error as BusinessError; 605 console.info("rpc write byte fail, errorCode " + e.code); 606 console.info("rpc write byte fail, errorMessage" + e.message); 607 } 608 ``` 609 610### writeShort 611 612writeShort(val: number): void 613 614Writes a short integer to this **MessageSequence** object. 615 616**System capability**: SystemCapability.Communication.IPC.Core 617 618**Parameters** 619 620| Name| Type | Mandatory| Description| 621| ------ | ------ | --- | --- | 622| val | number | Yes | Short integer to write.| 623 624**Error codes** 625 626For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 627 628| ID| Error Message| 629| -------- | -------- | 630| 1900009 | write data to message sequence failed | 631 632**Example** 633 634 ```ts 635 import { BusinessError } from '@ohos.base'; 636 637 let data = rpc.MessageSequence.create(); 638 try { 639 data.writeShort(8); 640 } catch(error) { 641 let e: BusinessError = error as BusinessError; 642 console.info("rpc write short fail, errorCode " + e.code); 643 console.info("rpc write short fail, errorMessage" + e.message); 644 } 645 ``` 646 647### readShort 648 649readShort(): number 650 651Reads the short integer from this **MessageSequence** object. 652 653**System capability**: SystemCapability.Communication.IPC.Core 654 655**Return value** 656 657| Type | Description | 658| ------ | -------------- | 659| number | Short integer read.| 660 661**Error codes** 662 663For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 664 665| ID| Error Message| 666| -------- | -------- | 667| 1900010 | read data from message sequence failed | 668 669**Example** 670 671 ```ts 672 import { BusinessError } from '@ohos.base'; 673 674 let data = rpc.MessageSequence.create(); 675 try { 676 data.writeShort(8); 677 } catch(error) { 678 let e: BusinessError = error as BusinessError; 679 console.info("rpc write short fail, errorCode " + e.code); 680 console.info("rpc write short fail, errorMessage" + e.message); 681 } 682 try { 683 let ret = data.readShort(); 684 console.log("RpcClient: readByte is: " + ret); 685 } catch(error) { 686 let e: BusinessError = error as BusinessError; 687 console.info("rpc read short fail, errorCode " + e.code); 688 console.info("rpc read short fail, errorMessage" + e.message); 689 } 690 ``` 691 692### writeInt 693 694writeInt(val: number): void 695 696Writes an 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 | Integer to write.| 705 706**Error codes** 707 708For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 709 710| ID| Error Message| 711| -------- | -------- | 712| 1900009 | write data to message sequence failed | 713 714**Example** 715 716 ```ts 717 import { BusinessError } from '@ohos.base'; 718 719 let data = rpc.MessageSequence.create(); 720 try { 721 data.writeInt(10); 722 } catch(error) { 723 let e: BusinessError = error as BusinessError; 724 console.info("rpc write int fail, errorCode " + e.code); 725 console.info("rpc write int fail, errorMessage" + e.message); 726 } 727 ``` 728 729### readInt 730 731readInt(): number 732 733Reads the integer from this **MessageSequence** object. 734 735**System capability**: SystemCapability.Communication.IPC.Core 736 737**Return value** 738 739| Type | Description | 740| ------ | ------------ | 741| number | Integer read.| 742 743**Error codes** 744 745For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 746 747| ID| Error Message| 748| -------- | -------- | 749| 1900010 | read data from message sequence failed | 750 751**Example** 752 753 ```ts 754 import { BusinessError } from '@ohos.base'; 755 756 let data = rpc.MessageSequence.create(); 757 try { 758 data.writeInt(10); 759 } catch(error) { 760 let e: BusinessError = error as BusinessError; 761 console.info("rpc write int fail, errorCode " + e.code); 762 console.info("rpc write int fail, errorMessage" + e.message); 763 } 764 try { 765 let ret = data.readInt(); 766 console.log("RpcClient: readInt is " + ret); 767 } catch(error) { 768 let e: BusinessError = error as BusinessError; 769 console.info("rpc read int fail, errorCode " + e.code); 770 console.info("rpc read int fail, errorMessage" + e.message); 771 } 772 ``` 773 774### writeLong 775 776writeLong(val: number): void 777 778Writes a long integer to this **MessageSequence** object. 779 780**System capability**: SystemCapability.Communication.IPC.Core 781 782**Parameters** 783 784| Name| Type | Mandatory| Description | 785| ------ | ------ | ---- | ---------------- | 786| val | number | Yes | Long integer to write.| 787 788**Error codes** 789 790For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 791 792| ID| Error Message| 793| -------- | -------- | 794| 1900009 | write data to message sequence failed | 795 796**Example** 797 798 ```ts 799 import { BusinessError } from '@ohos.base'; 800 801 let data = rpc.MessageSequence.create(); 802 try { 803 data.writeLong(10000); 804 } catch(error) { 805 let e: BusinessError = error as BusinessError; 806 console.info("rpc write long fail, errorCode " + e.code); 807 console.info("rpc write long fail, errorMessage" + e.message); 808 } 809 ``` 810 811### readLong 812 813readLong(): number 814 815Reads the long integer from this **MessageSequence** object. 816 817**System capability**: SystemCapability.Communication.IPC.Core 818 819**Return value** 820 821| Type | Description | 822| ------ | -------------- | 823| number | Long integer read.| 824 825**Error codes** 826 827For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 828 829| ID| Error Message| 830| -------- | -------- | 831| 1900010 | read data from message sequence failed | 832 833**Example** 834 835 ```ts 836 import { BusinessError } from '@ohos.base'; 837 838 let data = rpc.MessageSequence.create(); 839 try { 840 data.writeLong(10000); 841 } catch(error) { 842 let e: BusinessError = error as BusinessError; 843 console.info("rpc write long fail, errorCode " + e.code); 844 console.info("rpc write long fail, errorMessage" + e.message); 845 } 846 try { 847 let ret = data.readLong(); 848 console.log("RpcClient: readLong is " + ret); 849 } catch(error) { 850 let e: BusinessError = error as BusinessError; 851 console.info("rpc read long fail, errorCode " + e.code); 852 console.info("rpc read long fail, errorMessage" + e.message); 853 } 854 ``` 855 856### writeFloat 857 858writeFloat(val: number): void 859 860Writes a floating-point number to this **MessageSequence** object. 861 862**System capability**: SystemCapability.Communication.IPC.Core 863 864**Parameters** 865 866| Name| Type | Mandatory| Description | 867| ------ | ------ | ---- | ----- | 868| val | number | Yes | Floating-point number to write.| 869 870**Error codes** 871 872For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 873 874| ID| Error Message| 875| -------- | -------- | 876| 1900009 | write data to message sequence failed | 877 878**Example** 879 880 ```ts 881 import { BusinessError } from '@ohos.base'; 882 883 let data = rpc.MessageSequence.create(); 884 try { 885 data.writeFloat(1.2); 886 } catch(error) { 887 let e: BusinessError = error as BusinessError; 888 console.info("rpc write float fail, errorCode " + e.code); 889 console.info("rpc write float fail, errorMessage" + e.message); 890 } 891 ``` 892 893### readFloat 894 895readFloat(): number 896 897Reads the floating-pointer number from this **MessageSequence** object. 898 899**System capability**: SystemCapability.Communication.IPC.Core 900 901**Return value** 902 903| Type | Description | 904| ------ | ------------ | 905| number | Floating-point number read.| 906 907**Error codes** 908 909For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 910 911| ID| Error Message| 912| -------- | -------- | 913| 1900010 | read data from message sequence failed | 914 915**Example** 916 917 ```ts 918 import { BusinessError } from '@ohos.base'; 919 920 let data = rpc.MessageSequence.create(); 921 try { 922 data.writeFloat(1.2); 923 } catch(error) { 924 let e: BusinessError = error as BusinessError; 925 console.info("rpc write float fail, errorCode " + e.code); 926 console.info("rpc write float fail, errorMessage" + e.message); 927 } 928 try { 929 let ret = data.readFloat(); 930 console.log("RpcClient: readFloat is " + ret); 931 } catch(error) { 932 let e: BusinessError = error as BusinessError; 933 console.info("rpc read float fail, errorCode " + e.code); 934 console.info("rpc read float fail, errorMessage" + e.message); 935 } 936 ``` 937 938### writeDouble 939 940writeDouble(val: number): void 941 942Writes a double-precision floating-point number to this **MessageSequence** object. 943 944**System capability**: SystemCapability.Communication.IPC.Core 945 946**Parameters** 947 948| Name| Type | Mandatory| Description | 949| ------ | ------ | ---- | ---------------------- | 950| val | number | Yes | Double-precision floating-point number to write.| 951 952**Error codes** 953 954For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 955 956| ID| Error Message| 957| -------- | -------- | 958| 1900009 | write data to message sequence failed | 959 960**Example** 961 962 ```ts 963 import { BusinessError } from '@ohos.base'; 964 965 let data = rpc.MessageSequence.create(); 966 try { 967 data.writeDouble(10.2); 968 } catch(error) { 969 let e: BusinessError = error as BusinessError; 970 console.info("rpc read float fail, errorCode " + e.code); 971 console.info("rpc read float fail, errorMessage" + e.message); 972 } 973 ``` 974 975### readDouble 976 977readDouble(): number 978 979Reads the double-precision floating-point number from this **MessageSequence** object. 980 981**System capability**: SystemCapability.Communication.IPC.Core 982 983**Return value** 984 985| Type | Description | 986| ------ | ------------------ | 987| number | Double-precision floating-point number read.| 988 989**Error codes** 990 991For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 992 993| ID| Error Message| 994| -------- | -------- | 995| 1900010 | read data from message sequence failed | 996 997**Example** 998 999 ```ts 1000 import { BusinessError } from '@ohos.base'; 1001 1002 let data = rpc.MessageSequence.create(); 1003 try { 1004 data.writeDouble(10.2); 1005 } catch(error) { 1006 let e: BusinessError = error as BusinessError; 1007 console.info("rpc write double fail, errorCode " + e.code); 1008 console.info("rpc write double fail, errorMessage" + e.message); 1009 } 1010 try { 1011 let ret = data.readDouble(); 1012 console.log("RpcClient: readDouble is " + ret); 1013 } catch(error) { 1014 let e: BusinessError = error as BusinessError; 1015 console.info("rpc read double fail, errorCode " + e.code); 1016 console.info("rpc read double fail, errorMessage" + e.message); 1017 } 1018 ``` 1019 1020### writeBoolean 1021 1022writeBoolean(val: boolean): void 1023 1024Writes a Boolean value to this **MessageSequence** object. 1025 1026**System capability**: SystemCapability.Communication.IPC.Core 1027 1028**Parameters** 1029 1030| Name| Type | Mandatory| Description | 1031| ------ | ------- | ---- | ---------------- | 1032| val | boolean | Yes | Boolean value to write.| 1033 1034**Error codes** 1035 1036For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1037 1038| ID| Error Message| 1039| -------- | -------- | 1040| 1900009 | write data to message sequence failed | 1041 1042**Example** 1043 1044 ```ts 1045 import { BusinessError } from '@ohos.base'; 1046 1047 let data = rpc.MessageSequence.create(); 1048 try { 1049 data.writeBoolean(false); 1050 } catch(error) { 1051 let e: BusinessError = error as BusinessError; 1052 console.info("rpc write boolean fail, errorCode " + e.code); 1053 console.info("rpc write boolean fail, errorMessage" + e.message); 1054 } 1055 ``` 1056 1057### readBoolean 1058 1059readBoolean(): boolean 1060 1061Reads the Boolean value from this **MessageSequence** object. 1062 1063**System capability**: SystemCapability.Communication.IPC.Core 1064 1065**Return value** 1066 1067| Type | Description | 1068| ------- | -------------------- | 1069| boolean | Boolean value read.| 1070 1071**Error codes** 1072 1073For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1074 1075| ID| Error Message| 1076| -------- | -------- | 1077| 1900010 | read data from message sequence failed | 1078 1079**Example** 1080 1081 ```ts 1082 import { BusinessError } from '@ohos.base'; 1083 1084 let data = rpc.MessageSequence.create(); 1085 try { 1086 data.writeBoolean(false); 1087 } catch(error) { 1088 let e: BusinessError = error as BusinessError; 1089 console.info("rpc write boolean fail, errorCode " + e.code); 1090 console.info("rpc write boolean fail, errorMessage" + e.message); 1091 } 1092 try { 1093 let ret = data.readBoolean(); 1094 console.log("RpcClient: readBoolean is " + ret); 1095 } catch(error) { 1096 let e: BusinessError = error as BusinessError; 1097 console.info("rpc read boolean fail, errorCode " + e.code); 1098 console.info("rpc read boolean fail, errorMessage" + e.message); 1099 } 1100 ``` 1101 1102### writeChar 1103 1104writeChar(val: number): void 1105 1106Writes a character to this **MessageSequence** object. 1107 1108**System capability**: SystemCapability.Communication.IPC.Core 1109 1110**Parameters** 1111 1112| Name| Type | Mandatory| Description | 1113| ------ | ------ | ---- | -------------------- | 1114| val | number | Yes | Single character to write.| 1115 1116**Error codes** 1117 1118For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1119 1120| ID| Error Message| 1121| -------- | -------- | 1122| 1900009 | write data to message sequence failed | 1123 1124**Example** 1125 1126 ```ts 1127 import { BusinessError } from '@ohos.base'; 1128 1129 let data = rpc.MessageSequence.create(); 1130 try { 1131 data.writeChar(97); 1132 } catch(error) { 1133 let e: BusinessError = error as BusinessError; 1134 console.info("rpc write char fail, errorCode " + e.code); 1135 console.info("rpc write char fail, errorMessage" + e.message); 1136 } 1137 ``` 1138 1139### readChar 1140 1141readChar(): number 1142 1143Reads the character from this **MessageSequence** object. 1144 1145**System capability**: SystemCapability.Communication.IPC.Core 1146 1147**Return value** 1148 1149| Type | Description| 1150| ------ | ---- | 1151| number | Character read.| 1152 1153**Error codes** 1154 1155For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1156 1157| ID| Error Message| 1158| -------- | -------- | 1159| 1900010 | read data from message sequence failed | 1160 1161**Example** 1162 1163 ```ts 1164 import { BusinessError } from '@ohos.base'; 1165 1166 let data = rpc.MessageSequence.create(); 1167 try { 1168 data.writeChar(97); 1169 } catch(error) { 1170 let e: BusinessError = error as BusinessError; 1171 console.info("rpc write char fail, errorCode " + e.code); 1172 console.info("rpc write char fail, errorMessage" + e.message); 1173 } 1174 try { 1175 let ret = data.readChar(); 1176 console.log("RpcClient: readChar is " + ret); 1177 } catch(error) { 1178 let e: BusinessError = error as BusinessError; 1179 console.info("rpc read char fail, errorCode " + e.code); 1180 console.info("rpc read char fail, errorMessage" + e.message); 1181 } 1182 ``` 1183 1184### writeString 1185 1186writeString(val: string): void 1187 1188Writes a string to this **MessageSequence** object. 1189 1190**System capability**: SystemCapability.Communication.IPC.Core 1191 1192**Parameters** 1193 1194| Name| Type | Mandatory| Description | 1195| ------ | ------ | ---- | ----------------------------------------- | 1196| val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1197 1198**Error codes** 1199 1200For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1201 1202| ID| Error Message| 1203| -------- | -------- | 1204| 1900009 | write data to message sequence failed | 1205 1206**Example** 1207 1208 ```ts 1209 import { BusinessError } from '@ohos.base'; 1210 1211 let data = rpc.MessageSequence.create(); 1212 try { 1213 data.writeString('abc'); 1214 } catch(error) { 1215 let e: BusinessError = error as BusinessError; 1216 console.info("rpc write string fail, errorCode " + e.code); 1217 console.info("rpc write string fail, errorMessage" + e.message); 1218 } 1219 ``` 1220 1221### readString 1222 1223readString(): string 1224 1225Reads the string from this **MessageSequence** object. 1226 1227**System capability**: SystemCapability.Communication.IPC.Core 1228 1229**Return value** 1230 1231| Type | Description | 1232| ------ | -------------- | 1233| string | String read.| 1234 1235**Error codes** 1236 1237For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1238 1239| ID| Error Message| 1240| -------- | -------- | 1241| 1900010 | read data from message sequence failed | 1242 1243**Example** 1244 1245 ```ts 1246 import { BusinessError } from '@ohos.base'; 1247 1248 let data = rpc.MessageSequence.create(); 1249 try { 1250 data.writeString('abc'); 1251 } catch(error) { 1252 let e: BusinessError = error as BusinessError; 1253 console.info("rpc write string fail, errorCode " + e.code); 1254 console.info("rpc write string fail, errorMessage" + e.message); 1255 } 1256 try { 1257 let ret = data.readString(); 1258 console.log("RpcClient: readString is " + ret); 1259 } catch(error) { 1260 let e: BusinessError = error as BusinessError; 1261 console.info("rpc read string fail, errorCode " + e.code); 1262 console.info("rpc read string fail, errorMessage" + e.message); 1263 } 1264 ``` 1265 1266### writeParcelable 1267 1268writeParcelable(val: Parcelable): void 1269 1270Writes a **Parcelable** object to this **MessageSequence** object. 1271 1272**System capability**: SystemCapability.Communication.IPC.Core 1273 1274**Parameters** 1275 1276| Name| Type| Mandatory| Description| 1277| ------ | --------- | ---- | ------ | 1278| val | Parcelable | Yes | **Parcelable** object to write.| 1279 1280**Error codes** 1281 1282For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1283 1284| ID| Error Message| 1285| -------- | -------- | 1286| 1900009 | write data to message sequence failed | 1287 1288**Example** 1289 1290 ```ts 1291 import { BusinessError } from '@ohos.base'; 1292 1293 class MyParcelable implements rpc.Parcelable { 1294 num: number = 0; 1295 str: string = ''; 1296 constructor( num: number, str: string) { 1297 this.num = num; 1298 this.str = str; 1299 } 1300 marshalling(messageSequence: rpc.MessageSequence): boolean { 1301 messageSequence.writeInt(this.num); 1302 messageSequence.writeString(this.str); 1303 return true; 1304 } 1305 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1306 this.num = messageSequence.readInt(); 1307 this.str = messageSequence.readString(); 1308 return true; 1309 } 1310 } 1311 let parcelable = new MyParcelable(1, "aaa"); 1312 let data = rpc.MessageSequence.create(); 1313 try { 1314 data.writeParcelable(parcelable); 1315 } catch(error) { 1316 let e: BusinessError = error as BusinessError; 1317 console.info("rpc write parcelable fail, errorCode " + e.code); 1318 console.info("rpc write parcelable fail, errorMessage" + e.message); 1319 } 1320 ``` 1321 1322### readParcelable 1323 1324readParcelable(dataIn: Parcelable): void 1325 1326Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1327 1328**System capability**: SystemCapability.Communication.IPC.Core 1329 1330**Parameters** 1331 1332| Name| Type | Mandatory| Description | 1333| ------ | ------------------------- | ---- | ----------------------------------------- | 1334| dataIn | Parcelable | Yes | **Parcelable** object to read.| 1335 1336**Error codes** 1337 1338For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1339 1340| ID| Error Message| 1341| -------- | -------- | 1342| 1900010 | read data from message sequence failed | 1343| 1900012 | call js callback function failed | 1344 1345**Example** 1346 1347 ```ts 1348 import { BusinessError } from '@ohos.base'; 1349 1350 class MyParcelable implements rpc.Parcelable { 1351 num: number = 0; 1352 str: string = ''; 1353 constructor(num: number, str: string) { 1354 this.num = num; 1355 this.str = str; 1356 } 1357 marshalling(messageSequence: rpc.MessageSequence): boolean { 1358 messageSequence.writeInt(this.num); 1359 messageSequence.writeString(this.str); 1360 return true; 1361 } 1362 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1363 this.num = messageSequence.readInt(); 1364 this.str = messageSequence.readString(); 1365 return true; 1366 } 1367 } 1368 let parcelable = new MyParcelable(1, "aaa"); 1369 let data = rpc.MessageSequence.create(); 1370 data.writeParcelable(parcelable); 1371 let ret = new MyParcelable(0, ""); 1372 try { 1373 data.readParcelable(ret); 1374 }catch(error) { 1375 let e: BusinessError = error as BusinessError; 1376 console.info("rpc read parcelable fail, errorCode " + e.code); 1377 console.info("rpc read parcelable fail, errorMessage" + e.message); 1378 } 1379 ``` 1380 1381### writeByteArray 1382 1383writeByteArray(byteArray: number[]): void 1384 1385Writes a byte array to this **MessageSequence** object. 1386 1387**System capability**: SystemCapability.Communication.IPC.Core 1388 1389**Parameters** 1390 1391| Name | Type | Mandatory| Description | 1392| --------- | -------- | ---- | ------------------ | 1393| byteArray | number[] | Yes | Byte array to write.| 1394 1395**Error codes** 1396 1397For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1398 1399| ID| Error Message| 1400| -------- | -------- | 1401| 1900009 | write data to message sequence failed | 1402 1403**Example** 1404 1405 ```ts 1406 import { BusinessError } from '@ohos.base'; 1407 1408 let data = rpc.MessageSequence.create(); 1409 let ByteArrayVar = [1, 2, 3, 4, 5]; 1410 try { 1411 data.writeByteArray(ByteArrayVar); 1412 } catch(error) { 1413 let e: BusinessError = error as BusinessError; 1414 console.info("rpc write byteArray fail, errorCode " + e.code); 1415 console.info("rpc write byteArray fail, errorMessage" + e.message); 1416 } 1417 ``` 1418 1419### readByteArray 1420 1421readByteArray(dataIn: number[]): void 1422 1423Reads a byte array from this **MessageSequence** object. 1424 1425**System capability**: SystemCapability.Communication.IPC.Core 1426 1427**Parameters** 1428 1429| Name| Type | Mandatory| Description | 1430| ------ | -------- | ---- | ------------------ | 1431| dataIn | number[] | Yes | Byte array to read.| 1432 1433**Error codes** 1434 1435For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1436 1437| ID| Error Message| 1438| -------- | -------- | 1439| 1900010 | read data from message sequence failed | 1440 1441**Example** 1442 1443 ```ts 1444 import { BusinessError } from '@ohos.base'; 1445 1446 let data = rpc.MessageSequence.create(); 1447 let ByteArrayVar = [1, 2, 3, 4, 5]; 1448 try { 1449 data.writeByteArray(ByteArrayVar); 1450 } catch(error) { 1451 let e: BusinessError = error as BusinessError; 1452 console.info("rpc write byteArray fail, errorCode " + e.code); 1453 console.info("rpc write byteArray fail, errorMessage" + e.message); 1454 } 1455 try { 1456 let array: Array<number> = new Array(5); 1457 data.readByteArray(array); 1458 } catch(error) { 1459 let e: BusinessError = error as BusinessError; 1460 console.info("rpc write byteArray fail, errorCode " + e.code); 1461 console.info("rpc write byteArray fail, errorMessage" + e.message); 1462 } 1463 ``` 1464 1465### readByteArray 1466 1467readByteArray(): number[] 1468 1469Reads the byte array from this **MessageSequence** object. 1470 1471**System capability**: SystemCapability.Communication.IPC.Core 1472 1473**Return value** 1474 1475| Type | Description | 1476| -------- | -------------- | 1477| number[] | Byte array read.| 1478 1479**Error codes** 1480 1481For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1482 1483| ID| Error Message| 1484| -------- | -------- | 1485| 1900010 | read data from message sequence failed | 1486 1487**Example** 1488 1489 ```ts 1490 import { BusinessError } from '@ohos.base'; 1491 1492 let data = rpc.MessageSequence.create(); 1493 let byteArrayVar = [1, 2, 3, 4, 5]; 1494 try { 1495 data.writeByteArray(byteArrayVar); 1496 } catch(error) { 1497 let e: BusinessError = error as BusinessError; 1498 console.info("rpc write byteArray fail, errorCode " + e.code); 1499 console.info("rpc write byteArray fail, errorMessage" + e.message); 1500 } 1501 try { 1502 let array = data.readByteArray(); 1503 console.log("RpcClient: readByteArray is " + array); 1504 } catch(error) { 1505 let e: BusinessError = error as BusinessError; 1506 console.info("rpc read byteArray fail, errorCode " + e.code); 1507 console.info("rpc read byteArray fail, errorMessage" + e.message); 1508 } 1509 ``` 1510 1511### writeShortArray 1512 1513writeShortArray(shortArray: number[]): void 1514 1515Writes a short array to this **MessageSequence** object. 1516 1517**System capability**: SystemCapability.Communication.IPC.Core 1518 1519**Parameters** 1520 1521| Name | Type | Mandatory| Description | 1522| ---------- | -------- | ---- | -------------------- | 1523| shortArray | number[] | Yes | Short array to write.| 1524 1525**Error codes** 1526 1527For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1528 1529| ID| Error Message| 1530| -------- | -------- | 1531| 1900009 | write data to message sequence failed | 1532 1533**Example** 1534 1535 ```ts 1536 import { BusinessError } from '@ohos.base'; 1537 1538 let data = rpc.MessageSequence.create(); 1539 try { 1540 data.writeShortArray([11, 12, 13]); 1541 } catch(error) { 1542 let e: BusinessError = error as BusinessError; 1543 console.info("rpc read byteArray fail, errorCode " + e.code); 1544 console.info("rpc read byteArray fail, errorMessage" + e.message); 1545 } 1546 ``` 1547 1548### readShortArray 1549 1550readShortArray(dataIn: number[]): void 1551 1552Reads a short array from this **MessageSequence** object. 1553 1554**System capability**: SystemCapability.Communication.IPC.Core 1555 1556**Parameters** 1557 1558| Name| Type | Mandatory| Description | 1559| ------ | -------- | ---- | -------------------- | 1560| dataIn | number[] | Yes | Short array to read.| 1561 1562**Error codes** 1563 1564For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1565 1566| ID| Error Message| 1567| -------- | -------- | 1568| 1900010 | read data from message sequence failed | 1569 1570**Example** 1571 1572 ```ts 1573 import { BusinessError } from '@ohos.base'; 1574 1575 let data = rpc.MessageSequence.create(); 1576 try { 1577 data.writeShortArray([11, 12, 13]); 1578 } catch(error) { 1579 let e: BusinessError = error as BusinessError; 1580 console.info("rpc write shortArray fail, errorCode " + e.code); 1581 console.info("rpc write shortArray fail, errorMessage" + e.message); 1582 } 1583 try { 1584 let array: Array<number> = new Array(3); 1585 data.readShortArray(array); 1586 } catch(error) { 1587 let e: BusinessError = error as BusinessError; 1588 console.info("rpc read shortArray fail, errorCode " + e.code); 1589 console.info("rpc read shortArray fail, errorMessage" + e.message); 1590 } 1591 ``` 1592 1593### readShortArray 1594 1595readShortArray(): number[] 1596 1597Reads the short array from this **MessageSequence** object. 1598 1599**System capability**: SystemCapability.Communication.IPC.Core 1600 1601**Return value** 1602 1603| Type | Description | 1604| -------- | ---------------- | 1605| number[] | Short array read.| 1606 1607**Error codes** 1608 1609For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1610 1611| ID| Error Message| 1612| -------- | -------- | 1613| 1900010 | read data from message sequence failed | 1614 1615**Example** 1616 1617 ```ts 1618 import { BusinessError } from '@ohos.base'; 1619 1620 let data = rpc.MessageSequence.create(); 1621 try { 1622 data.writeShortArray([11, 12, 13]); 1623 } catch(error) { 1624 let e: BusinessError = error as BusinessError; 1625 console.info("rpc write shortArray fail, errorCode " + e.code); 1626 console.info("rpc write shortArray fail, errorMessage" + e.message); 1627 } 1628 try { 1629 let array = data.readShortArray(); 1630 console.log("RpcClient: readShortArray is " + array); 1631 } catch(error) { 1632 let e: BusinessError = error as BusinessError; 1633 console.info("rpc read shortArray fail, errorCode " + e.code); 1634 console.info("rpc read shortArray fail, errorMessage" + e.message); 1635 } 1636 ``` 1637 1638### writeIntArray 1639 1640writeIntArray(intArray: number[]): void 1641 1642Writes an integer array to this **MessageSequence** object. 1643 1644**System capability**: SystemCapability.Communication.IPC.Core 1645 1646**Parameters** 1647 1648| Name | Type | Mandatory| Description | 1649| -------- | -------- | ---- | ------------------ | 1650| intArray | number[] | Yes | Integer array to write.| 1651 1652**Error codes** 1653 1654For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1655 1656| ID| Error Message| 1657| -------- | -------- | 1658| 1900009 | write data to message sequence failed | 1659 1660**Example** 1661 1662 ```ts 1663 import { BusinessError } from '@ohos.base'; 1664 1665 let data = rpc.MessageSequence.create(); 1666 try { 1667 data.writeIntArray([100, 111, 112]); 1668 } catch(error) { 1669 let e: BusinessError = error as BusinessError; 1670 console.info("rpc write intArray fail, errorCode " + e.code); 1671 console.info("rpc write intArray fail, errorMessage" + e.message); 1672 } 1673 ``` 1674 1675### readIntArray 1676 1677readIntArray(dataIn: number[]): void 1678 1679Reads an integer array from this **MessageSequence** object. 1680 1681**System capability**: SystemCapability.Communication.IPC.Core 1682 1683**Parameters** 1684 1685| Name| Type | Mandatory| Description | 1686| ------ | -------- | ---- | ------------------ | 1687| dataIn | number[] | Yes | Integer array to read.| 1688 1689**Error codes** 1690 1691For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1692 1693| ID| Error Message| 1694| -------- | -------- | 1695| 1900010 | read data from message sequence failed | 1696 1697**Example** 1698 1699 ```ts 1700 import { BusinessError } from '@ohos.base'; 1701 1702 let data = rpc.MessageSequence.create(); 1703 try { 1704 data.writeIntArray([100, 111, 112]); 1705 } catch(error) { 1706 let e: BusinessError = error as BusinessError; 1707 console.info("rpc write intArray fail, errorCode " + e.code); 1708 console.info("rpc write intArray fail, errorMessage" + e.message); 1709 } 1710 let array: Array<number> = new Array(3); 1711 try { 1712 data.readIntArray(array); 1713 } catch(error) { 1714 let e: BusinessError = error as BusinessError; 1715 console.info("rpc read intArray fail, errorCode " + e.code); 1716 console.info("rpc read intArray fail, errorMessage" + e.message); 1717 } 1718 ``` 1719 1720### readIntArray 1721 1722readIntArray(): number[] 1723 1724Reads the integer array from this **MessageSequence** object. 1725 1726**System capability**: SystemCapability.Communication.IPC.Core 1727 1728**Return value** 1729 1730| Type | Description | 1731| -------- | -------------- | 1732| number[] | Integer array read.| 1733 1734**Error codes** 1735 1736For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1737 1738| ID| Error Message| 1739| -------- | -------- | 1740| 1900010 | read data from message sequence failed | 1741 1742**Example** 1743 1744 ```ts 1745 import { BusinessError } from '@ohos.base'; 1746 1747 let data = rpc.MessageSequence.create(); 1748 try { 1749 data.writeIntArray([100, 111, 112]); 1750 } catch(error) { 1751 let e: BusinessError = error as BusinessError; 1752 console.info("rpc write intArray fail, errorCode " + e.code); 1753 console.info("rpc write intArray fail, errorMessage" + e.message); 1754 } 1755 try { 1756 let array = data.readIntArray(); 1757 console.log("RpcClient: readIntArray is " + array); 1758 } catch(error) { 1759 let e: BusinessError = error as BusinessError; 1760 console.info("rpc read intArray fail, errorCode " + e.code); 1761 console.info("rpc read intArray fail, errorMessage" + e.message); 1762 } 1763 ``` 1764 1765### writeLongArray 1766 1767writeLongArray(longArray: number[]): void 1768 1769Writes a long array to this **MessageSequence** object. 1770 1771**System capability**: SystemCapability.Communication.IPC.Core 1772 1773**Parameters** 1774 1775| Name | Type | Mandatory| Description | 1776| --------- | -------- | ---- | -------------------- | 1777| longArray | number[] | Yes | Long array to write.| 1778 1779**Error codes** 1780 1781For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1782 1783| ID| Error Message| 1784| -------- | -------- | 1785| 1900009 | write data to message sequence failed | 1786 1787**Example** 1788 1789 ```ts 1790 import { BusinessError } from '@ohos.base'; 1791 1792 let data = rpc.MessageSequence.create(); 1793 try { 1794 data.writeLongArray([1111, 1112, 1113]); 1795 }catch(error){ 1796 let e: BusinessError = error as BusinessError; 1797 console.info("rpc write longArray fail, errorCode " + e.code); 1798 console.info("rpc write longArray fail, errorMessage" + e.message); 1799 } 1800 ``` 1801 1802### readLongArray 1803 1804readLongArray(dataIn: number[]): void 1805 1806Reads a long array from this **MessageSequence** object. 1807 1808**System capability**: SystemCapability.Communication.IPC.Core 1809 1810**Parameters** 1811 1812| Name| Type | Mandatory| Description | 1813| ------ | -------- | ---- | -------------------- | 1814| dataIn | number[] | Yes | Long array to read.| 1815 1816**Error codes** 1817 1818For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1819 1820| ID| Error Message| 1821| -------- | -------- | 1822| 1900010 | read data from message sequence failed | 1823 1824**Example** 1825 1826 ```ts 1827 import { BusinessError } from '@ohos.base'; 1828 1829 let data = rpc.MessageSequence.create(); 1830 try { 1831 data.writeLongArray([1111, 1112, 1113]); 1832 } catch(error) { 1833 let e: BusinessError = error as BusinessError; 1834 console.info("rpc write longArray fail, errorCode " + e.code); 1835 console.info("rpc write longArray fail, errorMessage" + e.message); 1836 } 1837 let array: Array<number> = new Array(3); 1838 try { 1839 data.readLongArray(array); 1840 } catch(error) { 1841 let e: BusinessError = error as BusinessError; 1842 console.info("rpc read longArray fail, errorCode " + e.code); 1843 console.info("rpc read longArray fail, errorMessage" + e.message); 1844 } 1845 ``` 1846 1847### readLongArray 1848 1849readLongArray(): number[] 1850 1851Reads the long array from this **MessageSequence** object. 1852 1853**System capability**: SystemCapability.Communication.IPC.Core 1854 1855**Return value** 1856 1857| Type | Description | 1858| -------- | ---------------- | 1859| number[] | Long array read.| 1860 1861**Error codes** 1862 1863For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1864 1865| ID| Error Message| 1866| -------- | -------- | 1867| 1900010 | read data from message sequence failed | 1868 1869**Example** 1870 1871 ```ts 1872 import { BusinessError } from '@ohos.base'; 1873 1874 let data = rpc.MessageSequence.create(); 1875 try { 1876 data.writeLongArray([1111, 1112, 1113]); 1877 } catch(error) { 1878 let e: BusinessError = error as BusinessError; 1879 console.info("rpc write longArray fail, errorCode " + e.code); 1880 console.info("rpc write longArray fail, errorMessage" + e.message); 1881 } 1882 try { 1883 let array = data.readLongArray(); 1884 console.log("RpcClient: readLongArray is " + array); 1885 } catch(error) { 1886 let e: BusinessError = error as BusinessError; 1887 console.info("rpc read longArray fail, errorCode " + e.code); 1888 console.info("rpc read longArray fail, errorMessage" + e.message); 1889 } 1890 ``` 1891 1892### writeFloatArray 1893 1894writeFloatArray(floatArray: number[]): void 1895 1896Writes a floating-point array to this **MessageSequence** object. 1897 1898**System capability**: SystemCapability.Communication.IPC.Core 1899 1900**Parameters** 1901 1902| Name | Type | Mandatory| Description | 1903| ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 1904| floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1905 1906**Error codes** 1907 1908For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1909 1910| ID| Error Message| 1911| -------- | -------- | 1912| 1900009 | write data to message sequence failed | 1913 1914**Example** 1915 1916 ```ts 1917 import { BusinessError } from '@ohos.base'; 1918 1919 let data = rpc.MessageSequence.create(); 1920 try { 1921 data.writeFloatArray([1.2, 1.3, 1.4]); 1922 } catch(error) { 1923 let e: BusinessError = error as BusinessError; 1924 console.info("rpc write floatArray fail, errorCode " + e.code); 1925 console.info("rpc write floatArray fail, errorMessage" + e.message); 1926 } 1927 ``` 1928 1929### readFloatArray 1930 1931readFloatArray(dataIn: number[]): void 1932 1933Reads a floating-point 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 | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 1942 1943**Error codes** 1944 1945For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1946 1947| ID| Error Message| 1948| -------- | -------- | 1949| 1900010 | read data from message sequence failed | 1950 1951**Example** 1952 1953 ```ts 1954 import { BusinessError } from '@ohos.base'; 1955 1956 let data = rpc.MessageSequence.create(); 1957 try { 1958 data.writeFloatArray([1.2, 1.3, 1.4]); 1959 }catch(error){ 1960 let e: BusinessError = error as BusinessError; 1961 console.info("rpc write floatArray fail, errorCode " + e.code); 1962 console.info("rpc write floatArray fail, errorMessage" + e.message); 1963 } 1964 let array: Array<number> = new Array(3); 1965 try { 1966 data.readFloatArray(array); 1967 } catch(error) { 1968 let e: BusinessError = error as BusinessError; 1969 console.info("rpc read floatArray fail, errorCode " + e.code); 1970 console.info("rpc read floatArray fail, errorMessage" + e.message); 1971 } 1972 ``` 1973 1974### readFloatArray 1975 1976readFloatArray(): number[] 1977 1978Reads the floating-point array from this **MessageSequence** object. 1979 1980**System capability**: SystemCapability.Communication.IPC.Core 1981 1982**Return value** 1983 1984| Type | Description | 1985| -------- | -------------- | 1986| number[] | Floating-point array read.| 1987 1988**Error codes** 1989 1990For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 1991 1992| ID| Error Message| 1993| -------- | -------- | 1994| 1900010 | read data from message sequence failed | 1995 1996**Example** 1997 1998 ```ts 1999 import { BusinessError } from '@ohos.base'; 2000 2001 let data = rpc.MessageSequence.create(); 2002 try { 2003 data.writeFloatArray([1.2, 1.3, 1.4]); 2004 } catch(error) { 2005 let e: BusinessError = error as BusinessError; 2006 console.info("rpc write floatArray fail, errorCode " + e.code); 2007 console.info("rpc write floatArray fail, errorMessage" + e.message); 2008 } 2009 try { 2010 let array = data.readFloatArray(); 2011 console.log("RpcClient: readFloatArray is " + array); 2012 } catch(error) { 2013 let e: BusinessError = error as BusinessError; 2014 console.info("rpc read floatArray fail, errorCode " + e.code); 2015 console.info("rpc read floatArray fail, errorMessage" + e.message); 2016 } 2017 ``` 2018 2019### writeDoubleArray 2020 2021writeDoubleArray(doubleArray: number[]): void 2022 2023Writes a double-precision floating-point array to this **MessageSequence** object. 2024 2025**System capability**: SystemCapability.Communication.IPC.Core 2026 2027**Parameters** 2028 2029| Name | Type | Mandatory| Description | 2030| ----------- | -------- | ---- | ------------------------ | 2031| doubleArray | number[] | Yes | Double-precision floating-point array to write.| 2032 2033**Error codes** 2034 2035For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2036 2037| ID| Error Message| 2038| -------- | -------- | 2039| 1900009 | write data to message sequence failed | 2040 2041**Example** 2042 2043 ```ts 2044 import { BusinessError } from '@ohos.base'; 2045 2046 let data = rpc.MessageSequence.create(); 2047 try { 2048 data.writeDoubleArray([11.1, 12.2, 13.3]); 2049 } catch(error) { 2050 let e: BusinessError = error as BusinessError; 2051 console.info("rpc write doubleArray fail, errorCode " + e.code); 2052 console.info("rpc write doubleArray fail, errorMessage" + e.message); 2053 } 2054 ``` 2055 2056### readDoubleArray 2057 2058readDoubleArray(dataIn: number[]): void 2059 2060Reads a double-precision floating-point array from this **MessageSequence** object. 2061 2062**System capability**: SystemCapability.Communication.IPC.Core 2063 2064**Parameters** 2065 2066| Name| Type | Mandatory| Description | 2067| ------ | -------- | ---- | ------------------------ | 2068| dataIn | number[] | Yes | Double-precision floating-point array to read.| 2069 2070**Error codes** 2071 2072For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2073 2074| ID| Error Message| 2075| -------- | -------- | 2076| 1900010 | read data from message sequence failed | 2077 2078**Example** 2079 2080 ```ts 2081 import { BusinessError } from '@ohos.base'; 2082 2083 let data = rpc.MessageSequence.create(); 2084 try { 2085 data.writeDoubleArray([11.1, 12.2, 13.3]); 2086 } catch(error) { 2087 let e: BusinessError = error as BusinessError; 2088 console.info("rpc write doubleArray fail, errorCode " + e.code); 2089 console.info("rpc write doubleArray fail, errorMessage" + e.message); 2090 } 2091 let array: Array<number> = new Array(3); 2092 try { 2093 data.readDoubleArray(array); 2094 } catch(error) { 2095 let e: BusinessError = error as BusinessError; 2096 console.info("rpc read doubleArray fail, errorCode " + e.code); 2097 console.info("rpc read doubleArray fail, errorMessage" + e.message); 2098 } 2099 ``` 2100 2101### readDoubleArray 2102 2103readDoubleArray(): number[] 2104 2105Reads the double-precision floating-point array from this **MessageSequence** object. 2106 2107**System capability**: SystemCapability.Communication.IPC.Core 2108 2109**Return value** 2110 2111| Type | Description | 2112| -------- | -------------------- | 2113| number[] | Double-precision floating-point array read.| 2114 2115**Error codes** 2116 2117For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2118 2119| ID| Error Message| 2120| -------- | -------- | 2121| 1900010 | read data from message sequence failed | 2122 2123**Example** 2124 2125 ```ts 2126 import { BusinessError } from '@ohos.base'; 2127 2128 let data = rpc.MessageSequence.create(); 2129 try { 2130 data.writeDoubleArray([11.1, 12.2, 13.3]); 2131 } catch(error) { 2132 let e: BusinessError = error as BusinessError; 2133 console.info("rpc write doubleArray fail, errorCode " + e.code); 2134 console.info("rpc write doubleArray fail, errorMessage" + e.message); 2135 } 2136 try { 2137 let array = data.readDoubleArray(); 2138 console.log("RpcClient: readDoubleArray is " + array); 2139 } catch(error) { 2140 let e: BusinessError = error as BusinessError; 2141 console.info("rpc read doubleArray fail, errorCode " + e.code); 2142 console.info("rpc read doubleArray fail, errorMessage" + e.message); 2143 } 2144 ``` 2145 2146### writeBooleanArray 2147 2148writeBooleanArray(booleanArray: boolean[]): void 2149 2150Writes a Boolean array to this **MessageSequence** object. 2151 2152**System capability**: SystemCapability.Communication.IPC.Core 2153 2154**Parameters** 2155 2156| Name | Type | Mandatory| Description | 2157| ------------ | --------- | ---- | ------------------ | 2158| booleanArray | boolean[] | Yes | Boolean array to write.| 2159 2160**Error codes** 2161 2162For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2163 2164| ID| Error Message| 2165| -------- | -------- | 2166| 1900009 | write data to message sequence failed | 2167 2168**Example** 2169 2170 ```ts 2171 import { BusinessError } from '@ohos.base'; 2172 2173 let data = rpc.MessageSequence.create(); 2174 try { 2175 data.writeBooleanArray([false, true, false]); 2176 } catch(error) { 2177 let e: BusinessError = error as BusinessError; 2178 console.info("rpc write booleanArray fail, errorCode " + e.code); 2179 console.info("rpc write booleanArray fail, errorMessage" + e.message); 2180 } 2181 ``` 2182 2183### readBooleanArray 2184 2185readBooleanArray(dataIn: boolean[]): void 2186 2187Reads a Boolean array from this **MessageSequence** object. 2188 2189**System capability**: SystemCapability.Communication.IPC.Core 2190 2191**Parameters** 2192 2193| Name| Type | Mandatory| Description | 2194| ------ | --------- | ---- | ------------------ | 2195| dataIn | boolean[] | Yes | Boolean array to read.| 2196 2197**Error codes** 2198 2199For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2200 2201| ID| Error Message| 2202| -------- | -------- | 2203| 1900010 | read data from message sequence failed | 2204 2205**Example** 2206 2207 ```ts 2208 import { BusinessError } from '@ohos.base'; 2209 2210 let data = rpc.MessageSequence.create(); 2211 try { 2212 data.writeBooleanArray([false, true, false]); 2213 } catch(error) { 2214 let e: BusinessError = error as BusinessError; 2215 console.info("rpc write booleanArray fail, errorCode " + e.code); 2216 console.info("rpc write booleanArray fail, errorMessage" + e.message); 2217 } 2218 let array: Array<boolean> = new Array(3); 2219 try { 2220 data.readBooleanArray(array); 2221 } catch(error) { 2222 let e: BusinessError = error as BusinessError; 2223 console.info("rpc read booleanArray fail, errorCode " + e.code); 2224 console.info("rpc read booleanArray fail, errorMessage" + e.message); 2225 } 2226 ``` 2227 2228### readBooleanArray 2229 2230readBooleanArray(): boolean[] 2231 2232Reads the Boolean array from this **MessageSequence** object. 2233 2234**System capability**: SystemCapability.Communication.IPC.Core 2235 2236**Return value** 2237 2238| Type | Description | 2239| --------- | -------------- | 2240| boolean[] | Boolean array read.| 2241 2242**Error codes** 2243 2244For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2245 2246| ID| Error Message| 2247| -------- | -------- | 2248| 1900010 | read data from message sequence failed | 2249 2250**Example** 2251 2252 ```ts 2253 import { BusinessError } from '@ohos.base'; 2254 2255 let data = rpc.MessageSequence.create(); 2256 try { 2257 data.writeBooleanArray([false, true, false]); 2258 } catch(error) { 2259 let e: BusinessError = error as BusinessError; 2260 console.info("rpc write booleanArray fail, errorCode " + e.code); 2261 console.info("rpc write booleanArray fail, errorMessage" + e.message); 2262 } 2263 try { 2264 let array = data.readBooleanArray(); 2265 console.log("RpcClient: readBooleanArray is " + array); 2266 } catch(error) { 2267 let e: BusinessError = error as BusinessError; 2268 console.info("rpc read booleanArray fail, errorCode " + e.code); 2269 console.info("rpc read booleanArray fail, errorMessage" + e.message); 2270 } 2271 ``` 2272 2273### writeCharArray 2274 2275writeCharArray(charArray: number[]): void 2276 2277Writes a character array to this **MessageSequence** object. 2278 2279**System capability**: SystemCapability.Communication.IPC.Core 2280 2281**Parameters** 2282 2283| Name | Type | Mandatory| Description | 2284| --------- | -------- | ---- | ---------------------- | 2285| charArray | number[] | Yes | Character array to write.| 2286 2287**Error codes** 2288 2289For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2290 2291| ID| Error Message| 2292| -------- | -------- | 2293| 1900009 | write data to message sequence failed | 2294 2295**Example** 2296 2297 ```ts 2298 import { BusinessError } from '@ohos.base'; 2299 2300 let data = rpc.MessageSequence.create(); 2301 try { 2302 data.writeCharArray([97, 98, 88]); 2303 } catch(error) { 2304 let e: BusinessError = error as BusinessError; 2305 console.info("rpc write charArray fail, errorCode " + e.code); 2306 console.info("rpc write charArray fail, errorMessage" + e.message); 2307 } 2308 ``` 2309 2310### readCharArray 2311 2312readCharArray(dataIn: number[]): void 2313 2314Reads a character array from this **MessageSequence** object. 2315 2316**System capability**: SystemCapability.Communication.IPC.Core 2317 2318**Parameters** 2319 2320| Name| Type | Mandatory| Description | 2321| ------ | -------- | ---- | ---------------------- | 2322| dataIn | number[] | Yes | Character array to read.| 2323 2324**Error codes** 2325 2326For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2327 2328| ID| Error Message| 2329| -------- | -------- | 2330| 1900010 | read data from message sequence failed | 2331 2332**Example** 2333 2334 ```ts 2335 import { BusinessError } from '@ohos.base'; 2336 2337 let data = rpc.MessageSequence.create(); 2338 try { 2339 data.writeCharArray([97, 98, 88]); 2340 } catch(error) { 2341 let e: BusinessError = error as BusinessError; 2342 console.info("rpc write charArray fail, errorCode " + e.code); 2343 console.info("rpc write charArray fail, errorMessage" + e.message); 2344 } 2345 let array: Array<number> = new Array(3); 2346 try { 2347 data.readCharArray(array); 2348 } catch(error) { 2349 let e: BusinessError = error as BusinessError; 2350 console.info("rpc read charArray fail, errorCode " + e.code); 2351 console.info("rpc read charArray fail, errorMessage" + e.message); 2352 } 2353 ``` 2354 2355### readCharArray 2356 2357readCharArray(): number[] 2358 2359Reads the character array from this **MessageSequence** object. 2360 2361**System capability**: SystemCapability.Communication.IPC.Core 2362 2363**Return value** 2364 2365| Type | Description | 2366| -------- | ------------------ | 2367| number[] | Character array read.| 2368 2369**Error codes** 2370 2371For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2372 2373| ID| Error Message| 2374| -------- | -------- | 2375| 1900010 | read data from message sequence failed | 2376 2377**Example** 2378 2379 ```ts 2380 import { BusinessError } from '@ohos.base'; 2381 2382 let data = rpc.MessageSequence.create(); 2383 try { 2384 data.writeCharArray([97, 98, 88]); 2385 } catch(error) { 2386 let e: BusinessError = error as BusinessError; 2387 console.info("rpc write charArray fail, errorCode " + e.code); 2388 console.info("rpc write charArray fail, errorMessage" + e.message); 2389 } 2390 try { 2391 let array = data.readCharArray(); 2392 console.log("RpcClient: readCharArray is " + array); 2393 } catch(error) { 2394 let e: BusinessError = error as BusinessError; 2395 console.info("rpc read charArray fail, errorCode " + e.code); 2396 console.info("rpc read charArray fail, errorMessage" + e.message); 2397 } 2398 ``` 2399 2400### writeStringArray 2401 2402writeStringArray(stringArray: string[]): void 2403 2404Writes a string array to this **MessageSequence** object. 2405 2406**System capability**: SystemCapability.Communication.IPC.Core 2407 2408**Parameters** 2409 2410| Name | Type | Mandatory| Description | 2411| ----------- | -------- | ---- | ------------------------------------------------------- | 2412| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2413 2414**Error codes** 2415 2416For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2417 2418| ID| Error Message| 2419| -------- | -------- | 2420| 1900009 | write data to message sequence failed | 2421 2422**Example** 2423 2424 ```ts 2425 import { BusinessError } from '@ohos.base'; 2426 2427 let data = rpc.MessageSequence.create(); 2428 try { 2429 data.writeStringArray(["abc", "def"]); 2430 } catch(error) { 2431 let e: BusinessError = error as BusinessError; 2432 console.info("rpc write stringArray fail, errorCode " + e.code); 2433 console.info("rpc write stringArray fail, errorMessage" + e.message); 2434 } 2435 ``` 2436 2437### readStringArray 2438 2439readStringArray(dataIn: string[]): void 2440 2441Reads a string array from this **MessageSequence** object. 2442 2443**System capability**: SystemCapability.Communication.IPC.Core 2444 2445**Parameters** 2446 2447| Name| Type | Mandatory| Description | 2448| ------ | -------- | ---- | -------------------- | 2449| dataIn | string[] | Yes | String array to read.| 2450 2451**Error codes** 2452 2453For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2454 2455| ID| Error Message| 2456| -------- | -------- | 2457| 1900010 | read data from message sequence failed | 2458 2459**Example** 2460 2461 ```ts 2462 import { BusinessError } from '@ohos.base'; 2463 2464 let data = rpc.MessageSequence.create(); 2465 try { 2466 data.writeStringArray(["abc", "def"]); 2467 } catch(error) { 2468 let e: BusinessError = error as BusinessError; 2469 console.info("rpc write stringArray fail, errorCode " + e.code); 2470 console.info("rpc write stringArray fail, errorMessage" + e.message); 2471 } 2472 let array: Array<string> = new Array(2); 2473 try { 2474 data.readStringArray(array); 2475 } catch(error) { 2476 let e: BusinessError = error as BusinessError; 2477 console.info("rpc read stringArray fail, errorCode " + e.code); 2478 console.info("rpc read stringArray fail, errorMessage" + e.message); 2479 } 2480 ``` 2481 2482### readStringArray 2483 2484readStringArray(): string[] 2485 2486Reads the string array from this **MessageSequence** object. 2487 2488**System capability**: SystemCapability.Communication.IPC.Core 2489 2490**Return value** 2491 2492| Type | Description | 2493| -------- | ---------------- | 2494| string[] | String array read.| 2495 2496**Error codes** 2497 2498For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2499 2500| ID| Error Message| 2501| -------- | -------- | 2502| 1900010 | read data from message sequence failed | 2503 2504**Example** 2505 2506 ```ts 2507 import { BusinessError } from '@ohos.base'; 2508 2509 let data = rpc.MessageSequence.create(); 2510 try { 2511 data.writeStringArray(["abc", "def"]); 2512 } catch(error) { 2513 let e: BusinessError = error as BusinessError; 2514 console.info("rpc write stringArray fail, errorCode " + e.code); 2515 console.info("rpc write stringArray fail, errorMessage" + e.message); 2516 } 2517 try { 2518 let array = data.readStringArray(); 2519 console.log("RpcClient: readStringArray is " + array); 2520 } catch(error) { 2521 let e: BusinessError = error as BusinessError; 2522 console.info("rpc read stringArray fail, errorCode " + e.code); 2523 console.info("rpc read stringArray fail, errorMessage" + e.message); 2524 } 2525 ``` 2526 2527### writeNoException 2528 2529writeNoException(): void 2530 2531Writes information to this **MessageSequence** object indicating that no exception occurred. 2532 2533**System capability**: SystemCapability.Communication.IPC.Core 2534 2535**Error codes** 2536 2537For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2538 2539| ID| Error Message| 2540| -------- | -------- | 2541| 1900009 | write data to message sequence failed | 2542 2543**Example** 2544 2545 ```ts 2546 import { BusinessError } from '@ohos.base'; 2547 2548 class TestRemoteObject extends rpc.RemoteObject { 2549 constructor(descriptor: string) { 2550 super(descriptor); 2551 } 2552 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2553 if (code === 1) { 2554 console.log("RpcServer: onRemoteMessageRequest called"); 2555 try { 2556 reply.writeNoException(); 2557 } catch(error) { 2558 let e: BusinessError = error as BusinessError; 2559 console.info("rpc write no exception fail, errorCode " + e.code); 2560 console.info("rpc write no exception fail, errorMessage" + e.message); 2561 } 2562 return true; 2563 } else { 2564 console.log("RpcServer: unknown code: " + code); 2565 return false; 2566 } 2567 } 2568 } 2569 ``` 2570 2571### readException 2572 2573readException(): void 2574 2575Reads the exception information from this **MessageSequence** object. 2576 2577**System capability**: SystemCapability.Communication.IPC.Core 2578 2579**Error codes** 2580 2581For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2582 2583| ID| Error Message| 2584| -------- | -------- | 2585| 1900010 | read data from message sequence failed | 2586 2587**Example** 2588 2589 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 2590 2591 ```ts 2592 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 2593 // import FA from "@ohos.ability.featureAbility"; 2594 import Want from '@ohos.app.ability.Want'; 2595 import common from '@ohos.app.ability.common'; 2596 2597 let proxy: rpc.IRemoteObject | undefined = undefined; 2598 let connect: common.ConnectOptions = { 2599 onConnect: (elementName, remoteProxy) => { 2600 console.log("RpcClient: js onConnect called."); 2601 proxy = remoteProxy; 2602 }, 2603 onDisconnect: (elementName) => { 2604 console.log("RpcClient: onDisconnect"); 2605 }, 2606 onFailed: () => { 2607 console.log("RpcClient: onFailed"); 2608 } 2609 }; 2610 let want: Want = { 2611 bundleName: "com.ohos.server", 2612 abilityName: "com.ohos.server.EntryAbility", 2613 }; 2614 2615 // Use this method to connect to the ability for the FA model. 2616 // FA.connectAbility(want,connect); 2617 2618 this.context.connectServiceExtensionAbility(want, connect); 2619 ``` 2620 2621 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. 2622 2623 ```ts 2624 import { BusinessError } from '@ohos.base'; 2625 2626 let option = new rpc.MessageOption(); 2627 let data = rpc.MessageSequence.create(); 2628 let reply = rpc.MessageSequence.create(); 2629 data.writeInt(1); 2630 data.writeString("hello"); 2631 proxy.sendMessageRequest(1, data, reply, option) 2632 .then((result: rpc.RequestResult) => { 2633 if (result.errCode === 0) { 2634 console.log("sendMessageRequest got result"); 2635 try { 2636 result.reply.readException(); 2637 } catch(error) { 2638 let e: BusinessError = error as BusinessError; 2639 console.info("rpc read exception fail, errorCode " + e.code); 2640 console.info("rpc read no exception fail, errorMessage" + e.message); 2641 } 2642 let msg = result.reply.readString(); 2643 console.log("RPCTest: reply msg: " + msg); 2644 } else { 2645 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 2646 } 2647 }).catch((e: Error) => { 2648 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 2649 }).finally (() => { 2650 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 2651 data.reclaim(); 2652 reply.reclaim(); 2653 }); 2654 ``` 2655 2656### writeParcelableArray 2657 2658writeParcelableArray(parcelableArray: Parcelable[]): void 2659 2660Writes a **Parcelable** array to this **MessageSequence** object. 2661 2662**System capability**: SystemCapability.Communication.IPC.Core 2663 2664**Parameters** 2665 2666| Name | Type | Mandatory| Description | 2667| --------------- | ------------ | ---- | -------------------------- | 2668| parcelableArray | Parcelable[] | Yes | **Parcelable** array to write.| 2669 2670**Error codes** 2671 2672For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2673 2674| ID| Error Message| 2675| -------- | -------- | 2676| 1900009 | write data to message sequence failed | 2677 2678**Example** 2679 2680 ```ts 2681 import { BusinessError } from '@ohos.base'; 2682 2683 class MyParcelable implements rpc.Parcelable { 2684 num: number = 0; 2685 str: string = ''; 2686 constructor(num: number, str: string) { 2687 this.num = num; 2688 this.str = str; 2689 } 2690 marshalling(messageSequence: rpc.MessageSequence): boolean { 2691 messageSequence.writeInt(this.num); 2692 messageSequence.writeString(this.str); 2693 return true; 2694 } 2695 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2696 this.num = messageSequence.readInt(); 2697 this.str = messageSequence.readString(); 2698 return true; 2699 } 2700 } 2701 let parcelable = new MyParcelable(1, "aaa"); 2702 let parcelable2 = new MyParcelable(2, "bbb"); 2703 let parcelable3 = new MyParcelable(3, "ccc"); 2704 let a = [parcelable, parcelable2, parcelable3]; 2705 let data = rpc.MessageSequence.create(); 2706 try { 2707 data.writeParcelableArray(a); 2708 } catch(error) { 2709 let e: BusinessError = error as BusinessError; 2710 console.info("rpc write parcelable array fail, errorCode " + e.code); 2711 console.info("rpc write parcelable array fail, errorMessage" + e.message); 2712 } 2713 ``` 2714 2715### readParcelableArray 2716 2717readParcelableArray(parcelableArray: Parcelable[]): void 2718 2719Reads a **Parcelable** array from this **MessageSequence** object. 2720 2721**System capability**: SystemCapability.Communication.IPC.Core 2722 2723**Parameters** 2724 2725| Name | Type | Mandatory| Description | 2726| --------------- | ------------ | ---- | -------------------------- | 2727| parcelableArray | Parcelable[] | Yes | **Parcelable** array to read.| 2728 2729**Error codes** 2730 2731For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2732 2733| ID| Error Message| 2734| -------- | -------- | 2735| 1900010 | read data from message sequence failed | 2736| 1900012 | call js callback function failed | 2737 2738**Example** 2739 2740 ```ts 2741 import { BusinessError } from '@ohos.base'; 2742 2743 class MyParcelable implements rpc.Parcelable { 2744 num: number = 0; 2745 str: string = ''; 2746 constructor(num: number, str: string) { 2747 this.num = num; 2748 this.str = str; 2749 } 2750 marshalling(messageSequence: rpc.MessageSequence): boolean { 2751 messageSequence.writeInt(this.num); 2752 messageSequence.writeString(this.str); 2753 return true; 2754 } 2755 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2756 this.num = messageSequence.readInt(); 2757 this.str = messageSequence.readString(); 2758 return true; 2759 } 2760 } 2761 let parcelable = new MyParcelable(1, "aaa"); 2762 let parcelable2 = new MyParcelable(2, "bbb"); 2763 let parcelable3 = new MyParcelable(3, "ccc"); 2764 let a = [parcelable, parcelable2, parcelable3]; 2765 let data = rpc.MessageSequence.create(); 2766 let result = data.writeParcelableArray(a); 2767 console.log("RpcClient: writeParcelableArray is " + result); 2768 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2769 try { 2770 data.readParcelableArray(b); 2771 } catch(error) { 2772 let e: BusinessError = error as BusinessError; 2773 console.info("rpc read parcelable array fail, errorCode " + e.code); 2774 console.info("rpc read parcelable array fail, errorMessage" + e.message); 2775 } 2776 ``` 2777 2778### writeRemoteObjectArray 2779 2780writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2781 2782Writes an array of **IRemoteObject** objects to this **MessageSequence** object. 2783 2784**System capability**: SystemCapability.Communication.IPC.Core 2785 2786**Parameters** 2787 2788| Name | Type | Mandatory| Description | 2789| ----------- | --------------- | ---- | ---------------------------------------------- | 2790| objectArray | IRemoteObject[] | Yes | Array of **IRemoteObject** objects to write.| 2791 2792**Error codes** 2793 2794For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2795 2796| ID| Error Message| 2797| -------- | -------- | 2798| 1900009 | write data to message sequence failed | 2799 2800**Example** 2801 2802 ```ts 2803 import { BusinessError } from '@ohos.base'; 2804 2805 class TestRemoteObject extends rpc.RemoteObject { 2806 constructor(descriptor: string) { 2807 super(descriptor); 2808 this.modifyLocalInterface(this, descriptor); 2809 } 2810 2811 asObject(): rpc.IRemoteObject { 2812 return this; 2813 } 2814 } 2815 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2816 let data = rpc.MessageSequence.create(); 2817 try { 2818 let result = data.writeRemoteObjectArray(a); 2819 console.log("RpcClient: writeRemoteObjectArray is " + result); 2820 } catch(error) { 2821 let e: BusinessError = error as BusinessError; 2822 console.info("rpc write remote object array fail, errorCode " + e.code); 2823 console.info("rpc write remote object array fail, errorMessage" + e.message); 2824 } 2825 ``` 2826 2827### readRemoteObjectArray 2828 2829readRemoteObjectArray(objects: IRemoteObject[]): void 2830 2831Reads an array of **IRemoteObject** objects from this **MessageSequence** object. 2832 2833**System capability**: SystemCapability.Communication.IPC.Core 2834 2835**Parameters** 2836 2837| Name | Type | Mandatory| Description | 2838| ------- | --------------- | ---- | ---------------------------------------------- | 2839| objects | IRemoteObject[] | Yes | **IRemoteObject** array to read.| 2840 2841**Error codes** 2842 2843For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2844 2845| ID| Error Message| 2846| -------- | -------- | 2847| 1900010 | read data from message sequence failed | 2848 2849**Example** 2850 2851 ```ts 2852 import { BusinessError } from '@ohos.base'; 2853 2854 class TestRemoteObject extends rpc.RemoteObject { 2855 constructor(descriptor: string) { 2856 super(descriptor); 2857 this.modifyLocalInterface(this, descriptor); 2858 } 2859 2860 asObject(): rpc.IRemoteObject { 2861 return this; 2862 } 2863 } 2864 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2865 let data = rpc.MessageSequence.create(); 2866 data.writeRemoteObjectArray(a); 2867 let b: Array<rpc.IRemoteObject> = new Array(3); 2868 try { 2869 data.readRemoteObjectArray(b); 2870 } catch(error) { 2871 let e: BusinessError = error as BusinessError; 2872 console.info("rpc read remote object array fail, errorCode " + e.code); 2873 console.info("rpc read remote object array fail, errorMessage" + e.message); 2874 } 2875 ``` 2876 2877### readRemoteObjectArray 2878 2879readRemoteObjectArray(): IRemoteObject[] 2880 2881Reads the **IRemoteObject** object array from this **MessageSequence** object. 2882 2883**System capability**: SystemCapability.Communication.IPC.Core 2884 2885**Return value** 2886 2887| Type | Description | 2888| --------------- | --------------------------- | 2889| IRemoteObject[] | **IRemoteObject** object array read.| 2890 2891**Error codes** 2892 2893For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2894 2895| ID| Error Message| 2896| -------- | -------- | 2897| 1900010 | read data from message sequence failed | 2898 2899**Example** 2900 2901 ```ts 2902 import { BusinessError } from '@ohos.base'; 2903 2904 class TestRemoteObject extends rpc.RemoteObject { 2905 constructor(descriptor: string) { 2906 super(descriptor); 2907 this.modifyLocalInterface(this, descriptor); 2908 } 2909 2910 asObject(): rpc.IRemoteObject { 2911 return this; 2912 } 2913 } 2914 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2915 let data = rpc.MessageSequence.create(); 2916 data.writeRemoteObjectArray(a); 2917 try { 2918 let b = data.readRemoteObjectArray(); 2919 console.log("RpcClient: readRemoteObjectArray is " + b); 2920 } catch(error) { 2921 let e: BusinessError = error as BusinessError; 2922 console.info("rpc read remote object array fail, errorCode " + e.code); 2923 console.info("rpc read remote object array fail, errorMessage" + e.message); 2924 } 2925 ``` 2926 2927### closeFileDescriptor<sup>9+</sup> 2928 2929static closeFileDescriptor(fd: number): void 2930 2931Closes a file descriptor. This API is a static method. 2932 2933**System capability**: SystemCapability.Communication.IPC.Core 2934 2935**Parameters** 2936 2937| Name| Type | Mandatory| Description | 2938| ------ | ------ | ---- | -------------------- | 2939| fd | number | Yes | File descriptor to close.| 2940 2941**Example** 2942 2943 ```ts 2944 import fs from '@ohos.file.fs'; 2945 import { BusinessError } from '@ohos.base'; 2946 2947 let filePath = "path/to/file"; 2948 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2949 try { 2950 rpc.MessageSequence.closeFileDescriptor(file.fd); 2951 } catch(error) { 2952 let e: BusinessError = error as BusinessError; 2953 console.info("rpc close file descriptor fail, errorCode " + e.code); 2954 console.info("rpc close file descriptor fail, errorMessage" + e.message); 2955 } 2956 ``` 2957 2958### dupFileDescriptor 2959 2960static dupFileDescriptor(fd: number) :number 2961 2962Duplicates a file descriptor. This API is a static method. 2963 2964**System capability**: SystemCapability.Communication.IPC.Core 2965 2966**Parameters** 2967 2968| Name| Type | Mandatory| Description | 2969| ------ | ------ | ---- | ------------------------ | 2970| fd | number | Yes | File descriptor to duplicate.| 2971 2972**Return value** 2973 2974| Type | Description | 2975| ------ | -------------------- | 2976| number | New file descriptor.| 2977 2978**Error codes** 2979 2980For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 2981 2982| ID| Error Message| 2983| -------- | -------- | 2984| 1900013 | call os dup function failed | 2985 2986**Example** 2987 2988 ```ts 2989 import fs from '@ohos.file.fs'; 2990 import { BusinessError } from '@ohos.base'; 2991 2992 let filePath = "path/to/file"; 2993 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2994 try { 2995 rpc.MessageSequence.dupFileDescriptor(file.fd); 2996 } catch(error) { 2997 let e: BusinessError = error as BusinessError; 2998 console.info("rpc dup file descriptor fail, errorCode " + e.code); 2999 console.info("rpc dup file descriptor fail, errorMessage" + e.message); 3000 } 3001 ``` 3002 3003### containFileDescriptors 3004 3005containFileDescriptors(): boolean 3006 3007Checks whether this **MessageSequence** object contains file descriptors. 3008 3009**System capability**: SystemCapability.Communication.IPC.Core 3010 3011**Return value** 3012 3013| Type | Description | 3014| ------- | -------------------------------------------------------------------- | 3015| boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 3016 3017**Example** 3018 3019 ```ts 3020 import fs from '@ohos.file.fs'; 3021 import { BusinessError } from '@ohos.base'; 3022 3023 let sequence = new rpc.MessageSequence(); 3024 let filePath = "path/to/file"; 3025 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3026 try { 3027 sequence.writeFileDescriptor(file.fd); 3028 } catch(error) { 3029 let e: BusinessError = error as BusinessError; 3030 console.info("rpc write file descriptor fail, errorCode " + e.code); 3031 console.info("rpc write file descriptor fail, errorMessage" + e.message); 3032 } 3033 try { 3034 let containFD = sequence.containFileDescriptors(); 3035 console.log("RpcTest: sequence after write fd containFd result is : " + containFD); 3036 } catch(error) { 3037 let e: BusinessError = error as BusinessError; 3038 console.info("rpc contain file descriptor fail, errorCode " + e.code); 3039 console.info("rpc contain file descriptor fail, errorMessage" + e.message); 3040 } 3041 ``` 3042 3043### writeFileDescriptor 3044 3045writeFileDescriptor(fd: number): void 3046 3047Writes a file descriptor to this **MessageSequence** object. 3048 3049**System capability**: SystemCapability.Communication.IPC.Core 3050 3051**Parameters** 3052 3053| Name| Type | Mandatory| Description | 3054| ------ | ------ | ---- | ------------ | 3055| fd | number | Yes | File descriptor to write.| 3056 3057**Error codes** 3058 3059For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3060 3061| ID| Error Message| 3062| -------- | -------- | 3063| 1900009 | write data to message sequence failed | 3064 3065**Example** 3066 3067 ```ts 3068 import fs from '@ohos.file.fs'; 3069 import { BusinessError } from '@ohos.base'; 3070 3071 let sequence = new rpc.MessageSequence(); 3072 let filePath = "path/to/file"; 3073 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3074 try { 3075 sequence.writeFileDescriptor(file.fd); 3076 } catch(error) { 3077 let e: BusinessError = error as BusinessError; 3078 console.info("rpc write file descriptor fail, errorCode " + e.code); 3079 console.info("rpc write file descriptor fail, errorMessage" + e.message); 3080 } 3081 ``` 3082 3083### readFileDescriptor 3084 3085readFileDescriptor(): number 3086 3087Reads the file descriptor from this **MessageSequence** object. 3088 3089**System capability**: SystemCapability.Communication.IPC.Core 3090 3091**Return value** 3092 3093| Type | Description | 3094| ------ | ---------------- | 3095| number | File descriptor read.| 3096 3097**Error codes** 3098 3099For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3100 3101| ID| Error Message| 3102| -------- | -------- | 3103| 1900010 | read data from message sequence failed | 3104 3105**Example** 3106 3107 ```ts 3108 import fs from '@ohos.file.fs'; 3109 import { BusinessError } from '@ohos.base'; 3110 3111 let sequence = new rpc.MessageSequence(); 3112 let filePath = "path/to/file"; 3113 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3114 try { 3115 sequence.writeFileDescriptor(file.fd); 3116 } catch(error) { 3117 let e: BusinessError = error as BusinessError; 3118 console.info("rpc write file descriptor fail, errorCode " + e.code); 3119 console.info("rpc write file descriptor fail, errorMessage" + e.message); 3120 } 3121 try { 3122 let readFD = sequence.readFileDescriptor(); 3123 console.log("RpcClient: readFileDescriptor is: " + readFD); 3124 } catch(error) { 3125 let e: BusinessError = error as BusinessError; 3126 console.info("rpc read file descriptor fail, errorCode " + e.code); 3127 console.info("rpc read file descriptor fail, errorMessage" + e.message); 3128 } 3129 ``` 3130 3131### writeAshmem 3132 3133writeAshmem(ashmem: Ashmem): void 3134 3135Writes an anonymous shared object to this **MessageSequence** object. 3136 3137**System capability**: SystemCapability.Communication.IPC.Core 3138 3139**Parameters** 3140 3141| Name| Type | Mandatory| Description | 3142| ------ | ------ | ---- | ------------------------------------- | 3143| ashmem | Ashmem | Yes | Anonymous shared object to write.| 3144 3145**Error codes** 3146 3147For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3148 3149| ID| Error Message| 3150| -------- | ------- | 3151| 1900003 | write to ashmem failed | 3152 3153**Example** 3154 3155 ```ts 3156 import { BusinessError } from '@ohos.base'; 3157 3158 let sequence = new rpc.MessageSequence(); 3159 let ashmem: rpc.Ashmem | undefined = undefined; 3160 try { 3161 ashmem = rpc.Ashmem.create("ashmem", 1024); 3162 try { 3163 sequence.writeAshmem(ashmem); 3164 } catch(error) { 3165 let e: BusinessError = error as BusinessError; 3166 console.info("rpc write ashmem fail, errorCode " + e.code); 3167 console.info("rpc write ashmem fail, errorMessage" + e.message); 3168 } 3169 } catch(error) { 3170 let e: BusinessError = error as BusinessError; 3171 console.info("rpc create ashmem fail, errorCode " + e.code); 3172 console.info("rpc creat ashmem fail, errorMessage" + e.message); 3173 } 3174 ``` 3175 3176### readAshmem 3177 3178readAshmem(): Ashmem 3179 3180Reads the anonymous shared object from this **MessageSequence** object. 3181 3182**System capability**: SystemCapability.Communication.IPC.Core 3183 3184**Return value** 3185 3186| Type | Description | 3187| ------ | ------------------ | 3188| Ashmem | Anonymous share object obtained.| 3189 3190**Error codes** 3191 3192For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3193 3194| ID| Error Message| 3195| -------- | -------- | 3196| 1900004 | read from ashmem failed | 3197 3198**Example** 3199 3200 ```ts 3201 import { BusinessError } from '@ohos.base'; 3202 3203 let sequence = new rpc.MessageSequence(); 3204 let ashmem: rpc.Ashmem | undefined = undefined; 3205 try { 3206 ashmem = rpc.Ashmem.create("ashmem", 1024); 3207 try { 3208 sequence.writeAshmem(ashmem); 3209 } catch(error) { 3210 let e: BusinessError = error as BusinessError; 3211 console.info("rpc write ashmem fail, errorCode " + e.code); 3212 console.info("rpc write ashmem fail, errorMessage" + e.message); 3213 } 3214 } catch(error) { 3215 let e: BusinessError = error as BusinessError; 3216 console.info("rpc create ashmem fail, errorCode " + e.code); 3217 console.info("rpc creat ashmem fail, errorMessage" + e.message); 3218 } 3219 try { 3220 let readAshmem = sequence.readAshmem(); 3221 console.log("RpcTest: read ashmem to result is : " + readAshmem); 3222 } catch(error) { 3223 let e: BusinessError = error as BusinessError; 3224 console.info("rpc read ashmem fail, errorCode " + e.code); 3225 console.info("rpc read ashmem fail, errorMessage" + e.message); 3226 } 3227 ``` 3228 3229### getRawDataCapacity 3230 3231getRawDataCapacity(): number 3232 3233Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3234 3235**System capability**: SystemCapability.Communication.IPC.Core 3236 3237**Return value** 3238 3239| Type | Description | 3240| ------ | ------------------------------------------------------------ | 3241| number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageSequence** object.| 3242 3243**Example** 3244 3245 ```ts 3246 let sequence = new rpc.MessageSequence(); 3247 let result = sequence.getRawDataCapacity(); 3248 console.log("RpcTest: sequence get RawDataCapacity result is : " + result); 3249 ``` 3250 3251### writeRawData 3252 3253writeRawData(rawData: number[], size: number): void 3254 3255Writes raw data to this **MessageSequence** object. 3256 3257**System capability**: SystemCapability.Communication.IPC.Core 3258 3259**Parameters** 3260 3261| Name | Type | Mandatory| Description | 3262| ------- | -------- | ---- | ---------------------------------- | 3263| rawData | number[] | Yes | Raw data to write. | 3264| size | number | Yes | Size of the raw data, in bytes.| 3265 3266**Error codes** 3267 3268For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3269 3270| ID| Error Message| 3271| -------- | -------- | 3272| 1900009 | write data to message sequence failed | 3273 3274**Example** 3275 3276 ```ts 3277 import { BusinessError } from '@ohos.base'; 3278 3279 let sequence = new rpc.MessageSequence(); 3280 let arr = [1, 2, 3, 4, 5]; 3281 try { 3282 sequence.writeRawData(arr, arr.length); 3283 } catch(error) { 3284 let e: BusinessError = error as BusinessError; 3285 console.info("rpc write rawdata fail, errorCode " + e.code); 3286 console.info("rpc write rawdata fail, errorMessage" + e.message); 3287 } 3288 ``` 3289 3290### readRawData 3291 3292readRawData(size: number): number[] 3293 3294Reads raw data from this **MessageSequence** object. 3295 3296**System capability**: SystemCapability.Communication.IPC.Core 3297 3298**Parameters** 3299 3300| Name| Type | Mandatory| Description | 3301| ------ | ------ | ---- | ------------------------ | 3302| size | number | Yes | Size of the raw data to read.| 3303 3304**Return value** 3305 3306| Type | Description | 3307| -------- | ------------------------------ | 3308| number[] | Raw data obtained, in bytes.| 3309 3310**Error codes** 3311 3312For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 3313 3314| ID| Error Message| 3315| -------- | -------- | 3316| 1900010 | read data from message sequence failed | 3317 3318**Example** 3319 3320 ```ts 3321 import { BusinessError } from '@ohos.base'; 3322 3323 let sequence = new rpc.MessageSequence(); 3324 let arr = [1, 2, 3, 4, 5]; 3325 try { 3326 sequence.writeRawData(arr, arr.length); 3327 } catch(error) { 3328 let e: BusinessError = error as BusinessError; 3329 console.info("rpc write rawdata fail, errorCode " + e.code); 3330 console.info("rpc write rawdata fail, errorMessage" + e.message); 3331 } 3332 try { 3333 let result = sequence.readRawData(5); 3334 console.log("RpcTest: sequence read raw data result is : " + result); 3335 } catch(error) { 3336 let e: BusinessError = error as BusinessError; 3337 console.info("rpc read rawdata fail, errorCode " + e.code); 3338 console.info("rpc read rawdata fail, errorMessage" + e.message); 3339 } 3340 ``` 3341 3342## MessageParcel<sup>(deprecated)</sup> 3343 3344>This class is no longer maintained since API version 9. You are advised to use [MessageSequence](#messagesequence9). 3345 3346Provides APIs for reading and writing data in specific format. 3347During 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. 3348 3349### create 3350 3351static create(): MessageParcel 3352 3353Creates a **MessageParcel** object. This method is a static method. 3354 3355**System capability**: SystemCapability.Communication.IPC.Core 3356 3357**Return value** 3358 3359| Type | Description | 3360| ------------- | ----------------------------- | 3361| MessageParcel | **MessageParcel** object created.| 3362 3363**Example** 3364 3365 ```ts 3366 let data = rpc.MessageParcel.create(); 3367 console.log("RpcClient: data is " + data); 3368 ``` 3369 3370### reclaim 3371 3372reclaim(): void 3373 3374Reclaims the **MessageParcel** object that is no longer used. 3375 3376**System capability**: SystemCapability.Communication.IPC.Core 3377 3378**Example** 3379 3380 ```ts 3381 let reply = rpc.MessageParcel.create(); 3382 reply.reclaim(); 3383 ``` 3384 3385### writeRemoteObject 3386 3387writeRemoteObject(object: [IRemoteObject](#iremoteobject)): boolean 3388 3389Serializes a remote object and writes it to this **MessageParcel** object. 3390 3391**System capability**: SystemCapability.Communication.IPC.Core 3392 3393**Parameters** 3394 3395| Name| Type | Mandatory| Description | 3396| ------ | ------------------------------- | ---- | --------------------------------------- | 3397| object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3398 3399**Return value** 3400 3401| Type | Description | 3402| ------- | ----------------------------------------- | 3403| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3404 3405**Example** 3406 3407 ```ts 3408 class MyDeathRecipient implements rpc.DeathRecipient { 3409 onRemoteDied() { 3410 console.log("server died"); 3411 } 3412 } 3413 class TestRemoteObject extends rpc.RemoteObject { 3414 constructor(descriptor: string) { 3415 super(descriptor); 3416 } 3417 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3418 return true; 3419 } 3420 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3421 return true; 3422 } 3423 isObjectDead(): boolean { 3424 return false; 3425 } 3426 } 3427 let data = rpc.MessageParcel.create(); 3428 let testRemoteObject = new TestRemoteObject("testObject"); 3429 data.writeRemoteObject(testRemoteObject); 3430 ``` 3431 3432### readRemoteObject 3433 3434readRemoteObject(): IRemoteObject 3435 3436Reads 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. 3437 3438**System capability**: SystemCapability.Communication.IPC.Core 3439 3440**Return value** 3441 3442| Type | Description | 3443| ------------------------------- | ------------------ | 3444| [IRemoteObject](#iremoteobject) | Remote object obtained.| 3445 3446**Example** 3447 3448 ```ts 3449 class MyDeathRecipient implements rpc.DeathRecipient { 3450 onRemoteDied() { 3451 console.log("server died"); 3452 } 3453 } 3454 class TestRemoteObject extends rpc.RemoteObject { 3455 constructor(descriptor: string) { 3456 super(descriptor); 3457 } 3458 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3459 return true; 3460 } 3461 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3462 return true; 3463 } 3464 isObjectDead(): boolean { 3465 return false; 3466 } 3467 } 3468 let data = rpc.MessageParcel.create(); 3469 let testRemoteObject = new TestRemoteObject("testObject"); 3470 data.writeRemoteObject(testRemoteObject); 3471 let proxy = data.readRemoteObject(); 3472 console.log("readRemoteObject is " + proxy); 3473 ``` 3474 3475### writeInterfaceToken 3476 3477writeInterfaceToken(token: string): boolean 3478 3479Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3480 3481**System capability**: SystemCapability.Communication.IPC.Core 3482 3483**Parameters** 3484 3485| Name| Type | Mandatory| Description | 3486| ------ | ------ | ---- | ------------------ | 3487| token | string | Yes | Interface token to write.| 3488 3489**Return value** 3490 3491| Type | Description | 3492| ------- | ----------------------------------------- | 3493| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3494 3495**Example** 3496 3497 ```ts 3498 let data = rpc.MessageParcel.create(); 3499 let result = data.writeInterfaceToken("aaa"); 3500 console.log("RpcServer: writeInterfaceToken is " + result); 3501 ``` 3502 3503### readInterfaceToken 3504 3505readInterfaceToken(): string 3506 3507Reads 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. 3508 3509**System capability**: SystemCapability.Communication.IPC.Core 3510 3511**Return value** 3512 3513| Type | Description | 3514| ------ | ------------------------ | 3515| string | Interface token obtained.| 3516 3517**Example** 3518 3519 ```ts 3520 class Stub extends rpc.RemoteObject { 3521 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3522 let interfaceToken = data.readInterfaceToken(); 3523 console.log("RpcServer: interfaceToken is " + interfaceToken); 3524 return true; 3525 } 3526 } 3527 ``` 3528 3529### getSize 3530 3531getSize(): number 3532 3533Obtains the data size of this **MessageParcel** object. 3534 3535**System capability**: SystemCapability.Communication.IPC.Core 3536 3537**Return value** 3538 3539| Type | Description | 3540| ------ | --------------------------------------------- | 3541| number | Size of the **MessageParcel** object obtained, in bytes.| 3542 3543**Example** 3544 3545 ```ts 3546 let data = rpc.MessageParcel.create(); 3547 let size = data.getSize(); 3548 console.log("RpcClient: size is " + size); 3549 ``` 3550 3551### getCapacity 3552 3553getCapacity(): number 3554 3555Obtains the capacity of this **MessageParcel** object. 3556 3557**System capability**: SystemCapability.Communication.IPC.Core 3558 3559**Return value** 3560 3561| Type | Description | 3562| ------ | --------------------------------------------- | 3563| number | **MessageParcel** capacity obtained, in bytes.| 3564 3565**Example** 3566 3567 ```ts 3568 let data = rpc.MessageParcel.create(); 3569 let result = data.getCapacity(); 3570 console.log("RpcClient: capacity is " + result); 3571 ``` 3572 3573### setSize 3574 3575setSize(size: number): boolean 3576 3577Sets the size of data contained in this **MessageParcel** object. 3578 3579**System capability**: SystemCapability.Communication.IPC.Core 3580 3581**Parameters** 3582 3583| Name| Type | Mandatory| Description | 3584| ------ | ------ | ---- | ------------------------------------------- | 3585| size | number | Yes | Data size to set, in bytes.| 3586 3587**Return value** 3588 3589| Type | Description | 3590| ------- | --------------------------------- | 3591| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3592 3593**Example** 3594 3595 ```ts 3596 let data = rpc.MessageParcel.create(); 3597 let setSize = data.setSize(16); 3598 console.log("RpcClient: setSize is " + setSize); 3599 ``` 3600 3601### setCapacity 3602 3603setCapacity(size: number): boolean 3604 3605Sets the storage capacity of this **MessageParcel** object. 3606 3607**System capability**: SystemCapability.Communication.IPC.Core 3608 3609**Parameters** 3610 3611| Name| Type | Mandatory| Description | 3612| ------ | ------ | ---- | ------------------------------------------- | 3613| size | number | Yes | Storage capacity to set, in bytes.| 3614 3615**Return value** 3616 3617| Type | Description | 3618| ------- | --------------------------------- | 3619| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3620 3621**Example** 3622 3623 ```ts 3624 let data = rpc.MessageParcel.create(); 3625 let result = data.setCapacity(100); 3626 console.log("RpcClient: setCapacity is " + result); 3627 ``` 3628 3629### getWritableBytes 3630 3631getWritableBytes(): number 3632 3633Obtains the writable capacity of this **MessageParcel** object. 3634 3635**System capability**: SystemCapability.Communication.IPC.Core 3636 3637**Return value** 3638 3639| Type | Description | 3640| ------ | --------------------------------------------------- | 3641| number | **MessageParcel** writable capacity obtained, in bytes.| 3642 3643**Example** 3644 3645 ```ts 3646 class Stub extends rpc.RemoteObject { 3647 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3648 let getWritableBytes = data.getWritableBytes(); 3649 console.log("RpcServer: getWritableBytes is " + getWritableBytes); 3650 return true; 3651 } 3652 } 3653 ``` 3654 3655### getReadableBytes 3656 3657getReadableBytes(): number 3658 3659Obtains the readable capacity of this **MessageParcel** object. 3660 3661**System capability**: SystemCapability.Communication.IPC.Core 3662 3663**Return value** 3664 3665| Type | Description | 3666| ------ | --------------------------------------------------- | 3667| number | **MessageParcel** object readable capacity, in bytes.| 3668 3669**Example** 3670 3671 ```ts 3672 class Stub extends rpc.RemoteObject { 3673 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3674 let result = data.getReadableBytes(); 3675 console.log("RpcServer: getReadableBytes is " + result); 3676 return true; 3677 } 3678 } 3679 ``` 3680 3681### getReadPosition 3682 3683getReadPosition(): number 3684 3685Obtains the read position of this **MessageParcel** object. 3686 3687**System capability**: SystemCapability.Communication.IPC.Core 3688 3689**Return value** 3690 3691| Type | Description | 3692| ------ | --------------------------------------- | 3693| number | Current read position of the **MessageParcel** object.| 3694 3695**Example** 3696 3697 ```ts 3698 let data = rpc.MessageParcel.create(); 3699 let readPos = data.getReadPosition(); 3700 console.log("RpcClient: readPos is " + readPos); 3701 ``` 3702 3703### getWritePosition 3704 3705getWritePosition(): number 3706 3707Obtains the write position of this **MessageParcel** object. 3708 3709**System capability**: SystemCapability.Communication.IPC.Core 3710 3711**Return value** 3712 3713| Type | Description | 3714| ------ | --------------------------------------- | 3715| number | Current write position of the **MessageParcel** object.| 3716 3717**Example** 3718 3719 ```ts 3720 let data = rpc.MessageParcel.create(); 3721 data.writeInt(10); 3722 let bwPos = data.getWritePosition(); 3723 console.log("RpcClient: bwPos is " + bwPos); 3724 ``` 3725 3726### rewindRead 3727 3728rewindRead(pos: number): boolean 3729 3730Moves the read pointer to the specified position. 3731 3732**System capability**: SystemCapability.Communication.IPC.Core 3733 3734**Parameters** 3735 3736| Name| Type | Mandatory| Description | 3737| ------ | ------ | ---- | ------------------------ | 3738| pos | number | Yes | Position from which data is to read.| 3739 3740**Return value** 3741 3742| Type | Description | 3743| ------- | ------------------------------------------------- | 3744| boolean | Returns **true** if the read position changes; returns **false** otherwise.| 3745 3746**Example** 3747 3748 ```ts 3749 let data = rpc.MessageParcel.create(); 3750 data.writeInt(12); 3751 data.writeString("parcel"); 3752 let number = data.readInt(); 3753 console.log("RpcClient: number is " + number); 3754 data.rewindRead(0); 3755 let number2 = data.readInt(); 3756 console.log("RpcClient: rewindRead is " + number2); 3757 ``` 3758 3759### rewindWrite 3760 3761rewindWrite(pos: number): boolean 3762 3763Moves the write pointer to the specified position. 3764 3765**System capability**: SystemCapability.Communication.IPC.Core 3766 3767**Parameters** 3768 3769| Name| Type | Mandatory| Description | 3770| ------ | ------ | ---- | ------------------------ | 3771| pos | number | Yes | Position from which data is to write.| 3772 3773**Return value** 3774 3775| Type | Description | 3776| ------- | --------------------------------------------- | 3777| boolean | Returns **true** if the write position changes; returns **false** otherwise.| 3778 3779**Example** 3780 3781 ```ts 3782 let data = rpc.MessageParcel.create(); 3783 data.writeInt(4); 3784 data.rewindWrite(0); 3785 data.writeInt(5); 3786 let number = data.readInt(); 3787 console.log("RpcClient: rewindWrite is: " + number); 3788 ``` 3789 3790### writeByte 3791 3792writeByte(val: number): boolean 3793 3794Writes a Byte value to this **MessageParcel** object. 3795 3796**System capability**: SystemCapability.Communication.IPC.Core 3797 3798**Parameters** 3799 3800| Name| Type | Mandatory| Description | 3801| ------ | ------ | ---- | ---------------- | 3802| val | number | Yes | Byte value to write.| 3803 3804**Return value** 3805 3806| Type | Description | 3807| ------- | ----------------------------- | 3808| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3809 3810**Example** 3811 3812 ```ts 3813 let data = rpc.MessageParcel.create(); 3814 let result = data.writeByte(2); 3815 console.log("RpcClient: writeByte is: " + result); 3816 ``` 3817 3818### readByte 3819 3820readByte(): number 3821 3822Reads the Byte value from this **MessageParcel** object. 3823 3824**System capability**: SystemCapability.Communication.IPC.Core 3825 3826**Return value** 3827 3828| Type | Description | 3829| ------ | ------------ | 3830| number | Byte value read.| 3831 3832**Example** 3833 3834 ```ts 3835 let data = rpc.MessageParcel.create(); 3836 let result = data.writeByte(2); 3837 console.log("RpcClient: writeByte is: " + result); 3838 let ret = data.readByte(); 3839 console.log("RpcClient: readByte is: " + ret); 3840 ``` 3841 3842### writeShort 3843 3844writeShort(val: number): boolean 3845 3846Writes a Short int value to this **MessageParcel** object. 3847 3848**System capability**: SystemCapability.Communication.IPC.Core 3849 3850**Parameters** 3851 3852| Name| Type | Mandatory| Description | 3853| ------ | ------ | ---- | ------------------ | 3854| val | number | Yes | Short int value to write.| 3855 3856**Return value** 3857 3858| Type | Description | 3859| ------- | ----------------------------- | 3860| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3861 3862**Example** 3863 3864 ```ts 3865 let data = rpc.MessageParcel.create(); 3866 let result = data.writeShort(8); 3867 console.log("RpcClient: writeShort is: " + result); 3868 ``` 3869 3870### readShort 3871 3872readShort(): number 3873 3874Reads the Short int value from this **MessageParcel** object. 3875 3876**System capability**: SystemCapability.Communication.IPC.Core 3877 3878**Return value** 3879 3880| Type | Description | 3881| ------ | -------------- | 3882| number | Short int value read.| 3883 3884**Example** 3885 3886 ```ts 3887 let data = rpc.MessageParcel.create(); 3888 let result = data.writeShort(8); 3889 console.log("RpcClient: writeShort is: " + result); 3890 let ret = data.readShort(); 3891 console.log("RpcClient: readShort is: " + ret); 3892 ``` 3893 3894### writeInt 3895 3896writeInt(val: number): boolean 3897 3898Writes an Int value to this **MessageParcel** object. 3899 3900**System capability**: SystemCapability.Communication.IPC.Core 3901 3902**Parameters** 3903 3904| Name| Type | Mandatory| Description | 3905| ------ | ------ | ---- | ---------------- | 3906| val | number | Yes | Int value to write.| 3907 3908**Return value** 3909 3910| Type | Description | 3911| ------- | ----------------------------- | 3912| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3913 3914**Example** 3915 3916 ```ts 3917 let data = rpc.MessageParcel.create(); 3918 let result = data.writeInt(10); 3919 console.log("RpcClient: writeInt is " + result); 3920 ``` 3921 3922### readInt 3923 3924readInt(): number 3925 3926Reads the Int value from this **MessageParcel** object. 3927 3928**System capability**: SystemCapability.Communication.IPC.Core 3929 3930**Return value** 3931 3932| Type | Description | 3933| ------ | ------------ | 3934| number | Int value read.| 3935 3936**Example** 3937 3938 ```ts 3939 let data = rpc.MessageParcel.create(); 3940 let result = data.writeInt(10); 3941 console.log("RpcClient: writeInt is " + result); 3942 let ret = data.readInt(); 3943 console.log("RpcClient: readInt is " + ret); 3944 ``` 3945 3946### writeLong 3947 3948writeLong(val: number): boolean 3949 3950Writes a Long int value to this **MessageParcel** object. 3951 3952**System capability**: SystemCapability.Communication.IPC.Core 3953 3954**Parameters** 3955 3956| Name| Type | Mandatory| Description | 3957| ------ | ------ | ---- | ---------------- | 3958| val | number | Yes | Long int value to write.| 3959 3960**Return value** 3961 3962| Type | Description | 3963| ------- | --------------------------------- | 3964| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 3965 3966**Example** 3967 3968 ```ts 3969 let data = rpc.MessageParcel.create(); 3970 let result = data.writeLong(10000); 3971 console.log("RpcClient: writeLong is " + result); 3972 ``` 3973 3974### readLong 3975 3976readLong(): number 3977 3978Reads the Long int value from this **MessageParcel** object. 3979 3980**System capability**: SystemCapability.Communication.IPC.Core 3981 3982**Return value** 3983 3984| Type | Description | 3985| ------ | -------------- | 3986| number | Long int value read.| 3987 3988**Example** 3989 3990 ```ts 3991 let data = rpc.MessageParcel.create(); 3992 let result = data.writeLong(10000); 3993 console.log("RpcClient: writeLong is " + result); 3994 let ret = data.readLong(); 3995 console.log("RpcClient: readLong is " + ret); 3996 ``` 3997 3998### writeFloat 3999 4000writeFloat(val: number): boolean 4001 4002Writes a Float value to this **MessageParcel** object. 4003 4004**System capability**: SystemCapability.Communication.IPC.Core 4005 4006**Parameters** 4007 4008| Name| Type | Mandatory| Description | 4009| ------ | ------ | ---- | ---------------- | 4010| val | number | Yes | Float value to write.| 4011 4012**Return value** 4013 4014| Type | Description | 4015| ------- | --------------------------------- | 4016| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4017 4018**Example** 4019 4020 ```ts 4021 let data = rpc.MessageParcel.create(); 4022 let result = data.writeFloat(1.2); 4023 console.log("RpcClient: writeFloat is " + result); 4024 ``` 4025 4026### readFloat 4027 4028readFloat(): number 4029 4030Reads the Float value from this **MessageParcel** object. 4031 4032**System capability**: SystemCapability.Communication.IPC.Core 4033 4034**Return value** 4035 4036| Type | Description | 4037| ------ | ------------ | 4038| number | Float value read.| 4039 4040**Example** 4041 4042 ```ts 4043 let data = rpc.MessageParcel.create(); 4044 let result = data.writeFloat(1.2); 4045 console.log("RpcClient: writeFloat is " + result); 4046 let ret = data.readFloat(); 4047 console.log("RpcClient: readFloat is " + ret); 4048 ``` 4049 4050### writeDouble 4051 4052writeDouble(val: number): boolean 4053 4054Writes a Double value to this **MessageParcel** object. 4055 4056**System capability**: SystemCapability.Communication.IPC.Core 4057 4058**Parameters** 4059 4060| Name| Type | Mandatory| Description | 4061| ------ | ------ | ---- | ---------------------- | 4062| val | number | Yes | Double value to write.| 4063 4064**Return value** 4065 4066| Type | Description | 4067| ------- | --------------------------------- | 4068| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4069 4070**Example** 4071 4072 ```ts 4073 let data = rpc.MessageParcel.create(); 4074 let result = data.writeDouble(10.2); 4075 console.log("RpcClient: writeDouble is " + result); 4076 ``` 4077 4078### readDouble 4079 4080readDouble(): number 4081 4082Reads the Double value from this **MessageParcel** object. 4083 4084**System capability**: SystemCapability.Communication.IPC.Core 4085 4086**Return value** 4087 4088| Type | Description | 4089| ------ | ------------------ | 4090| number | Double value read.| 4091 4092**Example** 4093 4094 ```ts 4095 let data = rpc.MessageParcel.create(); 4096 let result = data.writeDouble(10.2); 4097 console.log("RpcClient: writeDouble is " + result); 4098 let ret = data.readDouble(); 4099 console.log("RpcClient: readDouble is " + ret); 4100 ``` 4101 4102### writeBoolean 4103 4104writeBoolean(val: boolean): boolean 4105 4106Writes a Boolean value to this **MessageParcel** object. 4107 4108**System capability**: SystemCapability.Communication.IPC.Core 4109 4110**Parameters** 4111 4112| Name| Type | Mandatory| Description | 4113| ------ | ------- | ---- | ---------------- | 4114| val | boolean | Yes | Boolean value to write.| 4115 4116**Return value** 4117 4118| Type | Description | 4119| ------- | --------------------------------- | 4120| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4121 4122**Example** 4123 4124 ```ts 4125 let data = rpc.MessageParcel.create(); 4126 let result = data.writeBoolean(false); 4127 console.log("RpcClient: writeBoolean is " + result); 4128 ``` 4129 4130### readBoolean 4131 4132readBoolean(): boolean 4133 4134Reads the Boolean value from this **MessageParcel** object. 4135 4136**System capability**: SystemCapability.Communication.IPC.Core 4137 4138**Return value** 4139 4140| Type | Description | 4141| ------- | -------------------- | 4142| boolean | Boolean value read.| 4143 4144**Example** 4145 4146 ```ts 4147 let data = rpc.MessageParcel.create(); 4148 let result = data.writeBoolean(false); 4149 console.log("RpcClient: writeBoolean is " + result); 4150 let ret = data.readBoolean(); 4151 console.log("RpcClient: readBoolean is " + ret); 4152 ``` 4153 4154### writeChar 4155 4156writeChar(val: number): boolean 4157 4158Writes a Char value to this **MessageParcel** object. 4159 4160**System capability**: SystemCapability.Communication.IPC.Core 4161 4162**Parameters** 4163 4164| Name| Type | Mandatory| Description | 4165| ------ | ------ | ---- | -------------------- | 4166| val | number | Yes | Char value to write.| 4167 4168**Return value** 4169 4170| Type | Description | 4171| ------- | ----------------------------- | 4172| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4173 4174**Example** 4175 4176 ```ts 4177 let data = rpc.MessageParcel.create(); 4178 let result = data.writeChar(97); 4179 console.log("RpcClient: writeChar is " + result); 4180 ``` 4181 4182### readChar 4183 4184readChar(): number 4185 4186Reads the Char value from this **MessageParcel** object. 4187 4188**System capability**: SystemCapability.Communication.IPC.Core 4189 4190**Return value** 4191 4192| Type | Description | 4193| ------ | ---------------- | 4194| number | Char value read.| 4195 4196**Example** 4197 4198 ```ts 4199 let data = rpc.MessageParcel.create(); 4200 let result = data.writeChar(97); 4201 console.log("RpcClient: writeChar is " + result); 4202 let ret = data.readChar(); 4203 console.log("RpcClient: readChar is " + ret); 4204 ``` 4205 4206### writeString 4207 4208writeString(val: string): boolean 4209 4210Writes a string to this **MessageParcel** object. 4211 4212**System capability**: SystemCapability.Communication.IPC.Core 4213 4214**Parameters** 4215 4216| Name| Type | Mandatory| Description | 4217| ------ | ------ | ---- | ----------------------------------------- | 4218| val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 4219 4220**Return value** 4221 4222| Type | Description | 4223| ------- | --------------------------------- | 4224| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4225 4226**Example** 4227 4228 ```ts 4229 let data = rpc.MessageParcel.create(); 4230 let result = data.writeString('abc'); 4231 console.log("RpcClient: writeString is " + result); 4232 ``` 4233 4234### readString 4235 4236readString(): string 4237 4238Reads the string from this **MessageParcel** object. 4239 4240**System capability**: SystemCapability.Communication.IPC.Core 4241 4242**Return value** 4243 4244| Type | Description | 4245| ------ | -------------- | 4246| string | String read.| 4247 4248**Example** 4249 4250 ```ts 4251 let data = rpc.MessageParcel.create(); 4252 let result = data.writeString('abc'); 4253 console.log("RpcClient: writeString is " + result); 4254 let ret = data.readString(); 4255 console.log("RpcClient: readString is " + ret); 4256 ``` 4257 4258### writeSequenceable 4259 4260writeSequenceable(val: Sequenceable): boolean 4261 4262Writes a sequenceable object to this **MessageParcel** object. 4263 4264**System capability**: SystemCapability.Communication.IPC.Core 4265 4266**Parameters** 4267 4268| Name| Type | Mandatory| Description | 4269| ------ | ----------------------------- | ---- | -------------------- | 4270| val | [Sequenceable](#sequenceable) | Yes | Sequenceable object to write.| 4271 4272**Return value** 4273 4274| Type | Description | 4275| ------- | -------------------------------- | 4276| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4277 4278**Example** 4279 4280 ```ts 4281 class MySequenceable implements rpc.Sequenceable { 4282 num: number = 0; 4283 str: string = ''; 4284 constructor(num: number, str: string) { 4285 this.num = num; 4286 this.str = str; 4287 } 4288 marshalling(messageParcel: rpc.MessageParcel): boolean { 4289 messageParcel.writeInt(this.num); 4290 messageParcel.writeString(this.str); 4291 return true; 4292 } 4293 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4294 this.num = messageParcel.readInt(); 4295 this.str = messageParcel.readString(); 4296 return true; 4297 } 4298 } 4299 let sequenceable = new MySequenceable(1, "aaa"); 4300 let data = rpc.MessageParcel.create(); 4301 let result = data.writeSequenceable(sequenceable); 4302 console.log("RpcClient: writeSequenceable is " + result); 4303 ``` 4304 4305### readSequenceable 4306 4307readSequenceable(dataIn: Sequenceable): boolean 4308 4309Reads member variables from this **MessageParcel** object. 4310 4311**System capability**: SystemCapability.Communication.IPC.Core 4312 4313**Parameters** 4314 4315| Name| Type | Mandatory | Description | 4316| ------ | ----------------------------- | ------- | ---------------------------------------------- | 4317| dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4318 4319**Return value** 4320 4321| Type | Description | 4322| ------- | ---------------------------------------- | 4323| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4324 4325**Example** 4326 4327 ```ts 4328 class MySequenceable implements rpc.Sequenceable { 4329 num: number = 0; 4330 str: string = ''; 4331 constructor(num: number, str: string) { 4332 this.num = num; 4333 this.str = str; 4334 } 4335 marshalling(messageParcel: rpc.MessageParcel): boolean { 4336 messageParcel.writeInt(this.num); 4337 messageParcel.writeString(this.str); 4338 return true; 4339 } 4340 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4341 this.num = messageParcel.readInt(); 4342 this.str = messageParcel.readString(); 4343 return true; 4344 } 4345 } 4346 let sequenceable = new MySequenceable(1, "aaa"); 4347 let data = rpc.MessageParcel.create(); 4348 let result = data.writeSequenceable(sequenceable); 4349 console.log("RpcClient: writeSequenceable is " + result); 4350 let ret = new MySequenceable(0, ""); 4351 let result2 = data.readSequenceable(ret); 4352 console.log("RpcClient: writeSequenceable is " + result2); 4353 ``` 4354 4355### writeByteArray 4356 4357writeByteArray(byteArray: number[]): boolean 4358 4359Writes a byte array to this **MessageParcel** object. 4360 4361**System capability**: SystemCapability.Communication.IPC.Core 4362 4363**Parameters** 4364 4365| Name | Type | Mandatory| Description | 4366| --------- | -------- | ---- | ------------------ | 4367| byteArray | number[] | Yes | Byte array to write.| 4368 4369**Return value** 4370 4371| Type | Description | 4372| ------- | -------------------------------- | 4373| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4374 4375**Example** 4376 4377 ```ts 4378 let data = rpc.MessageParcel.create(); 4379 let ByteArrayVar = [1, 2, 3, 4, 5]; 4380 let result = data.writeByteArray(ByteArrayVar); 4381 console.log("RpcClient: writeByteArray is " + result); 4382 ``` 4383 4384### readByteArray 4385 4386readByteArray(dataIn: number[]): void 4387 4388Reads a byte array from this **MessageParcel** object. 4389 4390**System capability**: SystemCapability.Communication.IPC.Core 4391 4392**Parameters** 4393 4394| Name| Type | Mandatory| Description | 4395| ------ | -------- | ---- | ------------------ | 4396| dataIn | number[] | Yes | Byte array to read.| 4397 4398**Example** 4399 4400 ```ts 4401 let data = rpc.MessageParcel.create(); 4402 let ByteArrayVar = [1, 2, 3, 4, 5]; 4403 let result = data.writeByteArray(ByteArrayVar); 4404 console.log("RpcClient: writeByteArray is " + result); 4405 let array: Array<number> = new Array(5); 4406 data.readByteArray(array); 4407 ``` 4408 4409### readByteArray 4410 4411readByteArray(): number[] 4412 4413Reads the byte array from this **MessageParcel** object. 4414 4415**System capability**: SystemCapability.Communication.IPC.Core 4416 4417**Return value** 4418 4419| Type | Description | 4420| -------- | -------------- | 4421| number[] | Byte array read.| 4422 4423**Example** 4424 4425 ```ts 4426 let data = rpc.MessageParcel.create(); 4427 let ByteArrayVar = [1, 2, 3, 4, 5]; 4428 let result = data.writeByteArray(ByteArrayVar); 4429 console.log("RpcClient: writeByteArray is " + result); 4430 let array = data.readByteArray(); 4431 console.log("RpcClient: readByteArray is " + array); 4432 ``` 4433 4434### writeShortArray 4435 4436writeShortArray(shortArray: number[]): boolean 4437 4438Writes a short array to this **MessageParcel** object. 4439 4440**System capability**: SystemCapability.Communication.IPC.Core 4441 4442**Parameters** 4443 4444| Name | Type | Mandatory| Description | 4445| ---------- | -------- | ---- | -------------------- | 4446| shortArray | number[] | Yes | Short array to write.| 4447 4448**Return value** 4449 4450| Type | Description | 4451| ------- | -------------------------------- | 4452| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4453 4454**Example** 4455 4456 ```ts 4457 let data = rpc.MessageParcel.create(); 4458 let result = data.writeShortArray([11, 12, 13]); 4459 console.log("RpcClient: writeShortArray is " + result); 4460 ``` 4461 4462### readShortArray 4463 4464readShortArray(dataIn: number[]): void 4465 4466Reads a short array from this **MessageParcel** object. 4467 4468**System capability**: SystemCapability.Communication.IPC.Core 4469 4470**Parameters** 4471 4472| Name| Type | Mandatory| Description | 4473| ------ | -------- | ---- | -------------------- | 4474| dataIn | number[] | Yes | Short array to read.| 4475 4476**Example** 4477 4478 ```ts 4479 let data = rpc.MessageParcel.create(); 4480 let result = data.writeShortArray([11, 12, 13]); 4481 console.log("RpcClient: writeShortArray is " + result); 4482 let array: Array<number> = new Array(3); 4483 data.readShortArray(array); 4484 ``` 4485 4486### readShortArray 4487 4488readShortArray(): number[] 4489 4490Reads the short array from this **MessageParcel** object. 4491 4492**System capability**: SystemCapability.Communication.IPC.Core 4493 4494**Return value** 4495 4496| Type | Description | 4497| -------- | ---------------- | 4498| number[] | Short array read.| 4499 4500**Example** 4501 4502 ```ts 4503 let data = rpc.MessageParcel.create(); 4504 let result = data.writeShortArray([11, 12, 13]); 4505 console.log("RpcClient: writeShortArray is " + result); 4506 let array = data.readShortArray(); 4507 console.log("RpcClient: readShortArray is " + array); 4508 ``` 4509 4510### writeIntArray 4511 4512writeIntArray(intArray: number[]): boolean 4513 4514Writes an integer array to this **MessageParcel** object. 4515 4516**System capability**: SystemCapability.Communication.IPC.Core 4517 4518**Parameters** 4519 4520| Name | Type | Mandatory| Description | 4521| -------- | -------- | ---- | ------------------ | 4522| intArray | number[] | Yes | Integer array to write.| 4523 4524**Return value** 4525 4526| Type | Description | 4527| ------- | -------------------------------- | 4528| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4529 4530**Example** 4531 4532 ```ts 4533 let data = rpc.MessageParcel.create(); 4534 let result = data.writeIntArray([100, 111, 112]); 4535 console.log("RpcClient: writeIntArray is " + result); 4536 ``` 4537 4538### readIntArray 4539 4540readIntArray(dataIn: number[]): void 4541 4542Reads an integer array from this **MessageParcel** object. 4543 4544**System capability**: SystemCapability.Communication.IPC.Core 4545 4546**Parameters** 4547 4548| Name| Type | Mandatory| Description | 4549| ------ | -------- | ---- | ------------------ | 4550| dataIn | number[] | Yes | Integer array to read.| 4551 4552**Example** 4553 4554 ```ts 4555 let data = rpc.MessageParcel.create(); 4556 let result = data.writeIntArray([100, 111, 112]); 4557 console.log("RpcClient: writeIntArray is " + result); 4558 let array: Array<number> = new Array(3); 4559 data.readIntArray(array); 4560 ``` 4561 4562### readIntArray 4563 4564readIntArray(): number[] 4565 4566Reads the integer array from this **MessageParcel** object. 4567 4568**System capability**: SystemCapability.Communication.IPC.Core 4569 4570**Return value** 4571 4572| Type | Description | 4573| -------- | -------------- | 4574| number[] | Integer array read.| 4575 4576**Example** 4577 4578 ```ts 4579 let data = rpc.MessageParcel.create(); 4580 let result = data.writeIntArray([100, 111, 112]); 4581 console.log("RpcClient: writeIntArray is " + result); 4582 let array = data.readIntArray(); 4583 console.log("RpcClient: readIntArray is " + array); 4584 ``` 4585 4586### writeLongArray 4587 4588writeLongArray(longArray: number[]): boolean 4589 4590Writes a long array to this **MessageParcel** object. 4591 4592**System capability**: SystemCapability.Communication.IPC.Core 4593 4594**Parameters** 4595 4596| Name | Type | Mandatory| Description | 4597| --------- | -------- | ---- | -------------------- | 4598| longArray | number[] | Yes | Long array to write.| 4599 4600**Return value** 4601 4602| Type | Description | 4603| ------- | ----------------------------- | 4604| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4605 4606**Example** 4607 4608 ```ts 4609 let data = rpc.MessageParcel.create(); 4610 let result = data.writeLongArray([1111, 1112, 1113]); 4611 console.log("RpcClient: writeLongArray is " + result); 4612 ``` 4613 4614### readLongArray 4615 4616readLongArray(dataIn: number[]): void 4617 4618Reads a long array from this **MessageParcel** object. 4619 4620**System capability**: SystemCapability.Communication.IPC.Core 4621 4622**Parameters** 4623 4624| Name| Type | Mandatory| Description | 4625| ------ | -------- | ---- | -------------------- | 4626| dataIn | number[] | Yes | Long array to read.| 4627 4628**Example** 4629 4630 ```ts 4631 let data = rpc.MessageParcel.create(); 4632 let result = data.writeLongArray([1111, 1112, 1113]); 4633 console.log("RpcClient: writeLongArray is " + result); 4634 let array: Array<number> = new Array(3); 4635 data.readLongArray(array); 4636 ``` 4637 4638### readLongArray 4639 4640readLongArray(): number[] 4641 4642Reads the long array from this **MessageParcel** object. 4643 4644**System capability**: SystemCapability.Communication.IPC.Core 4645 4646**Return value** 4647 4648| Type | Description | 4649| -------- | ---------------- | 4650| number[] | Long array read.| 4651 4652**Example** 4653 4654 ```ts 4655 let data = rpc.MessageParcel.create(); 4656 let result = data.writeLongArray([1111, 1112, 1113]); 4657 console.log("RpcClient: writeLongArray is " + result); 4658 let array = data.readLongArray(); 4659 console.log("RpcClient: readLongArray is " + array); 4660 ``` 4661 4662### writeFloatArray 4663 4664writeFloatArray(floatArray: number[]): boolean 4665 4666Writes a FloatArray to this **MessageParcel** object. 4667 4668**System capability**: SystemCapability.Communication.IPC.Core 4669 4670**Parameters** 4671 4672| Name| Type| Mandatory| Description | 4673| ---------- | -------- | ---- | --- | 4674| floatArray | number[] | Yes | Floating-point array to write. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4675 4676**Return value** 4677 4678| Type | Description | 4679| ------- | -------------------------------- | 4680| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4681 4682**Example** 4683 4684 ```ts 4685 let data = rpc.MessageParcel.create(); 4686 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4687 console.log("RpcClient: writeFloatArray is " + result); 4688 ``` 4689 4690### readFloatArray 4691 4692readFloatArray(dataIn: number[]): void 4693 4694Reads a FloatArray from this **MessageParcel** object. 4695 4696**System capability**: SystemCapability.Communication.IPC.Core 4697 4698**Parameters** 4699 4700| Name| Type | Mandatory| Description | 4701| ------ | -------- | ---- | ------ | 4702| dataIn | number[] | Yes | Floating-point array to read. The system processes Float data as that of the Double type. Therefore, the total number of bytes occupied by a FloatArray must be calculated as the Double type.| 4703 4704**Example** 4705 4706 ```ts 4707 let data = rpc.MessageParcel.create(); 4708 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4709 console.log("RpcClient: writeFloatArray is " + result); 4710 let array: Array<number> = new Array(3); 4711 data.readFloatArray(array); 4712 ``` 4713 4714### readFloatArray 4715 4716readFloatArray(): number[] 4717 4718Reads the FloatArray from this **MessageParcel** object. 4719 4720**System capability**: SystemCapability.Communication.IPC.Core 4721 4722**Return value** 4723 4724| Type | Description | 4725| -------- | -------------- | 4726| number[] | FloatArray read.| 4727 4728**Example** 4729 4730 ```ts 4731 let data = rpc.MessageParcel.create(); 4732 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 4733 console.log("RpcClient: writeFloatArray is " + result); 4734 let array = data.readFloatArray(); 4735 console.log("RpcClient: readFloatArray is " + array); 4736 ``` 4737 4738### writeDoubleArray 4739 4740writeDoubleArray(doubleArray: number[]): boolean 4741 4742Writes a DoubleArray to this **MessageParcel** object. 4743 4744**System capability**: SystemCapability.Communication.IPC.Core 4745 4746**Parameters** 4747 4748| Name | Type | Mandatory| Description | 4749| ----------- | -------- | ---- | ------------------------ | 4750| doubleArray | number[] | Yes | DoubleArray to write.| 4751 4752**Return value** 4753 4754| Type | Description | 4755| ------- | -------------------------------- | 4756| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4757 4758**Example** 4759 4760 ```ts 4761 let data = rpc.MessageParcel.create(); 4762 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4763 console.log("RpcClient: writeDoubleArray is " + result); 4764 ``` 4765 4766### readDoubleArray 4767 4768readDoubleArray(dataIn: number[]): void 4769 4770Reads a DoubleArray from this **MessageParcel** object. 4771 4772**System capability**: SystemCapability.Communication.IPC.Core 4773 4774**Parameters** 4775 4776| Name| Type | Mandatory| Description | 4777| ------ | -------- | ---- | ------------------------ | 4778| dataIn | number[] | Yes | DoubleArray to read.| 4779 4780**Example** 4781 4782 ```ts 4783 let data = rpc.MessageParcel.create(); 4784 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4785 console.log("RpcClient: writeDoubleArray is " + result); 4786 let array: Array<number> = new Array(3); 4787 data.readDoubleArray(array); 4788 ``` 4789 4790### readDoubleArray 4791 4792readDoubleArray(): number[] 4793 4794Reads the DoubleArray from this **MessageParcel** object. 4795 4796**System capability**: SystemCapability.Communication.IPC.Core 4797 4798**Return value** 4799 4800| Type | Description | 4801| -------- | -------------------- | 4802| number[] | DoubleArray read.| 4803 4804**Example** 4805 4806 ```ts 4807 let data = rpc.MessageParcel.create(); 4808 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 4809 console.log("RpcClient: writeDoubleArray is " + result); 4810 let array = data.readDoubleArray(); 4811 console.log("RpcClient: readDoubleArray is " + array); 4812 ``` 4813 4814### writeBooleanArray 4815 4816writeBooleanArray(booleanArray: boolean[]): boolean 4817 4818Writes a Boolean array to this **MessageParcel** object. 4819 4820**System capability**: SystemCapability.Communication.IPC.Core 4821 4822**Parameters** 4823 4824| Name | Type | Mandatory| Description | 4825| ------------ | --------- | ---- | ------------------ | 4826| booleanArray | boolean[] | Yes | Boolean array to write.| 4827 4828**Return value** 4829 4830| Type | Description | 4831| ------- | -------------------------------- | 4832| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4833 4834**Example** 4835 4836 ```ts 4837 let data = rpc.MessageParcel.create(); 4838 let result = data.writeBooleanArray([false, true, false]); 4839 console.log("RpcClient: writeBooleanArray is " + result); 4840 ``` 4841 4842### readBooleanArray 4843 4844readBooleanArray(dataIn: boolean[]): void 4845 4846Reads a Boolean array from this **MessageParcel** object. 4847 4848**System capability**: SystemCapability.Communication.IPC.Core 4849 4850**Parameters** 4851 4852| Name| Type | Mandatory| Description | 4853| ------ | --------- | ---- | ------------------ | 4854| dataIn | boolean[] | Yes | Boolean array to read.| 4855 4856**Example** 4857 4858 ```ts 4859 let data = rpc.MessageParcel.create(); 4860 let result = data.writeBooleanArray([false, true, false]); 4861 console.log("RpcClient: writeBooleanArray is " + result); 4862 let array: Array<boolean> = new Array(3); 4863 data.readBooleanArray(array); 4864 ``` 4865 4866### readBooleanArray 4867 4868readBooleanArray(): boolean[] 4869 4870Reads the Boolean array from this **MessageParcel** object. 4871 4872**System capability**: SystemCapability.Communication.IPC.Core 4873 4874**Return value** 4875 4876| Type | Description | 4877| --------- | -------------- | 4878| boolean[] | Boolean array read.| 4879 4880**Example** 4881 4882 ```ts 4883 let data = rpc.MessageParcel.create(); 4884 let result = data.writeBooleanArray([false, true, false]); 4885 console.log("RpcClient: writeBooleanArray is " + result); 4886 let array = data.readBooleanArray(); 4887 console.log("RpcClient: readBooleanArray is " + array); 4888 ``` 4889 4890### writeCharArray 4891 4892writeCharArray(charArray: number[]): boolean 4893 4894Writes a character array to this **MessageParcel** object. 4895 4896**System capability**: SystemCapability.Communication.IPC.Core 4897 4898**Parameters** 4899 4900| Name | Type | Mandatory| Description | 4901| --------- | -------- | ---- | ---------------------- | 4902| charArray | number[] | Yes | Character array to write.| 4903 4904**Return value** 4905 4906| Type | Description | 4907| ------- | -------------------------------- | 4908| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4909 4910**Example** 4911 4912 ```ts 4913 let data = rpc.MessageParcel.create(); 4914 let result = data.writeCharArray([97, 98, 88]); 4915 console.log("RpcClient: writeCharArray is " + result); 4916 ``` 4917 4918### readCharArray 4919 4920readCharArray(dataIn: number[]): void 4921 4922Reads a character array from this **MessageParcel** object. 4923 4924**System capability**: SystemCapability.Communication.IPC.Core 4925 4926**Parameters** 4927 4928| Name| Type | Mandatory| Description | 4929| ------ | -------- | ---- | ---------------------- | 4930| dataIn | number[] | Yes | Character array to read.| 4931 4932**Example** 4933 4934 ```ts 4935 let data = rpc.MessageParcel.create(); 4936 let result = data.writeCharArray([97, 98, 99]); 4937 console.log("RpcClient: writeCharArray is " + result); 4938 let array: Array<number> = new Array(3); 4939 data.readCharArray(array); 4940 ``` 4941 4942### readCharArray 4943 4944readCharArray(): number[] 4945 4946Reads the character array from this **MessageParcel** object. 4947 4948**System capability**: SystemCapability.Communication.IPC.Core 4949 4950**Return value** 4951 4952| Type | Description | 4953| -------- | ------------------ | 4954| number[] | Character array read.| 4955 4956**Example** 4957 4958 ```ts 4959 let data = rpc.MessageParcel.create(); 4960 let result = data.writeCharArray([97, 98, 99]); 4961 console.log("RpcClient: writeCharArray is " + result); 4962 let array = data.readCharArray(); 4963 console.log("RpcClient: readCharArray is " + array); 4964 ``` 4965 4966### writeStringArray 4967 4968writeStringArray(stringArray: string[]): boolean 4969 4970Writes a string array to this **MessageParcel** object. 4971 4972**System capability**: SystemCapability.Communication.IPC.Core 4973 4974**Parameters** 4975 4976| Name | Type | Mandatory| Description | 4977| ----------- | -------- | ---- | ---------------- | 4978| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 4979 4980**Return value** 4981 4982| Type | Description| 4983| ------- | -------------------------------- | 4984| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4985 4986**Example** 4987 4988 ```ts 4989 let data = rpc.MessageParcel.create(); 4990 let result = data.writeStringArray(["abc", "def"]); 4991 console.log("RpcClient: writeStringArray is " + result); 4992 ``` 4993 4994### readStringArray 4995 4996readStringArray(dataIn: string[]): void 4997 4998Reads a string array from this **MessageParcel** object. 4999 5000**System capability**: SystemCapability.Communication.IPC.Core 5001 5002**Parameters** 5003 5004| Name| Type | Mandatory| Description | 5005| ------ | -------- | ---- | -------------------- | 5006| dataIn | string[] | Yes | String array to read.| 5007 5008**Example** 5009 5010 ```ts 5011 let data = rpc.MessageParcel.create(); 5012 let result = data.writeStringArray(["abc", "def"]); 5013 console.log("RpcClient: writeStringArray is " + result); 5014 let array: Array<string> = new Array(2); 5015 data.readStringArray(array); 5016 ``` 5017 5018### readStringArray 5019 5020readStringArray(): string[] 5021 5022Reads the string array from this **MessageParcel** object. 5023 5024**System capability**: SystemCapability.Communication.IPC.Core 5025 5026**Return value** 5027 5028| Type | Description | 5029| -------- | ---------------- | 5030| string[] | String array read.| 5031 5032**Example** 5033 5034 ```ts 5035 let data = rpc.MessageParcel.create(); 5036 let result = data.writeStringArray(["abc", "def"]); 5037 console.log("RpcClient: writeStringArray is " + result); 5038 let array = data.readStringArray(); 5039 console.log("RpcClient: readStringArray is " + array); 5040 ``` 5041 5042### writeNoException<sup>8+</sup> 5043 5044writeNoException(): void 5045 5046Writes information to this **MessageParcel** object indicating that no exception occurred. 5047 5048**System capability**: SystemCapability.Communication.IPC.Core 5049 5050**Example** 5051 5052 ```ts 5053 class MyDeathRecipient implements rpc.DeathRecipient { 5054 onRemoteDied() { 5055 console.log("server died"); 5056 } 5057 } 5058 class TestRemoteObject extends rpc.RemoteObject { 5059 constructor(descriptor: string) { 5060 super(descriptor); 5061 } 5062 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5063 return true; 5064 } 5065 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5066 return true; 5067 } 5068 isObjectDead(): boolean { 5069 return false; 5070 } 5071 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5072 if (code === 1) { 5073 console.log("RpcServer: onRemoteRequest called"); 5074 reply.writeNoException(); 5075 return true; 5076 } else { 5077 console.log("RpcServer: unknown code: " + code); 5078 return false; 5079 } 5080 } 5081 } 5082 ``` 5083 5084### readException<sup>8+</sup> 5085 5086readException(): void 5087 5088Reads the exception information from this **MessageParcel** object. 5089 5090**System capability**: SystemCapability.Communication.IPC.Core 5091 5092**Example** 5093 5094 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 5095 5096 ```ts 5097 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 5098 // import FA from "@ohos.ability.featureAbility"; 5099 import Want from '@ohos.app.ability.Want'; 5100 import common from '@ohos.app.ability.common'; 5101 5102 let proxy: rpc.IRemoteObject | undefined = undefined; 5103 let connect: common.ConnectOptions = { 5104 onConnect: (elementName, remoteProxy) => { 5105 console.log("RpcClient: js onConnect called."); 5106 proxy = remoteProxy; 5107 }, 5108 onDisconnect: (elementName) => { 5109 console.log("RpcClient: onDisconnect"); 5110 }, 5111 onFailed: () => { 5112 console.log("RpcClient: onFailed"); 5113 } 5114 }; 5115 let want: Want = { 5116 bundleName: "com.ohos.server", 5117 abilityName: "com.ohos.server.EntryAbility", 5118 }; 5119 5120 // Use this method to connect to the ability for the FA model. 5121 // FA.connectAbility(want,connect); 5122 5123 this.context.connectServiceExtensionAbility(want, connect); 5124 ``` 5125 5126 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. 5127 5128 ```ts 5129 let option = new rpc.MessageOption(); 5130 let data = rpc.MessageParcel.create(); 5131 let reply = rpc.MessageParcel.create(); 5132 data.writeInt(1); 5133 data.writeString("hello"); 5134 proxy.sendRequest(1, data, reply, option) 5135 .then((result: rpc.SendRequestResult) => { 5136 if (result.errCode === 0) { 5137 console.log("sendRequest got result"); 5138 result.reply.readException(); 5139 let msg = result.reply.readString(); 5140 console.log("RPCTest: reply msg: " + msg); 5141 } else { 5142 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 5143 } 5144 }).catch((e: Error) => { 5145 console.log("RPCTest: sendRequest got exception: " + e.message); 5146 }).finally (() => { 5147 console.log("RPCTest: sendRequest ends, reclaim parcel"); 5148 data.reclaim(); 5149 reply.reclaim(); 5150 }); 5151 ``` 5152 5153### writeSequenceableArray 5154 5155writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5156 5157Writes a sequenceable array to this **MessageParcel** object. 5158 5159**System capability**: SystemCapability.Communication.IPC.Core 5160 5161**Parameters** 5162 5163| Name | Type | Mandatory| Description | 5164| ----------------- | -------------- | ---- | -------------------------- | 5165| sequenceableArray | Sequenceable[] | Yes | Sequenceable array to write.| 5166 5167**Return value** 5168 5169| Type | Description | 5170| ------- | -------------------------------- | 5171| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5172 5173**Example** 5174 5175 ```ts 5176 class MySequenceable implements rpc.Sequenceable { 5177 num: number = 0; 5178 str: string = ''; 5179 constructor(num: number, str: string) { 5180 this.num = num; 5181 this.str = str; 5182 } 5183 marshalling(messageParcel: rpc.MessageParcel): boolean { 5184 messageParcel.writeInt(this.num); 5185 messageParcel.writeString(this.str); 5186 return true; 5187 } 5188 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5189 this.num = messageParcel.readInt(); 5190 this.str = messageParcel.readString(); 5191 return true; 5192 } 5193 } 5194 let sequenceable = new MySequenceable(1, "aaa"); 5195 let sequenceable2 = new MySequenceable(2, "bbb"); 5196 let sequenceable3 = new MySequenceable(3, "ccc"); 5197 let a = [sequenceable, sequenceable2, sequenceable3]; 5198 let data = rpc.MessageParcel.create(); 5199 let result = data.writeSequenceableArray(a); 5200 console.log("RpcClient: writeSequenceableArray is " + result); 5201 ``` 5202 5203### readSequenceableArray<sup>8+</sup> 5204 5205readSequenceableArray(sequenceableArray: Sequenceable[]): void 5206 5207Reads a sequenceable array from this **MessageParcel** object. 5208 5209**System capability**: SystemCapability.Communication.IPC.Core 5210 5211**Parameters** 5212 5213| Name | Type | Mandatory| Description | 5214| ----------------- | -------------- | ---- | -------------------------- | 5215| sequenceableArray | Sequenceable[] | Yes | Sequenceable array to read.| 5216 5217**Example** 5218 5219 ```ts 5220 class MySequenceable implements rpc.Sequenceable { 5221 num: number = 0; 5222 str: string = ''; 5223 constructor(num: number, str: string) { 5224 this.num = num; 5225 this.str = str; 5226 } 5227 marshalling(messageParcel: rpc.MessageParcel): boolean { 5228 messageParcel.writeInt(this.num); 5229 messageParcel.writeString(this.str); 5230 return true; 5231 } 5232 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5233 this.num = messageParcel.readInt(); 5234 this.str = messageParcel.readString(); 5235 return true; 5236 } 5237 } 5238 let sequenceable = new MySequenceable(1, "aaa"); 5239 let sequenceable2 = new MySequenceable(2, "bbb"); 5240 let sequenceable3 = new MySequenceable(3, "ccc"); 5241 let a = [sequenceable, sequenceable2, sequenceable3]; 5242 let data = rpc.MessageParcel.create(); 5243 let result = data.writeSequenceableArray(a); 5244 console.log("RpcClient: writeSequenceableArray is " + result); 5245 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5246 data.readSequenceableArray(b); 5247 ``` 5248 5249### writeRemoteObjectArray<sup>8+</sup> 5250 5251writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5252 5253Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 5254 5255**System capability**: SystemCapability.Communication.IPC.Core 5256 5257**Parameters** 5258 5259| Name | Type | Mandatory| Description | 5260| ----------- | --------------- | ---- | ----- | 5261| objectArray | IRemoteObject[] | Yes | Array of **IRemoteObject** objects to write.| 5262 5263**Return value** 5264 5265| Type | Description | 5266| ------- | -------------------------------- | 5267| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5268 5269**Example** 5270 5271 ```ts 5272 class MyDeathRecipient implements rpc.DeathRecipient { 5273 onRemoteDied() { 5274 console.log("server died"); 5275 } 5276 } 5277 class TestRemoteObject extends rpc.RemoteObject { 5278 constructor(descriptor: string) { 5279 super(descriptor); 5280 this.attachLocalInterface(this, descriptor); 5281 } 5282 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5283 return true; 5284 } 5285 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5286 return true; 5287 } 5288 isObjectDead(): boolean { 5289 return false; 5290 } 5291 asObject(): rpc.IRemoteObject { 5292 return this; 5293 } 5294 } 5295 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5296 let data = rpc.MessageParcel.create(); 5297 let result = data.writeRemoteObjectArray(a); 5298 console.log("RpcClient: writeRemoteObjectArray is " + result); 5299 ``` 5300 5301### readRemoteObjectArray<sup>8+</sup> 5302 5303readRemoteObjectArray(objects: IRemoteObject[]): void 5304 5305Reads an **IRemoteObject** array from this **MessageParcel** object. 5306 5307**System capability**: SystemCapability.Communication.IPC.Core 5308 5309**Parameters** 5310 5311| Name | Type | Mandatory| Description | 5312| ------- | --------------- | ---- | --------- | 5313| objects | IRemoteObject[] | Yes | **IRemoteObject** array to read.| 5314 5315**Example** 5316 5317 ```ts 5318 class MyDeathRecipient implements rpc.DeathRecipient { 5319 onRemoteDied() { 5320 console.log("server died"); 5321 } 5322 } 5323 class TestRemoteObject extends rpc.RemoteObject { 5324 constructor(descriptor: string) { 5325 super(descriptor); 5326 this.attachLocalInterface(this, descriptor); 5327 } 5328 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5329 return true; 5330 } 5331 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5332 return true; 5333 } 5334 isObjectDead(): boolean { 5335 return false; 5336 } 5337 asObject(): rpc.IRemoteObject { 5338 return this; 5339 } 5340 } 5341 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5342 let data = rpc.MessageParcel.create(); 5343 data.writeRemoteObjectArray(a); 5344 let b: Array<rpc.IRemoteObject> = new Array(3); 5345 data.readRemoteObjectArray(b); 5346 ``` 5347 5348### readRemoteObjectArray<sup>8+</sup> 5349 5350readRemoteObjectArray(): IRemoteObject[] 5351 5352Reads the **IRemoteObject** array from this **MessageParcel** object. 5353 5354**System capability**: SystemCapability.Communication.IPC.Core 5355 5356**Return value** 5357 5358| Type | Description | 5359| --------------- | --------------------------- | 5360| IRemoteObject[] | **IRemoteObject** object array obtained.| 5361 5362**Example** 5363 5364 ```ts 5365 class MyDeathRecipient implements rpc.DeathRecipient { 5366 onRemoteDied() { 5367 console.log("server died"); 5368 } 5369 } 5370 class TestRemoteObject extends rpc.RemoteObject { 5371 constructor(descriptor: string) { 5372 super(descriptor); 5373 this.attachLocalInterface(this, descriptor); 5374 } 5375 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5376 return true; 5377 } 5378 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5379 return true; 5380 } 5381 isObjectDead(): boolean { 5382 return false; 5383 } 5384 asObject(): rpc.IRemoteObject { 5385 return this; 5386 } 5387 } 5388 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5389 let data = rpc.MessageParcel.create(); 5390 let result = data.writeRemoteObjectArray(a); 5391 console.log("RpcClient: readRemoteObjectArray is " + result); 5392 let b = data.readRemoteObjectArray(); 5393 console.log("RpcClient: readRemoteObjectArray is " + b); 5394 ``` 5395 5396### closeFileDescriptor<sup>8+</sup> 5397 5398static closeFileDescriptor(fd: number): void 5399 5400Closes a file descriptor. This API is a static method. 5401 5402**System capability**: SystemCapability.Communication.IPC.Core 5403 5404**Parameters** 5405 5406| Name| Type | Mandatory| Description | 5407| ------ | ------ | ---- | -------------------- | 5408| fd | number | Yes | File descriptor to close.| 5409 5410**Example** 5411 5412 ```ts 5413 import fs from '@ohos.file.fs'; 5414 5415 let filePath = "path/to/file"; 5416 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5417 rpc.MessageParcel.closeFileDescriptor(file.fd); 5418 ``` 5419 5420### dupFileDescriptor<sup>8+</sup> 5421 5422static dupFileDescriptor(fd: number) :number 5423 5424Duplicates a file descriptor. This API is a static method. 5425 5426**System capability**: SystemCapability.Communication.IPC.Core 5427 5428**Parameters** 5429 5430| Name| Type | Mandatory| Description | 5431| ------ | ------ | ---- | ------------------------ | 5432| fd | number | Yes | File descriptor to duplicate.| 5433 5434**Return value** 5435 5436| Type | Description | 5437| ------ | -------------------- | 5438| number | New file descriptor.| 5439 5440**Example** 5441 5442 ```ts 5443 import fs from '@ohos.file.fs'; 5444 5445 let filePath = "path/to/file"; 5446 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5447 rpc.MessageParcel.dupFileDescriptor(file.fd); 5448 ``` 5449 5450### containFileDescriptors<sup>8+</sup> 5451 5452containFileDescriptors(): boolean 5453 5454Checks whether this **MessageParcel** object contains file descriptors. 5455 5456**System capability**: SystemCapability.Communication.IPC.Core 5457 5458**Return value** 5459 5460| Type | Description | 5461| ------- | --------------------------------------------- | 5462| boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 5463 5464**Example** 5465 5466 ```ts 5467 import fs from '@ohos.file.fs'; 5468 5469 let parcel = new rpc.MessageParcel(); 5470 let filePath = "path/to/file"; 5471 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5472 let writeResult = parcel.writeFileDescriptor(file.fd); 5473 console.log("RpcTest: parcel writeFd result is : " + writeResult); 5474 let containFD = parcel.containFileDescriptors(); 5475 console.log("RpcTest: parcel after write fd containFd result is : " + containFD); 5476 ``` 5477 5478### writeFileDescriptor<sup>8+</sup> 5479 5480writeFileDescriptor(fd: number): boolean 5481 5482Writes a file descriptor to this **MessageParcel** object. 5483 5484**System capability**: SystemCapability.Communication.IPC.Core 5485 5486**Parameters** 5487 5488| Name| Type | Mandatory| Description | 5489| ------ | ------ | ---- | ------------ | 5490| fd | number | Yes | File descriptor to write.| 5491 5492**Return value** 5493 5494| Type | Description | 5495| ------- | -------------------------------- | 5496| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5497 5498**Example** 5499 5500 ```ts 5501 import fs from '@ohos.file.fs'; 5502 5503 let parcel = new rpc.MessageParcel(); 5504 let filePath = "path/to/file"; 5505 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5506 let writeResult = parcel.writeFileDescriptor(file.fd); 5507 console.log("RpcTest: parcel writeFd result is : " + writeResult); 5508 ``` 5509 5510### readFileDescriptor<sup>8+</sup> 5511 5512readFileDescriptor(): number 5513 5514Reads the file descriptor from this **MessageParcel** object. 5515 5516**System capability**: SystemCapability.Communication.IPC.Core 5517 5518**Return value** 5519 5520| Type | Description | 5521| ------ | ---------------- | 5522| number | File descriptor read.| 5523 5524**Example** 5525 5526 ```ts 5527 import fs from '@ohos.file.fs'; 5528 5529 let parcel = new rpc.MessageParcel(); 5530 let filePath = "path/to/file"; 5531 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5532 let writeResult = parcel.writeFileDescriptor(file.fd); 5533 let readFD = parcel.readFileDescriptor(); 5534 console.log("RpcTest: parcel read fd is : " + readFD); 5535 ``` 5536 5537### writeAshmem<sup>8+</sup> 5538 5539writeAshmem(ashmem: Ashmem): boolean 5540 5541Writes an anonymous shared object to this **MessageParcel** object. 5542 5543**System capability**: SystemCapability.Communication.IPC.Core 5544 5545**Parameters** 5546 5547| Name| Type | Mandatory| Description | 5548| ------ | ------ | ---- | ----------------------------------- | 5549| ashmem | Ashmem | Yes | Anonymous shared object to write.| 5550 5551**Return value** 5552 5553| Type | Description | 5554| ------- | -------------------------------- | 5555| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5556 5557**Example** 5558 5559 ```ts 5560 let parcel = new rpc.MessageParcel(); 5561 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5562 let isWriteSuccess = parcel.writeAshmem(ashmem); 5563 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 5564 ``` 5565 5566### readAshmem<sup>8+</sup> 5567 5568readAshmem(): Ashmem 5569 5570Reads the anonymous shared object from this **MessageParcel** object. 5571 5572**System capability**: SystemCapability.Communication.IPC.Core 5573 5574**Return value** 5575 5576| Type | Description | 5577| ------ | ------------------ | 5578| Ashmem | Anonymous share object obtained.| 5579 5580**Example** 5581 5582 ```ts 5583 let parcel = new rpc.MessageParcel(); 5584 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 5585 let isWriteSuccess = parcel.writeAshmem(ashmem); 5586 console.log("RpcTest: write ashmem to result is : " + isWriteSuccess); 5587 let readAshmem = parcel.readAshmem(); 5588 console.log("RpcTest: read ashmem to result is : " + readAshmem); 5589 ``` 5590 5591### getRawDataCapacity<sup>8+</sup> 5592 5593getRawDataCapacity(): number 5594 5595Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 5596 5597**System capability**: SystemCapability.Communication.IPC.Core 5598 5599**Return value** 5600 5601| Type | Description | 5602| ------ | ---------------------------------------------------------- | 5603| number | 128 MB, which is the maximum amount of raw data that can be held by this **MessageParcel** object.| 5604 5605**Example** 5606 5607 ```ts 5608 let parcel = new rpc.MessageParcel(); 5609 let result = parcel.getRawDataCapacity(); 5610 console.log("RpcTest: parcel get RawDataCapacity result is : " + result); 5611 ``` 5612 5613### writeRawData<sup>8+</sup> 5614 5615writeRawData(rawData: number[], size: number): boolean 5616 5617Writes raw data to this **MessageParcel** object. 5618 5619**System capability**: SystemCapability.Communication.IPC.Core 5620 5621**Parameters** 5622 5623| Name | Type | Mandatory| Description | 5624| ------- | -------- | ---- | ---------------------------------- | 5625| rawData | number[] | Yes | Raw data to write. | 5626| size | number | Yes | Size of the raw data, in bytes.| 5627 5628**Return value** 5629 5630| Type | Description | 5631| ------- | -------------------------------- | 5632| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5633 5634**Example** 5635 5636 ```ts 5637 let parcel = new rpc.MessageParcel(); 5638 let arr = [1, 2, 3, 4, 5]; 5639 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5640 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 5641 ``` 5642 5643### readRawData<sup>8+</sup> 5644 5645readRawData(size: number): number[] 5646 5647Reads raw data from this **MessageParcel** object. 5648 5649**System capability**: SystemCapability.Communication.IPC.Core 5650 5651**Parameters** 5652 5653| Name| Type | Mandatory| Description | 5654| ------ | ------ | ---- | ------------------------ | 5655| size | number | Yes | Size of the raw data to read.| 5656 5657**Return value** 5658 5659| Type | Description | 5660| -------- | ------------------------------ | 5661| number[] | Raw data obtained, in bytes.| 5662 5663**Example** 5664 5665 ```ts 5666 let parcel = new rpc.MessageParcel(); 5667 let arr = [1, 2, 3, 4, 5]; 5668 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 5669 console.log("RpcTest: parcel write raw data result is : " + isWriteSuccess); 5670 let result = parcel.readRawData(5); 5671 console.log("RpcTest: parcel read raw data result is : " + result); 5672 ``` 5673 5674## Parcelable<sup>9+</sup> 5675 5676Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 5677 5678### marshalling 5679 5680marshalling(dataOut: MessageSequence): boolean 5681 5682Marshals this **Parcelable** object into a **MessageSequence** object. 5683 5684**System capability**: SystemCapability.Communication.IPC.Core 5685 5686**Parameters** 5687 5688| Name | Type | Mandatory| Description | 5689| ------- | --------------- | ---- | ------------------------------------------- | 5690| dataOut | MessageSequence | Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 5691 5692**Return value** 5693 5694| Type | Description | 5695| ------- | -------------------------------- | 5696| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5697 5698**Example** 5699 5700 ```ts 5701 class MyParcelable implements rpc.Parcelable { 5702 num: number = 0; 5703 str: string = ''; 5704 constructor(num: number, str: string) { 5705 this.num = num; 5706 this.str = str; 5707 } 5708 marshalling(messageSequence: rpc.MessageSequence): boolean { 5709 messageSequence.writeInt(this.num); 5710 messageSequence.writeString(this.str); 5711 return true; 5712 } 5713 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 5714 this.num = messageSequence.readInt(); 5715 this.str = messageSequence.readString(); 5716 return true; 5717 } 5718 } 5719 let parcelable = new MyParcelable(1, "aaa"); 5720 let data = rpc.MessageSequence.create(); 5721 let result = data.writeParcelable(parcelable); 5722 console.log("RpcClient: writeParcelable is " + result); 5723 let ret = new MyParcelable(0, ""); 5724 let result2 = data.readParcelable(ret); 5725 console.log("RpcClient: readParcelable is " + result2); 5726 ``` 5727 5728### unmarshalling 5729 5730unmarshalling(dataIn: MessageSequence): boolean 5731 5732Unmarshals this **Parcelable** object from a **MessageSequence** object. 5733 5734**System capability**: SystemCapability.Communication.IPC.Core 5735 5736**Parameters** 5737 5738| Name| Type | Mandatory| Description | 5739| ------ | --------------- | ---- | ----------------------------------------------- | 5740| dataIn | MessageSequence | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 5741 5742**Return value** 5743 5744| Type | Description | 5745| ------- | ---------------------------------------- | 5746| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5747 5748**Example** 5749 5750 ```ts 5751 class MyParcelable implements rpc.Parcelable { 5752 num: number = 0; 5753 str: string = ''; 5754 constructor(num: number, str: string) { 5755 this.num = num; 5756 this.str = str; 5757 } 5758 marshalling(messageSequence: rpc.MessageSequence): boolean { 5759 messageSequence.writeInt(this.num); 5760 messageSequence.writeString(this.str); 5761 return true; 5762 } 5763 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 5764 this.num = messageSequence.readInt(); 5765 this.str = messageSequence.readString(); 5766 return true; 5767 } 5768 } 5769 let parcelable = new MyParcelable(1, "aaa"); 5770 let data = rpc.MessageSequence.create(); 5771 let result = data.writeParcelable(parcelable); 5772 console.log("RpcClient: writeParcelable is " + result); 5773 let ret = new MyParcelable(0, ""); 5774 let result2 = data.readParcelable(ret); 5775 console.log("RpcClient: readParcelable is " + result2); 5776 ``` 5777 5778## Sequenceable<sup>(deprecated)</sup> 5779 5780>This class is no longer maintained since API version 9. You are advised to use the [Parcelable](#parcelable9). 5781 5782Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 5783 5784### marshalling 5785 5786marshalling(dataOut: MessageParcel): boolean 5787 5788Marshals the sequenceable object into a **MessageParcel** object. 5789 5790**System capability**: SystemCapability.Communication.IPC.Core 5791 5792**Parameters** 5793 5794| Name | Type | Mandatory| Description | 5795| ------- | ----------------------------------------- | ---- | ----------------------------------------- | 5796| dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 5797 5798**Return value** 5799 5800| Type | Description | 5801| ------- | -------------------------------- | 5802| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5803 5804**Example** 5805 5806 ```ts 5807 class MySequenceable implements rpc.Sequenceable { 5808 num: number = 0; 5809 str: string = ''; 5810 constructor(num: number, str: string) { 5811 this.num = num; 5812 this.str = str; 5813 } 5814 marshalling(messageParcel: rpc.MessageParcel): boolean { 5815 messageParcel.writeInt(this.num); 5816 messageParcel.writeString(this.str); 5817 return true; 5818 } 5819 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5820 this.num = messageParcel.readInt(); 5821 this.str = messageParcel.readString(); 5822 return true; 5823 } 5824 } 5825 let sequenceable = new MySequenceable(1, "aaa"); 5826 let data = rpc.MessageParcel.create(); 5827 let result = data.writeSequenceable(sequenceable); 5828 console.log("RpcClient: writeSequenceable is " + result); 5829 let ret = new MySequenceable(0, ""); 5830 let result2 = data.readSequenceable(ret); 5831 console.log("RpcClient: readSequenceable is " + result2); 5832 ``` 5833 5834### unmarshalling 5835 5836unmarshalling(dataIn: MessageParcel): boolean 5837 5838Unmarshals this sequenceable object from a **MessageParcel** object. 5839 5840**System capability**: SystemCapability.Communication.IPC.Core 5841 5842**Parameters** 5843 5844| Name| Type | Mandatory| Description | 5845| ------ | ----------------------------------------- | ---- | --------------------------------------------- | 5846| dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 5847 5848**Return value** 5849 5850| Type | Description | 5851| ------- | ---------------------------------------- | 5852| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 5853 5854**Example** 5855 5856 ```ts 5857 class MySequenceable implements rpc.Sequenceable { 5858 num: number = 0; 5859 str: string = ''; 5860 constructor(num: number, str: string) { 5861 this.num = num; 5862 this.str = str; 5863 } 5864 marshalling(messageParcel: rpc.MessageParcel): boolean { 5865 messageParcel.writeInt(this.num); 5866 messageParcel.writeString(this.str); 5867 return true; 5868 } 5869 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5870 this.num = messageParcel.readInt(); 5871 this.str = messageParcel.readString(); 5872 return true; 5873 } 5874 } 5875 let sequenceable = new MySequenceable(1, "aaa"); 5876 let data = rpc.MessageParcel.create(); 5877 let result = data.writeSequenceable(sequenceable); 5878 console.log("RpcClient: writeSequenceable is " + result); 5879 let ret = new MySequenceable(0, ""); 5880 let result2 = data.readSequenceable(ret); 5881 console.log("RpcClient: readSequenceable is " + result2); 5882 ``` 5883 5884## IRemoteBroker 5885 5886Provides the holder of a remote proxy object. 5887 5888### asObject 5889 5890asObject(): IRemoteObject 5891 5892Obtains a proxy or remote object. This API must be implemented by its derived classes. 5893 5894**System capability**: SystemCapability.Communication.IPC.Core 5895 5896**Return value** 5897 5898| Type | Description | 5899| ----- | ----- | 5900| [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.| 5901 5902**Example** 5903 5904 ```ts 5905 class TestAbility extends rpc.RemoteObject { 5906 asObject() { 5907 return this; 5908 } 5909 } 5910 let remoteObject = new TestAbility("testObject").asObject(); 5911 ``` 5912 5913**Example** 5914 5915 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 5916 5917 ```ts 5918 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 5919 // import FA from "@ohos.ability.featureAbility"; 5920 5921 import Want from '@ohos.app.ability.Want'; 5922 import common from '@ohos.app.ability.common'; 5923 5924 let proxy: rpc.IRemoteObject | undefined = undefined; 5925 let connect: common.ConnectOptions = { 5926 onConnect: (elementName, remoteProxy) => { 5927 console.log("RpcClient: js onConnect called."); 5928 proxy = remoteProxy; 5929 }, 5930 onDisconnect: (elementName) => { 5931 console.log("RpcClient: onDisconnect"); 5932 }, 5933 onFailed: () => { 5934 console.log("RpcClient: onFailed"); 5935 } 5936 }; 5937 let want: Want = { 5938 bundleName: "com.ohos.server", 5939 abilityName: "com.ohos.server.EntryAbility", 5940 }; 5941 5942 // Use this method to connect to the ability for the FA model. 5943 // FA.connectAbility(want,connect); 5944 5945 this.context.connectServiceExtensionAbility(want, connect); 5946 ``` 5947 5948 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. 5949 5950 ```ts 5951 class TestProxy { 5952 remote: rpc.RemoteObject; 5953 constructor(remote: rpc.RemoteObject) { 5954 this.remote = remote; 5955 } 5956 asObject() { 5957 return this.remote; 5958 } 5959 } 5960 let iRemoteObject = new TestProxy(proxy).asObject(); 5961 ``` 5962 5963## DeathRecipient 5964 5965Subscribes 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. 5966 5967### onRemoteDied 5968 5969onRemoteDied(): void 5970 5971Called to perform subsequent operations when a death notification of the remote object is received. 5972 5973**System capability**: SystemCapability.Communication.IPC.Core 5974 5975**Example** 5976 5977 ```ts 5978 class MyDeathRecipient implements rpc.DeathRecipient { 5979 onRemoteDied() { 5980 console.log("server died"); 5981 } 5982 } 5983 ``` 5984 5985## RequestResult<sup>9+</sup> 5986 5987Defines the response to the request. 5988 5989**System capability**: SystemCapability.Communication.IPC.Core 5990 5991| Name | Type | Readable| Writable| Description | 5992| ------- | --------------- | ---- | ---- |-------------------------------------- | 5993| errCode | number | Yes | No | Error code. | 5994| code | number | Yes | No | Message code. | 5995| data | MessageSequence | Yes | No | **MessageSequence** object sent to the remote process.| 5996| reply | MessageSequence | Yes | No | **MessageSequence** object returned by the remote process. | 5997 5998## SendRequestResult<sup>8+(deprecated)</sup> 5999 6000>This API is no longer maintained since API version 9. You are advised to use [RequestResult](#requestresult9). 6001 6002Defines the response to the request. 6003 6004**System capability**: SystemCapability.Communication.IPC.Core 6005 6006| Name | Type | Readable| Writable| Description | 6007| ------- | ------------- | ---- | ---- | ----------------------------------- | 6008| errCode | number | Yes | No | Error code. | 6009| code | number | Yes | No | Message code. | 6010| data | MessageParcel | Yes | No | **MessageParcel** object sent to the remote process.| 6011| reply | MessageParcel | Yes | No | **MessageParcel** object returned by the remote process. | 6012 6013## IRemoteObject 6014 6015Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 6016 6017### getLocalInterface<sup>9+</sup> 6018 6019getLocalInterface(descriptor: string): IRemoteBroker 6020 6021Obtains the interface descriptor. 6022 6023**System capability**: SystemCapability.Communication.IPC.Core 6024 6025**Parameters** 6026 6027| Name | Type | Mandatory| Description | 6028| ---------- | ------ | ---- | -------------------- | 6029| descriptor | string | Yes | Interface descriptor.| 6030 6031**Return value** 6032 6033| Type | Description | 6034| ------------- | --------------------------------------------- | 6035| IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 6036 6037### queryLocalInterface<sup>(deprecated)</sup> 6038 6039>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 6040 6041queryLocalInterface(descriptor: string): IRemoteBroker 6042 6043Queries the interface descriptor. 6044 6045**System capability**: SystemCapability.Communication.IPC.Core 6046 6047**Parameters** 6048 6049| Name | Type | Mandatory| Description | 6050| ---------- | ------ | ---- | -------------------- | 6051| descriptor | string | Yes | Interface descriptor.| 6052 6053**Return value** 6054 6055| Type | Description | 6056| ------------- | --------------------------------------------- | 6057| IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 6058 6059### sendRequest<sup>(deprecated)</sup> 6060 6061>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6062 6063sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6064 6065Sends 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. 6066 6067**System capability**: SystemCapability.Communication.IPC.Core 6068 6069**Parameters** 6070 6071| Name | Type | Mandatory| Description | 6072| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6073| 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.| 6074| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6075| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6076| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6077 6078**Return value** 6079 6080| Type | Description | 6081| ------- | -------------------------------- | 6082| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6083 6084### sendRequest<sup>8+(deprecated)</sup> 6085 6086>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6087 6088sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6089 6090Sends 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. 6091 6092**System capability**: SystemCapability.Communication.IPC.Core 6093 6094**Parameters** 6095 6096| Name | Type | Mandatory| Description | 6097| ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6098| 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.| 6099| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6100| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6101| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6102 6103**Return value** 6104 6105| Type | Description | 6106| -------------------------------- | --------------------------------------------- | 6107| Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 6108 6109### sendMessageRequest<sup>9+</sup> 6110 6111sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6112 6113Sends 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. 6114 6115**System capability**: SystemCapability.Communication.IPC.Core 6116 6117**Parameters** 6118 6119| Name | Type | Mandatory| Description | 6120| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6121| 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.| 6122| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6123| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6124| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6125 6126**Return value** 6127 6128| Type | Description | 6129| ---------------------------- | ----------------------------------------- | 6130| Promise<RequestResult> | Promise used to return the **requestResult** object.| 6131 6132### sendMessageRequest<sup>9+</sup> 6133 6134sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6135 6136Sends 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. 6137 6138**System capability**: SystemCapability.Communication.IPC.Core 6139 6140**Parameters** 6141 6142| Name | Type | Mandatory| Description | 6143| -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6144| 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.| 6145| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6146| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6147| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6148| callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 6149 6150### sendRequest<sup>8+(deprecated)</sup> 6151 6152>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6153 6154sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6155 6156Sends 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. 6157 6158**System capability**: SystemCapability.Communication.IPC.Core 6159 6160**Parameters** 6161 6162| Name | Type | Mandatory| Description | 6163| -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6164| 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.| 6165| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6166| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6167| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6168| callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 6169 6170### registerDeathRecipient<sup>9+</sup> 6171 6172registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6173 6174Registers a callback for receiving death notifications of the remote object. The callback will be called if the remote object process matching the **RemoteProxy** object is killed. 6175 6176**System capability**: SystemCapability.Communication.IPC.Core 6177 6178**Parameters** 6179 6180| Name | Type | Mandatory| Description | 6181| --------- | --------------------------------- | ---- | -------------- | 6182| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6183| flags | number | Yes | Flag of the death notification.| 6184 6185**Error codes** 6186 6187For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6188 6189| ID| Error Message| 6190| -------- | -------- | 6191| 1900008 | proxy or remote object is invalid | 6192 6193### addDeathrecipient<sup>(deprecated)</sup> 6194 6195>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 6196 6197addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6198 6199Adds 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. 6200 6201**System capability**: SystemCapability.Communication.IPC.Core 6202 6203**Parameters** 6204 6205| Name | Type | Mandatory| Description | 6206| --------- | --------------------------------- | ---- | -------------- | 6207| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add.| 6208| flags | number | Yes | Flag of the death notification.| 6209 6210**Return value** 6211 6212| Type | Description | 6213| ------- | ---------------------------------------- | 6214| boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6215 6216### unregisterDeathRecipient<sup>9+</sup> 6217 6218unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6219 6220Unregisters the callback used to receive death notifications of the remote object. 6221 6222**System capability**: SystemCapability.Communication.IPC.Core 6223 6224**Parameters** 6225 6226| Name | Type | Mandatory| Description | 6227| --------- | --------------------------------- | ---- | -------------- | 6228| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6229| flags | number | Yes | Flag of the death notification.| 6230 6231**Error codes** 6232 6233For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6234 6235| ID| Error Message| 6236| -------- | -------- | 6237| 1900008 | proxy or remote object is invalid | 6238 6239### removeDeathRecipient<sup>(deprecated)</sup> 6240 6241>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 6242 6243removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6244 6245Removes the callback used to receive death notifications of the remote object. 6246 6247**System capability**: SystemCapability.Communication.IPC.Core 6248 6249**Parameters** 6250 6251| Name | Type | Mandatory| Description | 6252| --------- | --------------------------------- | ---- | -------------- | 6253| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove.| 6254| flags | number | Yes | Flag of the death notification.| 6255 6256**Return value** 6257 6258| Type | Description | 6259| ------- | -----------------------------------------| 6260| boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6261 6262### getDescriptor<sup>9+</sup> 6263 6264getDescriptor(): string 6265 6266Obtains the interface descriptor (which is a string) of this object. 6267 6268**System capability**: SystemCapability.Communication.IPC.Core 6269 6270**Return value** 6271 6272| Type | Description | 6273| ------ | ---------------- | 6274| string | Interface descriptor obtained.| 6275 6276**Error codes** 6277 6278For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6279 6280| ID| Error Message| 6281| -------- | -------- | 6282| 1900008 | proxy or remote object is invalid | 6283 6284### getInterfaceDescriptor<sup>(deprecated)</sup> 6285 6286>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 6287 6288getInterfaceDescriptor(): string 6289 6290Obtains the interface descriptor (which is a string) of this object. 6291 6292**System capability**: SystemCapability.Communication.IPC.Core 6293 6294**Return value** 6295 6296| Type | Description | 6297| ------ | ---------------- | 6298| string | Interface descriptor obtained.| 6299 6300### isObjectDead 6301 6302isObjectDead(): boolean 6303 6304Checks whether this object is dead. 6305 6306**System capability**: SystemCapability.Communication.IPC.Core 6307 6308**Return value** 6309 6310| Type | Description | 6311| ------- | ---------------------------------- | 6312| boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6313 6314## RemoteProxy 6315 6316Provides APIs to implement **IRemoteObject**. 6317 6318**System capability**: SystemCapability.Communication.IPC.Core 6319 6320| Name | Value | Description | 6321| --------------------- | ----------------------- | --------------------------------- | 6322| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 6323| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. | 6324| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. | 6325| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 6326| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 6327 6328### sendRequest<sup>(deprecated)</sup> 6329 6330>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6331 6332sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6333 6334Sends 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. 6335 6336**System capability**: SystemCapability.Communication.IPC.Core 6337 6338**Parameters** 6339 6340| Name | Type | Mandatory| Description | 6341| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6342| 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.| 6343| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6344| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6345| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6346 6347**Return value** 6348 6349| Type | Description | 6350| ------- | ---------------------------------| 6351| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6352 6353**Example** 6354 6355 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6356 6357 ```ts 6358 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6359 // import FA from "@ohos.ability.featureAbility"; 6360 6361 import Want from '@ohos.app.ability.Want'; 6362 import common from '@ohos.app.ability.common'; 6363 6364 let proxy: rpc.IRemoteObject | undefined = undefined; 6365 let connect: common.ConnectOptions = { 6366 onConnect: (elementName, remoteProxy) => { 6367 console.log("RpcClient: js onConnect called."); 6368 proxy = remoteProxy; 6369 }, 6370 onDisconnect: (elementName) => { 6371 console.log("RpcClient: onDisconnect"); 6372 }, 6373 onFailed: () => { 6374 console.log("RpcClient: onFailed"); 6375 } 6376 }; 6377 let want: Want = { 6378 bundleName: "com.ohos.server", 6379 abilityName: "com.ohos.server.EntryAbility", 6380 }; 6381 6382 // Use this method to connect to the ability for the FA model. 6383 // FA.connectAbility(want,connect); 6384 6385 this.context.connectServiceExtensionAbility(want, connect); 6386 ``` 6387 6388 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. 6389 6390 ```ts 6391 let option = new rpc.MessageOption(); 6392 let data = rpc.MessageParcel.create(); 6393 let reply = rpc.MessageParcel.create(); 6394 data.writeInt(1); 6395 data.writeString("hello"); 6396 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6397 if (ret) { 6398 console.log("sendRequest got result"); 6399 let msg = reply.readString(); 6400 console.log("RPCTest: reply msg: " + msg); 6401 } else { 6402 console.log("RPCTest: sendRequest failed"); 6403 } 6404 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6405 data.reclaim(); 6406 reply.reclaim(); 6407 ``` 6408 6409### sendMessageRequest<sup>9+</sup> 6410 6411sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6412 6413Sends 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. 6414 6415**System capability**: SystemCapability.Communication.IPC.Core 6416 6417**Parameters** 6418 6419| Name | Type | Mandatory| Description | 6420| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6421| 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.| 6422| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6423| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6424| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6425 6426**Return value** 6427 6428| Type | Description | 6429| ---------------------------- | ----------------------------------------- | 6430| Promise<RequestResult> | Promise used to return the **requestResult** object.| 6431 6432**Example** 6433 6434 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6435 6436 ```ts 6437 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6438 // import FA from "@ohos.ability.featureAbility"; 6439 import Want from '@ohos.app.ability.Want'; 6440 import common from '@ohos.app.ability.common'; 6441 6442 let proxy: rpc.IRemoteObject | undefined = undefined; 6443 let connect: common.ConnectOptions = { 6444 onConnect: (elementName, remoteProxy) => { 6445 console.log("RpcClient: js onConnect called."); 6446 proxy = remoteProxy; 6447 }, 6448 onDisconnect: (elementName) => { 6449 console.log("RpcClient: onDisconnect"); 6450 }, 6451 onFailed: () => { 6452 console.log("RpcClient: onFailed"); 6453 } 6454 }; 6455 let want: Want = { 6456 bundleName: "com.ohos.server", 6457 abilityName: "com.ohos.server.EntryAbility", 6458 }; 6459 6460 // Use this method to connect to the ability for the FA model. 6461 // FA.connectAbility(want,connect); 6462 6463 this.context.connectServiceExtensionAbility(want, connect); 6464 ``` 6465 6466 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. 6467 6468 ```ts 6469 let option = new rpc.MessageOption(); 6470 let data = rpc.MessageSequence.create(); 6471 let reply = rpc.MessageSequence.create(); 6472 data.writeInt(1); 6473 data.writeString("hello"); 6474 proxy.sendMessageRequest(1, data, reply, option) 6475 .then((result: rpc.RequestResult) => { 6476 if (result.errCode === 0) { 6477 console.log("sendMessageRequest got result"); 6478 result.reply.readException(); 6479 let msg = result.reply.readString(); 6480 console.log("RPCTest: reply msg: " + msg); 6481 } else { 6482 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 6483 } 6484 }).catch((e: Error) => { 6485 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 6486 }).finally (() => { 6487 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 6488 data.reclaim(); 6489 reply.reclaim(); 6490 }); 6491 ``` 6492 6493### sendRequest<sup>8+(deprecated)</sup> 6494 6495>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6496 6497sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6498 6499Sends 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. 6500 6501**System capability**: SystemCapability.Communication.IPC.Core 6502 6503**Parameters** 6504 6505| Name | Type | Mandatory| Description | 6506| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6507| 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.| 6508| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6509| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6510| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6511 6512**Return value** 6513 6514| Type | Description | 6515| -------------------------------- | --------------------------------------------- | 6516| Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 6517 6518**Example** 6519 6520 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6521 6522 ```ts 6523 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6524 // import FA from "@ohos.ability.featureAbility"; 6525 import Want from '@ohos.app.ability.Want'; 6526 import common from '@ohos.app.ability.common'; 6527 6528 let proxy: rpc.IRemoteObject | undefined = undefined; 6529 let connect: common.ConnectOptions = { 6530 onConnect: (elementName, remoteProxy) => { 6531 console.log("RpcClient: js onConnect called."); 6532 proxy = remoteProxy; 6533 }, 6534 onDisconnect: (elementName) => { 6535 console.log("RpcClient: onDisconnect"); 6536 }, 6537 onFailed: () => { 6538 console.log("RpcClient: onFailed"); 6539 } 6540 }; 6541 let want: Want = { 6542 bundleName: "com.ohos.server", 6543 abilityName: "com.ohos.server.EntryAbility", 6544 }; 6545 6546 // Use this method to connect to the ability for the FA model. 6547 // FA.connectAbility(want,connect); 6548 6549 this.context.connectServiceExtensionAbility(want, connect); 6550 ``` 6551 6552 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. 6553 6554 ```ts 6555 let option = new rpc.MessageOption(); 6556 let data = rpc.MessageParcel.create(); 6557 let reply = rpc.MessageParcel.create(); 6558 data.writeInt(1); 6559 data.writeString("hello"); 6560 proxy.sendRequest(1, data, reply, option) 6561 .then((result: rpc.SendRequestResult) => { 6562 if (result.errCode === 0) { 6563 console.log("sendRequest got result"); 6564 result.reply.readException(); 6565 let msg = result.reply.readString(); 6566 console.log("RPCTest: reply msg: " + msg); 6567 } else { 6568 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6569 } 6570 }).catch((e: Error) => { 6571 console.log("RPCTest: sendRequest got exception: " + e.message); 6572 }).finally (() => { 6573 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6574 data.reclaim(); 6575 reply.reclaim(); 6576 }); 6577 ``` 6578 6579### sendMessageRequest<sup>9+</sup> 6580 6581sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6582 6583Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked at certain time after the response to **sendMessageRequest** is returned, and the reply contains the returned information. 6584 6585**System capability**: SystemCapability.Communication.IPC.Core 6586 6587**Parameters** 6588 6589| Name | Type | Mandatory| Description | 6590| -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6591| 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.| 6592| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6593| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6594| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6595| callback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 6596 6597**Example** 6598 6599 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6600 6601 ```ts 6602 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6603 // import FA from "@ohos.ability.featureAbility"; 6604 import Want from '@ohos.app.ability.Want'; 6605 import common from '@ohos.app.ability.common'; 6606 import { BusinessError } from '@ohos.base'; 6607 6608 let proxy: rpc.IRemoteObject | undefined = undefined; 6609 let connect: common.ConnectOptions = { 6610 onConnect: (elementName, remoteProxy) => { 6611 console.log("RpcClient: js onConnect called."); 6612 proxy = remoteProxy; 6613 }, 6614 onDisconnect: (elementName) => { 6615 console.log("RpcClient: onDisconnect"); 6616 }, 6617 onFailed: () => { 6618 console.log("RpcClient: onFailed"); 6619 } 6620 }; 6621 let want: Want = { 6622 bundleName: "com.ohos.server", 6623 abilityName: "com.ohos.server.EntryAbility", 6624 }; 6625 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 6626 if (result.errCode === 0) { 6627 console.log("sendRequest got result"); 6628 result.reply.readException(); 6629 let msg = result.reply.readString(); 6630 console.log("RPCTest: reply msg: " + msg); 6631 } else { 6632 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6633 } 6634 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6635 result.data.reclaim(); 6636 result.reply.reclaim(); 6637} 6638 6639 // Use this method to connect to the ability for the FA model. 6640 // FA.connectAbility(want,connect); 6641 6642 this.context.connectServiceExtensionAbility(want, connect); 6643 ``` 6644 6645 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. 6646 6647 ```ts 6648 import { BusinessError } from '@ohos.base'; 6649 6650 let option = new rpc.MessageOption(); 6651 let data = rpc.MessageSequence.create(); 6652 let reply = rpc.MessageSequence.create(); 6653 data.writeInt(1); 6654 data.writeString("hello"); 6655 try { 6656 proxy.sendMessageRequest(1, data, reply, option, sendRequestCallback); 6657 } catch(error) { 6658 let e: BusinessError = error as BusinessError; 6659 console.info("rpc send sequence request fail, errorCode " + e.code); 6660 console.info("rpc send sequence request fail, errorMessage " + e.message); 6661 } 6662 ``` 6663 6664### sendRequest<sup>8+(deprecated)</sup> 6665 6666>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 6667 6668sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6669 6670Sends 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. 6671 6672**System capability**: SystemCapability.Communication.IPC.Core 6673 6674**Parameters** 6675 6676| Name | Type | Mandatory| Description | 6677| -------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6678| 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.| 6679| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6680| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6681| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6682| callback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 6683 6684**Example** 6685 6686 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6687 6688 ```ts 6689 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6690 // import FA from "@ohos.ability.featureAbility"; 6691 import Want from '@ohos.app.ability.Want'; 6692 import common from '@ohos.app.ability.common'; 6693 import { BusinessError } from '@ohos.base'; 6694 6695 let proxy: rpc.IRemoteObject | undefined = undefined; 6696 let connect: common.ConnectOptions = { 6697 onConnect: (elementName, remoteProxy) => { 6698 console.log("RpcClient: js onConnect called."); 6699 proxy = remoteProxy; 6700 }, 6701 onDisconnect: (elementName) => { 6702 console.log("RpcClient: onDisconnect"); 6703 }, 6704 onFailed: () => { 6705 console.log("RpcClient: onFailed"); 6706 } 6707 }; 6708 let want: Want = { 6709 bundleName: "com.ohos.server", 6710 abilityName: "com.ohos.server.EntryAbility", 6711 }; 6712 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 6713 if (result.errCode === 0) { 6714 console.log("sendRequest got result"); 6715 result.reply.readException(); 6716 let msg = result.reply.readString(); 6717 console.log("RPCTest: reply msg: " + msg); 6718 } else { 6719 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 6720 } 6721 console.log("RPCTest: sendRequest ends, reclaim parcel"); 6722 result.data.reclaim(); 6723 result.reply.reclaim(); 6724} 6725 6726 // Use this method to connect to the ability for the FA model. 6727 // FA.connectAbility(want,connect); 6728 6729 this.context.connectServiceExtensionAbility(want, connect); 6730 ``` 6731 6732 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. 6733 6734 ```ts 6735 let option = new rpc.MessageOption(); 6736 let data = rpc.MessageParcel.create(); 6737 let reply = rpc.MessageParcel.create(); 6738 data.writeInt(1); 6739 data.writeString("hello"); 6740 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 6741 ``` 6742 6743### getLocalInterface<sup>9+</sup> 6744 6745getLocalInterface(interface: string): IRemoteBroker 6746 6747Obtains the **LocalInterface** object of an interface token. 6748 6749**System capability**: SystemCapability.Communication.IPC.Core 6750 6751**Parameters** 6752 6753| Name | Type | Mandatory| Description | 6754| --------- | ------ | ---- | ---------------------- | 6755| interface | string | Yes | Interface descriptor.| 6756 6757**Return value** 6758 6759| Type | Description | 6760| ------------- | ------------------------------------------ | 6761| IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6762 6763**Error codes** 6764 6765For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6766 6767| ID| Error Message| 6768| -------- | -------- | 6769| 1900006 | only remote object permitted | 6770 6771**Example** 6772 6773 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6774 6775 ```ts 6776 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6777 // import FA from "@ohos.ability.featureAbility"; 6778 import Want from '@ohos.app.ability.Want'; 6779 import common from '@ohos.app.ability.common'; 6780 6781 let proxy: rpc.IRemoteObject | undefined = undefined; 6782 let connect: common.ConnectOptions = { 6783 onConnect: (elementName, remoteProxy) => { 6784 console.log("RpcClient: js onConnect called."); 6785 proxy = remoteProxy; 6786 }, 6787 onDisconnect: (elementName) => { 6788 console.log("RpcClient: onDisconnect"); 6789 }, 6790 onFailed: () => { 6791 console.log("RpcClient: onFailed"); 6792 } 6793 }; 6794 let want: Want = { 6795 bundleName: "com.ohos.server", 6796 abilityName: "com.ohos.server.EntryAbility", 6797 }; 6798 6799 // Use this method to connect to the ability for the FA model. 6800 // FA.connectAbility(want,connect); 6801 6802 this.context.connectServiceExtensionAbility(want, connect); 6803 ``` 6804 6805 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. 6806 6807 ```ts 6808 import { BusinessError } from '@ohos.base'; 6809 6810 try { 6811 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 6812 console.log("RpcClient: getLocalInterface is " + broker); 6813 } catch(error) { 6814 let e: BusinessError = error as BusinessError; 6815 console.info("rpc get local interface fail, errorCode " + e.code); 6816 console.info("rpc get local interface fail, errorMessage " + e.message); 6817 } 6818 ``` 6819 6820### queryLocalInterface<sup>(deprecated)</sup> 6821 6822>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 6823 6824queryLocalInterface(interface: string): IRemoteBroker 6825 6826Obtains the **LocalInterface** object of an interface token. 6827 6828**System capability**: SystemCapability.Communication.IPC.Core 6829 6830**Parameters** 6831 6832| Name | Type | Mandatory| Description | 6833| --------- | ------ | ---- | ---------------------- | 6834| interface | string | Yes | Interface descriptor.| 6835 6836**Return value** 6837 6838| Type | Description | 6839| ------------- | ------------------------------------------ | 6840| IRemoteBroker | Returns **Null** by default, which indicates a proxy interface.| 6841 6842**Example** 6843 6844 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6845 6846 ```ts 6847 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6848 // import FA from "@ohos.ability.featureAbility"; 6849 import Want from '@ohos.app.ability.Want'; 6850 import common from '@ohos.app.ability.common'; 6851 6852 let proxy: rpc.IRemoteObject | undefined = undefined; 6853 let connect: common.ConnectOptions = { 6854 onConnect: (elementName, remoteProxy) => { 6855 console.log("RpcClient: js onConnect called."); 6856 proxy = remoteProxy; 6857 }, 6858 onDisconnect: (elementName) => { 6859 console.log("RpcClient: onDisconnect"); 6860 }, 6861 onFailed: () => { 6862 console.log("RpcClient: onFailed"); 6863 } 6864 }; 6865 let want: Want = { 6866 bundleName: "com.ohos.server", 6867 abilityName: "com.ohos.server.EntryAbility", 6868 }; 6869 6870 // Use this method to connect to the ability for the FA model. 6871 // FA.connectAbility(want,connect); 6872 6873 this.context.connectServiceExtensionAbility(want, connect); 6874 ``` 6875 6876 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. 6877 6878 ```ts 6879 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 6880 console.log("RpcClient: queryLocalInterface is " + broker); 6881 ``` 6882 6883### registerDeathRecipient<sup>9+</sup> 6884 6885registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6886 6887Registers a callback for receiving death notifications of the remote object. The callback will be invoked when the remote object process matching the **RemoteProxy** object is killed. 6888 6889**System capability**: SystemCapability.Communication.IPC.Core 6890 6891**Parameters** 6892 6893| Name | Type | Mandatory| Description | 6894| --------- | --------------------------------- | ---- | -------------- | 6895| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6896| flags | number | Yes | Flag of the death notification.| 6897 6898**Error codes** 6899 6900For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 6901 6902| ID| Error Message| 6903| -------- | -------- | 6904| 1900008 | proxy or remote object is invalid | 6905 6906**Example** 6907 6908 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6909 6910 ```ts 6911 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6912 // import FA from "@ohos.ability.featureAbility"; 6913 import Want from '@ohos.app.ability.Want'; 6914 import common from '@ohos.app.ability.common'; 6915 6916 let proxy: rpc.IRemoteObject | undefined = undefined; 6917 let connect: common.ConnectOptions = { 6918 onConnect: (elementName, remoteProxy) => { 6919 console.log("RpcClient: js onConnect called."); 6920 proxy = remoteProxy; 6921 }, 6922 onDisconnect: (elementName) => { 6923 console.log("RpcClient: onDisconnect"); 6924 }, 6925 onFailed: () => { 6926 console.log("RpcClient: onFailed"); 6927 } 6928 }; 6929 let want: Want = { 6930 bundleName: "com.ohos.server", 6931 abilityName: "com.ohos.server.EntryAbility", 6932 }; 6933 6934 // Use this method to connect to the ability for the FA model. 6935 // FA.connectAbility(want,connect); 6936 6937 this.context.connectServiceExtensionAbility(want, connect); 6938 ``` 6939 6940 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. 6941 6942 ```ts 6943 import { BusinessError } from '@ohos.base'; 6944 6945 class MyDeathRecipient implements rpc.DeathRecipient { 6946 onRemoteDied() { 6947 console.log("server died"); 6948 } 6949 } 6950 let deathRecipient = new MyDeathRecipient(); 6951 try { 6952 proxy.registerDeathRecipient(deathRecipient, 0); 6953 } catch(error) { 6954 let e: BusinessError = error as BusinessError; 6955 console.info("proxy register deathRecipient fail, errorCode " + e.code); 6956 console.info("proxy register deathRecipient fail, errorMessage " + e.message); 6957 } 6958 ``` 6959 6960### addDeathRecipient<sup>(deprecated)</sup> 6961 6962>This API is no longer maintained since API version 9. You are advised to use [registerDeathRecipient](#registerdeathrecipient9). 6963 6964addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6965 6966Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 6967 6968**System capability**: SystemCapability.Communication.IPC.Core 6969 6970**Parameters** 6971 6972| Name | Type | Mandatory| Description | 6973| --------- | --------------------------------- | ---- | --------------------------------- | 6974| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 6975| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 6976 6977**Return value** 6978 6979| Type | Description | 6980| ------- | ---------------------------------------- | 6981| boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6982 6983**Example** 6984 6985 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 6986 6987 ```ts 6988 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 6989 // import FA from "@ohos.ability.featureAbility"; 6990 import Want from '@ohos.app.ability.Want'; 6991 import common from '@ohos.app.ability.common'; 6992 6993 let proxy: rpc.IRemoteObject | undefined = undefined; 6994 let connect: common.ConnectOptions = { 6995 onConnect: (elementName, remoteProxy) => { 6996 console.log("RpcClient: js onConnect called."); 6997 proxy = remoteProxy; 6998 }, 6999 onDisconnect: (elementName) => { 7000 console.log("RpcClient: onDisconnect"); 7001 }, 7002 onFailed: () => { 7003 console.log("RpcClient: onFailed"); 7004 } 7005 }; 7006 let want: Want = { 7007 bundleName: "com.ohos.server", 7008 abilityName: "com.ohos.server.EntryAbility", 7009 }; 7010 7011 // Use this method to connect to the ability for the FA model. 7012 // FA.connectAbility(want,connect); 7013 7014 this.context.connectServiceExtensionAbility(want, connect); 7015 ``` 7016 7017 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. 7018 7019 ```ts 7020 class MyDeathRecipient implements rpc.DeathRecipient { 7021 onRemoteDied() { 7022 console.log("server died"); 7023 } 7024 } 7025 let deathRecipient = new MyDeathRecipient(); 7026 proxy.addDeathRecipient(deathRecipient, 0); 7027 ``` 7028 7029### unregisterDeathRecipient<sup>9+</sup> 7030 7031unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7032 7033Unregisters the callback used to receive death notifications of the remote object. 7034 7035**System capability**: SystemCapability.Communication.IPC.Core 7036 7037**Parameters** 7038 7039| Name | Type | Mandatory| Description | 7040| --------- | --------------------------------- | ---- | -------------- | 7041| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 7042| flags | number | Yes | Flag of the death notification.| 7043 7044**Error codes** 7045 7046For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7047 7048| ID| Error Message| 7049| -------- | -------- | 7050| 1900008 | proxy or remote object is invalid | 7051 7052**Example** 7053 7054 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7055 7056 ```ts 7057 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7058 // import FA from "@ohos.ability.featureAbility"; 7059 import Want from '@ohos.app.ability.Want'; 7060 import common from '@ohos.app.ability.common'; 7061 7062 let proxy: rpc.IRemoteObject | undefined = undefined; 7063 let connect: common.ConnectOptions = { 7064 onConnect: (elementName, remoteProxy) => { 7065 console.log("RpcClient: js onConnect called."); 7066 proxy = remoteProxy; 7067 }, 7068 onDisconnect: (elementName) => { 7069 console.log("RpcClient: onDisconnect"); 7070 }, 7071 onFailed: () => { 7072 console.log("RpcClient: onFailed"); 7073 } 7074 }; 7075 let want: Want = { 7076 bundleName: "com.ohos.server", 7077 abilityName: "com.ohos.server.EntryAbility", 7078 }; 7079 7080 // Use this method to connect to the ability for the FA model. 7081 // FA.connectAbility(want,connect); 7082 7083 this.context.connectServiceExtensionAbility(want, connect); 7084 ``` 7085 7086 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. 7087 7088 ```ts 7089 import { BusinessError } from '@ohos.base'; 7090 7091 class MyDeathRecipient implements rpc.DeathRecipient { 7092 onRemoteDied() { 7093 console.log("server died"); 7094 } 7095 } 7096 let deathRecipient = new MyDeathRecipient(); 7097 try { 7098 proxy.registerDeathRecipient(deathRecipient, 0); 7099 proxy.unregisterDeathRecipient(deathRecipient, 0); 7100 } catch(error) { 7101 let e: BusinessError = error as BusinessError; 7102 console.info("proxy register deathRecipient fail, errorCode " + e.code); 7103 console.info("proxy register deathRecipient fail, errorMessage " + e.message); 7104 } 7105 ``` 7106 7107### removeDeathRecipient<sup>(deprecated)</sup> 7108 7109>This API is no longer maintained since API version 9. You are advised to use [unregisterDeathRecipient](#unregisterdeathrecipient9). 7110 7111removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7112 7113Removes the callback used to receive death notifications of the remote object. 7114 7115**System capability**: SystemCapability.Communication.IPC.Core 7116 7117**Parameters** 7118 7119| Name | Type | Mandatory| Description | 7120| --------- | --------------------------------- | ---- | --------------------------------- | 7121| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 7122| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7123 7124**Return value** 7125 7126| Type | Description | 7127| ------- | ---------------------------------------- | 7128| boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 7129 7130**Example** 7131 7132 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7133 7134 ```ts 7135 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7136 // import FA from "@ohos.ability.featureAbility"; 7137 import Want from '@ohos.app.ability.Want'; 7138 import common from '@ohos.app.ability.common'; 7139 7140 let proxy: rpc.IRemoteObject | undefined = undefined; 7141 let connect: common.ConnectOptions = { 7142 onConnect: (elementName, remoteProxy) => { 7143 console.log("RpcClient: js onConnect called."); 7144 proxy = remoteProxy; 7145 }, 7146 onDisconnect: (elementName) => { 7147 console.log("RpcClient: onDisconnect"); 7148 }, 7149 onFailed: () => { 7150 console.log("RpcClient: onFailed"); 7151 } 7152 }; 7153 let want: Want = { 7154 bundleName: "com.ohos.server", 7155 abilityName: "com.ohos.server.EntryAbility", 7156 }; 7157 7158 // Use this method to connect to the ability for the FA model. 7159 // FA.connectAbility(want,connect); 7160 7161 this.context.connectServiceExtensionAbility(want, connect); 7162 ``` 7163 7164 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. 7165 7166 ```ts 7167 class MyDeathRecipient implements rpc.DeathRecipient { 7168 onRemoteDied() { 7169 console.log("server died"); 7170 } 7171 } 7172 let deathRecipient = new MyDeathRecipient(); 7173 proxy.addDeathRecipient(deathRecipient, 0); 7174 proxy.removeDeathRecipient(deathRecipient, 0); 7175 ``` 7176 7177### getDescriptor<sup>9+</sup> 7178 7179getDescriptor(): string 7180 7181Obtains the interface descriptor (which is a string) of this proxy object. 7182 7183**System capability**: SystemCapability.Communication.IPC.Core 7184 7185**Return value** 7186 7187| Type | Description | 7188| ------ | ---------------- | 7189| string | Interface descriptor obtained.| 7190 7191**Error codes** 7192 7193For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 7194 7195| ID| Error Message| 7196| -------- | -------- | 7197| 1900008 | proxy or remote object is invalid | 7198| 1900007 | communication failed | 7199 7200**Example** 7201 7202 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7203 7204 ```ts 7205 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7206 // import FA from "@ohos.ability.featureAbility"; 7207 import Want from '@ohos.app.ability.Want'; 7208 import common from '@ohos.app.ability.common'; 7209 7210 let proxy: rpc.IRemoteObject | undefined = undefined; 7211 let connect: common.ConnectOptions = { 7212 onConnect: (elementName, remoteProxy) => { 7213 console.log("RpcClient: js onConnect called."); 7214 proxy = remoteProxy; 7215 }, 7216 onDisconnect: (elementName) => { 7217 console.log("RpcClient: onDisconnect"); 7218 }, 7219 onFailed: () => { 7220 console.log("RpcClient: onFailed"); 7221 } 7222 }; 7223 let want: Want = { 7224 bundleName: "com.ohos.server", 7225 abilityName: "com.ohos.server.EntryAbility", 7226 }; 7227 7228 // Use this method to connect to the ability for the FA model. 7229 // FA.connectAbility(want,connect); 7230 7231 this.context.connectServiceExtensionAbility(want, connect); 7232 ``` 7233 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. 7234 7235 ```ts 7236 import { BusinessError } from '@ohos.base'; 7237 7238 try { 7239 let descriptor: string = proxy.getDescriptor(); 7240 console.log("RpcClient: descriptor is " + descriptor); 7241 } catch(error) { 7242 let e: BusinessError = error as BusinessError; 7243 console.info("rpc get interface descriptor fail, errorCode " + e.code); 7244 console.info("rpc get interface descriptor fail, errorMessage " + e.message); 7245 } 7246 ``` 7247 7248### getInterfaceDescriptor<sup>(deprecated)</sup> 7249 7250>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 7251 7252getInterfaceDescriptor(): string 7253 7254Obtains the interface descriptor of this proxy object. 7255 7256**System capability**: SystemCapability.Communication.IPC.Core 7257 7258**Return value** 7259 7260| Type | Description | 7261| ------ | ------------------ | 7262| string | Interface descriptor obtained.| 7263 7264**Example** 7265 7266 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7267 7268 ```ts 7269 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7270 // import FA from "@ohos.ability.featureAbility"; 7271 import Want from '@ohos.app.ability.Want'; 7272 import common from '@ohos.app.ability.common'; 7273 7274 let proxy: rpc.IRemoteObject | undefined = undefined; 7275 let connect: common.ConnectOptions = { 7276 onConnect: (elementName, remoteProxy) => { 7277 console.log("RpcClient: js onConnect called."); 7278 proxy = remoteProxy; 7279 }, 7280 onDisconnect: (elementName) => { 7281 console.log("RpcClient: onDisconnect"); 7282 }, 7283 onFailed: () => { 7284 console.log("RpcClient: onFailed"); 7285 } 7286 }; 7287 let want: Want = { 7288 bundleName: "com.ohos.server", 7289 abilityName: "com.ohos.server.EntryAbility", 7290 }; 7291 7292 // Use this method to connect to the ability for the FA model. 7293 // FA.connectAbility(want,connect); 7294 7295 this.context.connectServiceExtensionAbility(want, connect); 7296 ``` 7297 7298 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. 7299 7300 ```ts 7301 let descriptor: string = proxy.getInterfaceDescriptor(); 7302 console.log("RpcClient: descriptor is " + descriptor); 7303 ``` 7304 7305### isObjectDead 7306 7307isObjectDead(): boolean 7308 7309Checks whether the **RemoteObject** is dead. 7310 7311**System capability**: SystemCapability.Communication.IPC.Core 7312 7313**Return value** 7314 7315| Type | Description | 7316| ------- | ------------------------------------------------- | 7317| boolean | Returns **true** if the **RemoteObject** is dead; returns **false** otherwise.| 7318 7319**Example** 7320 7321 Before obtaining the ability for the application developed based on the stage model, obtain the context. For details, see [Obtaining the Context](#obtaining-the-context). 7322 7323 ```ts 7324 // Import @ohos.ability.featureAbility only for the application developed based on the FA model. 7325 // import FA from "@ohos.ability.featureAbility"; 7326 import Want from '@ohos.app.ability.Want'; 7327 import common from '@ohos.app.ability.common'; 7328 7329 let proxy: rpc.IRemoteObject | undefined = undefined; 7330 let connect: common.ConnectOptions = { 7331 onConnect: (elementName, remoteProxy) => { 7332 console.log("RpcClient: js onConnect called."); 7333 proxy = remoteProxy; 7334 }, 7335 onDisconnect: (elementName) => { 7336 console.log("RpcClient: onDisconnect"); 7337 }, 7338 onFailed: () => { 7339 console.log("RpcClient: onFailed"); 7340 } 7341 }; 7342 let want: Want = { 7343 bundleName: "com.ohos.server", 7344 abilityName: "com.ohos.server.EntryAbility", 7345 }; 7346 7347 // Use this method to connect to the ability for the FA model. 7348 // FA.connectAbility(want,connect); 7349 7350 this.context.connectServiceExtensionAbility(want, connect); 7351 ``` 7352 7353 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. 7354 7355 ```ts 7356 let isDead: boolean = proxy.isObjectDead(); 7357 console.log("RpcClient: isObjectDead is " + isDead); 7358 ``` 7359 7360## MessageOption 7361 7362Provides common message options (flag and wait time). Use the specified flag to construct the **MessageOption** object. 7363 7364**System capability**: SystemCapability.Communication.IPC.Core 7365 7366| Name | Value | Description | 7367| ------------- | --------- | ----------------------------------------------------------- | 7368| TF_SYNC | 0 (0x00) | Synchronous call. | 7369| TF_ASYNC | 1 (0x01) | Asynchronous call. | 7370| TF_ACCEPT_FDS | 16 (0x10) | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.| 7371| TF_WAIT_TIME | 4 (0x4) | Default waiting time, in seconds. | 7372 7373### constructor<sup>9+</sup> 7374 7375constructor(async?: boolean); 7376 7377A constructor used to create a **MessageOption** object. 7378 7379**System capability**: SystemCapability.Communication.IPC.Core 7380 7381**Parameters** 7382 7383| Name| Type | Mandatory| Description | 7384| ------ | ------- | ---- | -------------------------------------- | 7385| async | boolean | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**.| 7386 7387**Example** 7388 7389 ```ts 7390 class TestRemoteObject extends rpc.MessageOption { 7391 constructor(async: boolean) { 7392 super(async); 7393 } 7394 } 7395 ``` 7396 7397### constructor 7398 7399constructor(syncFlags?: number, waitTime?: number) 7400 7401A constructor used to create a **MessageOption** object. 7402 7403**System capability**: SystemCapability.Communication.IPC.Core 7404 7405**Parameters** 7406 7407| Name | Type | Mandatory| Description | 7408| --------- | ------ | ---- | --------------------------------------------- | 7409| syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 7410| waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 7411 7412**Example** 7413 7414 ```ts 7415 class TestRemoteObject extends rpc.MessageOption { 7416 constructor(syncFlags?: number,waitTime?: number) { 7417 super(syncFlags,waitTime); 7418 } 7419 } 7420 ``` 7421### isAsync<sup>9+</sup> 7422 7423isAsync(): boolean; 7424 7425Checks whether **SendMessageRequest** is called synchronously or asynchronously. 7426 7427**System capability**: SystemCapability.Communication.IPC.Core 7428 7429**Return value** 7430 7431| Type | Description | 7432| ------- | ---------------------------------------- | 7433| boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 7434 7435**Example** 7436 7437 ```ts 7438 let option = new rpc.MessageOption(); 7439 option.isAsync(); 7440 ``` 7441 7442### setAsync<sup>9+</sup> 7443 7444setAsync(async: boolean): void; 7445 7446Sets whether **SendMessageRequest** is called synchronously or asynchronously. 7447 7448**System capability**: SystemCapability.Communication.IPC.Core 7449 7450**Example** 7451 7452 ```ts 7453 let option = new rpc.MessageOption(); 7454 option.setAsync(true); 7455 console.log("Set synchronization flag"); 7456 ``` 7457 7458### getFlags 7459 7460getFlags(): number 7461 7462Obtains the call flag, which can be synchronous or asynchronous. 7463 7464**System capability**: SystemCapability.Communication.IPC.Core 7465 7466**Return value** 7467 7468| Type | Description | 7469| ------ | ------------------------------------ | 7470| number | Call mode obtained.| 7471 7472**Example** 7473 7474 ```ts 7475 try { 7476 let option = new rpc.MessageOption(); 7477 console.info("create object successfully."); 7478 let flog = option.getFlags(); 7479 console.info("run getFlags success, flog is " + flog); 7480 option.setFlags(1) 7481 console.info("run setFlags success"); 7482 let flog2 = option.getFlags(); 7483 console.info("run getFlags success, flog2 is " + flog2); 7484 } catch (error) { 7485 console.info("error " + error); 7486 } 7487 ``` 7488 7489### setFlags 7490 7491setFlags(flags: number): void 7492 7493Sets the call flag, which can be synchronous or asynchronous. 7494 7495**System capability**: SystemCapability.Communication.IPC.Core 7496 7497**Parameters** 7498 7499| Name| Type | Mandatory| Description | 7500| ------ | ------ | ---- | ------------------------ | 7501| flags | number | Yes | Call flag to set.| 7502 7503**Example** 7504 7505 ```ts 7506 try { 7507 let option = new rpc.MessageOption(); 7508 option.setFlags(1) 7509 console.info("run setFlags success"); 7510 let flog = option.getFlags(); 7511 console.info("run getFlags success, flog is " + flog); 7512 } catch (error) { 7513 console.info("error " + error); 7514 } 7515 ``` 7516 7517### getWaitTime 7518 7519getWaitTime(): number 7520 7521Obtains the maximum wait time for this RPC call. 7522 7523**System capability**: SystemCapability.Communication.IPC.Core 7524 7525**Return value** 7526 7527| Type | Description | 7528| ------ | ----------------- | 7529| number | Maximum wait time obtained.| 7530 7531**Example** 7532 7533 ```ts 7534 try { 7535 let option = new rpc.MessageOption(); 7536 let time = option.getWaitTime(); 7537 console.info("run getWaitTime success, time is " + time); 7538 option.setWaitTime(16); 7539 let time2 = option.getWaitTime(); 7540 console.info("run getWaitTime success, time is " + time2); 7541 } catch (error) { 7542 console.info("error " + error); 7543 } 7544 ``` 7545 7546### setWaitTime 7547 7548setWaitTime(waitTime: number): void 7549 7550Sets the maximum wait time for this RPC call. 7551 7552**System capability**: SystemCapability.Communication.IPC.Core 7553 7554**Parameters** 7555 7556| Name | Type | Mandatory| Description | 7557| -------- | ------ | ---- | --------------------- | 7558| waitTime | number | Yes | Maximum wait time to set.| 7559 7560**Example** 7561 7562 ```ts 7563 try { 7564 let option = new rpc.MessageOption(); 7565 option.setWaitTime(16); 7566 let time = option.getWaitTime(); 7567 console.info("run getWaitTime success, time is " + time); 7568 } catch (error) { 7569 console.info("error " + error); 7570 } 7571 ``` 7572 7573## IPCSkeleton 7574 7575Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 7576 7577### getContextObject 7578 7579static getContextObject(): IRemoteObject 7580 7581Obtains the system capability manager. This API is a static method. 7582 7583**System capability**: SystemCapability.Communication.IPC.Core 7584 7585**Return value** 7586 7587| Type | Description | 7588| ------------------------------- | -------------------- | 7589| [IRemoteObject](#iremoteobject) | System capability manager obtained.| 7590 7591**Example** 7592 7593 ```ts 7594 let samgr = rpc.IPCSkeleton.getContextObject(); 7595 console.log("RpcServer: getContextObject result: " + samgr); 7596 ``` 7597 7598### getCallingPid 7599 7600static getCallingPid(): number 7601 7602Obtains 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. 7603 7604**System capability**: SystemCapability.Communication.IPC.Core 7605 7606**Return value** 7607 7608| Type | Description | 7609| ------ | ----------------- | 7610| number | PID of the caller.| 7611 7612**Example** 7613 7614 ```ts 7615 class Stub extends rpc.RemoteObject { 7616 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7617 let callerPid = rpc.IPCSkeleton.getCallingPid(); 7618 console.log("RpcServer: getCallingPid result: " + callerPid); 7619 return true; 7620 } 7621 } 7622 ``` 7623 7624### getCallingUid 7625 7626static getCallingUid(): number 7627 7628Obtains 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. 7629 7630**System capability**: SystemCapability.Communication.IPC.Core 7631 7632**Return value** 7633 7634| Type | Description | 7635| ------ | ----------------- | 7636| number | UID of the caller.| 7637 7638**Example** 7639 7640 ```ts 7641 class Stub extends rpc.RemoteObject { 7642 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7643 let callerUid = rpc.IPCSkeleton.getCallingUid(); 7644 console.log("RpcServer: getCallingUid result: " + callerUid); 7645 return true; 7646 } 7647 } 7648 ``` 7649 7650### getCallingTokenId<sup>8+</sup> 7651 7652static getCallingTokenId(): number; 7653 7654Obtains the caller's token ID, which is used to verify the caller identity. 7655 7656**System capability**: SystemCapability.Communication.IPC.Core 7657 7658**Return value** 7659 7660| Type | Description | 7661| ------ | --------------------- | 7662| number | Token ID of the caller obtained.| 7663 7664**Example** 7665 7666 ```ts 7667 class Stub extends rpc.RemoteObject { 7668 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7669 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 7670 console.log("RpcServer: getCallingTokenId result: " + callerTokenId); 7671 return true; 7672 } 7673 } 7674 ``` 7675 7676### getCallingDeviceID 7677 7678static getCallingDeviceID(): string 7679 7680Obtains the ID of the device hosting the caller's process. This API is a static method. 7681 7682**System capability**: SystemCapability.Communication.IPC.Core 7683 7684**Return value** 7685 7686| Type | Description | 7687| ------ | ---------------------------- | 7688| string | Device ID obtained.| 7689 7690**Example** 7691 7692 ```ts 7693 class Stub extends rpc.RemoteObject { 7694 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7695 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 7696 console.log("RpcServer: callerDeviceID is: " + callerDeviceID); 7697 return true; 7698 } 7699 } 7700 ``` 7701 7702### getLocalDeviceID 7703 7704static getLocalDeviceID(): string 7705 7706Obtains the local device ID. This API is a static method. 7707 7708**System capability**: SystemCapability.Communication.IPC.Core 7709 7710**Return value** 7711 7712| Type | Description | 7713| ------ | ------------------ | 7714| string | Local device ID obtained.| 7715 7716**Example** 7717 7718 ```ts 7719 class Stub extends rpc.RemoteObject { 7720 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7721 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 7722 console.log("RpcServer: localDeviceID is: " + localDeviceID); 7723 return true; 7724 } 7725 } 7726 ``` 7727 7728### isLocalCalling 7729 7730static isLocalCalling(): boolean 7731 7732Checks whether the remote process is a process of the local device. This API is a static method. 7733 7734**System capability**: SystemCapability.Communication.IPC.Core 7735 7736**Return value** 7737 7738| Type | Description | 7739| ------- | -------------------------------------------------- | 7740| boolean | Returns **true** if the local and remote processes are on the same device; returns **false** otherwise.| 7741 7742**Example** 7743 7744 ```ts 7745 class Stub extends rpc.RemoteObject { 7746 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7747 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 7748 console.log("RpcServer: isLocalCalling is: " + isLocalCalling); 7749 return true; 7750 } 7751 } 7752 ``` 7753 7754### flushCmdBuffer<sup>9+</sup> 7755 7756static flushCmdBuffer(object: IRemoteObject): void 7757 7758Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed. 7759 7760**System capability**: SystemCapability.Communication.IPC.Core 7761 7762**Parameters** 7763 7764| Name| Type | Mandatory| Description | 7765| ------ | ------------------------------- | ---- | ------------------- | 7766| object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7767 7768**Example** 7769 7770 ```ts 7771 import { BusinessError } from '@ohos.base'; 7772 7773 class TestRemoteObject extends rpc.RemoteObject { 7774 constructor(descriptor: string) { 7775 super(descriptor); 7776 } 7777 } 7778 let remoteObject = new TestRemoteObject("aaa"); 7779 try { 7780 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 7781 } catch(error) { 7782 let e: BusinessError = error as BusinessError; 7783 console.info("proxy set calling identity fail, errorCode " + e.code); 7784 console.info("proxy set calling identity fail, errorMessage " + e.message); 7785 } 7786 ``` 7787 7788### flushCommands<sup>(deprecated)</sup> 7789 7790>This API is no longer maintained since API version 9. You are advised to use [flushCmdBuffer](#flushcmdbuffer9). 7791 7792static flushCommands(object: IRemoteObject): number 7793 7794Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. It is recommended that this method be called before any time-sensitive operation is performed. 7795 7796**System capability**: SystemCapability.Communication.IPC.Core 7797 7798**Parameters** 7799 7800| Name| Type | Mandatory| Description | 7801| ------ | ------------------------------- | ---- | ------------------- | 7802| object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 7803 7804**Return value** 7805 7806| Type | Description | 7807| ------ | --------------------------------------------------------------------------------- | 7808| 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.| 7809 7810**Example** 7811 7812 ```ts 7813 class MyDeathRecipient implements rpc.DeathRecipient { 7814 onRemoteDied() { 7815 console.log("server died"); 7816 } 7817 } 7818 class TestRemoteObject extends rpc.RemoteObject { 7819 constructor(descriptor: string) { 7820 super(descriptor); 7821 } 7822 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7823 return true; 7824 } 7825 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7826 return true; 7827 } 7828 isObjectDead(): boolean { 7829 return false; 7830 } 7831 } 7832 let remoteObject = new TestRemoteObject("aaa"); 7833 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 7834 console.log("RpcServer: flushCommands result: " + ret); 7835 ``` 7836 7837### resetCallingIdentity 7838 7839static resetCallingIdentity(): string 7840 7841Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication. 7842 7843**System capability**: SystemCapability.Communication.IPC.Core 7844 7845**Return value** 7846 7847| Type | Description | 7848| ------ | ------------------------------------ | 7849| string | String containing the UID and PID of the remote user.| 7850 7851**Example** 7852 7853 ```ts 7854 class Stub extends rpc.RemoteObject { 7855 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7856 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7857 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7858 return true; 7859 } 7860 } 7861 ``` 7862 7863### restoreCallingIdentity<sup>9+</sup> 7864 7865static restoreCallingIdentity(identity: string): void 7866 7867Changes the UID and PID of the remote user to the UID and PID of the local user. This API is a static method. You can use it in scenarios such as identity authentication. 7868 7869**System capability**: SystemCapability.Communication.IPC.Core 7870 7871**Parameters** 7872 7873| Name | Type | Mandatory| Description | 7874| -------- | ------ | ---- | ------------------------------------------------------------------ | 7875| identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7876 7877**Example** 7878 7879 ```ts 7880 class Stub extends rpc.RemoteObject { 7881 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7882 let callingIdentity: rpc.IPCSkeleton | undefined = undefined; 7883 try { 7884 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7885 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7886 } finally { 7887 rpc.IPCSkeleton.restoreCallingIdentity("callingIdentity "); 7888 } 7889 return true; 7890 } 7891 } 7892 ``` 7893 7894### setCallingIdentity<sup>(deprecated)</sup> 7895 7896>This API is no longer maintained since API version 9. You are advised to use [restoreCallingIdentity](#restorecallingidentity9). 7897 7898static setCallingIdentity(identity: string): boolean 7899 7900Sets the UID and PID of the remote user. This API is a static method. It is usually called when the UID and PID of the remote user are required. The UID and PID of the remote user are returned by **resetCallingIdentity**. 7901 7902**System capability**: SystemCapability.Communication.IPC.Core 7903 7904**Parameters** 7905 7906| Name | Type | Mandatory| Description | 7907| -------- | ------ | ---- | ------------------------------------------------------------------ | 7908| identity | string | Yes | String containing the remote user UID and PID, which are returned by **resetCallingIdentity**.| 7909 7910**Return value** 7911 7912| Type | Description | 7913| ------- | ---------------------------------| 7914| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 7915 7916**Example** 7917 7918 ```ts 7919 class Stub extends rpc.RemoteObject { 7920 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 7921 let callingIdentity: rpc.IPCSkeleton | undefined = undefined; 7922 try { 7923 callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 7924 console.log("RpcServer: callingIdentity is: " + callingIdentity); 7925 } finally { 7926 let ret = rpc.IPCSkeleton.setCallingIdentity("callingIdentity "); 7927 console.log("RpcServer: setCallingIdentity is: " + ret); 7928 } 7929 return true; 7930 } 7931 } 7932 ``` 7933 7934## RemoteObject 7935 7936Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 7937 7938### constructor 7939 7940constructor(descriptor: string) 7941 7942A constructor used to create a **RemoteObject** object. 7943 7944**System capability**: SystemCapability.Communication.IPC.Core 7945 7946**Parameters** 7947 7948| Name | Type | Mandatory| Description | 7949| ---------- | ------ | ---- | ------------ | 7950| descriptor | string | Yes | Interface descriptor.| 7951 7952### sendRequest<sup>(deprecated)</sup> 7953 7954>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 7955 7956sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 7957 7958Sends 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. 7959 7960**System capability**: SystemCapability.Communication.IPC.Core 7961 7962**Parameters** 7963 7964| Name | Type | Mandatory| Description | 7965| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7966| 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.| 7967| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7968| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7969| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7970 7971**Return value** 7972 7973| Type | Description | 7974| ------- | -------------------------------- | 7975| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 7976 7977**Example** 7978 7979 ```ts 7980 class MyDeathRecipient implements rpc.DeathRecipient { 7981 onRemoteDied() { 7982 console.log("server died"); 7983 } 7984 } 7985 class TestRemoteObject extends rpc.RemoteObject { 7986 constructor(descriptor: string) { 7987 super(descriptor); 7988 } 7989 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7990 return true; 7991 } 7992 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 7993 return true; 7994 } 7995 isObjectDead(): boolean { 7996 return false; 7997 } 7998 } 7999 let testRemoteObject = new TestRemoteObject("testObject"); 8000 let option = new rpc.MessageOption(); 8001 let data = rpc.MessageParcel.create(); 8002 let reply = rpc.MessageParcel.create(); 8003 data.writeInt(1); 8004 data.writeString("hello"); 8005 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8006 if (ret) { 8007 console.log("sendRequest got result"); 8008 let msg = reply.readString(); 8009 console.log("RPCTest: reply msg: " + msg); 8010 } else { 8011 console.log("RPCTest: sendRequest failed"); 8012 } 8013 console.log("RPCTest: sendRequest ends, reclaim parcel"); 8014 data.reclaim(); 8015 reply.reclaim(); 8016 ``` 8017 8018### sendRequest<sup>8+(deprecated)</sup> 8019 8020>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 8021 8022sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8023 8024Sends 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. 8025 8026**System capability**: SystemCapability.Communication.IPC.Core 8027 8028**Parameters** 8029 8030| Name | Type | Mandatory| Description | 8031| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8032| 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.| 8033| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8034| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8035| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8036 8037**Return value** 8038 8039| Type | Description | 8040| -------------------------------- | --------------------------------------------- | 8041| Promise<SendRequestResult> | Promise used to return the **sendRequestResult** object.| 8042 8043**Example** 8044 8045 ```ts 8046 class MyDeathRecipient implements rpc.DeathRecipient { 8047 onRemoteDied() { 8048 console.log("server died"); 8049 } 8050 } 8051 class TestRemoteObject extends rpc.RemoteObject { 8052 constructor(descriptor: string) { 8053 super(descriptor); 8054 } 8055 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8056 return true; 8057 } 8058 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8059 return true; 8060 } 8061 isObjectDead(): boolean { 8062 return false; 8063 } 8064 } 8065 let testRemoteObject = new TestRemoteObject("testObject"); 8066 let option = new rpc.MessageOption(); 8067 let data = rpc.MessageParcel.create(); 8068 let reply = rpc.MessageParcel.create(); 8069 data.writeInt(1); 8070 data.writeString("hello"); 8071 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8072 let b = a as Promise<rpc.SendRequestResult>; 8073 b.then((result: rpc.SendRequestResult) => { 8074 if (result.errCode === 0) { 8075 console.log("sendRequest got result"); 8076 result.reply.readException(); 8077 let msg = result.reply.readString(); 8078 console.log("RPCTest: reply msg: " + msg); 8079 } else { 8080 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 8081 } 8082 }).catch((e: Error) => { 8083 console.log("RPCTest: sendRequest got exception: " + e.message); 8084 }).finally (() => { 8085 console.log("RPCTest: sendRequest ends, reclaim parcel"); 8086 data.reclaim(); 8087 reply.reclaim(); 8088 }); 8089 ``` 8090 8091### sendMessageRequest<sup>9+</sup> 8092 8093sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8094 8095Sends 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. 8096 8097**System capability**: SystemCapability.Communication.IPC.Core 8098 8099**Parameters** 8100 8101| Name | Type | Mandatory| Description | 8102| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8103| 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.| 8104| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8105| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8106| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8107 8108**Return value** 8109 8110| Type | Description | 8111| ---------------------------- | --------------------------------------------- | 8112| Promise<RequestResult> | Promise used to return the **RequestResult** instance. | 8113 8114**Example** 8115 8116 ```ts 8117 class TestRemoteObject extends rpc.RemoteObject { 8118 constructor(descriptor: string) { 8119 super(descriptor); 8120 } 8121 } 8122 let testRemoteObject = new TestRemoteObject("testObject"); 8123 let option = new rpc.MessageOption(); 8124 let data = rpc.MessageSequence.create(); 8125 let reply = rpc.MessageSequence.create(); 8126 data.writeInt(1); 8127 data.writeString("hello"); 8128 testRemoteObject.sendMessageRequest(1, data, reply, option) 8129 .then((result: rpc.RequestResult) => { 8130 if (result.errCode === 0) { 8131 console.log("sendMessageRequest got result"); 8132 result.reply.readException(); 8133 let msg = result.reply.readString(); 8134 console.log("RPCTest: reply msg: " + msg); 8135 } else { 8136 console.log("RPCTest: sendMessageRequest failed, errCode: " + result.errCode); 8137 } 8138 }).catch((e: Error) => { 8139 console.log("RPCTest: sendMessageRequest got exception: " + e.message); 8140 }).finally (() => { 8141 console.log("RPCTest: sendMessageRequest ends, reclaim parcel"); 8142 data.reclaim(); 8143 reply.reclaim(); 8144 }); 8145 ``` 8146 8147### sendMessageRequest<sup>9+</sup> 8148 8149sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8150 8151Sends 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. 8152 8153**System capability**: SystemCapability.Communication.IPC.Core 8154 8155**Parameters** 8156 8157| Name | Type | Mandatory| Description | 8158| ------------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8159| 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.| 8160| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8161| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8162| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8163| AsyncCallback | AsyncCallback<RequestResult> | Yes | Callback for receiving the sending result. | 8164 8165**Example** 8166 8167 ```ts 8168 import { BusinessError } from '@ohos.base'; 8169 8170 class TestRemoteObject extends rpc.RemoteObject { 8171 constructor(descriptor: string) { 8172 super(descriptor); 8173 } 8174 } 8175 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 8176 if (result.errCode === 0) { 8177 console.log("sendRequest got result"); 8178 result.reply.readException(); 8179 let msg = result.reply.readString(); 8180 console.log("RPCTest: reply msg: " + msg); 8181 } else { 8182 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 8183 } 8184 console.log("RPCTest: sendRequest ends, reclaim parcel"); 8185 result.data.reclaim(); 8186 result.reply.reclaim(); 8187 } 8188 let testRemoteObject = new TestRemoteObject("testObject"); 8189 let option = new rpc.MessageOption(); 8190 let data = rpc.MessageSequence.create(); 8191 let reply = rpc.MessageSequence.create(); 8192 data.writeInt(1); 8193 data.writeString("hello"); 8194 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 8195 ``` 8196 8197### sendRequest<sup>8+(deprecated)</sup> 8198 8199>This API is no longer maintained since API version 9. You are advised to use [sendMessageRequest](#sendmessagerequest9). 8200 8201sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8202 8203Sends 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. 8204 8205**System capability**: SystemCapability.Communication.IPC.Core 8206 8207**Parameters** 8208 8209| Name | Type | Mandatory| Description | 8210| ------------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8211| 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.| 8212| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8213| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8214| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8215| AsyncCallback | AsyncCallback<SendRequestResult> | Yes | Callback for receiving the sending result. | 8216 8217**Example** 8218 8219 ```ts 8220 import { BusinessError } from '@ohos.base'; 8221 8222 class MyDeathRecipient implements rpc.DeathRecipient { 8223 onRemoteDied() { 8224 console.log("server died"); 8225 } 8226 } 8227 class TestRemoteObject extends rpc.RemoteObject { 8228 constructor(descriptor: string) { 8229 super(descriptor); 8230 } 8231 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8232 return true; 8233 } 8234 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8235 return true; 8236 } 8237 isObjectDead(): boolean { 8238 return false; 8239 } 8240 } 8241 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 8242 if (result.errCode === 0) { 8243 console.log("sendRequest got result"); 8244 result.reply.readException(); 8245 let msg = result.reply.readString(); 8246 console.log("RPCTest: reply msg: " + msg); 8247 } else { 8248 console.log("RPCTest: sendRequest failed, errCode: " + result.errCode); 8249 } 8250 console.log("RPCTest: sendRequest ends, reclaim parcel"); 8251 result.data.reclaim(); 8252 result.reply.reclaim(); 8253 } 8254 let testRemoteObject = new TestRemoteObject("testObject"); 8255 let option = new rpc.MessageOption(); 8256 let data = rpc.MessageParcel.create(); 8257 let reply = rpc.MessageParcel.create(); 8258 data.writeInt(1); 8259 data.writeString("hello"); 8260 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 8261 ``` 8262 8263### onRemoteRequest<sup>8+(deprecated)</sup> 8264 8265>This API is no longer maintained since API version 9. You are advised to use [onRemoteMessageRequest](#onremotemessagerequest9). 8266 8267onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8268 8269Provides a response to **sendMessageRequest()**. The server processes the request and returns a response in this API. 8270 8271**System capability**: SystemCapability.Communication.IPC.Core 8272 8273**Parameters** 8274 8275| Name| Type | Mandatory| Description | 8276| ------ | ----------------------------------------- | ---- | --------------------------------------- | 8277| code | number | Yes | Service request code sent by the remote end. | 8278| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 8279| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 8280| option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8281 8282**Return value** 8283 8284| Type | Description | 8285| ------- | -------------------------------- | 8286| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8287 8288**Example** 8289 8290 ```ts 8291 class MyDeathRecipient implements rpc.DeathRecipient { 8292 onRemoteDied() { 8293 console.log("server died"); 8294 } 8295 } 8296 class TestRemoteObject extends rpc.RemoteObject { 8297 constructor(descriptor: string) { 8298 super(descriptor); 8299 } 8300 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8301 return true; 8302 } 8303 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8304 return true; 8305 } 8306 isObjectDead(): boolean { 8307 return false; 8308 } 8309 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8310 if (code === 1) { 8311 console.log("RpcServer: onRemoteRequest called"); 8312 return true; 8313 } else { 8314 console.log("RpcServer: unknown code: " + code); 8315 return false; 8316 } 8317 } 8318 } 8319 ``` 8320 8321### onRemoteMessageRequest<sup>9+</sup> 8322 8323onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 8324 8325> **NOTE**<br/> 8326> 8327>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 8328>* If both onRemoteRequest() and onRemoteMessageRequest() are overloaded, only the onRemoteMessageRequest() takes effect. 8329 8330Provides a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 8331 8332**System capability**: SystemCapability.Communication.IPC.Core 8333 8334**Parameters** 8335 8336| Name| Type | Mandatory| Description | 8337| ------ | ------------------------------------ | ---- | ----------------------------------------- | 8338| code | number | Yes | Service request code sent by the remote end. | 8339| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 8340| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 8341| option | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 8342 8343**Return value** 8344 8345| Type | Description | 8346| ----------------- | ----------------------------------------------------------------------------------------------- | 8347| 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.| 8348| Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 8349 8350**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 8351 8352 ```ts 8353 class TestRemoteObject extends rpc.RemoteObject { 8354 constructor(descriptor: string) { 8355 super(descriptor); 8356 } 8357 8358 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8359 if (code === 1) { 8360 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8361 return true; 8362 } else { 8363 console.log("RpcServer: unknown code: " + code); 8364 return false; 8365 } 8366 } 8367 } 8368 ``` 8369 8370 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 8371 8372 ```ts 8373 class TestRemoteObject extends rpc.RemoteObject { 8374 constructor(descriptor: string) { 8375 super(descriptor); 8376 } 8377 8378 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 8379 if (code === 1) { 8380 console.log("RpcServer: async onRemoteMessageRequest is called"); 8381 } else { 8382 console.log("RpcServer: unknown code: " + code); 8383 return false; 8384 } 8385 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 8386 setTimeout(resolve, 100); 8387 }) 8388 return true; 8389 } 8390 } 8391 ``` 8392 8393**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 8394 8395 ```ts 8396 class TestRemoteObject extends rpc.RemoteObject { 8397 constructor(descriptor: string) { 8398 super(descriptor); 8399 } 8400 8401 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8402 if (code === 1) { 8403 console.log("RpcServer: sync onRemoteMessageRequest is called"); 8404 return true; 8405 } else { 8406 console.log("RpcServer: unknown code: " + code); 8407 return false; 8408 } 8409 } 8410 // Only onRemoteMessageRequest is executed. 8411 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8412 if (code === 1) { 8413 console.log("RpcServer: async onRemoteMessageRequest is called"); 8414 } else { 8415 console.log("RpcServer: unknown code: " + code); 8416 return false; 8417 } 8418 return true; 8419 } 8420 } 8421 ``` 8422 8423 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 8424 8425 ```ts 8426 class TestRemoteObject extends rpc.RemoteObject { 8427 constructor(descriptor: string) { 8428 super(descriptor); 8429 } 8430 8431 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 8432 if (code === 1) { 8433 console.log("RpcServer: sync onRemoteRequest is called"); 8434 return true; 8435 } else { 8436 console.log("RpcServer: unknown code: " + code); 8437 return false; 8438 } 8439 } 8440 // Only onRemoteMessageRequest is executed. 8441 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 8442 if (code === 1) { 8443 console.log("RpcServer: async onRemoteMessageRequest is called"); 8444 } else { 8445 console.log("RpcServer: unknown code: " + code); 8446 return false; 8447 } 8448 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 8449 setTimeout(resolve, 100); 8450 }) 8451 return true; 8452 } 8453 } 8454 ``` 8455 8456### getCallingUid 8457 8458getCallingUid(): number 8459 8460Obtains the UID of the remote process. 8461 8462**System capability**: SystemCapability.Communication.IPC.Core 8463 8464**Return value** 8465| Type | Description | 8466| ------ | ----------------------- | 8467| number | UID of the remote process obtained.| 8468 8469**Example** 8470 8471 ```ts 8472 class TestRemoteObject extends rpc.RemoteObject { 8473 constructor(descriptor: string) { 8474 super(descriptor); 8475 } 8476 } 8477 let testRemoteObject = new TestRemoteObject("testObject"); 8478 console.log("RpcServer: getCallingUid: " + testRemoteObject.getCallingUid()); 8479 ``` 8480 8481### getCallingPid 8482 8483getCallingPid(): number 8484 8485Obtains the PID of the remote process. 8486 8487**System capability**: SystemCapability.Communication.IPC.Core 8488 8489**Return value** 8490 8491| Type | Description | 8492| ------ | ----------------------- | 8493| number | PID of the remote process obtained.| 8494 8495**Example** 8496 8497 ```ts 8498 class TestRemoteObject extends rpc.RemoteObject { 8499 constructor(descriptor: string) { 8500 super(descriptor); 8501 } 8502 } 8503 let testRemoteObject = new TestRemoteObject("testObject"); 8504 console.log("RpcServer: getCallingPid: " + testRemoteObject.getCallingPid()); 8505 ``` 8506 8507### getLocalInterface<sup>9+</sup> 8508 8509getLocalInterface(descriptor: string): IRemoteBroker 8510 8511Obtains the interface descriptor. 8512 8513**System capability**: SystemCapability.Communication.IPC.Core 8514 8515**Parameters** 8516 8517| Name | Type | Mandatory| Description | 8518| ---------- | ------ | ---- | -------------------- | 8519| descriptor | string | Yes | Interface descriptor.| 8520 8521**Return value** 8522 8523| Type | Description | 8524| ------------- | --------------------------------------------- | 8525| IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 8526 8527**Example** 8528 8529 ```ts 8530 import { BusinessError } from '@ohos.base'; 8531 8532 class MyDeathRecipient implements rpc.DeathRecipient { 8533 onRemoteDied() { 8534 console.log("server died"); 8535 } 8536 } 8537 class TestRemoteObject extends rpc.RemoteObject { 8538 constructor(descriptor: string) { 8539 super(descriptor); 8540 this.modifyLocalInterface(this, descriptor); 8541 } 8542 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8543 // Implement the method logic based on service requirements. 8544 } 8545 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8546 // Implement the method logic based on service requirements. 8547 } 8548 isObjectDead(): boolean { 8549 return false; 8550 } 8551 } 8552 let testRemoteObject = new TestRemoteObject("testObject"); 8553 try { 8554 testRemoteObject.getLocalInterface("testObject"); 8555 } catch(error) { 8556 let e: BusinessError = error as BusinessError; 8557 console.info("rpc get local interface fail, errorCode " + e.code); 8558 console.info("rpc get local interface fail, errorMessage " + e.message); 8559 } 8560 ``` 8561 8562### queryLocalInterface<sup>(deprecated)</sup> 8563 8564>This API is no longer maintained since API version 9. You are advised to use [getLocalInterface](#getlocalinterface9). 8565 8566queryLocalInterface(descriptor: string): IRemoteBroker 8567 8568Checks whether the remote object corresponding to the specified interface token exists. 8569 8570**System capability**: SystemCapability.Communication.IPC.Core 8571 8572**Parameters** 8573 8574| Name | Type | Mandatory| Description | 8575| ---------- | ------ | ---- | ---------------------- | 8576| descriptor | string | Yes | Interface descriptor.| 8577 8578**Return value** 8579 8580| Type | Description | 8581| ------------- | ------------------------------------------------------------------ | 8582| IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 8583 8584**Example** 8585 8586 ```ts 8587 class MyDeathRecipient implements rpc.DeathRecipient { 8588 onRemoteDied() { 8589 console.log("server died"); 8590 } 8591 } 8592 class TestRemoteObject extends rpc.RemoteObject { 8593 constructor(descriptor: string) { 8594 super(descriptor); 8595 this.attachLocalInterface(this, descriptor); 8596 } 8597 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8598 return true; 8599 } 8600 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8601 return true; 8602 } 8603 isObjectDead(): boolean { 8604 return false; 8605 } 8606 } 8607 let testRemoteObject = new TestRemoteObject("testObject"); 8608 testRemoteObject.queryLocalInterface("testObject"); 8609 ``` 8610 8611### getDescriptor<sup>9+</sup> 8612 8613getDescriptor(): string 8614 8615Obtains the interface descriptor of this object. The interface descriptor is a string. 8616 8617**System capability**: SystemCapability.Communication.IPC.Core 8618 8619**Return value** 8620 8621| Type | Description | 8622| ------ | ---------------- | 8623| string | Interface descriptor obtained.| 8624 8625**Error codes** 8626 8627For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 8628 8629| ID| Error Message| 8630| -------- | -------- | 8631| 1900008 | proxy or remote object is invalid | 8632 8633**Example** 8634 8635 ```ts 8636 import { BusinessError } from '@ohos.base'; 8637 8638 class MyDeathRecipient implements rpc.DeathRecipient { 8639 onRemoteDied() { 8640 console.log("server died"); 8641 } 8642 } 8643 class TestRemoteObject extends rpc.RemoteObject { 8644 constructor(descriptor: string) { 8645 super(descriptor); 8646 } 8647 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8648 // Implement the method logic based on service requirements. 8649 } 8650 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8651 // Implement the method logic based on service requirements. 8652 } 8653 isObjectDead(): boolean { 8654 return false; 8655 } 8656 } 8657 let testRemoteObject = new TestRemoteObject("testObject"); 8658 try { 8659 let descriptor = testRemoteObject.getDescriptor(); 8660 console.log("RpcServer: descriptor is: " + descriptor); 8661 } catch(error) { 8662 let e: BusinessError = error as BusinessError; 8663 console.info("rpc get local interface fail, errorCode " + e.code); 8664 console.info("rpc get local interface fail, errorMessage " + e.message); 8665 } 8666 ``` 8667 8668### getInterfaceDescriptor<sup>(deprecated)</sup> 8669 8670>This API is no longer maintained since API version 9. You are advised to use [getDescriptor](#getdescriptor9). 8671 8672getInterfaceDescriptor(): string 8673 8674Obtains the interface descriptor. 8675 8676**System capability**: SystemCapability.Communication.IPC.Core 8677 8678**Return value** 8679 8680| Type | Description | 8681| ------ | ---------------- | 8682| string | Interface descriptor obtained.| 8683 8684**Example** 8685 8686 ```ts 8687 class MyDeathRecipient implements rpc.DeathRecipient { 8688 onRemoteDied() { 8689 console.log("server died"); 8690 } 8691 } 8692 class TestRemoteObject extends rpc.RemoteObject { 8693 constructor(descriptor: string) { 8694 super(descriptor); 8695 } 8696 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8697 return true; 8698 } 8699 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8700 return true; 8701 } 8702 isObjectDead(): boolean { 8703 return false; 8704 } 8705 } 8706 let testRemoteObject = new TestRemoteObject("testObject"); 8707 let descriptor = testRemoteObject.getInterfaceDescriptor(); 8708 console.log("RpcServer: descriptor is: " + descriptor); 8709 ``` 8710 8711### modifyLocalInterface<sup>9+</sup> 8712 8713modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8714 8715Binds an interface descriptor to an **IRemoteBroker** object. 8716 8717**System capability**: SystemCapability.Communication.IPC.Core 8718 8719**Parameters** 8720 8721| Name | Type | Mandatory| Description | 8722| -------------- | ------------- | ---- | ------------------------------------- | 8723| localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8724| descriptor | string | Yes | Interface descriptor.| 8725 8726**Example** 8727 8728 ```ts 8729 import { BusinessError } from '@ohos.base'; 8730 8731 class MyDeathRecipient implements rpc.DeathRecipient { 8732 onRemoteDied() { 8733 console.log("server died"); 8734 } 8735 } 8736 class TestRemoteObject extends rpc.RemoteObject { 8737 constructor(descriptor: string) { 8738 super(descriptor); 8739 try { 8740 this.modifyLocalInterface(this, descriptor); 8741 } catch(error) { 8742 let e: BusinessError = error as BusinessError; 8743 console.info(" rpc attach local interface fail, errorCode " + e.code); 8744 console.info(" rpc attach local interface fail, errorMessage " + e.message); 8745 } 8746 } 8747 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8748 // Implement the method logic based on service requirements. 8749 } 8750 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 8751 // Implement the method logic based on service requirements. 8752 } 8753 isObjectDead(): boolean { 8754 return false; 8755 } 8756 asObject(): rpc.IRemoteObject { 8757 return this; 8758 } 8759 } 8760 let testRemoteObject = new TestRemoteObject("testObject"); 8761 ``` 8762 8763### attachLocalInterface<sup>(deprecated)</sup> 8764 8765>This API is no longer maintained since API version 9. You are advised to use [modifyLocalInterface](#modifylocalinterface9). 8766 8767attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 8768 8769Binds an interface descriptor to an **IRemoteBroker** object. 8770 8771**System capability**: SystemCapability.Communication.IPC.Core 8772 8773**Parameters** 8774 8775| Name | Type | Mandatory| Description | 8776| -------------- | ------------- | ---- | ------------------------------------- | 8777| localInterface | IRemoteBroker | Yes | **IRemoteBroker** object. | 8778| descriptor | string | Yes | Interface descriptor.| 8779 8780**Example** 8781 8782 ```ts 8783 class MyDeathRecipient implements rpc.DeathRecipient { 8784 onRemoteDied() { 8785 console.log("server died"); 8786 } 8787 } 8788 class TestRemoteObject extends rpc.RemoteObject { 8789 constructor(descriptor: string) { 8790 super(descriptor); 8791 this.attachLocalInterface(this, descriptor); 8792 } 8793 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8794 return true; 8795 } 8796 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8797 return true; 8798 } 8799 isObjectDead(): boolean { 8800 return false; 8801 } 8802 asObject(): rpc.IRemoteObject { 8803 return this; 8804 } 8805 } 8806 let testRemoteObject = new TestRemoteObject("testObject"); 8807 ``` 8808 8809## Ashmem<sup>8+</sup> 8810 8811Provides 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. 8812 8813**System capability**: SystemCapability.Communication.IPC.Core 8814 8815The table below describes the protection types of the mapped memory. 8816 8817| Name | Value | Description | 8818| ---------- | --- | ------------------ | 8819| PROT_EXEC | 4 | The mapped memory is executable. | 8820| PROT_NONE | 0 | The mapped memory is inaccessible.| 8821| PROT_READ | 1 | The mapped memory is readable. | 8822| PROT_WRITE | 2 | The mapped memory is writeable. | 8823 8824### create<sup>9+</sup> 8825 8826static create(name: string, size: number): Ashmem 8827 8828Creates an **Ashmem** object with the specified name and size. This API is a static method. 8829 8830**System capability**: SystemCapability.Communication.IPC.Core 8831 8832**Parameters** 8833 8834| Name| Type | Mandatory| Description | 8835| ------ | ------ | ---- | ---------------------------- | 8836| name | string | Yes | Name of the **Ashmem** object to create. | 8837| size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8838 8839**Return value** 8840 8841| Type | Description | 8842| ------ | ---------------------------------------------- | 8843| Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8844 8845**Example** 8846 8847 ```ts 8848 import { BusinessError } from '@ohos.base'; 8849 8850 let ashmem: rpc.Ashmem | undefined = undefined; 8851 try { 8852 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8853 let size = ashmem.getAshmemSize(); 8854 console.log("RpcTest: get ashemm by create : " + ashmem + " size is : " + size); 8855 } catch(error) { 8856 let e: BusinessError = error as BusinessError; 8857 console.info("Rpc creat ashmem fail, errorCode " + e.code); 8858 console.info("Rpc creat ashmem fail, errorMessage " + e.message); 8859 } 8860 ``` 8861 8862### createAshmem<sup>8+(deprecated)</sup> 8863 8864>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8865 8866static createAshmem(name: string, size: number): Ashmem 8867 8868Creates an **Ashmem** object with the specified name and size. This API is a static method. 8869 8870**System capability**: SystemCapability.Communication.IPC.Core 8871 8872**Parameters** 8873 8874| Name| Type | Mandatory| Description | 8875| ------ | ------ | ---- | ---------------------------- | 8876| name | string | Yes | Name of the **Ashmem** object to create. | 8877| size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 8878 8879**Return value** 8880 8881| Type | Description | 8882| ------ | ---------------------------------------------- | 8883| Ashmem | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 8884 8885**Example** 8886 8887 ```ts 8888 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8889 let size = ashmem.getAshmemSize(); 8890 console.log("RpcTest: get ashemm by createAshmem : " + ashmem + " size is : " + size); 8891 ``` 8892 8893### create<sup>9+</sup> 8894 8895static create(ashmem: Ashmem): Ashmem 8896 8897Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8898 8899**System capability**: SystemCapability.Communication.IPC.Core 8900 8901**Parameters** 8902 8903| Name| Type | Mandatory| Description | 8904| ------ | ------ | ---- | -------------------- | 8905| ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8906 8907**Return value** 8908 8909| Type | Description | 8910| ------ | ---------------------- | 8911| Ashmem | **Ashmem** object created.| 8912 8913**Example** 8914 8915 ```ts 8916 import { BusinessError } from '@ohos.base'; 8917 8918 try { 8919 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8920 let ashmem2 = rpc.Ashmem.create(ashmem); 8921 let size = ashmem2.getAshmemSize(); 8922 console.log("RpcTest: get ashemm by create : " + ashmem2 + " size is : " + size); 8923 } catch(error) { 8924 let e: BusinessError = error as BusinessError; 8925 console.info("Rpc creat ashmem from existing fail, errorCode " + e.code); 8926 console.info("Rpc creat ashmem from existing fail, errorMessage " + e.message); 8927 } 8928 ``` 8929 8930### createAshmemFromExisting<sup>8+(deprecated)</sup> 8931 8932>This API is no longer maintained since API version 9. You are advised to use [create](#create9). 8933 8934static createAshmemFromExisting(ashmem: Ashmem): Ashmem 8935 8936Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 8937 8938**System capability**: SystemCapability.Communication.IPC.Core 8939 8940**Parameters** 8941 8942| Name| Type | Mandatory| Description | 8943| ------ | ------ | ---- | -------------------- | 8944| ashmem | Ashmem | Yes | Existing **Ashmem** object.| 8945 8946**Return value** 8947 8948| Type | Description | 8949| ------ | ---------------------- | 8950| Ashmem | **Ashmem** object created.| 8951 8952**Example** 8953 8954 ```ts 8955 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 8956 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 8957 let size = ashmem2.getAshmemSize(); 8958 console.log("RpcTest: get ashemm by createAshmemFromExisting : " + ashmem2 + " size is : " + size); 8959 ``` 8960 8961### closeAshmem<sup>8+</sup> 8962 8963closeAshmem(): void 8964 8965Closes this **Ashmem** object. 8966 8967**System capability**: SystemCapability.Communication.IPC.Core 8968 8969**Example** 8970 8971 ```ts 8972 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8973 ashmem.closeAshmem(); 8974 ``` 8975 8976### unmapAshmem<sup>8+</sup> 8977 8978unmapAshmem(): void 8979 8980Deletes the mappings for the specified address range of this **Ashmem** object. 8981 8982**System capability**: SystemCapability.Communication.IPC.Core 8983 8984**Example** 8985 8986 ```ts 8987 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 8988 ashmem.unmapAshmem(); 8989 ``` 8990 8991### getAshmemSize<sup>8+</sup> 8992 8993getAshmemSize(): number 8994 8995Obtains the memory size of this **Ashmem** object. 8996 8997**System capability**: SystemCapability.Communication.IPC.Core 8998 8999**Return value** 9000 9001| Type | Description | 9002| ------ | -------------------------- | 9003| number | **Ashmem** size obtained.| 9004 9005**Example** 9006 9007 ```ts 9008 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9009 let size = ashmem.getAshmemSize(); 9010 console.log("RpcTest: get ashmem is " + ashmem + " size is : " + size); 9011 ``` 9012 9013### mapTypedAshmem<sup>9+</sup> 9014 9015mapTypedAshmem(mapType: number): void 9016 9017Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9018 9019**System capability**: SystemCapability.Communication.IPC.Core 9020 9021**Parameters** 9022 9023| Name | Type | Mandatory| Description | 9024| ------- | ------ | ---- | ------------------------------ | 9025| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9026 9027**Error codes** 9028 9029For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9030 9031| ID| Error Message| 9032| -------- | -------- | 9033| 1900001 | call mmap function failed | 9034 9035**Example** 9036 9037 ```ts 9038 import { BusinessError } from '@ohos.base'; 9039 9040 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9041 try { 9042 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9043 } catch(error) { 9044 let e: BusinessError = error as BusinessError; 9045 console.info("Rpc map ashmem fail, errorCode " + e.code); 9046 console.info("Rpc map ashmem fail, errorMessage " + e.message); 9047 } 9048 ``` 9049 9050### mapAshmem<sup>8+(deprecated)</sup> 9051 9052>This API is no longer maintained since API version 9. You are advised to use [mapTypedAshmem](#maptypedashmem9). 9053 9054mapAshmem(mapType: number): boolean 9055 9056Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9057 9058**System capability**: SystemCapability.Communication.IPC.Core 9059 9060**Parameters** 9061 9062| Name | Type | Mandatory| Description | 9063| ------- | ------ | ---- | ------------------------------ | 9064| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9065 9066**Return value** 9067 9068| Type | Description | 9069| ------- | -------------------------------- | 9070| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9071 9072**Example** 9073 9074 ```ts 9075 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9076 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9077 console.log("RpcTest: map ashmem result is : " + mapReadAndWrite); 9078 ``` 9079 9080### mapReadWriteAshmem<sup>9+</sup> 9081 9082mapReadWriteAshmem(): void 9083 9084Maps the shared file to the readable and writable virtual address space of the process. 9085 9086**System capability**: SystemCapability.Communication.IPC.Core 9087 9088**Error codes** 9089 9090For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9091 9092| ID| Error Message| 9093| -------- | -------- | 9094| 1900001 | call mmap function failed | 9095 9096**Example** 9097 9098 ```ts 9099 import { BusinessError } from '@ohos.base'; 9100 9101 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9102 try { 9103 ashmem.mapReadWriteAshmem(); 9104 } catch(error) { 9105 let e: BusinessError = error as BusinessError; 9106 console.info("Rpc map read and write ashmem fail, errorCode " + e.code); 9107 console.info("Rpc map read and write ashmem fail, errorMessage " + e.message); 9108 } 9109 ``` 9110 9111### mapReadAndWriteAshmem<sup>8+(deprecated)</sup> 9112 9113>This API is no longer maintained since API version 9. You are advised to use [mapReadWriteAshmem](#mapreadwriteashmem9). 9114 9115mapReadAndWriteAshmem(): boolean 9116 9117Maps the shared file to the readable and writable virtual address space of the process. 9118 9119**System capability**: SystemCapability.Communication.IPC.Core 9120 9121**Return value** 9122 9123| Type | Description | 9124| ------- | -------------------------------- | 9125| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9126 9127**Example** 9128 9129 ```ts 9130 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9131 let mapResult = ashmem.mapReadAndWriteAshmem(); 9132 console.log("RpcTest: map ashmem result is : " + mapResult); 9133 ``` 9134 9135### mapReadonlyAshmem<sup>9+</sup> 9136 9137mapReadonlyAshmem(): void 9138 9139Maps the shared file to the read-only virtual address space of the process. 9140 9141**System capability**: SystemCapability.Communication.IPC.Core 9142 9143**Error codes** 9144 9145For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9146 9147| ID| Error Message| 9148| -------- | -------- | 9149| 1900001 | call mmap function failed | 9150 9151**Example** 9152 9153 ```ts 9154 import { BusinessError } from '@ohos.base'; 9155 9156 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9157 try { 9158 ashmem.mapReadonlyAshmem(); 9159 } catch(error) { 9160 let e: BusinessError = error as BusinessError; 9161 console.info("Rpc map read and write ashmem fail, errorCode " + e.code); 9162 console.info("Rpc map read and write ashmem fail, errorMessage " + e.message); 9163 } 9164 ``` 9165 9166### mapReadOnlyAshmem<sup>8+(deprecated)</sup> 9167 9168>This API is no longer maintained since API version 9. You are advised to use [mapReadonlyAshmem](#mapreadonlyashmem9). 9169 9170mapReadOnlyAshmem(): boolean 9171 9172Maps the shared file to the read-only virtual address space of the process. 9173 9174**System capability**: SystemCapability.Communication.IPC.Core 9175 9176**Return value** 9177 9178| Type | Description | 9179| ------- | -------------------------------- | 9180| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9181 9182**Example** 9183 9184 ```ts 9185 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9186 let mapResult = ashmem.mapReadOnlyAshmem(); 9187 console.log("RpcTest: Ashmem mapReadOnlyAshmem result is : " + mapResult); 9188 ``` 9189 9190### setProtectionType<sup>9+</sup> 9191 9192setProtectionType(protectionType: number): void 9193 9194Sets the protection level of the memory region to which the shared file is mapped. 9195 9196**System capability**: SystemCapability.Communication.IPC.Core 9197 9198**Parameters** 9199 9200| Name | Type | Mandatory| Description | 9201| -------------- | ------ | ---- | ------------------ | 9202| protectionType | number | Yes | Protection type to set.| 9203 9204**Error codes** 9205 9206For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9207 9208| ID| Error Message| 9209| -------- | -------- | 9210| 1900002 | call os ioctl function failed | 9211 9212**Example** 9213 9214 ```ts 9215 import { BusinessError } from '@ohos.base'; 9216 9217 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9218 try { 9219 ashmem.setProtection(ashmem.PROT_READ); 9220 } catch(error) { 9221 let e: BusinessError = error as BusinessError; 9222 console.info("Rpc set protection type fail, errorCode " + e.code); 9223 console.info("Rpc set protection type fail, errorMessage " + e.message); 9224 } 9225 ``` 9226 9227### setProtection<sup>8+(deprecated)</sup> 9228 9229>This API is no longer maintained since API version 9. You are advised to use [setProtectionType](#setprotectiontype9). 9230 9231setProtection(protectionType: number): boolean 9232 9233Sets the protection level of the memory region to which the shared file is mapped. 9234 9235**System capability**: SystemCapability.Communication.IPC.Core 9236 9237**Parameters** 9238 9239| Name | Type | Mandatory| Description | 9240| -------------- | ------ | ---- | ------------------ | 9241| protectionType | number | Yes | Protection type to set.| 9242 9243**Return value** 9244 9245| Type | Description | 9246| ------- | -------------------------------- | 9247| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 9248 9249**Example** 9250 9251 ```ts 9252 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9253 let result = ashmem.setProtection(ashmem.PROT_READ); 9254 console.log("RpcTest: Ashmem setProtection result is : " + result); 9255 ``` 9256 9257### writeAshmem<sup>9+</sup> 9258 9259writeAshmem(buf: number[], size: number, offset: number): void 9260 9261Writes data to the shared file associated with this **Ashmem** object. 9262 9263**System capability**: SystemCapability.Communication.IPC.Core 9264 9265**Parameters** 9266 9267| Name| Type | Mandatory| Description | 9268| ------ | -------- | ---- | -------------------------------------------------- | 9269| buf | number[] | Yes | Data to write. | 9270| size | number | Yes | Size of the data to write. | 9271| offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 9272 9273**Error codes** 9274 9275For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9276 9277| ID| Error Message| 9278| -------- | -------- | 9279| 1900003 | write to ashmem failed | 9280 9281**Example** 9282 9283 ```ts 9284 import { BusinessError } from '@ohos.base'; 9285 9286 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9287 ashmem.mapReadWriteAshmem(); 9288 let ByteArrayVar = [1, 2, 3, 4, 5]; 9289 try { 9290 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9291 } catch(error) { 9292 let e: BusinessError = error as BusinessError; 9293 console.info("Rpc write to ashmem fail, errorCode " + e.code); 9294 console.info("Rpc write to ashmem fail, errorMessage " + e.message); 9295 } 9296 ``` 9297 9298### writeToAshmem<sup>8+(deprecated)</sup> 9299 9300>This API is no longer maintained since API version 9. You are advised to use [writeAshmem](#writeashmem9). 9301 9302writeToAshmem(buf: number[], size: number, offset: number): boolean 9303 9304Writes data to the shared file associated with this **Ashmem** object. 9305 9306**System capability**: SystemCapability.Communication.IPC.Core 9307 9308**Parameters** 9309 9310| Name| Type | Mandatory| Description | 9311| ------ | -------- | ---- | -------------------------------------------------- | 9312| buf | number[] | Yes | Data to write. | 9313| size | number | Yes | Size of the data to write. | 9314| offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 9315 9316**Return value** 9317 9318| Type | Description | 9319| ------- | ----------------------------------------------------------------------------- | 9320| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 9321 9322**Example** 9323 9324 ```ts 9325 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9326 let mapResult = ashmem.mapReadAndWriteAshmem(); 9327 console.info("RpcTest map ashmem result is " + mapResult); 9328 let ByteArrayVar = [1, 2, 3, 4, 5]; 9329 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9330 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9331 ``` 9332 9333### readAshmem<sup>9+</sup> 9334 9335readAshmem(size: number, offset: number): number[] 9336 9337Reads data from the shared file associated with this **Ashmem** object. 9338 9339**System capability**: SystemCapability.Communication.IPC.Core 9340 9341**Parameters** 9342 9343| Name| Type | Mandatory| Description | 9344| ------ | ------ | ---- | -------------------------------------------------- | 9345| size | number | Yes | Size of the data to read. | 9346| offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9347 9348**Return value** 9349 9350| Type | Description | 9351| -------- | ---------------- | 9352| number[] | Data read.| 9353 9354**Error codes** 9355 9356For details about the error codes, see [RPC Error Codes](../errorcodes/errorcode-rpc.md). 9357 9358| ID| Error Message| 9359| -------- | -------- | 9360| 1900004 | read from ashmem failed | 9361 9362**Example** 9363 9364 ```ts 9365 import { BusinessError } from '@ohos.base'; 9366 9367 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9368 ashmem.mapReadWriteAshmem(); 9369 let ByteArrayVar = [1, 2, 3, 4, 5]; 9370 ashmem.writeAshmem(ByteArrayVar, 5, 0); 9371 try { 9372 let readResult = ashmem.readAshmem(5, 0); 9373 console.log("RpcTest: read from Ashmem result is : " + readResult); 9374 } catch(error) { 9375 let e: BusinessError = error as BusinessError; 9376 console.info("Rpc read from ashmem fail, errorCode " + e.code); 9377 console.info("Rpc read from ashmem fail, errorMessage " + e.message); 9378 } 9379 ``` 9380 9381### readFromAshmem<sup>8+(deprecated)</sup> 9382 9383>This API is no longer maintained since API version 9. You are advised to use [readAshmem](#readashmem9). 9384 9385readFromAshmem(size: number, offset: number): number[] 9386 9387Reads data from the shared file associated with this **Ashmem** object. 9388 9389**System capability**: SystemCapability.Communication.IPC.Core 9390 9391**Parameters** 9392 9393| Name| Type | Mandatory| Description | 9394| ------ | ------ | ---- | -------------------------------------------------- | 9395| size | number | Yes | Size of the data to read. | 9396| offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 9397 9398**Return value** 9399 9400| Type | Description | 9401| -------- | ---------------- | 9402| number[] | Data read.| 9403 9404**Example** 9405 9406 ``` ts 9407 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9408 let mapResult = ashmem.mapReadAndWriteAshmem(); 9409 console.info("RpcTest map ashmem result is " + mapResult); 9410 let ByteArrayVar = [1, 2, 3, 4, 5]; 9411 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 9412 console.log("RpcTest: write to Ashmem result is : " + writeResult); 9413 let readResult = ashmem.readFromAshmem(5, 0); 9414 console.log("RpcTest: read to Ashmem result is : " + readResult); 9415 ``` 9416 9417## Obtaining the Context 9418 9419**Example** 9420This example describes only one method of obtaining the context. For details about more methods, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 9421 ```ts 9422 import Ability from '@ohos.app.ability.UIAbility'; 9423 import Want from '@ohos.app.ability.Want'; 9424 import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 9425 import window from '@ohos.window'; 9426 9427 export default class MainAbility extends Ability { 9428 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 9429 console.log("[Demo] MainAbility onCreate"); 9430 let context = this.context; 9431 } 9432 onDestroy() { 9433 console.log("[Demo] MainAbility onDestroy"); 9434 } 9435 onWindowStageCreate(windowStage: window.WindowStage) { 9436 // Main window is created, set main page for this ability 9437 console.log("[Demo] MainAbility onWindowStageCreate"); 9438 } 9439 onWindowStageDestroy() { 9440 // Main window is destroyed, release UI related resources 9441 console.log("[Demo] MainAbility onWindowStageDestroy"); 9442 } 9443 onForeground() { 9444 // Ability has brought to foreground 9445 console.log("[Demo] MainAbility onForeground"); 9446 } 9447 onBackground() { 9448 // Ability has back to background 9449 console.log("[Demo] MainAbility onBackground"); 9450 } 9451 }; 9452 ``` 9453