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 '@kit.IPCKit'; 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## TypeCode<sup>12+</sup> 42 43Since API version 12, [writeArrayBuffer](#writearraybuffer12) and [readArrayBuffer](#readarraybuffer12) are added to pass ArrayBuffer data. The specific TypedArray type is determined by **TypeCode** defined as follows: 44 45**System capability**: SystemCapability.Communication.IPC.Core 46 47| Name | Value | Description | 48| ---------------------------- | ------ | -------------------------------------------- | 49| INT8_ARRAY | 0 | The TypedArray type is **INT8_ARRAY**. | 50| UINT8_ARRAY | 1 | The TypedArray type is **UINT8_ARRAY**. | 51| INT16_ARRAY | 2 | The TypedArray type is **INT16_ARRAY**. | 52| UINT16_ARRAY | 3 | The TypedArray type is **UINT16_ARRAY**. | 53| INT32_ARRAY | 4 | The TypedArray type is **INT32_ARRAY**. | 54| UINT32_ARRAY | 5 | The TypedArray type is **UINT32_ARRAY**. | 55| FLOAT32_ARRAY | 6 | The TypedArray type is **FLOAT32_ARRAY**. | 56| FLOAT64_ARRAY | 7 | The TypedArray type is **FLOAT64_ARRAY**. | 57| BIGINT64_ARRAY | 8 | The TypedArray type is **BIGINT64_ARRAY**. | 58| BIGUINT64_ARRAY | 9 | The TypedArray type is **BIGUINT64_ARRAY**. | 59 60 61## MessageSequence<sup>9+</sup> 62 63Provides APIs for reading and writing data in specific format. During RPC or IPC, the sender can use the **write()** method provided by **MessageSequence** to write data in specific format to a **MessageSequence** object. The receiver can use the **read()** method provided by **MessageSequence** to read data in specific format from a **MessageSequence** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 64 65### create 66 67static create(): MessageSequence 68 69Creates a **MessageSequence** object. This API is a static method. 70 71**System capability**: SystemCapability.Communication.IPC.Core 72 73**Return value** 74 75| Type | Description | 76| --------------- | ------------------------------- | 77| [MessageSequence](#messagesequence9) | **MessageSequence** object created.| 78 79**Example** 80 81 ```ts 82 import { hilog } from '@kit.PerformanceAnalysisKit'; 83 84 let data = rpc.MessageSequence.create(); 85 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 86 ``` 87 88### reclaim 89 90reclaim(): void 91 92Reclaims the **MessageSequence** object that is no longer used. 93 94**System capability**: SystemCapability.Communication.IPC.Core 95 96**Example** 97 98 ```ts 99 let reply = rpc.MessageSequence.create(); 100 reply.reclaim(); 101 ``` 102 103### writeRemoteObject 104 105writeRemoteObject(object: IRemoteObject): void 106 107Serializes a remote object and writes it to this **MessageSequence** object. 108 109**System capability**: SystemCapability.Communication.IPC.Core 110 111**Parameters** 112 113| Name| Type | Mandatory| Description | 114| ------ | ------------------------------- | ---- | ----------------------------------------- | 115| object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 116 117**Error codes** 118 119For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 120 121| ID| Error Message| 122| -------- | -------- | 123| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 124| 1900008 | The proxy or remote object is invalid. | 125| 1900009 | Failed to write data to the message sequence. | 126 127**Example** 128 129 ```ts 130 import { hilog } from '@kit.PerformanceAnalysisKit'; 131 import { BusinessError } from '@kit.BasicServicesKit'; 132 133 class TestRemoteObject extends rpc.RemoteObject { 134 constructor(descriptor: string) { 135 super(descriptor); 136 } 137 } 138 let data = rpc.MessageSequence.create(); 139 let testRemoteObject = new TestRemoteObject("testObject"); 140 try { 141 data.writeRemoteObject(testRemoteObject); 142 } catch (error) { 143 let e: BusinessError = error as BusinessError; 144 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 145 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 146 } 147 ``` 148 149### readRemoteObject 150 151readRemoteObject(): IRemoteObject 152 153Reads 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. 154 155**System capability**: SystemCapability.Communication.IPC.Core 156 157**Return value** 158 159| Type | Description | 160| ------------------------------- | ------------------ | 161| [IRemoteObject](#iremoteobject) | Remote object obtained.| 162 163**Error codes** 164 165For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 166 167| ID| Error Message| 168| -------- | -------- | 169| 1900008 | The proxy or remote object is invalid. | 170| 1900010 | Failed to read data from the message sequence. | 171 172**Example** 173 174 ```ts 175 import { hilog } from '@kit.PerformanceAnalysisKit'; 176 import { BusinessError } from '@kit.BasicServicesKit'; 177 178 class TestRemoteObject extends rpc.RemoteObject { 179 constructor(descriptor: string) { 180 super(descriptor); 181 } 182 } 183 let data = rpc.MessageSequence.create(); 184 let testRemoteObject = new TestRemoteObject("testObject"); 185 try { 186 data.writeRemoteObject(testRemoteObject); 187 let proxy = data.readRemoteObject(); 188 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 189 } catch (error) { 190 let e: BusinessError = error as BusinessError; 191 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 192 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 193 } 194 ``` 195 196### writeInterfaceToken 197 198writeInterfaceToken(token: string): void 199 200Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 201 202**System capability**: SystemCapability.Communication.IPC.Core 203 204**Parameters** 205 206| Name| Type | Mandatory| Description | 207| ------ | ------ | ---- | ------------------ | 208| token | string | Yes | Interface token to write.| 209 210**Error codes** 211 212For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 213 214| ID| Error Message| 215| -------- | -------- | 216| 401 | Parameter error. Possible causes:<br> 1.The number of parameters is incorrect;<br> 2.The parameter type does not match;<br> 3.The string length exceeds 40960 bytes;<br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 217| 1900009 | Failed to write data to the message sequence. | 218 219**Example** 220 221 ```ts 222 import { hilog } from '@kit.PerformanceAnalysisKit'; 223 import { BusinessError } from '@kit.BasicServicesKit'; 224 225 let data = rpc.MessageSequence.create(); 226 try { 227 data.writeInterfaceToken("aaa"); 228 } catch (error) { 229 let e: BusinessError = error as BusinessError; 230 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 231 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 232 } 233 ``` 234 235### readInterfaceToken 236 237readInterfaceToken(): string 238 239Reads 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. 240 241**System capability**: SystemCapability.Communication.IPC.Core 242 243**Return value** 244 245| Type | Description | 246| ------ | ------------------------ | 247| string | Interface token obtained.| 248 249**Error codes** 250 251For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 252 253| ID| Error Message| 254| -------- | -------- | 255| 1900010 | Failed to read data from the message sequence. | 256 257**Example** 258 259```ts 260import { hilog } from '@kit.PerformanceAnalysisKit'; 261import { BusinessError } from '@kit.BasicServicesKit'; 262 263class Stub extends rpc.RemoteObject { 264 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 265 try { 266 let interfaceToken = data.readInterfaceToken(); 267 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 268 } catch (error) { 269 let e: BusinessError = error as BusinessError; 270 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 271 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 272 } 273 return true; 274 } 275} 276``` 277 278### getSize 279 280getSize(): number 281 282Obtains the data size of this **MessageSequence** object. 283 284**System capability**: SystemCapability.Communication.IPC.Core 285 286**Return value** 287 288| Type | Description | 289| ------ | ----------------------------------------------- | 290| number | Size of the **MessageSequence** instance obtained, in bytes.| 291 292**Example** 293 294 ```ts 295 import { hilog } from '@kit.PerformanceAnalysisKit'; 296 297 let data = rpc.MessageSequence.create(); 298 let size = data.getSize(); 299 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 300 ``` 301 302### getCapacity 303 304getCapacity(): number 305 306Obtains the capacity of this **MessageSequence** object. 307 308**System capability**: SystemCapability.Communication.IPC.Core 309 310**Return value** 311 312| Type | Description | 313| ------ | ----- | 314| number | Capacity of the obtained **MessageSequence** object, in bytes.| 315 316**Example** 317 318 ```ts 319 import { hilog } from '@kit.PerformanceAnalysisKit'; 320 321 let data = rpc.MessageSequence.create(); 322 let result = data.getCapacity(); 323 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 324 ``` 325 326### setSize 327 328setSize(size: number): void 329 330Sets the size of the data contained in this **MessageSequence** object. 331 332**System capability**: SystemCapability.Communication.IPC.Core 333 334**Parameters** 335 336| Name| Type | Mandatory| Description | 337| ------ | ------ | ---- | ------ | 338| size | number | Yes | Data size to set, in bytes.| 339 340**Error codes** 341 342For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 343 344| ID| Error Message| 345| -------- | -------- | 346| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 347 348**Example** 349 350 ```ts 351 import { hilog } from '@kit.PerformanceAnalysisKit'; 352 import { BusinessError } from '@kit.BasicServicesKit'; 353 354 let data = rpc.MessageSequence.create(); 355 data.writeString('Hello World'); 356 try { 357 data.setSize(16); 358 } catch (error) { 359 let e: BusinessError = error as BusinessError; 360 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 361 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 362 } 363 ``` 364 365### setCapacity 366 367setCapacity(size: number): void 368 369Sets the storage capacity of this **MessageSequence** object. 370 371**System capability**: SystemCapability.Communication.IPC.Core 372 373**Parameters** 374 375| Name| Type | Mandatory| Description | 376| ------ | ------ | ---- | --------------------------------------------- | 377| size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 378 379**Error codes** 380 381For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 382 383| ID| Error Message| 384| -------- | -------- | 385| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 386| 1900011 | Memory allocation failed | 387 388**Example** 389 390 ```ts 391 import { hilog } from '@kit.PerformanceAnalysisKit'; 392 import { BusinessError } from '@kit.BasicServicesKit'; 393 394 let data = rpc.MessageSequence.create(); 395 try { 396 data.setCapacity(100); 397 } catch (error) { 398 let e: BusinessError = error as BusinessError; 399 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 400 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 401 } 402 ``` 403 404### getWritableBytes 405 406getWritableBytes(): number 407 408Obtains the writable capacity (in bytes) of this **MessageSequence** object. 409 410**System capability**: SystemCapability.Communication.IPC.Core 411 412**Return value** 413 414| Type | Description | 415| ------ | ------ | 416| number | Writable capacity of the **MessageSequence** instance, in bytes.| 417 418**Example** 419 420```ts 421import { hilog } from '@kit.PerformanceAnalysisKit'; 422 423class Stub extends rpc.RemoteObject { 424 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 425 let getWritableBytes = data.getWritableBytes(); 426 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 427 return true; 428 } 429} 430``` 431 432### getReadableBytes 433 434getReadableBytes(): number 435 436Obtains the readable capacity of this **MessageSequence** object. 437 438**System capability**: SystemCapability.Communication.IPC.Core 439 440**Return value** 441 442| Type | Description | 443| ------ | ------- | 444| number | Readable capacity of the **MessageSequence** instance, in bytes.| 445 446**Example** 447 448```ts 449import { hilog } from '@kit.PerformanceAnalysisKit'; 450 451class Stub extends rpc.RemoteObject { 452 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 453 let result = data.getReadableBytes(); 454 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 455 return true; 456 } 457} 458``` 459 460### getReadPosition 461 462getReadPosition(): number 463 464Obtains the read position of this **MessageSequence** object. 465 466**System capability**: SystemCapability.Communication.IPC.Core 467 468**Return value** 469 470| Type | Description | 471| ------ | ------ | 472| number | Read position obtained.| 473 474**Example** 475 476 ```ts 477 import { hilog } from '@kit.PerformanceAnalysisKit'; 478 479 let data = rpc.MessageSequence.create(); 480 let readPos = data.getReadPosition(); 481 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 482 ``` 483 484### getWritePosition 485 486getWritePosition(): number 487 488Obtains the write position of this **MessageSequence** object. 489 490**System capability**: SystemCapability.Communication.IPC.Core 491 492**Return value** 493 494| Type | Description | 495| ------ | ----- | 496| number | Write position obtained.| 497 498**Example** 499 500 ```ts 501 import { hilog } from '@kit.PerformanceAnalysisKit'; 502 503 let data = rpc.MessageSequence.create(); 504 data.writeInt(10); 505 let bwPos = data.getWritePosition(); 506 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 507 ``` 508 509### rewindRead 510 511rewindRead(pos: number): void 512 513Moves the read pointer to the specified position. 514 515**System capability**: SystemCapability.Communication.IPC.Core 516 517**Parameters** 518 519| Name| Type | Mandatory| Description | 520| ------ | ------ | ---- | ------- | 521| pos | number | Yes | Position from which data is to read.| 522 523**Error codes** 524 525For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 526 527| ID| Error Message| 528| -------- | -------- | 529| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 530 531**Example** 532 533 ```ts 534 import { hilog } from '@kit.PerformanceAnalysisKit'; 535 import { BusinessError } from '@kit.BasicServicesKit'; 536 537 let data = rpc.MessageSequence.create(); 538 data.writeInt(12); 539 data.writeString("sequence"); 540 let number = data.readInt(); 541 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 542 try { 543 data.rewindRead(0); 544 } catch (error) { 545 let e: BusinessError = error as BusinessError; 546 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 547 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 548 } 549 let number2 = data.readInt(); 550 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 551 ``` 552 553### rewindWrite 554 555rewindWrite(pos: number): void 556 557Moves the write pointer to the specified position. 558 559**System capability**: SystemCapability.Communication.IPC.Core 560 561**Parameters** 562 563| Name| Type | Mandatory| Description | 564| ------ | ------ | ---- | ----- | 565| pos | number | Yes | Position from which data is to write.| 566 567**Error codes** 568 569For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 570 571| ID| Error Message| 572| -------- | -------- | 573| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 574 575**Example** 576 577 ```ts 578 import { hilog } from '@kit.PerformanceAnalysisKit'; 579 import { BusinessError } from '@kit.BasicServicesKit'; 580 581 let data = rpc.MessageSequence.create(); 582 data.writeInt(4); 583 try { 584 data.rewindWrite(0); 585 } catch (error) { 586 let e: BusinessError = error as BusinessError; 587 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 588 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 589 } 590 data.writeInt(5); 591 let number = data.readInt(); 592 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 593 ``` 594 595### writeByte 596 597writeByte(val: number): void 598 599Writes a byte value to this **MessageSequence** object. 600 601**System capability**: SystemCapability.Communication.IPC.Core 602 603**Parameters** 604 605| Name| Type | Mandatory| Description | 606| ------ | ------ | ---- | ----- | 607| val | number | Yes | Byte value to write.| 608 609**Error codes** 610 611For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 612 613| ID| Error Message| 614| -------- | ------- | 615| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 616| 1900009 | Failed to write data to the message sequence. | 617 618**Example** 619 620 ```ts 621 import { hilog } from '@kit.PerformanceAnalysisKit'; 622 import { BusinessError } from '@kit.BasicServicesKit'; 623 624 let data = rpc.MessageSequence.create(); 625 try { 626 data.writeByte(2); 627 } catch (error) { 628 let e: BusinessError = error as BusinessError; 629 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 630 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 631 } 632 ``` 633 634### readByte 635 636readByte(): number 637 638Reads the byte value from this **MessageSequence** object. 639 640**System capability**: SystemCapability.Communication.IPC.Core 641 642**Return value** 643 644| Type | Description | 645| ------ | ----- | 646| number | Byte value read.| 647 648**Error codes** 649 650For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 651 652| ID| Error Message| 653| ------- | -------- | 654| 1900010 | Failed to read data from the message sequence. | 655 656**Example** 657 658 ```ts 659 import { hilog } from '@kit.PerformanceAnalysisKit'; 660 import { BusinessError } from '@kit.BasicServicesKit'; 661 662 let data = rpc.MessageSequence.create(); 663 try { 664 data.writeByte(2); 665 } catch (error) { 666 let e: BusinessError = error as BusinessError; 667 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 668 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 669 } 670 try { 671 let ret = data.readByte(); 672 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 673 } catch (error) { 674 let e: BusinessError = error as BusinessError; 675 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 676 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 677 } 678 ``` 679 680### writeShort 681 682writeShort(val: number): void 683 684Writes a short integer to this **MessageSequence** object. 685 686**System capability**: SystemCapability.Communication.IPC.Core 687 688**Parameters** 689 690| Name| Type | Mandatory| Description| 691| ------ | ------ | --- | --- | 692| val | number | Yes | Short integer to write.| 693 694**Error codes** 695 696For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 697 698| ID| Error Message| 699| -------- | -------- | 700| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 701| 1900009 | Failed to write data to the message sequence. | 702 703**Example** 704 705 ```ts 706 import { hilog } from '@kit.PerformanceAnalysisKit'; 707 import { BusinessError } from '@kit.BasicServicesKit'; 708 709 let data = rpc.MessageSequence.create(); 710 try { 711 data.writeShort(8); 712 } catch (error) { 713 let e: BusinessError = error as BusinessError; 714 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 715 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 716 } 717 ``` 718 719### readShort 720 721readShort(): number 722 723Reads the short integer from this **MessageSequence** object. 724 725**System capability**: SystemCapability.Communication.IPC.Core 726 727**Return value** 728 729| Type | Description | 730| ------ | -------------- | 731| number | Short integer read.| 732 733**Error codes** 734 735For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 736 737| ID| Error Message| 738| -------- | -------- | 739| 1900010 | Failed to read data from the message sequence. | 740 741**Example** 742 743 ```ts 744 import { hilog } from '@kit.PerformanceAnalysisKit'; 745 import { BusinessError } from '@kit.BasicServicesKit'; 746 747 let data = rpc.MessageSequence.create(); 748 try { 749 data.writeShort(8); 750 } catch (error) { 751 let e: BusinessError = error as BusinessError; 752 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 753 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 754 } 755 try { 756 let ret = data.readShort(); 757 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 758 } catch (error) { 759 let e: BusinessError = error as BusinessError; 760 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 761 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 762 } 763 ``` 764 765### writeInt 766 767writeInt(val: number): void 768 769Writes an integer to this **MessageSequence** object. 770 771**System capability**: SystemCapability.Communication.IPC.Core 772 773**Parameters** 774 775| Name| Type | Mandatory| Description | 776| ------ | ------ | ---- | ---------------- | 777| val | number | Yes | Integer to write.| 778 779**Error codes** 780 781For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 782 783| ID| Error Message| 784| -------- | -------- | 785| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 786| 1900009 | Failed to write data to the message sequence. | 787 788**Example** 789 790 ```ts 791 import { hilog } from '@kit.PerformanceAnalysisKit'; 792 import { BusinessError } from '@kit.BasicServicesKit'; 793 794 let data = rpc.MessageSequence.create(); 795 try { 796 data.writeInt(10); 797 } catch (error) { 798 let e: BusinessError = error as BusinessError; 799 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 800 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 801 } 802 ``` 803 804### readInt 805 806readInt(): number 807 808Reads the integer from this **MessageSequence** object. 809 810**System capability**: SystemCapability.Communication.IPC.Core 811 812**Return value** 813 814| Type | Description | 815| ------ | ------------ | 816| number | Integer read.| 817 818**Error codes** 819 820For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 821 822| ID| Error Message| 823| -------- | -------- | 824| 1900010 | Failed to read data from the message sequence. | 825 826**Example** 827 828 ```ts 829 import { hilog } from '@kit.PerformanceAnalysisKit'; 830 import { BusinessError } from '@kit.BasicServicesKit'; 831 832 let data = rpc.MessageSequence.create(); 833 try { 834 data.writeInt(10); 835 } catch (error) { 836 let e: BusinessError = error as BusinessError; 837 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 838 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 839 } 840 try { 841 let ret = data.readInt(); 842 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 843 } catch (error) { 844 let e: BusinessError = error as BusinessError; 845 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 846 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 847 } 848 ``` 849 850### writeLong 851 852writeLong(val: number): void 853 854Writes a long integer to this **MessageSequence** object. 855 856**System capability**: SystemCapability.Communication.IPC.Core 857 858**Parameters** 859 860| Name| Type | Mandatory| Description | 861| ------ | ------ | ---- | ---------------- | 862| val | number | Yes | Long integer to write.| 863 864**Error codes** 865 866For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 867 868| ID| Error Message| 869| -------- | -------- | 870| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 871| 1900009 | Failed to write data to the message sequence. | 872 873**Example** 874 875 ```ts 876 import { hilog } from '@kit.PerformanceAnalysisKit'; 877 import { BusinessError } from '@kit.BasicServicesKit'; 878 879 let data = rpc.MessageSequence.create(); 880 try { 881 data.writeLong(10000); 882 } catch (error) { 883 let e: BusinessError = error as BusinessError; 884 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 885 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 886 } 887 ``` 888 889### readLong 890 891readLong(): number 892 893Reads the long integer from this **MessageSequence** object. 894 895**System capability**: SystemCapability.Communication.IPC.Core 896 897**Return value** 898 899| Type | Description | 900| ------ | -------------- | 901| number | Long integer read.| 902 903**Error codes** 904 905For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 906 907| ID| Error Message| 908| -------- | -------- | 909| 1900010 | Failed to read data from the message sequence. | 910 911**Example** 912 913 ```ts 914 import { hilog } from '@kit.PerformanceAnalysisKit'; 915 import { BusinessError } from '@kit.BasicServicesKit'; 916 917 let data = rpc.MessageSequence.create(); 918 try { 919 data.writeLong(10000); 920 } catch (error) { 921 let e: BusinessError = error as BusinessError; 922 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 923 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 924 } 925 try { 926 let ret = data.readLong(); 927 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 928 } catch (error) { 929 let e: BusinessError = error as BusinessError; 930 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 931 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 932 } 933 ``` 934 935### writeFloat 936 937writeFloat(val: number): void 938 939Writes a floating-point number to this **MessageSequence** object. 940 941**System capability**: SystemCapability.Communication.IPC.Core 942 943**Parameters** 944 945| Name| Type | Mandatory| Description | 946| ------ | ------ | ---- | ----- | 947| val | number | Yes | Floating-point number to write.| 948 949**Error codes** 950 951For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 952 953| ID| Error Message| 954| -------- | -------- | 955| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 956| 1900009 | Failed to write data to the message sequence. | 957 958**Example** 959 960 ```ts 961 import { hilog } from '@kit.PerformanceAnalysisKit'; 962 import { BusinessError } from '@kit.BasicServicesKit'; 963 964 let data = rpc.MessageSequence.create(); 965 try { 966 data.writeFloat(1.2); 967 } catch (error) { 968 let e: BusinessError = error as BusinessError; 969 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 970 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 971 } 972 ``` 973 974### readFloat 975 976readFloat(): number 977 978Reads the floating-point number from this **MessageSequence** object. 979 980**System capability**: SystemCapability.Communication.IPC.Core 981 982**Return value** 983 984| Type | Description | 985| ------ | ------------ | 986| number | Floating-point number read.| 987 988**Error codes** 989 990For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 991 992| ID| Error Message| 993| -------- | -------- | 994| 1900010 | Failed to read data from the message sequence. | 995 996**Example** 997 998 ```ts 999 import { hilog } from '@kit.PerformanceAnalysisKit'; 1000 import { BusinessError } from '@kit.BasicServicesKit'; 1001 1002 let data = rpc.MessageSequence.create(); 1003 try { 1004 data.writeFloat(1.2); 1005 } catch (error) { 1006 let e: BusinessError = error as BusinessError; 1007 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1008 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1009 } 1010 try { 1011 let ret = data.readFloat(); 1012 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1013 } catch (error) { 1014 let e: BusinessError = error as BusinessError; 1015 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1016 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1017 } 1018 ``` 1019 1020### writeDouble 1021 1022writeDouble(val: number): void 1023 1024Writes a double-precision floating-point number to this **MessageSequence** object. 1025 1026**System capability**: SystemCapability.Communication.IPC.Core 1027 1028**Parameters** 1029 1030| Name| Type | Mandatory| Description | 1031| ------ | ------ | ---- | ---------------------- | 1032| val | number | Yes | Double-precision floating-point number to write.| 1033 1034**Error codes** 1035 1036For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1037 1038| ID| Error Message| 1039| -------- | -------- | 1040| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1041| 1900009 | Failed to write data to the message sequence. | 1042 1043**Example** 1044 1045 ```ts 1046 import { hilog } from '@kit.PerformanceAnalysisKit'; 1047 import { BusinessError } from '@kit.BasicServicesKit'; 1048 1049 let data = rpc.MessageSequence.create(); 1050 try { 1051 data.writeDouble(10.2); 1052 } catch (error) { 1053 let e: BusinessError = error as BusinessError; 1054 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1055 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1056 } 1057 ``` 1058 1059### readDouble 1060 1061readDouble(): number 1062 1063Reads the double-precision floating-point number from this **MessageSequence** object. 1064 1065**System capability**: SystemCapability.Communication.IPC.Core 1066 1067**Return value** 1068 1069| Type | Description | 1070| ------ | ------------------ | 1071| number | Double-precision floating-point number read.| 1072 1073**Error codes** 1074 1075For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1076 1077| ID| Error Message| 1078| -------- | -------- | 1079| 1900010 | Failed to read data from the message sequence. | 1080 1081**Example** 1082 1083 ```ts 1084 import { hilog } from '@kit.PerformanceAnalysisKit'; 1085 import { BusinessError } from '@kit.BasicServicesKit'; 1086 1087 let data = rpc.MessageSequence.create(); 1088 try { 1089 data.writeDouble(10.2); 1090 } catch (error) { 1091 let e: BusinessError = error as BusinessError; 1092 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1093 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1094 } 1095 try { 1096 let ret = data.readDouble(); 1097 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1098 } catch (error) { 1099 let e: BusinessError = error as BusinessError; 1100 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1101 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1102 } 1103 ``` 1104 1105### writeBoolean 1106 1107writeBoolean(val: boolean): void 1108 1109Writes a Boolean value to this **MessageSequence** object. 1110 1111**System capability**: SystemCapability.Communication.IPC.Core 1112 1113**Parameters** 1114 1115| Name| Type | Mandatory| Description | 1116| ------ | ------- | ---- | ---------------- | 1117| val | boolean | Yes | Boolean value to write.| 1118 1119**Error codes** 1120 1121For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1122 1123| ID| Error Message| 1124| -------- | -------- | 1125| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1126| 1900009 | Failed to write data to the message sequence. | 1127 1128**Example** 1129 1130 ```ts 1131 import { hilog } from '@kit.PerformanceAnalysisKit'; 1132 import { BusinessError } from '@kit.BasicServicesKit'; 1133 1134 let data = rpc.MessageSequence.create(); 1135 try { 1136 data.writeBoolean(false); 1137 } catch (error) { 1138 let e: BusinessError = error as BusinessError; 1139 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1140 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1141 } 1142 ``` 1143 1144### readBoolean 1145 1146readBoolean(): boolean 1147 1148Reads the Boolean value from this **MessageSequence** object. 1149 1150**System capability**: SystemCapability.Communication.IPC.Core 1151 1152**Return value** 1153 1154| Type | Description | 1155| ------- | -------------------- | 1156| boolean | Boolean value read.| 1157 1158**Error codes** 1159 1160For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1161 1162| ID| Error Message| 1163| -------- | -------- | 1164| 1900010 | Failed to read data from the message sequence. | 1165 1166**Example** 1167 1168 ```ts 1169 import { hilog } from '@kit.PerformanceAnalysisKit'; 1170 import { BusinessError } from '@kit.BasicServicesKit'; 1171 1172 let data = rpc.MessageSequence.create(); 1173 try { 1174 data.writeBoolean(false); 1175 } catch (error) { 1176 let e: BusinessError = error as BusinessError; 1177 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1178 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1179 } 1180 try { 1181 let ret = data.readBoolean(); 1182 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1183 } catch (error) { 1184 let e: BusinessError = error as BusinessError; 1185 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1186 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1187 } 1188 ``` 1189 1190### writeChar 1191 1192writeChar(val: number): void 1193 1194Writes a character to this **MessageSequence** object. 1195 1196**System capability**: SystemCapability.Communication.IPC.Core 1197 1198**Parameters** 1199 1200| Name| Type | Mandatory| Description | 1201| ------ | ------ | ---- | -------------------- | 1202| val | number | Yes | Single character to write.| 1203 1204**Error codes** 1205 1206For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1207 1208| ID| Error Message| 1209| -------- | -------- | 1210| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1211| 1900009 | Failed to write data to the message sequence. | 1212 1213**Example** 1214 1215 ```ts 1216 import { hilog } from '@kit.PerformanceAnalysisKit'; 1217 import { BusinessError } from '@kit.BasicServicesKit'; 1218 1219 let data = rpc.MessageSequence.create(); 1220 try { 1221 data.writeChar(97); 1222 } catch (error) { 1223 let e: BusinessError = error as BusinessError; 1224 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1225 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1226 } 1227 ``` 1228 1229### readChar 1230 1231readChar(): number 1232 1233Reads the character from this **MessageSequence** object. 1234 1235**System capability**: SystemCapability.Communication.IPC.Core 1236 1237**Return value** 1238 1239| Type | Description| 1240| ------ | ---- | 1241| number | Character read.| 1242 1243**Error codes** 1244 1245For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1246 1247| ID| Error Message| 1248| -------- | -------- | 1249| 1900010 | Failed to read data from the message sequence. | 1250 1251**Example** 1252 1253 ```ts 1254 import { hilog } from '@kit.PerformanceAnalysisKit'; 1255 import { BusinessError } from '@kit.BasicServicesKit'; 1256 1257 let data = rpc.MessageSequence.create(); 1258 try { 1259 data.writeChar(97); 1260 } catch (error) { 1261 let e: BusinessError = error as BusinessError; 1262 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1263 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1264 } 1265 try { 1266 let ret = data.readChar(); 1267 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1268 } catch (error) { 1269 let e: BusinessError = error as BusinessError; 1270 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1271 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1272 } 1273 ``` 1274 1275### writeString 1276 1277writeString(val: string): void 1278 1279Writes a string to this **MessageSequence** object. 1280 1281**System capability**: SystemCapability.Communication.IPC.Core 1282 1283**Parameters** 1284 1285| Name| Type | Mandatory| Description | 1286| ------ | ------ | ---- | ----------------------------------------- | 1287| val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1288 1289**Error codes** 1290 1291For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1292 1293| ID| Error Message| 1294| -------- | -------- | 1295| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 1296| 1900009 | Failed to write data to the message sequence. | 1297 1298**Example** 1299 1300 ```ts 1301 import { hilog } from '@kit.PerformanceAnalysisKit'; 1302 import { BusinessError } from '@kit.BasicServicesKit'; 1303 1304 let data = rpc.MessageSequence.create(); 1305 try { 1306 data.writeString('abc'); 1307 } catch (error) { 1308 let e: BusinessError = error as BusinessError; 1309 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1310 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1311 } 1312 ``` 1313 1314### readString 1315 1316readString(): string 1317 1318Reads the string from this **MessageSequence** object. 1319 1320**System capability**: SystemCapability.Communication.IPC.Core 1321 1322**Return value** 1323 1324| Type | Description | 1325| ------ | -------------- | 1326| string | String read.| 1327 1328**Error codes** 1329 1330For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1331 1332| ID| Error Message| 1333| -------- | -------- | 1334| 1900010 | Failed to read data from the message sequence. | 1335 1336**Example** 1337 1338 ```ts 1339 import { hilog } from '@kit.PerformanceAnalysisKit'; 1340 import { BusinessError } from '@kit.BasicServicesKit'; 1341 1342 let data = rpc.MessageSequence.create(); 1343 try { 1344 data.writeString('abc'); 1345 } catch (error) { 1346 let e: BusinessError = error as BusinessError; 1347 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1348 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1349 } 1350 try { 1351 let ret = data.readString(); 1352 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1353 } catch (error) { 1354 let e: BusinessError = error as BusinessError; 1355 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1356 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1357 } 1358 ``` 1359 1360### writeParcelable 1361 1362writeParcelable(val: Parcelable): void 1363 1364Writes a **Parcelable** object to this **MessageSequence** object. 1365 1366**System capability**: SystemCapability.Communication.IPC.Core 1367 1368**Parameters** 1369 1370| Name| Type| Mandatory| Description| 1371| ------ | --------- | ---- | ------ | 1372| val | [Parcelable](#parcelable9) | Yes | **Parcelable** object to write.| 1373 1374**Error codes** 1375 1376For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1377 1378| ID| Error Message| 1379| -------- | -------- | 1380| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1381| 1900009 | Failed to write data to the message sequence. | 1382 1383**Example** 1384 1385 ```ts 1386 import { hilog } from '@kit.PerformanceAnalysisKit'; 1387 import { BusinessError } from '@kit.BasicServicesKit'; 1388 1389 class MyParcelable implements rpc.Parcelable { 1390 num: number = 0; 1391 str: string = ''; 1392 constructor( num: number, str: string) { 1393 this.num = num; 1394 this.str = str; 1395 } 1396 marshalling(messageSequence: rpc.MessageSequence): boolean { 1397 messageSequence.writeInt(this.num); 1398 messageSequence.writeString(this.str); 1399 return true; 1400 } 1401 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1402 this.num = messageSequence.readInt(); 1403 this.str = messageSequence.readString(); 1404 return true; 1405 } 1406 } 1407 let parcelable = new MyParcelable(1, "aaa"); 1408 let data = rpc.MessageSequence.create(); 1409 try { 1410 data.writeParcelable(parcelable); 1411 } catch (error) { 1412 let e: BusinessError = error as BusinessError; 1413 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1414 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1415 } 1416 ``` 1417 1418### readParcelable 1419 1420readParcelable(dataIn: Parcelable): void 1421 1422Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1423 1424**System capability**: SystemCapability.Communication.IPC.Core 1425 1426**Parameters** 1427 1428| Name| Type | Mandatory| Description | 1429| ------ | -------------------------- | ---- | ----------------------------------------- | 1430| dataIn | [Parcelable](#parcelable9) | Yes | **Parcelable** object to read.| 1431 1432**Error codes** 1433 1434For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1435 1436| ID| Error Message| 1437| -------- | -------- | 1438| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect. | 1439| 1900010 | Failed to read data from the message sequence. | 1440| 1900012 | Failed to call the JS callback function. | 1441 1442**Example** 1443 1444 ```ts 1445 import { hilog } from '@kit.PerformanceAnalysisKit'; 1446 import { BusinessError } from '@kit.BasicServicesKit'; 1447 1448 class MyParcelable implements rpc.Parcelable { 1449 num: number = 0; 1450 str: string = ''; 1451 constructor(num: number, str: string) { 1452 this.num = num; 1453 this.str = str; 1454 } 1455 marshalling(messageSequence: rpc.MessageSequence): boolean { 1456 messageSequence.writeInt(this.num); 1457 messageSequence.writeString(this.str); 1458 return true; 1459 } 1460 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1461 this.num = messageSequence.readInt(); 1462 this.str = messageSequence.readString(); 1463 return true; 1464 } 1465 } 1466 let parcelable = new MyParcelable(1, "aaa"); 1467 let data = rpc.MessageSequence.create(); 1468 data.writeParcelable(parcelable); 1469 let ret = new MyParcelable(0, ""); 1470 try { 1471 data.readParcelable(ret); 1472 }catch (error) { 1473 let e: BusinessError = error as BusinessError; 1474 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1475 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1476 } 1477 ``` 1478 1479### writeByteArray 1480 1481writeByteArray(byteArray: number[]): void 1482 1483Writes a byte array to this **MessageSequence** object. 1484 1485**System capability**: SystemCapability.Communication.IPC.Core 1486 1487**Parameters** 1488 1489| Name | Type | Mandatory| Description | 1490| --------- | -------- | ---- | ------------------ | 1491| byteArray | number[] | Yes | Byte array to write.| 1492 1493**Error codes** 1494 1495For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1496 1497| ID| Error Message| 1498| -------- | -------- | 1499| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. <br> 5.The type of the element in the array is incorrect. | 1500| 1900009 | Failed to write data to the message sequence. | 1501 1502**Example** 1503 1504 ```ts 1505 import { hilog } from '@kit.PerformanceAnalysisKit'; 1506 import { BusinessError } from '@kit.BasicServicesKit'; 1507 1508 let data = rpc.MessageSequence.create(); 1509 let ByteArrayVar = [1, 2, 3, 4, 5]; 1510 try { 1511 data.writeByteArray(ByteArrayVar); 1512 } catch (error) { 1513 let e: BusinessError = error as BusinessError; 1514 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1515 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1516 } 1517 ``` 1518 1519### readByteArray 1520 1521readByteArray(dataIn: number[]): void 1522 1523Reads a byte array from this **MessageSequence** object. 1524 1525**System capability**: SystemCapability.Communication.IPC.Core 1526 1527**Parameters** 1528 1529| Name| Type | Mandatory| Description | 1530| ------ | -------- | ---- | ------------------ | 1531| dataIn | number[] | Yes | Byte array to read.| 1532 1533**Error codes** 1534 1535For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1536 1537| ID| Error Message| 1538| -------- | -------- | 1539| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1540| 1900010 | Failed to read data from the message sequence. | 1541 1542**Example** 1543 1544 ```ts 1545 import { hilog } from '@kit.PerformanceAnalysisKit'; 1546 import { BusinessError } from '@kit.BasicServicesKit'; 1547 1548 let data = rpc.MessageSequence.create(); 1549 let ByteArrayVar = [1, 2, 3, 4, 5]; 1550 try { 1551 data.writeByteArray(ByteArrayVar); 1552 } catch (error) { 1553 let e: BusinessError = error as BusinessError; 1554 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1555 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1556 } 1557 try { 1558 let array: Array<number> = new Array(5); 1559 data.readByteArray(array); 1560 } catch (error) { 1561 let e: BusinessError = error as BusinessError; 1562 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1563 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1564 } 1565 ``` 1566 1567### readByteArray 1568 1569readByteArray(): number[] 1570 1571Reads the byte array from this **MessageSequence** object. 1572 1573**System capability**: SystemCapability.Communication.IPC.Core 1574 1575**Return value** 1576 1577| Type | Description | 1578| -------- | -------------- | 1579| number[] | Byte array read.| 1580 1581**Error codes** 1582 1583For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1584 1585| ID| Error Message| 1586| -------- | -------- | 1587| 401 | check param failed | 1588| 1900010 | Failed to read data from the message sequence. | 1589 1590**Example** 1591 1592 ```ts 1593 import { hilog } from '@kit.PerformanceAnalysisKit'; 1594 import { BusinessError } from '@kit.BasicServicesKit'; 1595 1596 let data = rpc.MessageSequence.create(); 1597 let byteArrayVar = [1, 2, 3, 4, 5]; 1598 try { 1599 data.writeByteArray(byteArrayVar); 1600 } catch (error) { 1601 let e: BusinessError = error as BusinessError; 1602 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1603 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1604 } 1605 try { 1606 let array = data.readByteArray(); 1607 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1608 } catch (error) { 1609 let e: BusinessError = error as BusinessError; 1610 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1611 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1612 } 1613 ``` 1614 1615### writeShortArray 1616 1617writeShortArray(shortArray: number[]): void 1618 1619Writes a short array to this **MessageSequence** object. 1620 1621**System capability**: SystemCapability.Communication.IPC.Core 1622 1623**Parameters** 1624 1625| Name | Type | Mandatory| Description | 1626| ---------- | -------- | ---- | -------------------- | 1627| shortArray | number[] | Yes | Short array to write.| 1628 1629**Error codes** 1630 1631For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1632 1633| ID| Error Message| 1634| -------- | -------- | 1635| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1636| 1900009 | Failed to write data to the message sequence. | 1637 1638**Example** 1639 1640 ```ts 1641 import { hilog } from '@kit.PerformanceAnalysisKit'; 1642 import { BusinessError } from '@kit.BasicServicesKit'; 1643 1644 let data = rpc.MessageSequence.create(); 1645 try { 1646 data.writeShortArray([11, 12, 13]); 1647 } catch (error) { 1648 let e: BusinessError = error as BusinessError; 1649 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1650 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1651 } 1652 ``` 1653 1654### readShortArray 1655 1656readShortArray(dataIn: number[]): void 1657 1658Reads a short array from this **MessageSequence** object. 1659 1660**System capability**: SystemCapability.Communication.IPC.Core 1661 1662**Parameters** 1663 1664| Name| Type | Mandatory| Description | 1665| ------ | -------- | ---- | -------------------- | 1666| dataIn | number[] | Yes | Short array to read.| 1667 1668**Error codes** 1669 1670For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1671 1672| ID| Error Message| 1673| -------- | -------- | 1674| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1675| 1900010 | Failed to read data from the message sequence. | 1676 1677**Example** 1678 1679 ```ts 1680 import { hilog } from '@kit.PerformanceAnalysisKit'; 1681 import { BusinessError } from '@kit.BasicServicesKit'; 1682 1683 let data = rpc.MessageSequence.create(); 1684 try { 1685 data.writeShortArray([11, 12, 13]); 1686 } catch (error) { 1687 let e: BusinessError = error as BusinessError; 1688 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1689 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1690 } 1691 try { 1692 let array: Array<number> = new Array(3); 1693 data.readShortArray(array); 1694 } catch (error) { 1695 let e: BusinessError = error as BusinessError; 1696 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1697 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1698 } 1699 ``` 1700 1701### readShortArray 1702 1703readShortArray(): number[] 1704 1705Reads the short array from this **MessageSequence** object. 1706 1707**System capability**: SystemCapability.Communication.IPC.Core 1708 1709**Return value** 1710 1711| Type | Description | 1712| -------- | ---------------- | 1713| number[] | Short array read.| 1714 1715**Error codes** 1716 1717For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1718 1719| ID| Error Message| 1720| -------- | -------- | 1721| 1900010 | Failed to read data from the message sequence. | 1722 1723**Example** 1724 1725 ```ts 1726 import { hilog } from '@kit.PerformanceAnalysisKit'; 1727 import { BusinessError } from '@kit.BasicServicesKit'; 1728 1729 let data = rpc.MessageSequence.create(); 1730 try { 1731 data.writeShortArray([11, 12, 13]); 1732 } catch (error) { 1733 let e: BusinessError = error as BusinessError; 1734 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1735 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1736 } 1737 try { 1738 let array = data.readShortArray(); 1739 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1740 } catch (error) { 1741 let e: BusinessError = error as BusinessError; 1742 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1743 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1744 } 1745 ``` 1746 1747### writeIntArray 1748 1749writeIntArray(intArray: number[]): void 1750 1751Writes an integer array to this **MessageSequence** object. 1752 1753**System capability**: SystemCapability.Communication.IPC.Core 1754 1755**Parameters** 1756 1757| Name | Type | Mandatory| Description | 1758| -------- | -------- | ---- | ------------------ | 1759| intArray | number[] | Yes | Integer array to write.| 1760 1761**Error codes** 1762 1763For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1764 1765| ID| Error Message| 1766| -------- | -------- | 1767| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1768| 1900009 | Failed to write data to the message sequence. | 1769 1770**Example** 1771 1772 ```ts 1773 import { hilog } from '@kit.PerformanceAnalysisKit'; 1774 import { BusinessError } from '@kit.BasicServicesKit'; 1775 1776 let data = rpc.MessageSequence.create(); 1777 try { 1778 data.writeIntArray([100, 111, 112]); 1779 } catch (error) { 1780 let e: BusinessError = error as BusinessError; 1781 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1782 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1783 } 1784 ``` 1785 1786### readIntArray 1787 1788readIntArray(dataIn: number[]): void 1789 1790Reads an integer array from this **MessageSequence** object. 1791 1792**System capability**: SystemCapability.Communication.IPC.Core 1793 1794**Parameters** 1795 1796| Name| Type | Mandatory| Description | 1797| ------ | -------- | ---- | ------------------ | 1798| dataIn | number[] | Yes | Integer array to read.| 1799 1800**Error codes** 1801 1802For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1803 1804| ID| Error Message| 1805| -------- | -------- | 1806| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1807| 1900010 | Failed to read data from the message sequence. | 1808 1809**Example** 1810 1811 ```ts 1812 import { hilog } from '@kit.PerformanceAnalysisKit'; 1813 import { BusinessError } from '@kit.BasicServicesKit'; 1814 1815 let data = rpc.MessageSequence.create(); 1816 try { 1817 data.writeIntArray([100, 111, 112]); 1818 } catch (error) { 1819 let e: BusinessError = error as BusinessError; 1820 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1821 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1822 } 1823 let array: Array<number> = new Array(3); 1824 try { 1825 data.readIntArray(array); 1826 } catch (error) { 1827 let e: BusinessError = error as BusinessError; 1828 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1829 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1830 } 1831 ``` 1832 1833### readIntArray 1834 1835readIntArray(): number[] 1836 1837Reads the integer array from this **MessageSequence** object. 1838 1839**System capability**: SystemCapability.Communication.IPC.Core 1840 1841**Return value** 1842 1843| Type | Description | 1844| -------- | -------------- | 1845| number[] | Integer array read.| 1846 1847**Error codes** 1848 1849For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1850 1851| ID| Error Message| 1852| -------- | -------- | 1853| 1900010 | Failed to read data from the message sequence. | 1854 1855**Example** 1856 1857 ```ts 1858 import { hilog } from '@kit.PerformanceAnalysisKit'; 1859 import { BusinessError } from '@kit.BasicServicesKit'; 1860 1861 let data = rpc.MessageSequence.create(); 1862 try { 1863 data.writeIntArray([100, 111, 112]); 1864 } catch (error) { 1865 let e: BusinessError = error as BusinessError; 1866 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1867 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1868 } 1869 try { 1870 let array = data.readIntArray(); 1871 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1872 } catch (error) { 1873 let e: BusinessError = error as BusinessError; 1874 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1875 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1876 } 1877 ``` 1878 1879### writeLongArray 1880 1881writeLongArray(longArray: number[]): void 1882 1883Writes a long array to this **MessageSequence** object. 1884 1885**System capability**: SystemCapability.Communication.IPC.Core 1886 1887**Parameters** 1888 1889| Name | Type | Mandatory| Description | 1890| --------- | -------- | ---- | -------------------- | 1891| longArray | number[] | Yes | Long array to write.| 1892 1893**Error codes** 1894 1895For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1896 1897| ID| Error Message| 1898| -------- | -------- | 1899| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 1900| 1900009 | Failed to write data to the message sequence. | 1901 1902**Example** 1903 1904 ```ts 1905 import { hilog } from '@kit.PerformanceAnalysisKit'; 1906 import { BusinessError } from '@kit.BasicServicesKit'; 1907 1908 let data = rpc.MessageSequence.create(); 1909 try { 1910 data.writeLongArray([1111, 1112, 1113]); 1911 }catch (error){ 1912 let e: BusinessError = error as BusinessError; 1913 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1914 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1915 } 1916 ``` 1917 1918### readLongArray 1919 1920readLongArray(dataIn: number[]): void 1921 1922Reads a long array from this **MessageSequence** object. 1923 1924**System capability**: SystemCapability.Communication.IPC.Core 1925 1926**Parameters** 1927 1928| Name| Type | Mandatory| Description | 1929| ------ | -------- | ---- | -------------------- | 1930| dataIn | number[] | Yes | Long array to read.| 1931 1932**Error codes** 1933 1934For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1935 1936| ID| Error Message| 1937| -------- | -------- | 1938| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 1939| 1900010 | Failed to read data from the message sequence. | 1940 1941**Example** 1942 1943 ```ts 1944 import { hilog } from '@kit.PerformanceAnalysisKit'; 1945 import { BusinessError } from '@kit.BasicServicesKit'; 1946 1947 let data = rpc.MessageSequence.create(); 1948 try { 1949 data.writeLongArray([1111, 1112, 1113]); 1950 } catch (error) { 1951 let e: BusinessError = error as BusinessError; 1952 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1953 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1954 } 1955 let array: Array<number> = new Array(3); 1956 try { 1957 data.readLongArray(array); 1958 } catch (error) { 1959 let e: BusinessError = error as BusinessError; 1960 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1961 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1962 } 1963 ``` 1964 1965### readLongArray 1966 1967readLongArray(): number[] 1968 1969Reads the long integer array from this **MessageSequence** object. 1970 1971**System capability**: SystemCapability.Communication.IPC.Core 1972 1973**Return value** 1974 1975| Type | Description | 1976| -------- | ---------------- | 1977| number[] | Long array read.| 1978 1979**Error codes** 1980 1981For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1982 1983| ID| Error Message| 1984| -------- | -------- | 1985| 1900010 | Failed to read data from the message sequence. | 1986 1987**Example** 1988 1989 ```ts 1990 import { hilog } from '@kit.PerformanceAnalysisKit'; 1991 import { BusinessError } from '@kit.BasicServicesKit'; 1992 1993 let data = rpc.MessageSequence.create(); 1994 try { 1995 data.writeLongArray([1111, 1112, 1113]); 1996 } catch (error) { 1997 let e: BusinessError = error as BusinessError; 1998 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1999 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2000 } 2001 try { 2002 let array = data.readLongArray(); 2003 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2004 } catch (error) { 2005 let e: BusinessError = error as BusinessError; 2006 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2007 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2008 } 2009 ``` 2010 2011### writeFloatArray 2012 2013writeFloatArray(floatArray: number[]): void 2014 2015Writes a floating-point array to this **MessageSequence** object. 2016 2017**System capability**: SystemCapability.Communication.IPC.Core 2018 2019**Parameters** 2020 2021| Name | Type | Mandatory| Description | 2022| ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2023| 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.| 2024 2025**Error codes** 2026 2027For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2028 2029| ID| Error Message| 2030| -------- | -------- | 2031| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 2032| 1900009 | Failed to write data to the message sequence. | 2033 2034**Example** 2035 2036 ```ts 2037 import { hilog } from '@kit.PerformanceAnalysisKit'; 2038 import { BusinessError } from '@kit.BasicServicesKit'; 2039 2040 let data = rpc.MessageSequence.create(); 2041 try { 2042 data.writeFloatArray([1.2, 1.3, 1.4]); 2043 } catch (error) { 2044 let e: BusinessError = error as BusinessError; 2045 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2046 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2047 } 2048 ``` 2049 2050### readFloatArray 2051 2052readFloatArray(dataIn: number[]): void 2053 2054Reads a floating-point array from this **MessageSequence** object. 2055 2056**System capability**: SystemCapability.Communication.IPC.Core 2057 2058**Parameters** 2059 2060| Name| Type | Mandatory| Description | 2061| ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2062| 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.| 2063 2064**Error codes** 2065 2066For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2067 2068| ID| Error Message| 2069| -------- | -------- | 2070| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2071| 1900010 | Failed to read data from the message sequence. | 2072 2073**Example** 2074 2075 ```ts 2076 import { hilog } from '@kit.PerformanceAnalysisKit'; 2077 import { BusinessError } from '@kit.BasicServicesKit'; 2078 2079 let data = rpc.MessageSequence.create(); 2080 try { 2081 data.writeFloatArray([1.2, 1.3, 1.4]); 2082 }catch (error){ 2083 let e: BusinessError = error as BusinessError; 2084 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2085 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2086 } 2087 let array: Array<number> = new Array(3); 2088 try { 2089 data.readFloatArray(array); 2090 } catch (error) { 2091 let e: BusinessError = error as BusinessError; 2092 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2093 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2094 } 2095 ``` 2096 2097### readFloatArray 2098 2099readFloatArray(): number[] 2100 2101Reads the floating-point array from this **MessageSequence** object. 2102 2103**System capability**: SystemCapability.Communication.IPC.Core 2104 2105**Return value** 2106 2107| Type | Description | 2108| -------- | -------------- | 2109| number[] | Floating-point array read.| 2110 2111**Error codes** 2112 2113For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2114 2115| ID| Error Message| 2116| -------- | -------- | 2117| 1900010 | Failed to read data from the message sequence. | 2118 2119**Example** 2120 2121 ```ts 2122 import { hilog } from '@kit.PerformanceAnalysisKit'; 2123 import { BusinessError } from '@kit.BasicServicesKit'; 2124 2125 let data = rpc.MessageSequence.create(); 2126 try { 2127 data.writeFloatArray([1.2, 1.3, 1.4]); 2128 } catch (error) { 2129 let e: BusinessError = error as BusinessError; 2130 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2131 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2132 } 2133 try { 2134 let array = data.readFloatArray(); 2135 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2136 } catch (error) { 2137 let e: BusinessError = error as BusinessError; 2138 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2139 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2140 } 2141 ``` 2142 2143### writeDoubleArray 2144 2145writeDoubleArray(doubleArray: number[]): void 2146 2147Writes a double-precision floating-point array to this **MessageSequence** object. 2148 2149**System capability**: SystemCapability.Communication.IPC.Core 2150 2151**Parameters** 2152 2153| Name | Type | Mandatory| Description | 2154| ----------- | -------- | ---- | ------------------------ | 2155| doubleArray | number[] | Yes | Double-precision floating-point array to write.| 2156 2157**Error codes** 2158 2159For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2160 2161| ID| Error Message| 2162| -------- | -------- | 2163| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The type of the element in the array is incorrect. | 2164| 1900009 | Failed to write data to the message sequence. | 2165 2166**Example** 2167 2168 ```ts 2169 import { hilog } from '@kit.PerformanceAnalysisKit'; 2170 import { BusinessError } from '@kit.BasicServicesKit'; 2171 2172 let data = rpc.MessageSequence.create(); 2173 try { 2174 data.writeDoubleArray([11.1, 12.2, 13.3]); 2175 } catch (error) { 2176 let e: BusinessError = error as BusinessError; 2177 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2178 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2179 } 2180 ``` 2181 2182### readDoubleArray 2183 2184readDoubleArray(dataIn: number[]): void 2185 2186Reads a double-precision floating-point array from this **MessageSequence** object. 2187 2188**System capability**: SystemCapability.Communication.IPC.Core 2189 2190**Parameters** 2191 2192| Name| Type | Mandatory| Description | 2193| ------ | -------- | ---- | ------------------------ | 2194| dataIn | number[] | Yes | Double-precision floating-point array to read.| 2195 2196**Error codes** 2197 2198For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2199 2200| ID| Error Message| 2201| -------- | -------- | 2202| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2203| 1900010 | Failed to read data from the message sequence. | 2204 2205**Example** 2206 2207 ```ts 2208 import { hilog } from '@kit.PerformanceAnalysisKit'; 2209 import { BusinessError } from '@kit.BasicServicesKit'; 2210 2211 let data = rpc.MessageSequence.create(); 2212 try { 2213 data.writeDoubleArray([11.1, 12.2, 13.3]); 2214 } catch (error) { 2215 let e: BusinessError = error as BusinessError; 2216 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2217 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2218 } 2219 let array: Array<number> = new Array(3); 2220 try { 2221 data.readDoubleArray(array); 2222 } catch (error) { 2223 let e: BusinessError = error as BusinessError; 2224 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2225 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2226 } 2227 ``` 2228 2229### readDoubleArray 2230 2231readDoubleArray(): number[] 2232 2233Reads the double-precision floating-point array from this **MessageSequence** object. 2234 2235**System capability**: SystemCapability.Communication.IPC.Core 2236 2237**Return value** 2238 2239| Type | Description | 2240| -------- | -------------------- | 2241| number[] | Double-precision floating-point array read.| 2242 2243**Error codes** 2244 2245For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2246 2247| ID| Error Message| 2248| -------- | -------- | 2249| 1900010 | Failed to read data from the message sequence. | 2250 2251**Example** 2252 2253 ```ts 2254 import { hilog } from '@kit.PerformanceAnalysisKit'; 2255 import { BusinessError } from '@kit.BasicServicesKit'; 2256 2257 let data = rpc.MessageSequence.create(); 2258 try { 2259 data.writeDoubleArray([11.1, 12.2, 13.3]); 2260 } catch (error) { 2261 let e: BusinessError = error as BusinessError; 2262 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2263 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2264 } 2265 try { 2266 let array = data.readDoubleArray(); 2267 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2268 } catch (error) { 2269 let e: BusinessError = error as BusinessError; 2270 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2271 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2272 } 2273 ``` 2274 2275### writeBooleanArray 2276 2277writeBooleanArray(booleanArray: boolean[]): void 2278 2279Writes a Boolean array to this **MessageSequence** object. 2280 2281**System capability**: SystemCapability.Communication.IPC.Core 2282 2283**Parameters** 2284 2285| Name | Type | Mandatory| Description | 2286| ------------ | --------- | ---- | ------------------ | 2287| booleanArray | boolean[] | Yes | Boolean array to write.| 2288 2289**Error codes** 2290 2291For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2292 2293| ID| Error Message| 2294| -------- | -------- | 2295| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2296| 1900009 | Failed to write data to the message sequence. | 2297 2298**Example** 2299 2300 ```ts 2301 import { hilog } from '@kit.PerformanceAnalysisKit'; 2302 import { BusinessError } from '@kit.BasicServicesKit'; 2303 2304 let data = rpc.MessageSequence.create(); 2305 try { 2306 data.writeBooleanArray([false, true, false]); 2307 } catch (error) { 2308 let e: BusinessError = error as BusinessError; 2309 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2310 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2311 } 2312 ``` 2313 2314### readBooleanArray 2315 2316readBooleanArray(dataIn: boolean[]): void 2317 2318Reads a Boolean array from this **MessageSequence** object. 2319 2320**System capability**: SystemCapability.Communication.IPC.Core 2321 2322**Parameters** 2323 2324| Name| Type | Mandatory| Description | 2325| ------ | --------- | ---- | ------------------ | 2326| dataIn | boolean[] | Yes | Boolean array to read.| 2327 2328**Error codes** 2329 2330For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2331 2332| ID| Error Message| 2333| -------- | -------- | 2334| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2335| 1900010 | Failed to read data from the message sequence. | 2336 2337**Example** 2338 2339 ```ts 2340 import { hilog } from '@kit.PerformanceAnalysisKit'; 2341 import { BusinessError } from '@kit.BasicServicesKit'; 2342 2343 let data = rpc.MessageSequence.create(); 2344 try { 2345 data.writeBooleanArray([false, true, false]); 2346 } catch (error) { 2347 let e: BusinessError = error as BusinessError; 2348 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2349 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2350 } 2351 let array: Array<boolean> = new Array(3); 2352 try { 2353 data.readBooleanArray(array); 2354 } catch (error) { 2355 let e: BusinessError = error as BusinessError; 2356 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2357 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2358 } 2359 ``` 2360 2361### readBooleanArray 2362 2363readBooleanArray(): boolean[] 2364 2365Reads the Boolean array from this **MessageSequence** object. 2366 2367**System capability**: SystemCapability.Communication.IPC.Core 2368 2369**Return value** 2370 2371| Type | Description | 2372| --------- | -------------- | 2373| boolean[] | Boolean array read.| 2374 2375**Error codes** 2376 2377For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2378 2379| ID| Error Message| 2380| -------- | -------- | 2381| 1900010 | Failed to read data from the message sequence. | 2382 2383**Example** 2384 2385 ```ts 2386 import { hilog } from '@kit.PerformanceAnalysisKit'; 2387 import { BusinessError } from '@kit.BasicServicesKit'; 2388 2389 let data = rpc.MessageSequence.create(); 2390 try { 2391 data.writeBooleanArray([false, true, false]); 2392 } catch (error) { 2393 let e: BusinessError = error as BusinessError; 2394 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2395 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2396 } 2397 try { 2398 let array = data.readBooleanArray(); 2399 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2400 } catch (error) { 2401 let e: BusinessError = error as BusinessError; 2402 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2403 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2404 } 2405 ``` 2406 2407### writeCharArray 2408 2409writeCharArray(charArray: number[]): void 2410 2411Writes a character array to this **MessageSequence** object. 2412 2413**System capability**: SystemCapability.Communication.IPC.Core 2414 2415**Parameters** 2416 2417| Name | Type | Mandatory| Description | 2418| --------- | -------- | ---- | ---------------------- | 2419| charArray | number[] | Yes | Character array to write.| 2420 2421**Error codes** 2422 2423For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2424 2425| ID| Error Message| 2426| -------- | -------- | 2427| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2428| 1900009 | Failed to write data to the message sequence. | 2429 2430**Example** 2431 2432 ```ts 2433 import { hilog } from '@kit.PerformanceAnalysisKit'; 2434 import { BusinessError } from '@kit.BasicServicesKit'; 2435 2436 let data = rpc.MessageSequence.create(); 2437 try { 2438 data.writeCharArray([97, 98, 88]); 2439 } catch (error) { 2440 let e: BusinessError = error as BusinessError; 2441 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2442 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2443 } 2444 ``` 2445 2446### readCharArray 2447 2448readCharArray(dataIn: number[]): void 2449 2450Reads a character array from this **MessageSequence** object. 2451 2452**System capability**: SystemCapability.Communication.IPC.Core 2453 2454**Parameters** 2455 2456| Name| Type | Mandatory| Description | 2457| ------ | -------- | ---- | ---------------------- | 2458| dataIn | number[] | Yes | Character array to read.| 2459 2460**Error codes** 2461 2462For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2463 2464| ID| Error Message| 2465| -------- | -------- | 2466| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2467| 1900010 | Failed to read data from the message sequence. | 2468 2469**Example** 2470 2471 ```ts 2472 import { hilog } from '@kit.PerformanceAnalysisKit'; 2473 import { BusinessError } from '@kit.BasicServicesKit'; 2474 2475 let data = rpc.MessageSequence.create(); 2476 try { 2477 data.writeCharArray([97, 98, 88]); 2478 } catch (error) { 2479 let e: BusinessError = error as BusinessError; 2480 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2481 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2482 } 2483 let array: Array<number> = new Array(3); 2484 try { 2485 data.readCharArray(array); 2486 } catch (error) { 2487 let e: BusinessError = error as BusinessError; 2488 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2489 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2490 } 2491 ``` 2492 2493### readCharArray 2494 2495readCharArray(): number[] 2496 2497Reads the character array from this **MessageSequence** object. 2498 2499**System capability**: SystemCapability.Communication.IPC.Core 2500 2501**Return value** 2502 2503| Type | Description | 2504| -------- | ------------------ | 2505| number[] | Character array read.| 2506 2507**Error codes** 2508 2509For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2510 2511| ID| Error Message| 2512| -------- | -------- | 2513| 1900010 | Failed to read data from the message sequence. | 2514 2515**Example** 2516 2517 ```ts 2518 import { hilog } from '@kit.PerformanceAnalysisKit'; 2519 import { BusinessError } from '@kit.BasicServicesKit'; 2520 2521 let data = rpc.MessageSequence.create(); 2522 try { 2523 data.writeCharArray([97, 98, 88]); 2524 } catch (error) { 2525 let e: BusinessError = error as BusinessError; 2526 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2527 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2528 } 2529 try { 2530 let array = data.readCharArray(); 2531 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2532 } catch (error) { 2533 let e: BusinessError = error as BusinessError; 2534 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2535 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2536 } 2537 ``` 2538 2539### writeStringArray 2540 2541writeStringArray(stringArray: string[]): void 2542 2543Writes a string array to this **MessageSequence** object. 2544 2545**System capability**: SystemCapability.Communication.IPC.Core 2546 2547**Parameters** 2548 2549| Name | Type | Mandatory| Description | 2550| ----------- | -------- | ---- | ------------------------------------------------------- | 2551| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2552 2553**Error codes** 2554 2555For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2556 2557| ID| Error Message| 2558| -------- | -------- | 2559| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The string length exceeds 40960 bytes; <br> 5.The number of bytes copied to the buffer is different from the length of the obtained string. | 2560| 1900009 | Failed to write data to the message sequence. | 2561 2562**Example** 2563 2564 ```ts 2565 import { hilog } from '@kit.PerformanceAnalysisKit'; 2566 import { BusinessError } from '@kit.BasicServicesKit'; 2567 2568 let data = rpc.MessageSequence.create(); 2569 try { 2570 data.writeStringArray(["abc", "def"]); 2571 } catch (error) { 2572 let e: BusinessError = error as BusinessError; 2573 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2574 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2575 } 2576 ``` 2577 2578### readStringArray 2579 2580readStringArray(dataIn: string[]): void 2581 2582Reads a string array from this **MessageSequence** object. 2583 2584**System capability**: SystemCapability.Communication.IPC.Core 2585 2586**Parameters** 2587 2588| Name| Type | Mandatory| Description | 2589| ------ | -------- | ---- | -------------------- | 2590| dataIn | string[] | Yes | String array to read.| 2591 2592**Error codes** 2593 2594For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2595 2596| ID| Error Message| 2597| -------- | -------- | 2598| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match. | 2599| 1900010 | Failed to read data from the message sequence. | 2600 2601**Example** 2602 2603 ```ts 2604 import { hilog } from '@kit.PerformanceAnalysisKit'; 2605 import { BusinessError } from '@kit.BasicServicesKit'; 2606 2607 let data = rpc.MessageSequence.create(); 2608 try { 2609 data.writeStringArray(["abc", "def"]); 2610 } catch (error) { 2611 let e: BusinessError = error as BusinessError; 2612 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2613 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2614 } 2615 let array: Array<string> = new Array(2); 2616 try { 2617 data.readStringArray(array); 2618 } catch (error) { 2619 let e: BusinessError = error as BusinessError; 2620 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2621 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2622 } 2623 ``` 2624 2625### readStringArray 2626 2627readStringArray(): string[] 2628 2629Reads the string array from this **MessageSequence** object. 2630 2631**System capability**: SystemCapability.Communication.IPC.Core 2632 2633**Return value** 2634 2635| Type | Description | 2636| -------- | ---------------- | 2637| string[] | String array read.| 2638 2639**Error codes** 2640 2641For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2642 2643| ID| Error Message| 2644| -------- | -------- | 2645| 1900010 | Failed to read data from the message sequence. | 2646 2647**Example** 2648 2649 ```ts 2650 import { hilog } from '@kit.PerformanceAnalysisKit'; 2651 import { BusinessError } from '@kit.BasicServicesKit'; 2652 2653 let data = rpc.MessageSequence.create(); 2654 try { 2655 data.writeStringArray(["abc", "def"]); 2656 } catch (error) { 2657 let e: BusinessError = error as BusinessError; 2658 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2659 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2660 } 2661 try { 2662 let array = data.readStringArray(); 2663 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2664 } catch (error) { 2665 let e: BusinessError = error as BusinessError; 2666 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2667 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2668 } 2669 ``` 2670 2671### writeNoException 2672 2673writeNoException(): void 2674 2675Writes information to this **MessageSequence** object indicating that no exception occurred. 2676 2677**System capability**: SystemCapability.Communication.IPC.Core 2678 2679**Error codes** 2680 2681For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2682 2683| ID| Error Message| 2684| -------- | -------- | 2685| 1900009 | Failed to write data to the message sequence. | 2686 2687**Example** 2688 2689 ```ts 2690 import { hilog } from '@kit.PerformanceAnalysisKit'; 2691 import { BusinessError } from '@kit.BasicServicesKit'; 2692 2693 class TestRemoteObject extends rpc.RemoteObject { 2694 constructor(descriptor: string) { 2695 super(descriptor); 2696 } 2697 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2698 if (code === 1) { 2699 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2700 try { 2701 reply.writeNoException(); 2702 } catch (error) { 2703 let e: BusinessError = error as BusinessError; 2704 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2705 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2706 } 2707 return true; 2708 } else { 2709 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2710 return false; 2711 } 2712 } 2713 } 2714 ``` 2715 2716### readException 2717 2718readException(): void 2719 2720Reads the exception information from this **MessageSequence** object. 2721 2722**System capability**: SystemCapability.Communication.IPC.Core 2723 2724**Error codes** 2725 2726For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2727 2728| ID| Error Message| 2729| -------- | -------- | 2730| 1900010 | Failed to read data from the message sequence. | 2731 2732**Example** 2733 2734 ```ts 2735 // If the FA model is used, import featureAbility from @kit.AbilityKit. 2736 // import { featureAbility } from '@kit.AbilityKit'; 2737 import { Want, common } from '@kit.AbilityKit'; 2738 import { hilog } from '@kit.PerformanceAnalysisKit'; 2739 2740 let proxy: rpc.IRemoteObject | undefined; 2741 let connect: common.ConnectOptions = { 2742 onConnect: (elementName, remoteProxy) => { 2743 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2744 proxy = remoteProxy; 2745 }, 2746 onDisconnect: (elementName) => { 2747 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2748 }, 2749 onFailed: () => { 2750 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2751 } 2752 }; 2753 let want: Want = { 2754 bundleName: "com.ohos.server", 2755 abilityName: "com.ohos.server.EntryAbility", 2756 }; 2757 2758 // Use this method to connect to the ability for the FA model. 2759 // FA.connectAbility(want,connect); 2760 2761 // Save the connection ID, which will be used for the subsequent service disconnection. 2762 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 2763 // Save the connection ID, which will be used for the subsequent service disconnection. 2764 let connectionId = context.connectServiceExtensionAbility(want, connect); 2765 ``` 2766 2767The 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. 2768 2769 ```ts 2770 import { BusinessError } from '@kit.BasicServicesKit'; 2771 import { hilog } from '@kit.PerformanceAnalysisKit'; 2772 2773 let option = new rpc.MessageOption(); 2774 let data = rpc.MessageSequence.create(); 2775 let reply = rpc.MessageSequence.create(); 2776 data.writeNoException(); 2777 data.writeInt(6); 2778 if (proxy != undefined) { 2779 proxy.sendMessageRequest(1, data, reply, option) 2780 .then((result: rpc.RequestResult) => { 2781 if (result.errCode === 0) { 2782 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2783 try { 2784 result.reply.readException(); 2785 } catch (error) { 2786 let e: BusinessError = error as BusinessError; 2787 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2788 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2789 } 2790 let num = result.reply.readInt(); 2791 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2792 } else { 2793 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2794 } 2795 }).catch((e: Error) => { 2796 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2797 }).finally (() => { 2798 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2799 data.reclaim(); 2800 reply.reclaim(); 2801 }); 2802 } 2803 ``` 2804 2805### writeParcelableArray 2806 2807writeParcelableArray(parcelableArray: Parcelable[]): void 2808 2809Writes a **Parcelable** array to this **MessageSequence** object. 2810 2811**System capability**: SystemCapability.Communication.IPC.Core 2812 2813**Parameters** 2814 2815| Name | Type | Mandatory| Description | 2816| --------------- | ------------ | ---- | -------------------------- | 2817| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to write.| 2818 2819**Error codes** 2820 2821For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2822 2823| ID| Error Message| 2824| -------- | -------- | 2825| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array. | 2826| 1900009 | Failed to write data to the message sequence. | 2827 2828**Example** 2829 2830 ```ts 2831 import { hilog } from '@kit.PerformanceAnalysisKit'; 2832 import { BusinessError } from '@kit.BasicServicesKit'; 2833 2834 class MyParcelable implements rpc.Parcelable { 2835 num: number = 0; 2836 str: string = ''; 2837 constructor(num: number, str: string) { 2838 this.num = num; 2839 this.str = str; 2840 } 2841 marshalling(messageSequence: rpc.MessageSequence): boolean { 2842 messageSequence.writeInt(this.num); 2843 messageSequence.writeString(this.str); 2844 return true; 2845 } 2846 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2847 this.num = messageSequence.readInt(); 2848 this.str = messageSequence.readString(); 2849 return true; 2850 } 2851 } 2852 let parcelable = new MyParcelable(1, "aaa"); 2853 let parcelable2 = new MyParcelable(2, "bbb"); 2854 let parcelable3 = new MyParcelable(3, "ccc"); 2855 let a = [parcelable, parcelable2, parcelable3]; 2856 let data = rpc.MessageSequence.create(); 2857 try { 2858 data.writeParcelableArray(a); 2859 } catch (error) { 2860 let e: BusinessError = error as BusinessError; 2861 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2862 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2863 } 2864 ``` 2865 2866### readParcelableArray 2867 2868readParcelableArray(parcelableArray: Parcelable[]): void 2869 2870Reads a **Parcelable** array from this **MessageSequence** object. 2871 2872**System capability**: SystemCapability.Communication.IPC.Core 2873 2874**Parameters** 2875 2876| Name | Type | Mandatory| Description | 2877| --------------- | ------------ | ---- | -------------------------- | 2878| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to read.| 2879 2880**Error codes** 2881 2882For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2883 2884| ID| Error Message| 2885| -------- | -------- | 2886| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The length of the array passed when reading is not equal to the length passed when writing to the array; <br> 5.The element does not exist in the array. | 2887| 1900010 | Failed to read data from the message sequence. | 2888| 1900012 | Failed to call the JS callback function. | 2889 2890**Example** 2891 2892 ```ts 2893 import { hilog } from '@kit.PerformanceAnalysisKit'; 2894 import { BusinessError } from '@kit.BasicServicesKit'; 2895 2896 class MyParcelable implements rpc.Parcelable { 2897 num: number = 0; 2898 str: string = ''; 2899 constructor(num: number, str: string) { 2900 this.num = num; 2901 this.str = str; 2902 } 2903 marshalling(messageSequence: rpc.MessageSequence): boolean { 2904 messageSequence.writeInt(this.num); 2905 messageSequence.writeString(this.str); 2906 return true; 2907 } 2908 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2909 this.num = messageSequence.readInt(); 2910 this.str = messageSequence.readString(); 2911 return true; 2912 } 2913 } 2914 let parcelable = new MyParcelable(1, "aaa"); 2915 let parcelable2 = new MyParcelable(2, "bbb"); 2916 let parcelable3 = new MyParcelable(3, "ccc"); 2917 let a = [parcelable, parcelable2, parcelable3]; 2918 let data = rpc.MessageSequence.create(); 2919 data.writeParcelableArray(a); 2920 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2921 try { 2922 data.readParcelableArray(b); 2923 } catch (error) { 2924 let e: BusinessError = error as BusinessError; 2925 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2926 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2927 } 2928 ``` 2929 2930### writeRemoteObjectArray 2931 2932writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2933 2934Writes an array of **IRemoteObject** objects to this **MessageSequence** object. 2935 2936**System capability**: SystemCapability.Communication.IPC.Core 2937 2938**Parameters** 2939 2940| Name | Type | Mandatory| Description | 2941| ----------- | --------------- | ---- | ---------------------------------------------- | 2942| objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 2943 2944**Error codes** 2945 2946For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2947 2948| ID| Error Message| 2949| -------- | -------- | 2950| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The element does not exist in the array; <br> 5.The obtained remoteObject is null. | 2951| 1900009 | Failed to write data to the message sequence. | 2952 2953**Example** 2954 2955 ```ts 2956 import { hilog } from '@kit.PerformanceAnalysisKit'; 2957 import { BusinessError } from '@kit.BasicServicesKit'; 2958 2959 class TestRemoteObject extends rpc.RemoteObject { 2960 constructor(descriptor: string) { 2961 super(descriptor); 2962 this.modifyLocalInterface(this, descriptor); 2963 } 2964 2965 asObject(): rpc.IRemoteObject { 2966 return this; 2967 } 2968 } 2969 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2970 let data = rpc.MessageSequence.create(); 2971 try { 2972 data.writeRemoteObjectArray(a); 2973 } catch (error) { 2974 let e: BusinessError = error as BusinessError; 2975 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2976 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2977 } 2978 ``` 2979 2980### readRemoteObjectArray 2981 2982readRemoteObjectArray(objects: IRemoteObject[]): void 2983 2984Reads an array of **IRemoteObject** objects from this **MessageSequence** object. 2985 2986**System capability**: SystemCapability.Communication.IPC.Core 2987 2988**Parameters** 2989 2990| Name | Type | Mandatory| Description | 2991| ------- | --------------- | ---- | ---------------------------------------------- | 2992| objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 2993 2994**Error codes** 2995 2996For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2997 2998| ID| Error Message| 2999| -------- | -------- | 3000| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The length of the array passed when reading is not equal to the length passed when writing to the array. | 3001| 1900010 | Failed to read data from the message sequence. | 3002 3003**Example** 3004 3005 ```ts 3006 import { hilog } from '@kit.PerformanceAnalysisKit'; 3007 import { BusinessError } from '@kit.BasicServicesKit'; 3008 3009 class TestRemoteObject extends rpc.RemoteObject { 3010 constructor(descriptor: string) { 3011 super(descriptor); 3012 this.modifyLocalInterface(this, descriptor); 3013 } 3014 3015 asObject(): rpc.IRemoteObject { 3016 return this; 3017 } 3018 } 3019 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3020 let data = rpc.MessageSequence.create(); 3021 data.writeRemoteObjectArray(a); 3022 let b: Array<rpc.IRemoteObject> = new Array(3); 3023 try { 3024 data.readRemoteObjectArray(b); 3025 } catch (error) { 3026 let e: BusinessError = error as BusinessError; 3027 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3028 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3029 } 3030 ``` 3031 3032### readRemoteObjectArray 3033 3034readRemoteObjectArray(): IRemoteObject[] 3035 3036Reads the **IRemoteObject** object array from this **MessageSequence** object. 3037 3038**System capability**: SystemCapability.Communication.IPC.Core 3039 3040**Return value** 3041 3042| Type | Description | 3043| --------------- | --------------------------- | 3044| [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array read.| 3045 3046**Error codes** 3047 3048For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3049 3050| ID| Error Message| 3051| -------- | -------- | 3052| 1900010 | Failed to read data from the message sequence. | 3053 3054**Example** 3055 3056 ```ts 3057 import { hilog } from '@kit.PerformanceAnalysisKit'; 3058 import { BusinessError } from '@kit.BasicServicesKit'; 3059 3060 class TestRemoteObject extends rpc.RemoteObject { 3061 constructor(descriptor: string) { 3062 super(descriptor); 3063 this.modifyLocalInterface(this, descriptor); 3064 } 3065 3066 asObject(): rpc.IRemoteObject { 3067 return this; 3068 } 3069 } 3070 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3071 let data = rpc.MessageSequence.create(); 3072 data.writeRemoteObjectArray(a); 3073 try { 3074 let b = data.readRemoteObjectArray(); 3075 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3076 } catch (error) { 3077 let e: BusinessError = error as BusinessError; 3078 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3079 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3080 } 3081 ``` 3082 3083### closeFileDescriptor 3084 3085static closeFileDescriptor(fd: number): void 3086 3087Closes a file descriptor. This API is a static method. 3088 3089**System capability**: SystemCapability.Communication.IPC.Core 3090 3091**Parameters** 3092 3093| Name| Type | Mandatory| Description | 3094| ------ | ------ | ---- | -------------------- | 3095| fd | number | Yes | File descriptor to close.| 3096 3097**Error codes** 3098 3099For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3100 3101| ID| Error Message| 3102| -------- | -------- | 3103| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3104 3105**Example** 3106 3107 ```ts 3108 import { fileIo } from '@kit.CoreFileKit'; 3109 import { hilog } from '@kit.PerformanceAnalysisKit'; 3110 import { BusinessError } from '@kit.BasicServicesKit'; 3111 3112 let filePath = "path/to/file"; 3113 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3114 try { 3115 rpc.MessageSequence.closeFileDescriptor(file.fd); 3116 } catch (error) { 3117 let e: BusinessError = error as BusinessError; 3118 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3119 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3120 } 3121 ``` 3122 3123### dupFileDescriptor 3124 3125static dupFileDescriptor(fd: number): number 3126 3127Duplicates a file descriptor. This API is a static method. 3128 3129**System capability**: SystemCapability.Communication.IPC.Core 3130 3131**Parameters** 3132 3133| Name| Type | Mandatory| Description | 3134| ------ | ------ | ---- | ------------------------ | 3135| fd | number | Yes | File descriptor to duplicate.| 3136 3137**Return value** 3138 3139| Type | Description | 3140| ------ | -------------------- | 3141| number | New file descriptor.| 3142 3143**Error codes** 3144 3145For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3146 3147| ID| Error Message| 3148| -------- | -------- | 3149| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3150| 1900013 | Failed to call dup. | 3151 3152**Example** 3153 3154 ```ts 3155 import { fileIo } from '@kit.CoreFileKit'; 3156 import { hilog } from '@kit.PerformanceAnalysisKit'; 3157 import { BusinessError } from '@kit.BasicServicesKit'; 3158 3159 let filePath = "path/to/file"; 3160 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3161 try { 3162 rpc.MessageSequence.dupFileDescriptor(file.fd); 3163 } catch (error) { 3164 let e: BusinessError = error as BusinessError; 3165 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3166 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3167 } 3168 ``` 3169 3170### containFileDescriptors 3171 3172containFileDescriptors(): boolean 3173 3174Checks whether this **MessageSequence** object contains file descriptors. 3175 3176**System capability**: SystemCapability.Communication.IPC.Core 3177 3178**Return value** 3179 3180| Type | Description | 3181| ------- | -------------------------------------------------------------------- | 3182| boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 3183 3184**Example** 3185 3186 ```ts 3187 import { fileIo } from '@kit.CoreFileKit'; 3188 import { hilog } from '@kit.PerformanceAnalysisKit'; 3189 import { BusinessError } from '@kit.BasicServicesKit'; 3190 3191 let sequence = new rpc.MessageSequence(); 3192 let filePath = "path/to/file"; 3193 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3194 try { 3195 sequence.writeFileDescriptor(file.fd); 3196 } catch (error) { 3197 let e: BusinessError = error as BusinessError; 3198 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3199 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3200 } 3201 try { 3202 let containFD = sequence.containFileDescriptors(); 3203 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3204 } catch (error) { 3205 let e: BusinessError = error as BusinessError; 3206 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3207 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3208 } 3209 ``` 3210 3211### writeFileDescriptor 3212 3213writeFileDescriptor(fd: number): void 3214 3215Writes a file descriptor to this **MessageSequence** object. 3216 3217**System capability**: SystemCapability.Communication.IPC.Core 3218 3219**Parameters** 3220 3221| Name| Type | Mandatory| Description | 3222| ------ | ------ | ---- | ------------ | 3223| fd | number | Yes | File descriptor to write.| 3224 3225**Error codes** 3226 3227For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3228 3229| ID| Error Message| 3230| -------- | -------- | 3231| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3232| 1900009 | Failed to write data to the message sequence. | 3233 3234**Example** 3235 3236 ```ts 3237 import { fileIo } from '@kit.CoreFileKit'; 3238 import { hilog } from '@kit.PerformanceAnalysisKit'; 3239 import { BusinessError } from '@kit.BasicServicesKit'; 3240 3241 let sequence = new rpc.MessageSequence(); 3242 let filePath = "path/to/file"; 3243 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3244 try { 3245 sequence.writeFileDescriptor(file.fd); 3246 } catch (error) { 3247 let e: BusinessError = error as BusinessError; 3248 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3249 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3250 } 3251 ``` 3252 3253### readFileDescriptor 3254 3255readFileDescriptor(): number 3256 3257Reads the file descriptor from this **MessageSequence** object. 3258 3259**System capability**: SystemCapability.Communication.IPC.Core 3260 3261**Return value** 3262 3263| Type | Description | 3264| ------ | ---------------- | 3265| number | File descriptor read.| 3266 3267**Error codes** 3268 3269For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3270 3271| ID| Error Message| 3272| -------- | -------- | 3273| 1900010 | Failed to read data from the message sequence. | 3274 3275**Example** 3276 3277 ```ts 3278 import { fileIo } from '@kit.CoreFileKit'; 3279 import { hilog } from '@kit.PerformanceAnalysisKit'; 3280 import { BusinessError } from '@kit.BasicServicesKit'; 3281 3282 let sequence = new rpc.MessageSequence(); 3283 let filePath = "path/to/file"; 3284 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3285 try { 3286 sequence.writeFileDescriptor(file.fd); 3287 } catch (error) { 3288 let e: BusinessError = error as BusinessError; 3289 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3290 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3291 } 3292 try { 3293 let readFD = sequence.readFileDescriptor(); 3294 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3295 } catch (error) { 3296 let e: BusinessError = error as BusinessError; 3297 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3298 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3299 } 3300 ``` 3301 3302### writeAshmem 3303 3304writeAshmem(ashmem: Ashmem): void 3305 3306Writes an anonymous shared object to this **MessageSequence** object. 3307 3308**System capability**: SystemCapability.Communication.IPC.Core 3309 3310**Parameters** 3311 3312| Name| Type | Mandatory| Description | 3313| ------ | ------ | ---- | ------------------------------------- | 3314| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 3315 3316**Error codes** 3317 3318For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3319 3320| ID| Error Message| 3321| -------- | ------- | 3322| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter is not an instance of the Ashmem object. | 3323| 1900003 | Failed to write data to the shared memory. | 3324 3325**Example** 3326 3327 ```ts 3328 import { hilog } from '@kit.PerformanceAnalysisKit'; 3329 import { BusinessError } from '@kit.BasicServicesKit'; 3330 3331 let sequence = new rpc.MessageSequence(); 3332 let ashmem: rpc.Ashmem | undefined = undefined; 3333 try { 3334 ashmem = rpc.Ashmem.create("ashmem", 1024); 3335 try { 3336 sequence.writeAshmem(ashmem); 3337 } catch (error) { 3338 let e: BusinessError = error as BusinessError; 3339 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3340 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3341 } 3342 } catch (error) { 3343 let e: BusinessError = error as BusinessError; 3344 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3345 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3346 } 3347 ``` 3348 3349### readAshmem 3350 3351readAshmem(): Ashmem 3352 3353Reads the anonymous shared object from this **MessageSequence** object. 3354 3355**System capability**: SystemCapability.Communication.IPC.Core 3356 3357**Return value** 3358 3359| Type | Description | 3360| ------ | ------------------ | 3361| [Ashmem](#ashmem8) | Anonymous share object obtained.| 3362 3363**Error codes** 3364 3365For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3366 3367| ID| Error Message| 3368| -------- | -------- | 3369| 401 | check param failed | 3370| 1900004 | Failed to read data from the shared memory. | 3371 3372**Example** 3373 3374 ```ts 3375 import { hilog } from '@kit.PerformanceAnalysisKit'; 3376 import { BusinessError } from '@kit.BasicServicesKit'; 3377 3378 let sequence = new rpc.MessageSequence(); 3379 let ashmem: rpc.Ashmem | undefined = undefined; 3380 try { 3381 ashmem = rpc.Ashmem.create("ashmem", 1024); 3382 try { 3383 sequence.writeAshmem(ashmem); 3384 } catch (error) { 3385 let e: BusinessError = error as BusinessError; 3386 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3387 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3388 } 3389 } catch (error) { 3390 let e: BusinessError = error as BusinessError; 3391 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3392 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3393 } 3394 try { 3395 sequence.readAshmem(); 3396 } catch (error) { 3397 let e: BusinessError = error as BusinessError; 3398 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3399 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3400 } 3401 ``` 3402 3403### getRawDataCapacity 3404 3405getRawDataCapacity(): number 3406 3407Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3408 3409**System capability**: SystemCapability.Communication.IPC.Core 3410 3411**Return value** 3412 3413| Type | Description | 3414| ------ | ------------------------------------------------------------ | 3415| number | Maximum amount of raw data that **MessageSequence** can hold, that is, 128 MB.| 3416 3417**Example** 3418 3419 ```ts 3420 import { hilog } from '@kit.PerformanceAnalysisKit'; 3421 3422 let sequence = new rpc.MessageSequence(); 3423 let result = sequence.getRawDataCapacity(); 3424 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3425 ``` 3426 3427### writeRawData<sup>(deprecated)</sup> 3428 3429>**NOTE**<br>This API is deprecated since API version 11. Use [writeRawDataBuffer](#writerawdatabuffer11) instead. 3430 3431writeRawData(rawData: number[], size: number): void 3432 3433Writes raw data to this **MessageSequence** object. 3434 3435**System capability**: SystemCapability.Communication.IPC.Core 3436 3437**Parameters** 3438 3439| Name | Type | Mandatory| Description | 3440| ------- | -------- | ---- | ---------------------------------- | 3441| rawData | number[] | Yes | Raw data to write. | 3442| size | number | Yes | Size of the raw data, in bytes.| 3443 3444**Error codes** 3445 3446For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3447 3448| ID| Error Message| 3449| -------- | -------- | 3450| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The transferred size cannot be obtained; <br> 5.The transferred size is less than or equal to 0;<br> 6.The element does not exist in the array; <br> 7.Failed to obtain typedArray information; <br> 8.The array is not of type int32; <br> 9.The length of typedarray is smaller than the size of the original data sent. | 3451| 1900009 | Failed to write data to the message sequence. | 3452 3453**Example** 3454 3455 ```ts 3456 import { hilog } from '@kit.PerformanceAnalysisKit'; 3457 import { BusinessError } from '@kit.BasicServicesKit'; 3458 3459 let sequence = new rpc.MessageSequence(); 3460 let arr = [1, 2, 3, 4, 5]; 3461 try { 3462 sequence.writeRawData(arr, arr.length); 3463 } catch (error) { 3464 let e: BusinessError = error as BusinessError; 3465 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3466 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3467 } 3468 ``` 3469 3470### writeRawDataBuffer<sup>11+</sup> 3471 3472writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3473 3474Writes raw data to this **MessageSequence** object. 3475 3476**System capability**: SystemCapability.Communication.IPC.Core 3477 3478**Parameters** 3479 3480| Name | Type | Mandatory| Description | 3481| ------- | -------- | ---- | ---------------------------------- | 3482| rawData | ArrayBuffer | Yes | Raw data to write. | 3483| size | number | Yes | Size of the raw data, in bytes.| 3484 3485**Error codes** 3486 3487For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3488 3489| ID| Error Message| 3490| -------- | -------- | 3491| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain arrayBuffer information; <br> 4.The transferred size cannot be obtained; <br> 5.The transferred size is less than or equal to 0; <br> 6.The transferred size is greater than the byte length of ArrayBuffer. | 3492| 1900009 | Failed to write data to the message sequence. | 3493 3494**Example** 3495 3496 ```ts 3497 import { hilog } from '@kit.PerformanceAnalysisKit'; 3498 import { BusinessError } from '@kit.BasicServicesKit'; 3499 3500 let buffer = new ArrayBuffer(64 * 1024); 3501 let int32View = new Int32Array(buffer); 3502 for (let i = 0; i < int32View.length; i++) { 3503 int32View[i] = i * 2 + 1; 3504 } 3505 let size = buffer.byteLength; 3506 let sequence = new rpc.MessageSequence(); 3507 try { 3508 sequence.writeRawDataBuffer(buffer, size); 3509 } catch (error) { 3510 let e: BusinessError = error as BusinessError; 3511 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3512 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3513 } 3514 ``` 3515 3516### readRawData<sup>(deprecated)</sup> 3517 3518>**NOTE**<br>This API is deprecated since API version 11. Use [readRawDataBuffer](#readrawdatabuffer11) instead. 3519 3520readRawData(size: number): number[] 3521 3522Reads raw data from this **MessageSequence** object. 3523 3524**System capability**: SystemCapability.Communication.IPC.Core 3525 3526**Parameters** 3527 3528| Name| Type | Mandatory| Description | 3529| ------ | ------ | ---- | ------------------------ | 3530| size | number | Yes | Size of the raw data to read.| 3531 3532**Return value** 3533 3534| Type | Description | 3535| -------- | ------------------------------ | 3536| number[] | Raw data obtained, in bytes.| 3537 3538**Error codes** 3539 3540For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3541 3542| ID| Error Message| 3543| -------- | -------- | 3544| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3545| 1900010 | Failed to read data from the message sequence. | 3546 3547**Example** 3548 3549 ```ts 3550 import { hilog } from '@kit.PerformanceAnalysisKit'; 3551 import { BusinessError } from '@kit.BasicServicesKit'; 3552 3553 let sequence = new rpc.MessageSequence(); 3554 let arr = [1, 2, 3, 4, 5]; 3555 try { 3556 sequence.writeRawData(arr, arr.length); 3557 } catch (error) { 3558 let e: BusinessError = error as BusinessError; 3559 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3560 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3561 } 3562 try { 3563 let result = sequence.readRawData(5); 3564 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3565 } catch (error) { 3566 let e: BusinessError = error as BusinessError; 3567 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3568 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3569 } 3570 ``` 3571 3572### readRawDataBuffer<sup>11+</sup> 3573 3574readRawDataBuffer(size: number): ArrayBuffer 3575 3576Reads raw data from this **MessageSequence** object. 3577 3578**System capability**: SystemCapability.Communication.IPC.Core 3579 3580**Parameters** 3581 3582| Name| Type | Mandatory| Description | 3583| ------ | ------ | ---- | ------------------------ | 3584| size | number | Yes | Size of the raw data to read.| 3585 3586**Return value** 3587 3588| Type | Description | 3589| -------- | ------------------------------ | 3590| ArrayBuffer | Raw data obtained, in bytes.| 3591 3592**Error codes** 3593 3594For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3595 3596| ID| Error Message| 3597| -------- | -------- | 3598| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3599| 1900010 | Failed to read data from the message sequence. | 3600 3601**Example** 3602 3603 ```ts 3604 import { hilog } from '@kit.PerformanceAnalysisKit'; 3605 import { BusinessError } from '@kit.BasicServicesKit'; 3606 3607 let buffer = new ArrayBuffer(64 * 1024); 3608 let int32View = new Int32Array(buffer); 3609 for (let i = 0; i < int32View.length; i++) { 3610 int32View[i] = i * 2 + 1; 3611 } 3612 let size = buffer.byteLength; 3613 let sequence = new rpc.MessageSequence(); 3614 try { 3615 sequence.writeRawDataBuffer(buffer, size); 3616 } catch (error) { 3617 let e: BusinessError = error as BusinessError; 3618 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3619 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3620 } 3621 try { 3622 let result = sequence.readRawDataBuffer(size); 3623 let readInt32View = new Int32Array(result); 3624 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3625 } catch (error) { 3626 let e: BusinessError = error as BusinessError; 3627 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3628 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3629 } 3630 ``` 3631 3632### writeArrayBuffer<sup>12+</sup> 3633 3634writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3635 3636Writes data of the ArrayBuffer type to this **MessageSequence** object. 3637 3638**System capability**: SystemCapability.Communication.IPC.Core 3639 3640**Parameters** 3641 3642| Name | Type | Mandatory| Description | 3643| --------- | ------------------------- | ---- | --------------------------- | 3644| buf | ArrayBuffer | Yes | Data to write. | 3645| typeCode | [TypeCode](#typecode12) | Yes | TypedArray type of the ArrayBuffer data.<br>The underlying write mode is determined based on the enum value of **TypeCode** passed by the service.| 3646 3647**Error codes** 3648 3649For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3650 3651| ID| Error Message| 3652| -------- | -------- | 3653| 401 | Parameter error. Possible causes: <br> 1.The parameter is an empty array; <br> 2.The number of parameters is incorrect; <br> 3.The parameter type does not match; <br> 4.The obtained value of typeCode is incorrect; <br> 5.Failed to obtain arrayBuffer information. | 3654| 1900009 | Failed to write data to the message sequence. | 3655 3656**Example** 3657 3658 ```ts 3659 // In this example, the value of TypeCode is Int16Array. 3660 import { hilog } from '@kit.PerformanceAnalysisKit'; 3661 import { BusinessError } from '@kit.BasicServicesKit'; 3662 3663 const data = rpc.MessageSequence.create(); 3664 3665 let buffer = new ArrayBuffer(10); 3666 let int16View = new Int16Array(buffer); 3667 for (let i = 0; i < int16View.length; i++) { 3668 int16View[i] = i * 2 + 1; 3669 } 3670 3671 try { 3672 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3673 } catch (error) { 3674 let e: BusinessError = error as BusinessError; 3675 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3676 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3677 } 3678 ``` 3679 3680### readArrayBuffer<sup>12+</sup> 3681 3682readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3683 3684Reads data of the ArrayBuffer type from this **MessageSequence**. 3685 3686**System capability**: SystemCapability.Communication.IPC.Core 3687 3688**Parameters** 3689 3690| Name | Type | Mandatory| Description | 3691| -------- | ----------------------- | ---- | ------------------------| 3692| typeCode | [TypeCode](#typecode12) | Yes | TypedArray type of the ArrayBuffer data.<br>The underlying read mode is determined based on the enum value of **TypeCode** passed by the service. | 3693 3694**Return value** 3695 3696| Type | Description | 3697| -------- | -------------------------------------------- | 3698| ArrayBuffer | Data of the ArrayBuffer type read, in bytes.| 3699 3700**Error codes** 3701 3702For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3703 3704| ID| Error Message| 3705| -------- | -------- | 3706| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The obtained value of typeCode is incorrect; | 3707| 1900010 | Failed to read data from the message sequence. | 3708 3709**Example** 3710 3711 ```ts 3712 // In this example, the value of TypeCode is Int16Array. 3713 import { hilog } from '@kit.PerformanceAnalysisKit'; 3714 import { BusinessError } from '@kit.BasicServicesKit'; 3715 3716 const data = rpc.MessageSequence.create(); 3717 3718 let buffer = new ArrayBuffer(10); 3719 let int16View = new Int16Array(buffer); 3720 for (let i = 0; i < int16View.length; i++) { 3721 int16View[i] = i * 2 + 1; 3722 } 3723 3724 try { 3725 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3726 } catch (error) { 3727 let e: BusinessError = error as BusinessError; 3728 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3729 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3730 } 3731 try { 3732 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3733 let readInt16View = new Int16Array(result); 3734 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3735 } catch (error) { 3736 let e: BusinessError = error as BusinessError; 3737 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3738 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3739 } 3740 ``` 3741 3742## MessageParcel<sup>(deprecated)</sup> 3743 3744>**NOTE**<br>This API is deprecated since API version 9. Use [MessageSequence](#messagesequence9) instead. 3745 3746Provides APIs for reading and writing data in specific format. During RPC, the sender can use the **write()** method provided by **MessageParcel** to write data in specific format to a **MessageParcel** object. The receiver can use the **read()** method provided by **MessageParcel** to read data in specific format from a **MessageParcel** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 3747 3748### create 3749 3750static create(): MessageParcel 3751 3752Creates a **MessageParcel** object. This method is a static method. 3753 3754**System capability**: SystemCapability.Communication.IPC.Core 3755 3756**Return value** 3757 3758| Type | Description | 3759| ------------- | ----------------------------- | 3760| [MessageParcel](#messageparceldeprecated) | **MessageParcel** object created.| 3761 3762**Example** 3763 3764 ```ts 3765 import { hilog } from '@kit.PerformanceAnalysisKit'; 3766 3767 let data = rpc.MessageParcel.create(); 3768 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3769 ``` 3770 3771### reclaim 3772 3773reclaim(): void 3774 3775Reclaims the **MessageParcel** object that is no longer used. 3776 3777**System capability**: SystemCapability.Communication.IPC.Core 3778 3779**Example** 3780 3781 ```ts 3782 let reply = rpc.MessageParcel.create(); 3783 reply.reclaim(); 3784 ``` 3785 3786### writeRemoteObject 3787 3788writeRemoteObject(object: IRemoteObject): boolean 3789 3790Serializes a remote object and writes it to this **MessageParcel** object. 3791 3792**System capability**: SystemCapability.Communication.IPC.Core 3793 3794**Parameters** 3795 3796| Name| Type | Mandatory| Description | 3797| ------ | ------------------------------- | ---- | --------------------------------------- | 3798| object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3799 3800**Return value** 3801 3802| Type | Description | 3803| ------- | ----------------------------------------- | 3804| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3805 3806**Example** 3807 3808 ```ts 3809 import { hilog } from '@kit.PerformanceAnalysisKit'; 3810 3811 class MyDeathRecipient implements rpc.DeathRecipient { 3812 onRemoteDied() { 3813 hilog.info(0x0000, 'testTag', 'server died'); 3814 } 3815 } 3816 class TestRemoteObject extends rpc.RemoteObject { 3817 constructor(descriptor: string) { 3818 super(descriptor); 3819 } 3820 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3821 return true; 3822 } 3823 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3824 return true; 3825 } 3826 isObjectDead(): boolean { 3827 return false; 3828 } 3829 } 3830 let data = rpc.MessageParcel.create(); 3831 let testRemoteObject = new TestRemoteObject("testObject"); 3832 data.writeRemoteObject(testRemoteObject); 3833 ``` 3834 3835### readRemoteObject 3836 3837readRemoteObject(): IRemoteObject 3838 3839Reads 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. 3840 3841**System capability**: SystemCapability.Communication.IPC.Core 3842 3843**Return value** 3844 3845| Type | Description | 3846| ------------------------------- | ------------------ | 3847| [IRemoteObject](#iremoteobject) | Remote object obtained.| 3848 3849**Example** 3850 3851 ```ts 3852 import { hilog } from '@kit.PerformanceAnalysisKit'; 3853 3854 class MyDeathRecipient implements rpc.DeathRecipient { 3855 onRemoteDied() { 3856 hilog.info(0x0000, 'testTag', 'server died'); 3857 } 3858 } 3859 class TestRemoteObject extends rpc.RemoteObject { 3860 constructor(descriptor: string) { 3861 super(descriptor); 3862 } 3863 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3864 return true; 3865 } 3866 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3867 return true; 3868 } 3869 isObjectDead(): boolean { 3870 return false; 3871 } 3872 } 3873 let data = rpc.MessageParcel.create(); 3874 let testRemoteObject = new TestRemoteObject("testObject"); 3875 data.writeRemoteObject(testRemoteObject); 3876 let proxy = data.readRemoteObject(); 3877 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3878 ``` 3879 3880### writeInterfaceToken 3881 3882writeInterfaceToken(token: string): boolean 3883 3884Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3885 3886**System capability**: SystemCapability.Communication.IPC.Core 3887 3888**Parameters** 3889 3890| Name| Type | Mandatory| Description | 3891| ------ | ------ | ---- | ------------------ | 3892| token | string | Yes | Interface token to write.| 3893 3894**Return value** 3895 3896| Type | Description | 3897| ------- | ----------------------------------------- | 3898| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3899 3900**Example** 3901 3902 ```ts 3903 import { hilog } from '@kit.PerformanceAnalysisKit'; 3904 3905 let data = rpc.MessageParcel.create(); 3906 let result = data.writeInterfaceToken("aaa"); 3907 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3908 ``` 3909 3910### readInterfaceToken 3911 3912readInterfaceToken(): string 3913 3914Reads 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. 3915 3916**System capability**: SystemCapability.Communication.IPC.Core 3917 3918**Return value** 3919 3920| Type | Description | 3921| ------ | ------------------------ | 3922| string | Interface token obtained.| 3923 3924**Example** 3925 3926 ```ts 3927 import { hilog } from '@kit.PerformanceAnalysisKit'; 3928 3929 class Stub extends rpc.RemoteObject { 3930 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3931 let interfaceToken = data.readInterfaceToken(); 3932 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3933 return true; 3934 } 3935 } 3936 ``` 3937 3938### getSize 3939 3940getSize(): number 3941 3942Obtains the data size of this **MessageParcel** object. 3943 3944**System capability**: SystemCapability.Communication.IPC.Core 3945 3946**Return value** 3947 3948| Type | Description | 3949| ------ | --------------------------------------------- | 3950| number | Size of the **MessageParcel** object obtained, in bytes.| 3951 3952**Example** 3953 3954 ```ts 3955 import { hilog } from '@kit.PerformanceAnalysisKit'; 3956 3957 let data = rpc.MessageParcel.create(); 3958 let size = data.getSize(); 3959 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3960 ``` 3961 3962### getCapacity 3963 3964getCapacity(): number 3965 3966Obtains the capacity of this **MessageParcel** object. 3967 3968**System capability**: SystemCapability.Communication.IPC.Core 3969 3970**Return value** 3971 3972| Type | Description | 3973| ------ | --------------------------------------------- | 3974| number | **MessageParcel** capacity obtained, in bytes.| 3975 3976**Example** 3977 3978 ```ts 3979 import { hilog } from '@kit.PerformanceAnalysisKit'; 3980 3981 let data = rpc.MessageParcel.create(); 3982 let result = data.getCapacity(); 3983 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 3984 ``` 3985 3986### setSize 3987 3988setSize(size: number): boolean 3989 3990Sets the size of data contained in this **MessageParcel** object. 3991 3992**System capability**: SystemCapability.Communication.IPC.Core 3993 3994**Parameters** 3995 3996| Name| Type | Mandatory| Description | 3997| ------ | ------ | ---- | ------------------------------------------- | 3998| size | number | Yes | Data size to set, in bytes.| 3999 4000**Return value** 4001 4002| Type | Description | 4003| ------- | --------------------------------- | 4004| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4005 4006**Example** 4007 4008 ```ts 4009 import { hilog } from '@kit.PerformanceAnalysisKit'; 4010 4011 let data = rpc.MessageParcel.create(); 4012 let setSize = data.setSize(16); 4013 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4014 ``` 4015 4016### setCapacity 4017 4018setCapacity(size: number): boolean 4019 4020Sets the storage capacity of this **MessageParcel** object. 4021 4022**System capability**: SystemCapability.Communication.IPC.Core 4023 4024**Parameters** 4025 4026| Name| Type | Mandatory| Description | 4027| ------ | ------ | ---- | ------------------------------------------- | 4028| size | number | Yes | Storage capacity to set, in bytes.| 4029 4030**Return value** 4031 4032| Type | Description | 4033| ------- | --------------------------------- | 4034| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4035 4036**Example** 4037 4038 ```ts 4039 import { hilog } from '@kit.PerformanceAnalysisKit'; 4040 4041 let data = rpc.MessageParcel.create(); 4042 let result = data.setCapacity(100); 4043 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4044 ``` 4045 4046### getWritableBytes 4047 4048getWritableBytes(): number 4049 4050Obtains the writable capacity of this **MessageParcel** object. 4051 4052**System capability**: SystemCapability.Communication.IPC.Core 4053 4054**Return value** 4055 4056| Type | Description | 4057| ------ | --------------------------------------------------- | 4058| number | **MessageParcel** writable capacity obtained, in bytes.| 4059 4060**Example** 4061 4062 ```ts 4063 import { hilog } from '@kit.PerformanceAnalysisKit'; 4064 4065 class Stub extends rpc.RemoteObject { 4066 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4067 let getWritableBytes = data.getWritableBytes(); 4068 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4069 return true; 4070 } 4071 } 4072 ``` 4073 4074### getReadableBytes 4075 4076getReadableBytes(): number 4077 4078Obtains the readable capacity of this **MessageParcel** object. 4079 4080**System capability**: SystemCapability.Communication.IPC.Core 4081 4082**Return value** 4083 4084| Type | Description | 4085| ------ | --------------------------------------------------- | 4086| number | **MessageParcel** object readable capacity, in bytes.| 4087 4088**Example** 4089 4090 ```ts 4091 import { hilog } from '@kit.PerformanceAnalysisKit'; 4092 4093 class Stub extends rpc.RemoteObject { 4094 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4095 let result = data.getReadableBytes(); 4096 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4097 return true; 4098 } 4099 } 4100 ``` 4101 4102### getReadPosition 4103 4104getReadPosition(): number 4105 4106Obtains the read position of this **MessageParcel** object. 4107 4108**System capability**: SystemCapability.Communication.IPC.Core 4109 4110**Return value** 4111 4112| Type | Description | 4113| ------ | --------------------------------------- | 4114| number | Current read position of the **MessageParcel** object.| 4115 4116**Example** 4117 4118 ```ts 4119 import { hilog } from '@kit.PerformanceAnalysisKit'; 4120 4121 let data = rpc.MessageParcel.create(); 4122 let readPos = data.getReadPosition(); 4123 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4124 ``` 4125 4126### getWritePosition 4127 4128getWritePosition(): number 4129 4130Obtains the write position of this **MessageParcel** object. 4131 4132**System capability**: SystemCapability.Communication.IPC.Core 4133 4134**Return value** 4135 4136| Type | Description | 4137| ------ | --------------------------------------- | 4138| number | Current write position of the **MessageParcel** object.| 4139 4140**Example** 4141 4142 ```ts 4143 import { hilog } from '@kit.PerformanceAnalysisKit'; 4144 4145 let data = rpc.MessageParcel.create(); 4146 data.writeInt(10); 4147 let bwPos = data.getWritePosition(); 4148 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4149 ``` 4150 4151### rewindRead 4152 4153rewindRead(pos: number): boolean 4154 4155Moves the read pointer to the specified position. 4156 4157**System capability**: SystemCapability.Communication.IPC.Core 4158 4159**Parameters** 4160 4161| Name| Type | Mandatory| Description | 4162| ------ | ------ | ---- | ------------------------ | 4163| pos | number | Yes | Position from which data is to read.| 4164 4165**Return value** 4166 4167| Type | Description | 4168| ------- | ------------------------------------------------- | 4169| boolean | Returns **true** if the read position changes; returns **false** otherwise.| 4170 4171**Example** 4172 4173 ```ts 4174 import { hilog } from '@kit.PerformanceAnalysisKit'; 4175 4176 let data = rpc.MessageParcel.create(); 4177 data.writeInt(12); 4178 data.writeString("parcel"); 4179 let number = data.readInt(); 4180 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4181 data.rewindRead(0); 4182 let number2 = data.readInt(); 4183 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4184 ``` 4185 4186### rewindWrite 4187 4188rewindWrite(pos: number): boolean 4189 4190Moves the write pointer to the specified position. 4191 4192**System capability**: SystemCapability.Communication.IPC.Core 4193 4194**Parameters** 4195 4196| Name| Type | Mandatory| Description | 4197| ------ | ------ | ---- | ------------------------ | 4198| pos | number | Yes | Position from which data is to write.| 4199 4200**Return value** 4201 4202| Type | Description | 4203| ------- | --------------------------------------------- | 4204| boolean | Returns **true** if the write position changes; returns **false** otherwise.| 4205 4206**Example** 4207 4208 ```ts 4209 import { hilog } from '@kit.PerformanceAnalysisKit'; 4210 4211 let data = rpc.MessageParcel.create(); 4212 data.writeInt(4); 4213 data.rewindWrite(0); 4214 data.writeInt(5); 4215 let number = data.readInt(); 4216 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4217 ``` 4218 4219### writeByte 4220 4221writeByte(val: number): boolean 4222 4223Writes a Byte value to this **MessageParcel** object. 4224 4225**System capability**: SystemCapability.Communication.IPC.Core 4226 4227**Parameters** 4228 4229| Name| Type | Mandatory| Description | 4230| ------ | ------ | ---- | ---------------- | 4231| val | number | Yes | Byte value to write.| 4232 4233**Return value** 4234 4235| Type | Description | 4236| ------- | ----------------------------- | 4237| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4238 4239**Example** 4240 4241 ```ts 4242 import { hilog } from '@kit.PerformanceAnalysisKit'; 4243 4244 let data = rpc.MessageParcel.create(); 4245 let result = data.writeByte(2); 4246 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4247 ``` 4248 4249### readByte 4250 4251readByte(): number 4252 4253Reads the Byte value from this **MessageParcel** object. 4254 4255**System capability**: SystemCapability.Communication.IPC.Core 4256 4257**Return value** 4258 4259| Type | Description | 4260| ------ | ------------ | 4261| number | Byte value read.| 4262 4263**Example** 4264 4265 ```ts 4266 import { hilog } from '@kit.PerformanceAnalysisKit'; 4267 4268 let data = rpc.MessageParcel.create(); 4269 let result = data.writeByte(2); 4270 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4271 let ret = data.readByte(); 4272 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4273 ``` 4274 4275### writeShort 4276 4277writeShort(val: number): boolean 4278 4279Writes a Short int value to this **MessageParcel** object. 4280 4281**System capability**: SystemCapability.Communication.IPC.Core 4282 4283**Parameters** 4284 4285| Name| Type | Mandatory| Description | 4286| ------ | ------ | ---- | ------------------ | 4287| val | number | Yes | Short int value to write.| 4288 4289**Return value** 4290 4291| Type | Description | 4292| ------- | ----------------------------- | 4293| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4294 4295**Example** 4296 4297 ```ts 4298 import { hilog } from '@kit.PerformanceAnalysisKit'; 4299 4300 let data = rpc.MessageParcel.create(); 4301 let result = data.writeShort(8); 4302 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4303 ``` 4304 4305### readShort 4306 4307readShort(): number 4308 4309Reads the Short int value from this **MessageParcel** object. 4310 4311**System capability**: SystemCapability.Communication.IPC.Core 4312 4313**Return value** 4314 4315| Type | Description | 4316| ------ | -------------- | 4317| number | Short int value read.| 4318 4319**Example** 4320 4321 ```ts 4322 import { hilog } from '@kit.PerformanceAnalysisKit'; 4323 4324 let data = rpc.MessageParcel.create(); 4325 let result = data.writeShort(8); 4326 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4327 let ret = data.readShort(); 4328 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4329 ``` 4330 4331### writeInt 4332 4333writeInt(val: number): boolean 4334 4335Writes an Int value to this **MessageParcel** object. 4336 4337**System capability**: SystemCapability.Communication.IPC.Core 4338 4339**Parameters** 4340 4341| Name| Type | Mandatory| Description | 4342| ------ | ------ | ---- | ---------------- | 4343| val | number | Yes | Int value to write.| 4344 4345**Return value** 4346 4347| Type | Description | 4348| ------- | ----------------------------- | 4349| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4350 4351**Example** 4352 4353 ```ts 4354 import { hilog } from '@kit.PerformanceAnalysisKit'; 4355 4356 let data = rpc.MessageParcel.create(); 4357 let result = data.writeInt(10); 4358 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4359 ``` 4360 4361### readInt 4362 4363readInt(): number 4364 4365Reads the Int value from this **MessageParcel** object. 4366 4367**System capability**: SystemCapability.Communication.IPC.Core 4368 4369**Return value** 4370 4371| Type | Description | 4372| ------ | ------------ | 4373| number | Int value read.| 4374 4375**Example** 4376 4377 ```ts 4378 import { hilog } from '@kit.PerformanceAnalysisKit'; 4379 4380 let data = rpc.MessageParcel.create(); 4381 let result = data.writeInt(10); 4382 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4383 let ret = data.readInt(); 4384 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4385 ``` 4386 4387### writeLong 4388 4389writeLong(val: number): boolean 4390 4391Writes a Long int value to this **MessageParcel** object. 4392 4393**System capability**: SystemCapability.Communication.IPC.Core 4394 4395**Parameters** 4396 4397| Name| Type | Mandatory| Description | 4398| ------ | ------ | ---- | ---------------- | 4399| val | number | Yes | Long int value to write.| 4400 4401**Return value** 4402 4403| Type | Description | 4404| ------- | --------------------------------- | 4405| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4406 4407**Example** 4408 4409 ```ts 4410 import { hilog } from '@kit.PerformanceAnalysisKit'; 4411 4412 let data = rpc.MessageParcel.create(); 4413 let result = data.writeLong(10000); 4414 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4415 ``` 4416 4417### readLong 4418 4419readLong(): number 4420 4421Reads the Long int value from this **MessageParcel** object. 4422 4423**System capability**: SystemCapability.Communication.IPC.Core 4424 4425**Return value** 4426 4427| Type | Description | 4428| ------ | -------------- | 4429| number | Long integer read.| 4430 4431**Example** 4432 4433 ```ts 4434 import { hilog } from '@kit.PerformanceAnalysisKit'; 4435 4436 let data = rpc.MessageParcel.create(); 4437 let result = data.writeLong(10000); 4438 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4439 let ret = data.readLong(); 4440 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4441 ``` 4442 4443### writeFloat 4444 4445writeFloat(val: number): boolean 4446 4447Writes a Float value to this **MessageParcel** object. 4448 4449**System capability**: SystemCapability.Communication.IPC.Core 4450 4451**Parameters** 4452 4453| Name| Type | Mandatory| Description | 4454| ------ | ------ | ---- | ---------------- | 4455| val | number | Yes | Float value to write.| 4456 4457**Return value** 4458 4459| Type | Description | 4460| ------- | --------------------------------- | 4461| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4462 4463**Example** 4464 4465 ```ts 4466 import { hilog } from '@kit.PerformanceAnalysisKit'; 4467 4468 let data = rpc.MessageParcel.create(); 4469 let result = data.writeFloat(1.2); 4470 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4471 ``` 4472 4473### readFloat 4474 4475readFloat(): number 4476 4477Reads the Float value from this **MessageParcel** object. 4478 4479**System capability**: SystemCapability.Communication.IPC.Core 4480 4481**Return value** 4482 4483| Type | Description | 4484| ------ | ------------ | 4485| number | Float value read.| 4486 4487**Example** 4488 4489 ```ts 4490 import { hilog } from '@kit.PerformanceAnalysisKit'; 4491 4492 let data = rpc.MessageParcel.create(); 4493 let result = data.writeFloat(1.2); 4494 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4495 let ret = data.readFloat(); 4496 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4497 ``` 4498 4499### writeDouble 4500 4501writeDouble(val: number): boolean 4502 4503Writes a Double value to this **MessageParcel** object. 4504 4505**System capability**: SystemCapability.Communication.IPC.Core 4506 4507**Parameters** 4508 4509| Name| Type | Mandatory| Description | 4510| ------ | ------ | ---- | ---------------------- | 4511| val | number | Yes | Double value to write.| 4512 4513**Return value** 4514 4515| Type | Description | 4516| ------- | --------------------------------- | 4517| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4518 4519**Example** 4520 4521 ```ts 4522 import { hilog } from '@kit.PerformanceAnalysisKit'; 4523 4524 let data = rpc.MessageParcel.create(); 4525 let result = data.writeDouble(10.2); 4526 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4527 ``` 4528 4529### readDouble 4530 4531readDouble(): number 4532 4533Reads the Double value from this **MessageParcel** object. 4534 4535**System capability**: SystemCapability.Communication.IPC.Core 4536 4537**Return value** 4538 4539| Type | Description | 4540| ------ | ------------------ | 4541| number | Double value read.| 4542 4543**Example** 4544 4545 ```ts 4546 import { hilog } from '@kit.PerformanceAnalysisKit'; 4547 4548 let data = rpc.MessageParcel.create(); 4549 let result = data.writeDouble(10.2); 4550 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4551 let ret = data.readDouble(); 4552 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4553 ``` 4554 4555### writeBoolean 4556 4557writeBoolean(val: boolean): boolean 4558 4559Writes a Boolean value to this **MessageParcel** object. 4560 4561**System capability**: SystemCapability.Communication.IPC.Core 4562 4563**Parameters** 4564 4565| Name| Type | Mandatory| Description | 4566| ------ | ------- | ---- | ---------------- | 4567| val | boolean | Yes | Boolean value to write.| 4568 4569**Return value** 4570 4571| Type | Description | 4572| ------- | --------------------------------- | 4573| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4574 4575**Example** 4576 4577 ```ts 4578 import { hilog } from '@kit.PerformanceAnalysisKit'; 4579 4580 let data = rpc.MessageParcel.create(); 4581 let result = data.writeBoolean(false); 4582 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4583 ``` 4584 4585### readBoolean 4586 4587readBoolean(): boolean 4588 4589Reads the Boolean value from this **MessageParcel** object. 4590 4591**System capability**: SystemCapability.Communication.IPC.Core 4592 4593**Return value** 4594 4595| Type | Description | 4596| ------- | -------------------- | 4597| boolean | Boolean value read.| 4598 4599**Example** 4600 4601 ```ts 4602 import { hilog } from '@kit.PerformanceAnalysisKit'; 4603 4604 let data = rpc.MessageParcel.create(); 4605 let result = data.writeBoolean(false); 4606 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4607 let ret = data.readBoolean(); 4608 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4609 ``` 4610 4611### writeChar 4612 4613writeChar(val: number): boolean 4614 4615Writes a Char value to this **MessageParcel** object. 4616 4617**System capability**: SystemCapability.Communication.IPC.Core 4618 4619**Parameters** 4620 4621| Name| Type | Mandatory| Description | 4622| ------ | ------ | ---- | -------------------- | 4623| val | number | Yes | Char value to write.| 4624 4625**Return value** 4626 4627| Type | Description | 4628| ------- | ----------------------------- | 4629| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4630 4631**Example** 4632 4633 ```ts 4634 import { hilog } from '@kit.PerformanceAnalysisKit'; 4635 4636 let data = rpc.MessageParcel.create(); 4637 let result = data.writeChar(97); 4638 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4639 ``` 4640 4641### readChar 4642 4643readChar(): number 4644 4645Reads the single character value from this **MessageParcel** object. 4646 4647**System capability**: SystemCapability.Communication.IPC.Core 4648 4649**Return value** 4650 4651| Type | Description | 4652| ------ | ---------------- | 4653| number | Single character value read.| 4654 4655**Example** 4656 4657 ```ts 4658 import { hilog } from '@kit.PerformanceAnalysisKit'; 4659 4660 let data = rpc.MessageParcel.create(); 4661 let result = data.writeChar(97); 4662 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4663 let ret = data.readChar(); 4664 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4665 ``` 4666 4667### writeString 4668 4669writeString(val: string): boolean 4670 4671Writes a string to this **MessageParcel** object. 4672 4673**System capability**: SystemCapability.Communication.IPC.Core 4674 4675**Parameters** 4676 4677| Name| Type | Mandatory| Description | 4678| ------ | ------ | ---- | ----------------------------------------- | 4679| val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 4680 4681**Return value** 4682 4683| Type | Description | 4684| ------- | --------------------------------- | 4685| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4686 4687**Example** 4688 4689 ```ts 4690 import { hilog } from '@kit.PerformanceAnalysisKit'; 4691 4692 let data = rpc.MessageParcel.create(); 4693 let result = data.writeString('abc'); 4694 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4695 ``` 4696 4697### readString 4698 4699readString(): string 4700 4701Reads the string from this **MessageParcel** object. 4702 4703**System capability**: SystemCapability.Communication.IPC.Core 4704 4705**Return value** 4706 4707| Type | Description | 4708| ------ | -------------- | 4709| string | String read.| 4710 4711**Example** 4712 4713 ```ts 4714 import { hilog } from '@kit.PerformanceAnalysisKit'; 4715 4716 let data = rpc.MessageParcel.create(); 4717 let result = data.writeString('abc'); 4718 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4719 let ret = data.readString(); 4720 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4721 ``` 4722 4723### writeSequenceable 4724 4725writeSequenceable(val: Sequenceable): boolean 4726 4727Writes a sequenceable object to this **MessageParcel** object. 4728 4729**System capability**: SystemCapability.Communication.IPC.Core 4730 4731**Parameters** 4732 4733| Name| Type | Mandatory| Description | 4734| ------ | ----------------------------- | ---- | -------------------- | 4735| val | [Sequenceable](#sequenceabledeprecated) | Yes | Sequenceable object to write.| 4736 4737**Return value** 4738 4739| Type | Description | 4740| ------- | -------------------------------- | 4741| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4742 4743**Example** 4744 4745 ```ts 4746 import { hilog } from '@kit.PerformanceAnalysisKit'; 4747 4748 class MySequenceable implements rpc.Sequenceable { 4749 num: number = 0; 4750 str: string = ''; 4751 constructor(num: number, str: string) { 4752 this.num = num; 4753 this.str = str; 4754 } 4755 marshalling(messageParcel: rpc.MessageParcel): boolean { 4756 messageParcel.writeInt(this.num); 4757 messageParcel.writeString(this.str); 4758 return true; 4759 } 4760 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4761 this.num = messageParcel.readInt(); 4762 this.str = messageParcel.readString(); 4763 return true; 4764 } 4765 } 4766 let sequenceable = new MySequenceable(1, "aaa"); 4767 let data = rpc.MessageParcel.create(); 4768 let result = data.writeSequenceable(sequenceable); 4769 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4770 ``` 4771 4772### readSequenceable 4773 4774readSequenceable(dataIn: Sequenceable): boolean 4775 4776Reads member variables from this **MessageParcel** object. 4777 4778**System capability**: SystemCapability.Communication.IPC.Core 4779 4780**Parameters** 4781 4782| Name| Type | Mandatory | Description | 4783| ------ | ----------------------------- | ------- | ---------------------------------------------- | 4784| dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4785 4786**Return value** 4787 4788| Type | Description | 4789| ------- | ---------------------------------------- | 4790| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4791 4792**Example** 4793 4794 ```ts 4795 import { hilog } from '@kit.PerformanceAnalysisKit'; 4796 4797 class MySequenceable implements rpc.Sequenceable { 4798 num: number = 0; 4799 str: string = ''; 4800 constructor(num: number, str: string) { 4801 this.num = num; 4802 this.str = str; 4803 } 4804 marshalling(messageParcel: rpc.MessageParcel): boolean { 4805 messageParcel.writeInt(this.num); 4806 messageParcel.writeString(this.str); 4807 return true; 4808 } 4809 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4810 this.num = messageParcel.readInt(); 4811 this.str = messageParcel.readString(); 4812 return true; 4813 } 4814 } 4815 let sequenceable = new MySequenceable(1, "aaa"); 4816 let data = rpc.MessageParcel.create(); 4817 let result = data.writeSequenceable(sequenceable); 4818 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4819 let ret = new MySequenceable(0, ""); 4820 let result2 = data.readSequenceable(ret); 4821 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4822 ``` 4823 4824### writeByteArray 4825 4826writeByteArray(byteArray: number[]): boolean 4827 4828Writes a byte array to this **MessageParcel** object. 4829 4830**System capability**: SystemCapability.Communication.IPC.Core 4831 4832**Parameters** 4833 4834| Name | Type | Mandatory| Description | 4835| --------- | -------- | ---- | ------------------ | 4836| byteArray | number[] | Yes | Byte array to write.| 4837 4838**Return value** 4839 4840| Type | Description | 4841| ------- | -------------------------------- | 4842| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4843 4844**Example** 4845 4846 ```ts 4847 import { hilog } from '@kit.PerformanceAnalysisKit'; 4848 4849 let data = rpc.MessageParcel.create(); 4850 let ByteArrayVar = [1, 2, 3, 4, 5]; 4851 let result = data.writeByteArray(ByteArrayVar); 4852 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4853 ``` 4854 4855### readByteArray 4856 4857readByteArray(dataIn: number[]): void 4858 4859Reads a byte array from this **MessageParcel** object. 4860 4861**System capability**: SystemCapability.Communication.IPC.Core 4862 4863**Parameters** 4864 4865| Name| Type | Mandatory| Description | 4866| ------ | -------- | ---- | ------------------ | 4867| dataIn | number[] | Yes | Byte array to read.| 4868 4869**Example** 4870 4871 ```ts 4872 import { hilog } from '@kit.PerformanceAnalysisKit'; 4873 4874 let data = rpc.MessageParcel.create(); 4875 let ByteArrayVar = [1, 2, 3, 4, 5]; 4876 let result = data.writeByteArray(ByteArrayVar); 4877 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4878 let array: Array<number> = new Array(5); 4879 data.readByteArray(array); 4880 ``` 4881 4882### readByteArray 4883 4884readByteArray(): number[] 4885 4886Reads the byte array from this **MessageParcel** object. 4887 4888**System capability**: SystemCapability.Communication.IPC.Core 4889 4890**Return value** 4891 4892| Type | Description | 4893| -------- | -------------- | 4894| number[] | Byte array read.| 4895 4896**Example** 4897 4898 ```ts 4899 import { hilog } from '@kit.PerformanceAnalysisKit'; 4900 4901 let data = rpc.MessageParcel.create(); 4902 let ByteArrayVar = [1, 2, 3, 4, 5]; 4903 let result = data.writeByteArray(ByteArrayVar); 4904 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4905 let array = data.readByteArray(); 4906 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4907 ``` 4908 4909### writeShortArray 4910 4911writeShortArray(shortArray: number[]): boolean 4912 4913Writes a short array to this **MessageParcel** object. 4914 4915**System capability**: SystemCapability.Communication.IPC.Core 4916 4917**Parameters** 4918 4919| Name | Type | Mandatory| Description | 4920| ---------- | -------- | ---- | -------------------- | 4921| shortArray | number[] | Yes | Short array to write.| 4922 4923**Return value** 4924 4925| Type | Description | 4926| ------- | -------------------------------- | 4927| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4928 4929**Example** 4930 4931 ```ts 4932 import { hilog } from '@kit.PerformanceAnalysisKit'; 4933 4934 let data = rpc.MessageParcel.create(); 4935 let result = data.writeShortArray([11, 12, 13]); 4936 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4937 ``` 4938 4939### readShortArray 4940 4941readShortArray(dataIn: number[]): void 4942 4943Reads a short array from this **MessageParcel** object. 4944 4945**System capability**: SystemCapability.Communication.IPC.Core 4946 4947**Parameters** 4948 4949| Name| Type | Mandatory| Description | 4950| ------ | -------- | ---- | -------------------- | 4951| dataIn | number[] | Yes | Short array to read.| 4952 4953**Example** 4954 4955 ```ts 4956 import { hilog } from '@kit.PerformanceAnalysisKit'; 4957 4958 let data = rpc.MessageParcel.create(); 4959 let result = data.writeShortArray([11, 12, 13]); 4960 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4961 let array: Array<number> = new Array(3); 4962 data.readShortArray(array); 4963 ``` 4964 4965### readShortArray 4966 4967readShortArray(): number[] 4968 4969Reads the short array from this **MessageParcel** object. 4970 4971**System capability**: SystemCapability.Communication.IPC.Core 4972 4973**Return value** 4974 4975| Type | Description | 4976| -------- | ---------------- | 4977| number[] | Short array read.| 4978 4979**Example** 4980 4981 ```ts 4982 import { hilog } from '@kit.PerformanceAnalysisKit'; 4983 4984 let data = rpc.MessageParcel.create(); 4985 let result = data.writeShortArray([11, 12, 13]); 4986 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4987 let array = data.readShortArray(); 4988 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 4989 ``` 4990 4991### writeIntArray 4992 4993writeIntArray(intArray: number[]): boolean 4994 4995Writes an integer array to this **MessageParcel** object. 4996 4997**System capability**: SystemCapability.Communication.IPC.Core 4998 4999**Parameters** 5000 5001| Name | Type | Mandatory| Description | 5002| -------- | -------- | ---- | ------------------ | 5003| intArray | number[] | Yes | Integer array to write.| 5004 5005**Return value** 5006 5007| Type | Description | 5008| ------- | -------------------------------- | 5009| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5010 5011**Example** 5012 5013 ```ts 5014 import { hilog } from '@kit.PerformanceAnalysisKit'; 5015 5016 let data = rpc.MessageParcel.create(); 5017 let result = data.writeIntArray([100, 111, 112]); 5018 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5019 ``` 5020 5021### readIntArray 5022 5023readIntArray(dataIn: number[]): void 5024 5025Reads an integer array from this **MessageParcel** object. 5026 5027**System capability**: SystemCapability.Communication.IPC.Core 5028 5029**Parameters** 5030 5031| Name| Type | Mandatory| Description | 5032| ------ | -------- | ---- | ------------------ | 5033| dataIn | number[] | Yes | Integer array to read.| 5034 5035**Example** 5036 5037 ```ts 5038 import { hilog } from '@kit.PerformanceAnalysisKit'; 5039 5040 let data = rpc.MessageParcel.create(); 5041 let result = data.writeIntArray([100, 111, 112]); 5042 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5043 let array: Array<number> = new Array(3); 5044 data.readIntArray(array); 5045 ``` 5046 5047### readIntArray 5048 5049readIntArray(): number[] 5050 5051Reads the integer array from this **MessageParcel** object. 5052 5053**System capability**: SystemCapability.Communication.IPC.Core 5054 5055**Return value** 5056 5057| Type | Description | 5058| -------- | -------------- | 5059| number[] | Integer array read.| 5060 5061**Example** 5062 5063 ```ts 5064 import { hilog } from '@kit.PerformanceAnalysisKit'; 5065 5066 let data = rpc.MessageParcel.create(); 5067 let result = data.writeIntArray([100, 111, 112]); 5068 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5069 let array = data.readIntArray(); 5070 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5071 ``` 5072 5073### writeLongArray 5074 5075writeLongArray(longArray: number[]): boolean 5076 5077Writes a long array to this **MessageParcel** object. 5078 5079**System capability**: SystemCapability.Communication.IPC.Core 5080 5081**Parameters** 5082 5083| Name | Type | Mandatory| Description | 5084| --------- | -------- | ---- | -------------------- | 5085| longArray | number[] | Yes | Long array to write.| 5086 5087**Return value** 5088 5089| Type | Description | 5090| ------- | ----------------------------- | 5091| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5092 5093**Example** 5094 5095 ```ts 5096 import { hilog } from '@kit.PerformanceAnalysisKit'; 5097 5098 let data = rpc.MessageParcel.create(); 5099 let result = data.writeLongArray([1111, 1112, 1113]); 5100 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5101 ``` 5102 5103### readLongArray 5104 5105readLongArray(dataIn: number[]): void 5106 5107Reads a long array from this **MessageParcel** object. 5108 5109**System capability**: SystemCapability.Communication.IPC.Core 5110 5111**Parameters** 5112 5113| Name| Type | Mandatory| Description | 5114| ------ | -------- | ---- | -------------------- | 5115| dataIn | number[] | Yes | Long array to read.| 5116 5117**Example** 5118 5119 ```ts 5120 import { hilog } from '@kit.PerformanceAnalysisKit'; 5121 5122 let data = rpc.MessageParcel.create(); 5123 let result = data.writeLongArray([1111, 1112, 1113]); 5124 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5125 let array: Array<number> = new Array(3); 5126 data.readLongArray(array); 5127 ``` 5128 5129### readLongArray 5130 5131readLongArray(): number[] 5132 5133Reads the long array from this **MessageParcel** object. 5134 5135**System capability**: SystemCapability.Communication.IPC.Core 5136 5137**Return value** 5138 5139| Type | Description | 5140| -------- | ---------------- | 5141| number[] | Long array read.| 5142 5143**Example** 5144 5145 ```ts 5146 import { hilog } from '@kit.PerformanceAnalysisKit'; 5147 5148 let data = rpc.MessageParcel.create(); 5149 let result = data.writeLongArray([1111, 1112, 1113]); 5150 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5151 let array = data.readLongArray(); 5152 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5153 ``` 5154 5155### writeFloatArray 5156 5157writeFloatArray(floatArray: number[]): boolean 5158 5159Writes a FloatArray to this **MessageParcel** object. 5160 5161**System capability**: SystemCapability.Communication.IPC.Core 5162 5163**Parameters** 5164 5165| Name| Type| Mandatory| Description | 5166| ---------- | -------- | ---- | --- | 5167| 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.| 5168 5169**Return value** 5170 5171| Type | Description | 5172| ------- | -------------------------------- | 5173| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5174 5175**Example** 5176 5177 ```ts 5178 import { hilog } from '@kit.PerformanceAnalysisKit'; 5179 5180 let data = rpc.MessageParcel.create(); 5181 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5182 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5183 ``` 5184 5185### readFloatArray 5186 5187readFloatArray(dataIn: number[]): void 5188 5189Reads a FloatArray from this **MessageParcel** object. 5190 5191**System capability**: SystemCapability.Communication.IPC.Core 5192 5193**Parameters** 5194 5195| Name| Type | Mandatory| Description | 5196| ------ | -------- | ---- | ------ | 5197| 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.| 5198 5199**Example** 5200 5201 ```ts 5202 import { hilog } from '@kit.PerformanceAnalysisKit'; 5203 5204 let data = rpc.MessageParcel.create(); 5205 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5206 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5207 let array: Array<number> = new Array(3); 5208 data.readFloatArray(array); 5209 ``` 5210 5211### readFloatArray 5212 5213readFloatArray(): number[] 5214 5215Reads the FloatArray from this **MessageParcel** object. 5216 5217**System capability**: SystemCapability.Communication.IPC.Core 5218 5219**Return value** 5220 5221| Type | Description | 5222| -------- | -------------- | 5223| number[] | FloatArray read.| 5224 5225**Example** 5226 5227 ```ts 5228 import { hilog } from '@kit.PerformanceAnalysisKit'; 5229 5230 let data = rpc.MessageParcel.create(); 5231 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5232 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5233 let array = data.readFloatArray(); 5234 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5235 ``` 5236 5237### writeDoubleArray 5238 5239writeDoubleArray(doubleArray: number[]): boolean 5240 5241Writes a DoubleArray to this **MessageParcel** object. 5242 5243**System capability**: SystemCapability.Communication.IPC.Core 5244 5245**Parameters** 5246 5247| Name | Type | Mandatory| Description | 5248| ----------- | -------- | ---- | ------------------------ | 5249| doubleArray | number[] | Yes | DoubleArray to write.| 5250 5251**Return value** 5252 5253| Type | Description | 5254| ------- | -------------------------------- | 5255| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5256 5257**Example** 5258 5259 ```ts 5260 import { hilog } from '@kit.PerformanceAnalysisKit'; 5261 5262 let data = rpc.MessageParcel.create(); 5263 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5264 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5265 ``` 5266 5267### readDoubleArray 5268 5269readDoubleArray(dataIn: number[]): void 5270 5271Reads a DoubleArray from this **MessageParcel** object. 5272 5273**System capability**: SystemCapability.Communication.IPC.Core 5274 5275**Parameters** 5276 5277| Name| Type | Mandatory| Description | 5278| ------ | -------- | ---- | ------------------------ | 5279| dataIn | number[] | Yes | DoubleArray to read.| 5280 5281**Example** 5282 5283 ```ts 5284 import { hilog } from '@kit.PerformanceAnalysisKit'; 5285 5286 let data = rpc.MessageParcel.create(); 5287 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5288 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5289 let array: Array<number> = new Array(3); 5290 data.readDoubleArray(array); 5291 ``` 5292 5293### readDoubleArray 5294 5295readDoubleArray(): number[] 5296 5297Reads the DoubleArray from this **MessageParcel** object. 5298 5299**System capability**: SystemCapability.Communication.IPC.Core 5300 5301**Return value** 5302 5303| Type | Description | 5304| -------- | -------------------- | 5305| number[] | DoubleArray read.| 5306 5307**Example** 5308 5309 ```ts 5310 import { hilog } from '@kit.PerformanceAnalysisKit'; 5311 5312 let data = rpc.MessageParcel.create(); 5313 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5314 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5315 let array = data.readDoubleArray(); 5316 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5317 ``` 5318 5319### writeBooleanArray 5320 5321writeBooleanArray(booleanArray: boolean[]): boolean 5322 5323Writes a Boolean array to this **MessageParcel** object. 5324 5325**System capability**: SystemCapability.Communication.IPC.Core 5326 5327**Parameters** 5328 5329| Name | Type | Mandatory| Description | 5330| ------------ | --------- | ---- | ------------------ | 5331| booleanArray | boolean[] | Yes | Boolean array to write.| 5332 5333**Return value** 5334 5335| Type | Description | 5336| ------- | -------------------------------- | 5337| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5338 5339**Example** 5340 5341 ```ts 5342 import { hilog } from '@kit.PerformanceAnalysisKit'; 5343 5344 let data = rpc.MessageParcel.create(); 5345 let result = data.writeBooleanArray([false, true, false]); 5346 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5347 ``` 5348 5349### readBooleanArray 5350 5351readBooleanArray(dataIn: boolean[]): void 5352 5353Reads a Boolean array from this **MessageParcel** object. 5354 5355**System capability**: SystemCapability.Communication.IPC.Core 5356 5357**Parameters** 5358 5359| Name| Type | Mandatory| Description | 5360| ------ | --------- | ---- | ------------------ | 5361| dataIn | boolean[] | Yes | Boolean array to read.| 5362 5363**Example** 5364 5365 ```ts 5366 import { hilog } from '@kit.PerformanceAnalysisKit'; 5367 5368 let data = rpc.MessageParcel.create(); 5369 let result = data.writeBooleanArray([false, true, false]); 5370 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5371 let array: Array<boolean> = new Array(3); 5372 data.readBooleanArray(array); 5373 ``` 5374 5375### readBooleanArray 5376 5377readBooleanArray(): boolean[] 5378 5379Reads the Boolean array from this **MessageParcel** object. 5380 5381**System capability**: SystemCapability.Communication.IPC.Core 5382 5383**Return value** 5384 5385| Type | Description | 5386| --------- | -------------- | 5387| boolean[] | Boolean array read.| 5388 5389**Example** 5390 5391 ```ts 5392 import { hilog } from '@kit.PerformanceAnalysisKit'; 5393 5394 let data = rpc.MessageParcel.create(); 5395 let result = data.writeBooleanArray([false, true, false]); 5396 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5397 let array = data.readBooleanArray(); 5398 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5399 ``` 5400 5401### writeCharArray 5402 5403writeCharArray(charArray: number[]): boolean 5404 5405Writes a character array to this **MessageParcel** object. 5406 5407**System capability**: SystemCapability.Communication.IPC.Core 5408 5409**Parameters** 5410 5411| Name | Type | Mandatory| Description | 5412| --------- | -------- | ---- | ---------------------- | 5413| charArray | number[] | Yes | Character array to write.| 5414 5415**Return value** 5416 5417| Type | Description | 5418| ------- | -------------------------------- | 5419| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5420 5421**Example** 5422 5423 ```ts 5424 import { hilog } from '@kit.PerformanceAnalysisKit'; 5425 5426 let data = rpc.MessageParcel.create(); 5427 let result = data.writeCharArray([97, 98, 88]); 5428 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5429 ``` 5430 5431### readCharArray 5432 5433readCharArray(dataIn: number[]): void 5434 5435Reads a character array from this **MessageParcel** object. 5436 5437**System capability**: SystemCapability.Communication.IPC.Core 5438 5439**Parameters** 5440 5441| Name| Type | Mandatory| Description | 5442| ------ | -------- | ---- | ---------------------- | 5443| dataIn | number[] | Yes | Character array to read.| 5444 5445**Example** 5446 5447 ```ts 5448 import { hilog } from '@kit.PerformanceAnalysisKit'; 5449 5450 let data = rpc.MessageParcel.create(); 5451 let result = data.writeCharArray([97, 98, 99]); 5452 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5453 let array: Array<number> = new Array(3); 5454 data.readCharArray(array); 5455 ``` 5456 5457### readCharArray 5458 5459readCharArray(): number[] 5460 5461Reads the character array from this **MessageParcel** object. 5462 5463**System capability**: SystemCapability.Communication.IPC.Core 5464 5465**Return value** 5466 5467| Type | Description | 5468| -------- | ------------------ | 5469| number[] | Character array read.| 5470 5471**Example** 5472 5473 ```ts 5474 import { hilog } from '@kit.PerformanceAnalysisKit'; 5475 5476 let data = rpc.MessageParcel.create(); 5477 let result = data.writeCharArray([97, 98, 99]); 5478 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5479 let array = data.readCharArray(); 5480 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5481 ``` 5482 5483### writeStringArray 5484 5485writeStringArray(stringArray: string[]): boolean 5486 5487Writes a string array to this **MessageParcel** object. 5488 5489**System capability**: SystemCapability.Communication.IPC.Core 5490 5491**Parameters** 5492 5493| Name | Type | Mandatory| Description | 5494| ----------- | -------- | ---- | ---------------- | 5495| stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 5496 5497**Return value** 5498 5499| Type | Description| 5500| ------- | -------------------------------- | 5501| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5502 5503**Example** 5504 5505 ```ts 5506 import { hilog } from '@kit.PerformanceAnalysisKit'; 5507 5508 let data = rpc.MessageParcel.create(); 5509 let result = data.writeStringArray(["abc", "def"]); 5510 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5511 ``` 5512 5513### readStringArray 5514 5515readStringArray(dataIn: string[]): void 5516 5517Reads a string array from this **MessageParcel** object. 5518 5519**System capability**: SystemCapability.Communication.IPC.Core 5520 5521**Parameters** 5522 5523| Name| Type | Mandatory| Description | 5524| ------ | -------- | ---- | -------------------- | 5525| dataIn | string[] | Yes | String array to read.| 5526 5527**Example** 5528 5529 ```ts 5530 import { hilog } from '@kit.PerformanceAnalysisKit'; 5531 5532 let data = rpc.MessageParcel.create(); 5533 let result = data.writeStringArray(["abc", "def"]); 5534 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5535 let array: Array<string> = new Array(2); 5536 data.readStringArray(array); 5537 ``` 5538 5539### readStringArray 5540 5541readStringArray(): string[] 5542 5543Reads the string array from this **MessageParcel** object. 5544 5545**System capability**: SystemCapability.Communication.IPC.Core 5546 5547**Return value** 5548 5549| Type | Description | 5550| -------- | ---------------- | 5551| string[] | String array read.| 5552 5553**Example** 5554 5555 ```ts 5556 import { hilog } from '@kit.PerformanceAnalysisKit'; 5557 5558 let data = rpc.MessageParcel.create(); 5559 let result = data.writeStringArray(["abc", "def"]); 5560 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5561 let array = data.readStringArray(); 5562 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5563 ``` 5564 5565### writeNoException<sup>8+</sup> 5566 5567writeNoException(): void 5568 5569Writes information to this **MessageParcel** object indicating that no exception occurred. 5570 5571**System capability**: SystemCapability.Communication.IPC.Core 5572 5573**Example** 5574 5575 ```ts 5576 import { hilog } from '@kit.PerformanceAnalysisKit'; 5577 5578 class MyDeathRecipient implements rpc.DeathRecipient { 5579 onRemoteDied() { 5580 hilog.info(0x0000, 'testTag', 'server died'); 5581 } 5582 } 5583 class TestRemoteObject extends rpc.RemoteObject { 5584 constructor(descriptor: string) { 5585 super(descriptor); 5586 } 5587 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5588 return true; 5589 } 5590 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5591 return true; 5592 } 5593 isObjectDead(): boolean { 5594 return false; 5595 } 5596 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5597 if (code === 1) { 5598 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5599 reply.writeNoException(); 5600 return true; 5601 } else { 5602 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5603 return false; 5604 } 5605 } 5606 } 5607 ``` 5608 5609### readException<sup>8+</sup> 5610 5611readException(): void 5612 5613Reads the exception information from this **MessageParcel** object. 5614 5615**System capability**: SystemCapability.Communication.IPC.Core 5616 5617**Example** 5618 5619 ```ts 5620 // If the FA model is used, import featureAbility from @kit.AbilityKit. 5621 // import { featureAbility } from '@kit.AbilityKit'; 5622 import { Want, common } from '@kit.AbilityKit'; 5623 import { hilog } from '@kit.PerformanceAnalysisKit'; 5624 5625 let proxy: rpc.IRemoteObject | undefined; 5626 let connect: common.ConnectOptions = { 5627 onConnect: (elementName, remoteProxy) => { 5628 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5629 proxy = remoteProxy; 5630 }, 5631 onDisconnect: (elementName) => { 5632 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5633 }, 5634 onFailed: () => { 5635 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5636 } 5637 }; 5638 let want: Want = { 5639 bundleName: "com.ohos.server", 5640 abilityName: "com.ohos.server.EntryAbility", 5641 }; 5642 5643 // Use this method to connect to the ability for the FA model. 5644 // FA.connectAbility(want,connect); 5645 5646 // Save the connection ID, which will be used for the subsequent service disconnection. 5647 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 5648 // Save the connection ID, which will be used for the subsequent service disconnection. 5649 let connectionId = context.connectServiceExtensionAbility(want, connect); 5650 ``` 5651 5652The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 5653 5654 ```ts 5655 import { hilog } from '@kit.PerformanceAnalysisKit'; 5656 5657 let option = new rpc.MessageOption(); 5658 let data = rpc.MessageParcel.create(); 5659 let reply = rpc.MessageParcel.create(); 5660 data.writeNoException(); 5661 data.writeString('hello'); 5662 if (proxy != undefined) { 5663 let a = proxy.sendRequest(1, data, reply, option) as Object; 5664 let b = a as Promise<rpc.SendRequestResult>; 5665 b.then((result: rpc.SendRequestResult) => { 5666 if (result.errCode === 0) { 5667 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5668 result.reply.readException(); 5669 let msg = result.reply.readString(); 5670 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5671 } else { 5672 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5673 } 5674 }).catch((e: Error) => { 5675 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5676 }).finally (() => { 5677 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5678 data.reclaim(); 5679 reply.reclaim(); 5680 }); 5681 } 5682 ``` 5683 5684### writeSequenceableArray 5685 5686writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5687 5688Writes a sequenceable array to this **MessageParcel** object. 5689 5690**System capability**: SystemCapability.Communication.IPC.Core 5691 5692**Parameters** 5693 5694| Name | Type | Mandatory| Description | 5695| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5696| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to write.| 5697 5698**Return value** 5699 5700| Type | Description | 5701| ------- | -------------------------------- | 5702| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5703 5704**Example** 5705 5706 ```ts 5707 import { hilog } from '@kit.PerformanceAnalysisKit'; 5708 5709 class MySequenceable implements rpc.Sequenceable { 5710 num: number = 0; 5711 str: string = ''; 5712 constructor(num: number, str: string) { 5713 this.num = num; 5714 this.str = str; 5715 } 5716 marshalling(messageParcel: rpc.MessageParcel): boolean { 5717 messageParcel.writeInt(this.num); 5718 messageParcel.writeString(this.str); 5719 return true; 5720 } 5721 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5722 this.num = messageParcel.readInt(); 5723 this.str = messageParcel.readString(); 5724 return true; 5725 } 5726 } 5727 let sequenceable = new MySequenceable(1, "aaa"); 5728 let sequenceable2 = new MySequenceable(2, "bbb"); 5729 let sequenceable3 = new MySequenceable(3, "ccc"); 5730 let a = [sequenceable, sequenceable2, sequenceable3]; 5731 let data = rpc.MessageParcel.create(); 5732 let result = data.writeSequenceableArray(a); 5733 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5734 ``` 5735 5736### readSequenceableArray<sup>8+</sup> 5737 5738readSequenceableArray(sequenceableArray: Sequenceable[]): void 5739 5740Reads a sequenceable array from this **MessageParcel** object. 5741 5742**System capability**: SystemCapability.Communication.IPC.Core 5743 5744**Parameters** 5745 5746| Name | Type | Mandatory| Description | 5747| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5748| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to read.| 5749 5750**Example** 5751 5752 ```ts 5753 import { hilog } from '@kit.PerformanceAnalysisKit'; 5754 5755 class MySequenceable implements rpc.Sequenceable { 5756 num: number = 0; 5757 str: string = ''; 5758 constructor(num: number, str: string) { 5759 this.num = num; 5760 this.str = str; 5761 } 5762 marshalling(messageParcel: rpc.MessageParcel): boolean { 5763 messageParcel.writeInt(this.num); 5764 messageParcel.writeString(this.str); 5765 return true; 5766 } 5767 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5768 this.num = messageParcel.readInt(); 5769 this.str = messageParcel.readString(); 5770 return true; 5771 } 5772 } 5773 let sequenceable = new MySequenceable(1, "aaa"); 5774 let sequenceable2 = new MySequenceable(2, "bbb"); 5775 let sequenceable3 = new MySequenceable(3, "ccc"); 5776 let a = [sequenceable, sequenceable2, sequenceable3]; 5777 let data = rpc.MessageParcel.create(); 5778 let result = data.writeSequenceableArray(a); 5779 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5780 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5781 data.readSequenceableArray(b); 5782 ``` 5783 5784### writeRemoteObjectArray<sup>8+</sup> 5785 5786writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5787 5788Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 5789 5790**System capability**: SystemCapability.Communication.IPC.Core 5791 5792**Parameters** 5793 5794| Name | Type | Mandatory| Description | 5795| ----------- | --------------- | ---- | ----- | 5796| objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 5797 5798**Return value** 5799 5800| Type | Description | 5801| ------- | -------------------------------- | 5802| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5803 5804**Example** 5805 5806 ```ts 5807 import { hilog } from '@kit.PerformanceAnalysisKit'; 5808 5809 class MyDeathRecipient implements rpc.DeathRecipient { 5810 onRemoteDied() { 5811 hilog.info(0x0000, 'testTag', 'server died'); 5812 } 5813 } 5814 class TestRemoteObject extends rpc.RemoteObject { 5815 constructor(descriptor: string) { 5816 super(descriptor); 5817 this.attachLocalInterface(this, descriptor); 5818 } 5819 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5820 return true; 5821 } 5822 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5823 return true; 5824 } 5825 isObjectDead(): boolean { 5826 return false; 5827 } 5828 asObject(): rpc.IRemoteObject { 5829 return this; 5830 } 5831 } 5832 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5833 let data = rpc.MessageParcel.create(); 5834 let result = data.writeRemoteObjectArray(a); 5835 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5836 ``` 5837 5838### readRemoteObjectArray<sup>8+</sup> 5839 5840readRemoteObjectArray(objects: IRemoteObject[]): void 5841 5842Reads an **IRemoteObject** array from this **MessageParcel** object. 5843 5844**System capability**: SystemCapability.Communication.IPC.Core 5845 5846**Parameters** 5847 5848| Name | Type | Mandatory| Description | 5849| ------- | --------------- | ---- | --------- | 5850| objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 5851 5852**Example** 5853 5854 ```ts 5855 import { hilog } from '@kit.PerformanceAnalysisKit'; 5856 5857 class MyDeathRecipient implements rpc.DeathRecipient { 5858 onRemoteDied() { 5859 hilog.info(0x0000, 'testTag', 'server died'); 5860 } 5861 } 5862 class TestRemoteObject extends rpc.RemoteObject { 5863 constructor(descriptor: string) { 5864 super(descriptor); 5865 this.attachLocalInterface(this, descriptor); 5866 } 5867 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5868 return true; 5869 } 5870 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5871 return true; 5872 } 5873 isObjectDead(): boolean { 5874 return false; 5875 } 5876 asObject(): rpc.IRemoteObject { 5877 return this; 5878 } 5879 } 5880 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5881 let data = rpc.MessageParcel.create(); 5882 data.writeRemoteObjectArray(a); 5883 let b: Array<rpc.IRemoteObject> = new Array(3); 5884 data.readRemoteObjectArray(b); 5885 ``` 5886 5887### readRemoteObjectArray<sup>8+</sup> 5888 5889readRemoteObjectArray(): IRemoteObject[] 5890 5891Reads the **IRemoteObject** array from this **MessageParcel** object. 5892 5893**System capability**: SystemCapability.Communication.IPC.Core 5894 5895**Return value** 5896 5897| Type | Description | 5898| --------------- | --------------------------- | 5899| [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 5900 5901**Example** 5902 5903 ```ts 5904 import { hilog } from '@kit.PerformanceAnalysisKit'; 5905 5906 class MyDeathRecipient implements rpc.DeathRecipient { 5907 onRemoteDied() { 5908 hilog.info(0x0000, 'testTag', 'server died'); 5909 } 5910 } 5911 class TestRemoteObject extends rpc.RemoteObject { 5912 constructor(descriptor: string) { 5913 super(descriptor); 5914 this.attachLocalInterface(this, descriptor); 5915 } 5916 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5917 return true; 5918 } 5919 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5920 return true; 5921 } 5922 isObjectDead(): boolean { 5923 return false; 5924 } 5925 asObject(): rpc.IRemoteObject { 5926 return this; 5927 } 5928 } 5929 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5930 let data = rpc.MessageParcel.create(); 5931 let result = data.writeRemoteObjectArray(a); 5932 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5933 let b = data.readRemoteObjectArray(); 5934 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5935 ``` 5936 5937### closeFileDescriptor<sup>8+</sup> 5938 5939static closeFileDescriptor(fd: number): void 5940 5941Closes a file descriptor. This API is a static method. 5942 5943**System capability**: SystemCapability.Communication.IPC.Core 5944 5945**Parameters** 5946 5947| Name| Type | Mandatory| Description | 5948| ------ | ------ | ---- | -------------------- | 5949| fd | number | Yes | File descriptor to close.| 5950 5951**Example** 5952 5953 ```ts 5954 import { fileIo } from '@kit.CoreFileKit'; 5955 5956 let filePath = "path/to/file"; 5957 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5958 rpc.MessageParcel.closeFileDescriptor(file.fd); 5959 ``` 5960 5961### dupFileDescriptor<sup>8+</sup> 5962 5963static dupFileDescriptor(fd: number) :number 5964 5965Duplicates a file descriptor. This API is a static method. 5966 5967**System capability**: SystemCapability.Communication.IPC.Core 5968 5969**Parameters** 5970 5971| Name| Type | Mandatory| Description | 5972| ------ | ------ | ---- | ------------------------ | 5973| fd | number | Yes | File descriptor to duplicate.| 5974 5975**Return value** 5976 5977| Type | Description | 5978| ------ | -------------------- | 5979| number | New file descriptor.| 5980 5981**Example** 5982 5983 ```ts 5984 import { fileIo } from '@kit.CoreFileKit'; 5985 5986 let filePath = "path/to/file"; 5987 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5988 rpc.MessageParcel.dupFileDescriptor(file.fd); 5989 ``` 5990 5991### containFileDescriptors<sup>8+</sup> 5992 5993containFileDescriptors(): boolean 5994 5995Checks whether this **MessageParcel** object contains file descriptors. 5996 5997**System capability**: SystemCapability.Communication.IPC.Core 5998 5999**Return value** 6000 6001| Type | Description | 6002| ------- | --------------------------------------------- | 6003| boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 6004 6005**Example** 6006 6007 ```ts 6008 import { fileIo } from '@kit.CoreFileKit'; 6009 import { hilog } from '@kit.PerformanceAnalysisKit'; 6010 6011 let parcel = new rpc.MessageParcel(); 6012 let filePath = "path/to/file"; 6013 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6014 let writeResult = parcel.writeFileDescriptor(file.fd); 6015 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6016 let containFD = parcel.containFileDescriptors(); 6017 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6018 ``` 6019 6020### writeFileDescriptor<sup>8+</sup> 6021 6022writeFileDescriptor(fd: number): boolean 6023 6024Writes a file descriptor to this **MessageParcel** object. 6025 6026**System capability**: SystemCapability.Communication.IPC.Core 6027 6028**Parameters** 6029 6030| Name| Type | Mandatory| Description | 6031| ------ | ------ | ---- | ------------ | 6032| fd | number | Yes | File descriptor to write.| 6033 6034**Return value** 6035 6036| Type | Description | 6037| ------- | -------------------------------- | 6038| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6039 6040**Example** 6041 6042 ```ts 6043 import { fileIo } from '@kit.CoreFileKit'; 6044 import { hilog } from '@kit.PerformanceAnalysisKit'; 6045 6046 let parcel = new rpc.MessageParcel(); 6047 let filePath = "path/to/file"; 6048 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6049 let writeResult = parcel.writeFileDescriptor(file.fd); 6050 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6051 ``` 6052 6053### readFileDescriptor<sup>8+</sup> 6054 6055readFileDescriptor(): number 6056 6057Reads the file descriptor from this **MessageParcel** object. 6058 6059**System capability**: SystemCapability.Communication.IPC.Core 6060 6061**Return value** 6062 6063| Type | Description | 6064| ------ | ---------------- | 6065| number | File descriptor read.| 6066 6067**Example** 6068 6069 ```ts 6070 import { fileIo } from '@kit.CoreFileKit'; 6071 import { hilog } from '@kit.PerformanceAnalysisKit'; 6072 6073 let parcel = new rpc.MessageParcel(); 6074 let filePath = "path/to/file"; 6075 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6076 parcel.writeFileDescriptor(file.fd); 6077 let readFD = parcel.readFileDescriptor(); 6078 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6079 ``` 6080 6081### writeAshmem<sup>8+</sup> 6082 6083writeAshmem(ashmem: Ashmem): boolean 6084 6085Writes an anonymous shared object to this **MessageParcel** object. 6086 6087**System capability**: SystemCapability.Communication.IPC.Core 6088 6089**Parameters** 6090 6091| Name| Type | Mandatory| Description | 6092| ------ | ------ | ---- | ----------------------------------- | 6093| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 6094 6095**Return value** 6096 6097| Type | Description | 6098| ------- | -------------------------------- | 6099| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6100 6101**Example** 6102 6103 ```ts 6104 import { hilog } from '@kit.PerformanceAnalysisKit'; 6105 6106 let parcel = new rpc.MessageParcel(); 6107 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6108 let isWriteSuccess = parcel.writeAshmem(ashmem); 6109 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6110 ``` 6111 6112### readAshmem<sup>8+</sup> 6113 6114readAshmem(): Ashmem 6115 6116Reads the anonymous shared object from this **MessageParcel** object. 6117 6118**System capability**: SystemCapability.Communication.IPC.Core 6119 6120**Return value** 6121 6122| Type | Description | 6123| ------ | ------------------ | 6124| [Ashmem](#ashmem8) | Anonymous share object obtained.| 6125 6126**Example** 6127 6128 ```ts 6129 import { hilog } from '@kit.PerformanceAnalysisKit'; 6130 6131 let parcel = new rpc.MessageParcel(); 6132 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6133 let isWriteSuccess = parcel.writeAshmem(ashmem); 6134 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6135 let readAshmem = parcel.readAshmem(); 6136 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6137 ``` 6138 6139### getRawDataCapacity<sup>8+</sup> 6140 6141getRawDataCapacity(): number 6142 6143Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 6144 6145**System capability**: SystemCapability.Communication.IPC.Core 6146 6147**Return value** 6148 6149| Type | Description | 6150| ------ | ---------------------------------------------------------- | 6151| number | Maximum amount of raw data that **MessageParcel** can hold, that is, 128 MB.| 6152 6153**Example** 6154 6155 ```ts 6156 import { hilog } from '@kit.PerformanceAnalysisKit'; 6157 6158 let parcel = new rpc.MessageParcel(); 6159 let result = parcel.getRawDataCapacity(); 6160 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6161 ``` 6162 6163### writeRawData<sup>8+</sup> 6164 6165writeRawData(rawData: number[], size: number): boolean 6166 6167Writes raw data to this **MessageParcel** object. 6168 6169**System capability**: SystemCapability.Communication.IPC.Core 6170 6171**Parameters** 6172 6173| Name | Type | Mandatory| Description | 6174| ------- | -------- | ---- | ---------------------------------- | 6175| rawData | number[] | Yes | Raw data to write. | 6176| size | number | Yes | Size of the raw data, in bytes.| 6177 6178**Return value** 6179 6180| Type | Description | 6181| ------- | -------------------------------- | 6182| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6183 6184**Example** 6185 6186 ```ts 6187 import { hilog } from '@kit.PerformanceAnalysisKit'; 6188 6189 let parcel = new rpc.MessageParcel(); 6190 let arr = [1, 2, 3, 4, 5]; 6191 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6192 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6193 ``` 6194 6195### readRawData<sup>8+</sup> 6196 6197readRawData(size: number): number[] 6198 6199Reads raw data from this **MessageParcel** object. 6200 6201**System capability**: SystemCapability.Communication.IPC.Core 6202 6203**Parameters** 6204 6205| Name| Type | Mandatory| Description | 6206| ------ | ------ | ---- | ------------------------ | 6207| size | number | Yes | Size of the raw data to read.| 6208 6209**Return value** 6210 6211| Type | Description | 6212| -------- | ------------------------------ | 6213| number[] | Raw data obtained, in bytes.| 6214 6215**Example** 6216 6217 ```ts 6218 import { hilog } from '@kit.PerformanceAnalysisKit'; 6219 6220 let parcel = new rpc.MessageParcel(); 6221 let arr = [1, 2, 3, 4, 5]; 6222 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6223 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6224 let result = parcel.readRawData(5); 6225 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6226 ``` 6227 6228## Parcelable<sup>9+</sup> 6229 6230Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 6231 6232### marshalling 6233 6234marshalling(dataOut: MessageSequence): boolean 6235 6236Marshals this **Parcelable** object into a **MessageSequence** object. 6237 6238**System capability**: SystemCapability.Communication.IPC.Core 6239 6240**Parameters** 6241 6242| Name | Type | Mandatory| Description | 6243| ------- | --------------- | ---- | ------------------------------------------- | 6244| dataOut |[MessageSequence](#messagesequence9)| Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 6245 6246**Return value** 6247 6248| Type | Description | 6249| ------- | -------------------------------- | 6250| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6251 6252**Example** 6253 6254 ```ts 6255 import { hilog } from '@kit.PerformanceAnalysisKit'; 6256 6257 class MyParcelable implements rpc.Parcelable { 6258 num: number = 0; 6259 str: string = ''; 6260 constructor(num: number, str: string) { 6261 this.num = num; 6262 this.str = str; 6263 } 6264 marshalling(messageSequence: rpc.MessageSequence): boolean { 6265 messageSequence.writeInt(this.num); 6266 messageSequence.writeString(this.str); 6267 return true; 6268 } 6269 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6270 this.num = messageSequence.readInt(); 6271 this.str = messageSequence.readString(); 6272 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6273 return true; 6274 } 6275 } 6276 let parcelable = new MyParcelable(1, "aaa"); 6277 let data = rpc.MessageSequence.create(); 6278 data.writeParcelable(parcelable); 6279 let ret = new MyParcelable(0, ""); 6280 data.readParcelable(ret); 6281 ``` 6282 6283### unmarshalling 6284 6285unmarshalling(dataIn: MessageSequence): boolean 6286 6287Unmarshals this **Parcelable** object from a **MessageSequence** object. 6288 6289**System capability**: SystemCapability.Communication.IPC.Core 6290 6291**Parameters** 6292 6293| Name| Type | Mandatory| Description | 6294| ------ | --------------- | ---- | ----------------------------------------------- | 6295| dataIn | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 6296 6297**Return value** 6298 6299| Type | Description | 6300| ------- | ---------------------------------------- | 6301| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6302 6303**Example** 6304 6305 ```ts 6306 import { hilog } from '@kit.PerformanceAnalysisKit'; 6307 6308 class MyParcelable implements rpc.Parcelable { 6309 num: number = 0; 6310 str: string = ''; 6311 constructor(num: number, str: string) { 6312 this.num = num; 6313 this.str = str; 6314 } 6315 marshalling(messageSequence: rpc.MessageSequence): boolean { 6316 messageSequence.writeInt(this.num); 6317 messageSequence.writeString(this.str); 6318 return true; 6319 } 6320 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6321 this.num = messageSequence.readInt(); 6322 this.str = messageSequence.readString(); 6323 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6324 return true; 6325 } 6326 } 6327 let parcelable = new MyParcelable(1, "aaa"); 6328 let data = rpc.MessageSequence.create(); 6329 data.writeParcelable(parcelable); 6330 let ret = new MyParcelable(0, ""); 6331 data.readParcelable(ret); 6332 ``` 6333 6334## Sequenceable<sup>(deprecated)</sup> 6335 6336>**NOTE**<br>This API is deprecated since API version 9. Use [Parcelable](#parcelable9) instead. 6337 6338Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 6339 6340### marshalling 6341 6342marshalling(dataOut: MessageParcel): boolean 6343 6344Marshals the sequenceable object into a **MessageParcel** object. 6345 6346**System capability**: SystemCapability.Communication.IPC.Core 6347 6348**Parameters** 6349 6350| Name | Type | Mandatory| Description | 6351| ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6352| dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 6353 6354**Return value** 6355 6356| Type | Description | 6357| ------- | -------------------------------- | 6358| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6359 6360**Example** 6361 6362 ```ts 6363 import { hilog } from '@kit.PerformanceAnalysisKit'; 6364 6365 class MySequenceable implements rpc.Sequenceable { 6366 num: number = 0; 6367 str: string = ''; 6368 constructor(num: number, str: string) { 6369 this.num = num; 6370 this.str = str; 6371 } 6372 marshalling(messageParcel: rpc.MessageParcel): boolean { 6373 messageParcel.writeInt(this.num); 6374 messageParcel.writeString(this.str); 6375 return true; 6376 } 6377 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6378 this.num = messageParcel.readInt(); 6379 this.str = messageParcel.readString(); 6380 return true; 6381 } 6382 } 6383 let sequenceable = new MySequenceable(1, "aaa"); 6384 let data = rpc.MessageParcel.create(); 6385 let result = data.writeSequenceable(sequenceable); 6386 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6387 let ret = new MySequenceable(0, ""); 6388 let result2 = data.readSequenceable(ret); 6389 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6390 ``` 6391 6392### unmarshalling 6393 6394unmarshalling(dataIn: MessageParcel): boolean 6395 6396Unmarshals this sequenceable object from a **MessageParcel** object. 6397 6398**System capability**: SystemCapability.Communication.IPC.Core 6399 6400**Parameters** 6401 6402| Name| Type | Mandatory| Description | 6403| ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6404| dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 6405 6406**Return value** 6407 6408| Type | Description | 6409| ------- | ---------------------------------------- | 6410| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6411 6412**Example** 6413 6414 ```ts 6415 import { hilog } from '@kit.PerformanceAnalysisKit'; 6416 6417 class MySequenceable implements rpc.Sequenceable { 6418 num: number = 0; 6419 str: string = ''; 6420 constructor(num: number, str: string) { 6421 this.num = num; 6422 this.str = str; 6423 } 6424 marshalling(messageParcel: rpc.MessageParcel): boolean { 6425 messageParcel.writeInt(this.num); 6426 messageParcel.writeString(this.str); 6427 return true; 6428 } 6429 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6430 this.num = messageParcel.readInt(); 6431 this.str = messageParcel.readString(); 6432 return true; 6433 } 6434 } 6435 let sequenceable = new MySequenceable(1, "aaa"); 6436 let data = rpc.MessageParcel.create(); 6437 let result = data.writeSequenceable(sequenceable); 6438 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6439 let ret = new MySequenceable(0, ""); 6440 let result2 = data.readSequenceable(ret); 6441 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6442 ``` 6443 6444## IRemoteBroker 6445 6446Represents the holder of a remote proxy object. 6447 6448### asObject 6449 6450asObject(): IRemoteObject 6451 6452Obtains a proxy or remote object. This API must be implemented by its derived classes. 6453 6454**System capability**: SystemCapability.Communication.IPC.Core 6455 6456**Return value** 6457 6458| Type | Description | 6459| ----- | ----- | 6460| [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.| 6461 6462**Example** 6463 6464 ```ts 6465 class TestAbility extends rpc.RemoteObject { 6466 asObject() { 6467 return this; 6468 } 6469 } 6470 let remoteObject = new TestAbility("testObject").asObject(); 6471 ``` 6472 6473**Example** 6474 6475 ```ts 6476 // If the FA model is used, import featureAbility from @kit.AbilityKit. 6477 // import { featureAbility } from '@kit.AbilityKit'; 6478 import { Want, common } from '@kit.AbilityKit'; 6479 import { hilog } from '@kit.PerformanceAnalysisKit'; 6480 6481 let proxy: rpc.IRemoteObject | undefined; 6482 let connect: common.ConnectOptions = { 6483 onConnect: (elementName, remoteProxy) => { 6484 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6485 proxy = remoteProxy; 6486 }, 6487 onDisconnect: (elementName) => { 6488 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6489 }, 6490 onFailed: () => { 6491 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6492 } 6493 }; 6494 let want: Want = { 6495 bundleName: "com.ohos.server", 6496 abilityName: "com.ohos.server.EntryAbility", 6497 }; 6498 6499 // Use this method to connect to the ability for the FA model. 6500 // FA.connectAbility(want,connect); 6501 6502 // Save the connection ID, which will be used for the subsequent service disconnection. 6503 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6504 // Save the connection ID, which will be used for the subsequent service disconnection. 6505 let connectionId = context.connectServiceExtensionAbility(want, connect); 6506 ``` 6507 6508The 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. 6509 6510 ```ts 6511 class TestProxy { 6512 remote: rpc.IRemoteObject; 6513 constructor(remote: rpc.IRemoteObject) { 6514 this.remote = remote; 6515 } 6516 asObject() { 6517 return this.remote; 6518 } 6519 } 6520 if (proxy != undefined) { 6521 let iRemoteObject = new TestProxy(proxy).asObject(); 6522 } 6523 ``` 6524 6525## DeathRecipient 6526 6527Subscribes 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. 6528 6529### onRemoteDied 6530 6531onRemoteDied(): void 6532 6533Called to perform subsequent operations when a death notification of the remote object is received. 6534 6535**System capability**: SystemCapability.Communication.IPC.Core 6536 6537**Example** 6538 6539 ```ts 6540 import { hilog } from '@kit.PerformanceAnalysisKit'; 6541 6542 class MyDeathRecipient implements rpc.DeathRecipient { 6543 onRemoteDied() { 6544 hilog.info(0x0000, 'testTag', 'server died'); 6545 } 6546 } 6547 ``` 6548 6549## RequestResult<sup>9+</sup> 6550 6551Defines the response to the request. 6552 6553**System capability**: SystemCapability.Communication.IPC.Core 6554 6555| Name | Type | Readable| Writable| Description | 6556| ------- | --------------- | ---- | ---- |-------------------------------------- | 6557| errCode | number | Yes | No | Error code. | 6558| code | number | Yes | No | Message code. | 6559| data | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object sent to the remote process.| 6560| reply | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object returned by the remote process. | 6561 6562## SendRequestResult<sup>(deprecated)</sup> 6563 6564>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [RequestResult](#requestresult9) instead. 6565 6566Defines the response to the request. 6567 6568**System capability**: SystemCapability.Communication.IPC.Core 6569 6570| Name | Type | Readable| Writable| Description | 6571| ------- | ------------- | ---- | ---- | ----------------------------------- | 6572| errCode | number | Yes | No | Error code. | 6573| code | number | Yes | No | Message code. | 6574| data | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object sent to the remote process.| 6575| reply | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object returned by the remote process. | 6576 6577## IRemoteObject 6578 6579Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 6580 6581### getLocalInterface<sup>9+</sup> 6582 6583getLocalInterface(descriptor: string): IRemoteBroker 6584 6585Obtains the string of the interface descriptor. 6586 6587**System capability**: SystemCapability.Communication.IPC.Core 6588 6589**Parameters** 6590 6591| Name | Type | Mandatory| Description | 6592| ---------- | ------ | ---- | -------------------- | 6593| descriptor | string | Yes | Interface descriptor.| 6594 6595**Return value** 6596 6597| Type | Description | 6598| ------------- | --------------------------------------------- | 6599| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6600 6601**Error codes** 6602 6603For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6604 6605| ID| Error Message| 6606| -------- | -------- | 6607| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 6608 6609### queryLocalInterface<sup>(deprecated)</sup> 6610 6611>**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9) instead. 6612 6613queryLocalInterface(descriptor: string): IRemoteBroker 6614 6615Queries the string of the interface descriptor. 6616 6617**System capability**: SystemCapability.Communication.IPC.Core 6618 6619**Parameters** 6620 6621| Name | Type | Mandatory| Description | 6622| ---------- | ------ | ---- | -------------------- | 6623| descriptor | string | Yes | Interface descriptor.| 6624 6625**Return value** 6626 6627| Type | Description | 6628| ------------- | --------------------------------------------- | 6629| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6630 6631### sendRequest<sup>(deprecated)</sup> 6632 6633>**NOTE**<br>This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9) instead. 6634 6635sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6636 6637Sends 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. 6638 6639**System capability**: SystemCapability.Communication.IPC.Core 6640 6641**Parameters** 6642 6643| Name | Type | Mandatory| Description | 6644| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6645| 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.| 6646| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6647| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6648| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6649 6650**Return value** 6651 6652| Type | Description | 6653| ------- | -------------------------------- | 6654| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6655 6656### sendMessageRequest<sup>9+</sup> 6657 6658sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6659 6660Sends 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. 6661 6662**System capability**: SystemCapability.Communication.IPC.Core 6663 6664**Parameters** 6665 6666| Name | Type | Mandatory| Description | 6667| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6668| 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.| 6669| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6670| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6671| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6672 6673**Return value** 6674 6675| Type | Description | 6676| ---------------------------- | ----------------------------------------- | 6677| Promise<[RequestResult](#requestresult9)> | Promise used to return the **requestResult** object.| 6678 6679**Error codes** 6680 6681For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6682 6683| ID| Error Message| 6684| -------- | -------- | 6685| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 6686 6687### sendRequest<sup>(deprecated)</sup> 6688 6689>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9) instead. 6690 6691sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6692 6693Sends 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. 6694 6695**System capability**: SystemCapability.Communication.IPC.Core 6696 6697**Parameters** 6698 6699| Name | Type | Mandatory| Description | 6700| ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6701| 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.| 6702| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6703| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6704| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6705 6706**Return value** 6707 6708| Type | Description | 6709| ------------------------------------------------------------ | --------------------------------------------- | 6710| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 6711 6712### sendMessageRequest<sup>9+</sup> 6713 6714sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6715 6716Sends 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. 6717 6718**System capability**: SystemCapability.Communication.IPC.Core 6719 6720**Parameters** 6721 6722| Name | Type | Mandatory| Description | 6723| -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6724| 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.| 6725| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6726| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6727| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6728| callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 6729 6730**Error codes** 6731 6732For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6733 6734| ID| Error Message| 6735| -------- | -------- | 6736| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 6737 6738### sendRequest<sup>(deprecated)</sup> 6739 6740>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-1) instead. 6741 6742sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6743 6744Sends 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. 6745 6746**System capability**: SystemCapability.Communication.IPC.Core 6747 6748**Parameters** 6749 6750| Name | Type | Mandatory| Description | 6751| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6752| 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.| 6753| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6754| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6755| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6756| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 6757 6758### registerDeathRecipient<sup>9+</sup> 6759 6760registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6761 6762Registers a callback for receiving death notifications of the remote object. This callback will be called if the remote object process matching the **RemoteProxy** object is killed. 6763 6764**System capability**: SystemCapability.Communication.IPC.Core 6765 6766**Parameters** 6767 6768| Name | Type | Mandatory| Description | 6769| --------- | --------------------------------- | ---- | -------------- | 6770| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6771| flags | number | Yes | Flag of the death notification.| 6772 6773**Error codes** 6774 6775For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6776 6777| ID| Error Message| 6778| -------- | -------- | 6779| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 6780| 1900008 | The proxy or remote object is invalid. | 6781 6782### addDeathrecipient<sup>(deprecated)</sup> 6783 6784>**NOTE**<br>This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9) instead. 6785 6786addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6787 6788Adds 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. 6789 6790**System capability**: SystemCapability.Communication.IPC.Core 6791 6792**Parameters** 6793 6794| Name | Type | Mandatory| Description | 6795| --------- | --------------------------------- | ---- | -------------- | 6796| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add.| 6797| flags | number | Yes | Flag of the death notification.| 6798 6799**Return value** 6800 6801| Type | Description | 6802| ------- | ---------------------------------------- | 6803| boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6804 6805### unregisterDeathRecipient<sup>9+</sup> 6806 6807unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6808 6809Unregisters the callback used to receive death notifications of the remote object. 6810 6811**System capability**: SystemCapability.Communication.IPC.Core 6812 6813**Parameters** 6814 6815| Name | Type | Mandatory| Description | 6816| --------- | --------------------------------- | ---- | -------------- | 6817| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6818| flags | number | Yes | Flag of the death notification.| 6819 6820**Error codes** 6821 6822For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6823 6824| ID| Error Message| 6825| -------- | -------- | 6826| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 6827| 1900008 | The proxy or remote object is invalid. | 6828 6829### removeDeathRecipient<sup>(deprecated)</sup> 6830 6831>**NOTE**<br>This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9) instead. 6832 6833removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6834 6835Removes the callback used to receive death notifications of the remote object. 6836 6837**System capability**: SystemCapability.Communication.IPC.Core 6838 6839**Parameters** 6840 6841| Name | Type | Mandatory| Description | 6842| --------- | --------------------------------- | ---- | -------------- | 6843| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove.| 6844| flags | number | Yes | Flag of the death notification.| 6845 6846**Return value** 6847 6848| Type | Description | 6849| ------- | -----------------------------------------| 6850| boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6851 6852### getDescriptor<sup>9+</sup> 6853 6854getDescriptor(): string 6855 6856Obtains the interface descriptor (which is a string) of this object. 6857 6858**System capability**: SystemCapability.Communication.IPC.Core 6859 6860**Return value** 6861 6862| Type | Description | 6863| ------ | ---------------- | 6864| string | Interface descriptor obtained.| 6865 6866**Error codes** 6867 6868For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6869 6870| ID| Error Message| 6871| -------- | -------- | 6872| 1900008 | The proxy or remote object is invalid. | 6873 6874### getInterfaceDescriptor<sup>(deprecated)</sup> 6875 6876>**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9) instead. 6877 6878getInterfaceDescriptor(): string 6879 6880Obtains the interface descriptor (which is a string) of this object. 6881 6882**System capability**: SystemCapability.Communication.IPC.Core 6883 6884**Return value** 6885 6886| Type | Description | 6887| ------ | ---------------- | 6888| string | Interface descriptor obtained.| 6889 6890### isObjectDead 6891 6892isObjectDead(): boolean 6893 6894Checks whether this object is dead. 6895 6896**System capability**: SystemCapability.Communication.IPC.Core 6897 6898**Return value** 6899 6900| Type | Description | 6901| ------- | ---------------------------------- | 6902| boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6903 6904## RemoteProxy 6905 6906Provides APIs to implement **IRemoteObject**. 6907 6908**System capability**: SystemCapability.Communication.IPC.Core 6909 6910| Name | Value | Description | 6911| --------------------- | ----------------------- | --------------------------------- | 6912| PING_TRANSACTION | 1599098439 (0x5f504e47) | Internal instruction code used to test whether the IPC service is normal.| 6913| DUMP_TRANSACTION | 1598311760 (0x5f444d50) | Internal instruction code used to obtain the internal status of the binder. | 6914| INTERFACE_TRANSACTION | 1598968902 (0x5f4e5446) | Internal instruction code used to obtain the remote interface token. | 6915| MIN_TRANSACTION_ID | 1 (0x00000001) | Minimum valid instruction code. | 6916| MAX_TRANSACTION_ID | 16777215 (0x00FFFFFF) | Maximum valid instruction code. | 6917 6918### sendRequest<sup>(deprecated)</sup> 6919 6920>**NOTE**<br>This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 6921 6922sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6923 6924Sends 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. 6925 6926**System capability**: SystemCapability.Communication.IPC.Core 6927 6928**Parameters** 6929 6930| Name | Type | Mandatory| Description | 6931| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6932| 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.| 6933| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6934| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6935| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6936 6937**Return value** 6938 6939| Type | Description | 6940| ------- | ---------------------------------| 6941| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6942 6943**Example** 6944 6945 ```ts 6946 // If the FA model is used, import featureAbility from @kit.AbilityKit. 6947 // import { featureAbility } from '@kit.AbilityKit'; 6948 import { Want, common } from '@kit.AbilityKit'; 6949 import { hilog } from '@kit.PerformanceAnalysisKit'; 6950 6951 let proxy: rpc.IRemoteObject | undefined; 6952 let connect: common.ConnectOptions = { 6953 onConnect: (elementName, remoteProxy) => { 6954 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6955 proxy = remoteProxy; 6956 }, 6957 onDisconnect: (elementName) => { 6958 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6959 }, 6960 onFailed: () => { 6961 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6962 } 6963 }; 6964 let want: Want = { 6965 bundleName: "com.ohos.server", 6966 abilityName: "com.ohos.server.EntryAbility", 6967 }; 6968 6969 // Use this method to connect to the ability for the FA model. 6970 // FA.connectAbility(want,connect); 6971 6972 // Save the connection ID, which will be used for the subsequent service disconnection. 6973 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6974 // Save the connection ID, which will be used for the subsequent service disconnection. 6975 let connectionId = context.connectServiceExtensionAbility(want, connect); 6976 ``` 6977 6978The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 6979 6980 ```ts 6981 import { hilog } from '@kit.PerformanceAnalysisKit'; 6982 6983 let option = new rpc.MessageOption(); 6984 let data = rpc.MessageParcel.create(); 6985 let reply = rpc.MessageParcel.create(); 6986 data.writeInt(1); 6987 data.writeString("hello"); 6988 if (proxy != undefined) { 6989 let ret: boolean = proxy.sendRequest(1, data, reply, option); 6990 if (ret) { 6991 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 6992 let msg = reply.readString(); 6993 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 6994 } else { 6995 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 6996 } 6997 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 6998 data.reclaim(); 6999 reply.reclaim(); 7000 } 7001 ``` 7002 7003### sendMessageRequest<sup>9+</sup> 7004 7005sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7006 7007Sends 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. 7008 7009**System capability**: SystemCapability.Communication.IPC.Core 7010 7011**Parameters** 7012 7013| Name | Type | Mandatory| Description | 7014| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7015| 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.| 7016| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7017| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7018| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7019 7020**Return value** 7021 7022| Type | Description | 7023| ---------------------------- | ----------------------------------------- | 7024| Promise<[RequestResult](#requestresult9)> | Promise used to return a **requestResult** instance.| 7025 7026 7027**Error codes** 7028 7029For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7030 7031| ID| Error Message| 7032| -------- | -------- | 7033| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 7034 7035**Example** 7036 7037 ```ts 7038 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7039 // import { featureAbility } from '@kit.AbilityKit'; 7040 import { Want, common } from '@kit.AbilityKit'; 7041 import { hilog } from '@kit.PerformanceAnalysisKit'; 7042 7043 let proxy: rpc.IRemoteObject | undefined; 7044 let connect: common.ConnectOptions = { 7045 onConnect: (elementName, remoteProxy) => { 7046 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7047 proxy = remoteProxy; 7048 }, 7049 onDisconnect: (elementName) => { 7050 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7051 }, 7052 onFailed: () => { 7053 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7054 } 7055 }; 7056 let want: Want = { 7057 bundleName: "com.ohos.server", 7058 abilityName: "com.ohos.server.EntryAbility", 7059 }; 7060 7061 // Use this method to connect to the ability for the FA model. 7062 // FA.connectAbility(want,connect); 7063 7064 // Save the connection ID, which will be used for the subsequent service disconnection. 7065 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7066 // Save the connection ID, which will be used for the subsequent service disconnection. 7067 let connectionId = context.connectServiceExtensionAbility(want, connect); 7068 ``` 7069 7070The 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. 7071 7072 ```ts 7073 import { hilog } from '@kit.PerformanceAnalysisKit'; 7074 7075 let option = new rpc.MessageOption(); 7076 let data = rpc.MessageSequence.create(); 7077 let reply = rpc.MessageSequence.create(); 7078 data.writeInt(1); 7079 data.writeString("hello"); 7080 if (proxy != undefined) { 7081 proxy.sendMessageRequest(1, data, reply, option) 7082 .then((result: rpc.RequestResult) => { 7083 if (result.errCode === 0) { 7084 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7085 let num = result.reply.readInt(); 7086 let msg = result.reply.readString(); 7087 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7088 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7089 } else { 7090 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7091 } 7092 }).catch((e: Error) => { 7093 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7094 }).finally (() => { 7095 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7096 data.reclaim(); 7097 reply.reclaim(); 7098 }); 7099 } 7100 ``` 7101 7102### sendRequest<sup>(deprecated)</sup> 7103 7104>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 7105 7106sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7107 7108Sends 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. 7109 7110**System capability**: SystemCapability.Communication.IPC.Core 7111 7112**Parameters** 7113 7114| Name | Type | Mandatory| Description | 7115| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7116| 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.| 7117| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7118| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7119| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7120 7121**Return value** 7122 7123| Type | Description | 7124| ------------------------------------------------------------ | --------------------------------------------- | 7125| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 7126 7127**Example** 7128 7129 ```ts 7130 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7131 // import { featureAbility } from '@kit.AbilityKit'; 7132 import { Want, common } from '@kit.AbilityKit'; 7133 import { hilog } from '@kit.PerformanceAnalysisKit'; 7134 7135 let proxy: rpc.IRemoteObject | undefined; 7136 let connect: common.ConnectOptions = { 7137 onConnect: (elementName, remoteProxy) => { 7138 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7139 proxy = remoteProxy; 7140 }, 7141 onDisconnect: (elementName) => { 7142 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7143 }, 7144 onFailed: () => { 7145 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7146 } 7147 }; 7148 let want: Want = { 7149 bundleName: "com.ohos.server", 7150 abilityName: "com.ohos.server.EntryAbility", 7151 }; 7152 7153 // Use this method to connect to the ability for the FA model. 7154 // FA.connectAbility(want,connect); 7155 7156 // Save the connection ID, which will be used for the subsequent service disconnection. 7157 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7158 // Save the connection ID, which will be used for the subsequent service disconnection. 7159 let connectionId = context.connectServiceExtensionAbility(want, connect); 7160 ``` 7161 7162The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7163 7164 ```ts 7165 import { hilog } from '@kit.PerformanceAnalysisKit'; 7166 7167 let option = new rpc.MessageOption(); 7168 let data = rpc.MessageParcel.create(); 7169 let reply = rpc.MessageParcel.create(); 7170 data.writeInt(1); 7171 data.writeString("hello"); 7172 if (proxy != undefined) { 7173 let a = proxy.sendRequest(1, data, reply, option) as Object; 7174 let b = a as Promise<rpc.SendRequestResult>; 7175 b.then((result: rpc.SendRequestResult) => { 7176 if (result.errCode === 0) { 7177 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7178 let num = result.reply.readInt(); 7179 let msg = result.reply.readString(); 7180 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7181 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7182 } else { 7183 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7184 } 7185 }).catch((e: Error) => { 7186 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7187 }).finally (() => { 7188 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7189 data.reclaim(); 7190 reply.reclaim(); 7191 }); 7192 } 7193 ``` 7194 7195### sendMessageRequest<sup>9+</sup> 7196 7197sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7198 7199Sends a **MessageSequence** message to the remote process in synchronous or asynchronous mode. If asynchronous mode is set in **options**, a callback will be called immediately, and the reply message is empty. The specific reply needs to be obtained from the callback on the service side. If synchronous mode is set in **options**, a callback will be invoked at certain time after the response to **RequestResult** is returned, and the reply contains the returned information. 7200 7201**System capability**: SystemCapability.Communication.IPC.Core 7202 7203**Parameters** 7204 7205| Name | Type | Mandatory| Description | 7206| -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7207| 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.| 7208| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7209| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7210| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7211| callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 7212 7213 7214**Error codes** 7215 7216For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7217 7218| ID| Error Message| 7219| -------- | -------- | 7220| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 7221 7222**Example** 7223 7224 ```ts 7225 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7226 // import { featureAbility } from '@kit.AbilityKit'; 7227 import { Want, common } from '@kit.AbilityKit'; 7228 import { hilog } from '@kit.PerformanceAnalysisKit'; 7229 import { BusinessError } from '@kit.BasicServicesKit'; 7230 7231 let proxy: rpc.IRemoteObject | undefined; 7232 let connect: common.ConnectOptions = { 7233 onConnect: (elementName, remoteProxy) => { 7234 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7235 proxy = remoteProxy; 7236 }, 7237 onDisconnect: (elementName) => { 7238 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7239 }, 7240 onFailed: () => { 7241 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7242 } 7243 }; 7244 let want: Want = { 7245 bundleName: "com.ohos.server", 7246 abilityName: "com.ohos.server.EntryAbility", 7247 }; 7248 function sendMessageRequestCallback(err: BusinessError, result: rpc.RequestResult) { 7249 if (result.errCode === 0) { 7250 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7251 let num = result.reply.readInt(); 7252 let msg = result.reply.readString(); 7253 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7254 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7255 } else { 7256 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7257 } 7258 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7259 result.data.reclaim(); 7260 result.reply.reclaim(); 7261} 7262 7263 // Use this method to connect to the ability for the FA model. 7264 // FA.connectAbility(want,connect); 7265 7266 // Save the connection ID, which will be used for the subsequent service disconnection. 7267 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7268 // Save the connection ID, which will be used for the subsequent service disconnection. 7269 let connectionId = context.connectServiceExtensionAbility(want, connect); 7270 ``` 7271 7272The 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. 7273 7274 ```ts 7275 import { hilog } from '@kit.PerformanceAnalysisKit'; 7276 import { BusinessError } from '@kit.BasicServicesKit'; 7277 7278 let option = new rpc.MessageOption(); 7279 let data = rpc.MessageSequence.create(); 7280 let reply = rpc.MessageSequence.create(); 7281 data.writeInt(1); 7282 data.writeString("hello"); 7283 if (proxy != undefined) { 7284 try { 7285 proxy.sendMessageRequest(1, data, reply, option, sendMessageRequestCallback); 7286 } catch (error) { 7287 let e: BusinessError = error as BusinessError; 7288 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorCode ' + e.code); 7289 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorMessage ' + e.message); 7290 } 7291 } 7292 ``` 7293 7294### sendRequest<sup>(deprecated)</sup> 7295 7296>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-3) instead. 7297 7298sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7299 7300Sends 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. 7301 7302**System capability**: SystemCapability.Communication.IPC.Core 7303 7304**Parameters** 7305 7306| Name | Type | Mandatory| Description | 7307| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7308| 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.| 7309| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7310| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7311| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7312| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 7313 7314**Example** 7315 7316 ```ts 7317 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7318 // import { featureAbility } from '@kit.AbilityKit'; 7319 import { Want, common } from '@kit.AbilityKit'; 7320 import { hilog } from '@kit.PerformanceAnalysisKit'; 7321 import { BusinessError } from '@kit.BasicServicesKit'; 7322 7323 let proxy: rpc.IRemoteObject | undefined; 7324 let connect: common.ConnectOptions = { 7325 onConnect: (elementName, remoteProxy) => { 7326 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7327 proxy = remoteProxy; 7328 }, 7329 onDisconnect: (elementName) => { 7330 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7331 }, 7332 onFailed: () => { 7333 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7334 } 7335 }; 7336 let want: Want = { 7337 bundleName: "com.ohos.server", 7338 abilityName: "com.ohos.server.EntryAbility", 7339 }; 7340 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 7341 if (result.errCode === 0) { 7342 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7343 let num = result.reply.readInt(); 7344 let msg = result.reply.readString(); 7345 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7346 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7347 } else { 7348 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7349 } 7350 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7351 result.data.reclaim(); 7352 result.reply.reclaim(); 7353} 7354 7355 // Use this method to connect to the ability for the FA model. 7356 // FA.connectAbility(want,connect); 7357 7358 // Save the connection ID, which will be used for the subsequent service disconnection. 7359 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7360 // Save the connection ID, which will be used for the subsequent service disconnection. 7361 let connectionId = context.connectServiceExtensionAbility(want, connect); 7362 ``` 7363 7364The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7365 7366 ```ts 7367 let option = new rpc.MessageOption(); 7368 let data = rpc.MessageParcel.create(); 7369 let reply = rpc.MessageParcel.create(); 7370 data.writeInt(1); 7371 data.writeString("hello"); 7372 if (proxy != undefined) { 7373 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 7374 } 7375 ``` 7376 7377### getLocalInterface<sup>9+</sup> 7378 7379getLocalInterface(interface: string): IRemoteBroker 7380 7381Obtains the **LocalInterface** object of an interface token. 7382 7383**System capability**: SystemCapability.Communication.IPC.Core 7384 7385**Parameters** 7386 7387| Name | Type | Mandatory| Description | 7388| --------- | ------ | ---- | ---------------------- | 7389| interface | string | Yes | Interface descriptor.| 7390 7391**Return value** 7392 7393| Type | Description | 7394| ------------------------------- | ------------------------------------------ | 7395| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7396 7397**Error codes** 7398 7399For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7400 7401| ID| Error Message| 7402| -------- | -------- | 7403| 401 | check param failed | 7404| 1900006 | Operation allowed only for the remote object. | 7405 7406**Example** 7407 7408 ```ts 7409 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7410 // import { featureAbility } from '@kit.AbilityKit'; 7411 import { Want, common } from '@kit.AbilityKit'; 7412 import { hilog } from '@kit.PerformanceAnalysisKit'; 7413 7414 let proxy: rpc.IRemoteObject | undefined; 7415 let connect: common.ConnectOptions = { 7416 onConnect: (elementName, remoteProxy) => { 7417 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7418 proxy = remoteProxy; 7419 }, 7420 onDisconnect: (elementName) => { 7421 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7422 }, 7423 onFailed: () => { 7424 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7425 } 7426 }; 7427 let want: Want = { 7428 bundleName: "com.ohos.server", 7429 abilityName: "com.ohos.server.EntryAbility", 7430 }; 7431 7432 // Use this method to connect to the ability for the FA model. 7433 // FA.connectAbility(want,connect); 7434 7435 // Save the connection ID, which will be used for the subsequent service disconnection. 7436 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7437 // Save the connection ID, which will be used for the subsequent service disconnection. 7438 let connectionId = context.connectServiceExtensionAbility(want, connect); 7439 ``` 7440 7441The 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. 7442 7443 ```ts 7444 import { hilog } from '@kit.PerformanceAnalysisKit'; 7445 import { BusinessError } from '@kit.BasicServicesKit'; 7446 7447 if (proxy != undefined) { 7448 try { 7449 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7450 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7451 } catch (error) { 7452 let e: BusinessError = error as BusinessError; 7453 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7454 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7455 } 7456 } 7457 ``` 7458 7459### queryLocalInterface<sup>(deprecated)</sup> 7460 7461>**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-1) instead. 7462 7463queryLocalInterface(interface: string): IRemoteBroker 7464 7465Obtains the **LocalInterface** object of an interface token. 7466 7467**System capability**: SystemCapability.Communication.IPC.Core 7468 7469**Parameters** 7470 7471| Name | Type | Mandatory| Description | 7472| --------- | ------ | ---- | ---------------------- | 7473| interface | string | Yes | Interface descriptor.| 7474 7475**Return value** 7476 7477| Type | Description | 7478| ------------------------------- | ------------------------------------------ | 7479| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7480 7481**Example** 7482 7483 ```ts 7484 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7485 // import { featureAbility } from '@kit.AbilityKit'; 7486 import { Want, common } from '@kit.AbilityKit'; 7487 import { hilog } from '@kit.PerformanceAnalysisKit'; 7488 7489 let proxy: rpc.IRemoteObject | undefined; 7490 let connect: common.ConnectOptions = { 7491 onConnect: (elementName, remoteProxy) => { 7492 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7493 proxy = remoteProxy; 7494 }, 7495 onDisconnect: (elementName) => { 7496 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7497 }, 7498 onFailed: () => { 7499 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7500 } 7501 }; 7502 let want: Want = { 7503 bundleName: "com.ohos.server", 7504 abilityName: "com.ohos.server.EntryAbility", 7505 }; 7506 7507 // Use this method to connect to the ability for the FA model. 7508 // FA.connectAbility(want,connect); 7509 7510 // Save the connection ID, which will be used for the subsequent service disconnection. 7511 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7512 // Save the connection ID, which will be used for the subsequent service disconnection. 7513 let connectionId = context.connectServiceExtensionAbility(want, connect); 7514 ``` 7515 7516The 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. 7517 7518 ```ts 7519 import { hilog } from '@kit.PerformanceAnalysisKit'; 7520 7521 if (proxy != undefined) { 7522 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7523 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7524 } 7525 ``` 7526 7527### registerDeathRecipient<sup>9+</sup> 7528 7529registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7530 7531Registers a callback for receiving death notifications of the remote object. This callback will be called if the remote object process matching the **RemoteProxy** object is killed. 7532 7533**System capability**: SystemCapability.Communication.IPC.Core 7534 7535**Parameters** 7536 7537| Name | Type | Mandatory| Description | 7538| --------- | --------------------------------- | ---- | -------------- | 7539| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 7540| flags | number | Yes | Flag of the death notification.| 7541 7542**Error codes** 7543 7544For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7545 7546| ID| Error Message| 7547| -------- | -------- | 7548| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 7549| 1900008 | The proxy or remote object is invalid. | 7550 7551**Example** 7552 7553 ```ts 7554 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7555 // import { featureAbility } from '@kit.AbilityKit'; 7556 import { Want, common } from '@kit.AbilityKit'; 7557 import { hilog } from '@kit.PerformanceAnalysisKit'; 7558 7559 let proxy: rpc.IRemoteObject | undefined; 7560 let connect: common.ConnectOptions = { 7561 onConnect: (elementName, remoteProxy) => { 7562 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7563 proxy = remoteProxy; 7564 }, 7565 onDisconnect: (elementName) => { 7566 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7567 }, 7568 onFailed: () => { 7569 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7570 } 7571 }; 7572 let want: Want = { 7573 bundleName: "com.ohos.server", 7574 abilityName: "com.ohos.server.EntryAbility", 7575 }; 7576 7577 // Use this method to connect to the ability for the FA model. 7578 // FA.connectAbility(want,connect); 7579 7580 // Save the connection ID, which will be used for the subsequent service disconnection. 7581 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7582 // Save the connection ID, which will be used for the subsequent service disconnection. 7583 let connectionId = context.connectServiceExtensionAbility(want, connect); 7584 ``` 7585 7586The 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. 7587 7588 ```ts 7589 import { hilog } from '@kit.PerformanceAnalysisKit'; 7590 import { BusinessError } from '@kit.BasicServicesKit'; 7591 7592 class MyDeathRecipient implements rpc.DeathRecipient { 7593 onRemoteDied() { 7594 hilog.info(0x0000, 'testTag', 'server died'); 7595 } 7596 } 7597 let deathRecipient = new MyDeathRecipient(); 7598 if (proxy != undefined) { 7599 try { 7600 proxy.registerDeathRecipient(deathRecipient, 0); 7601 } catch (error) { 7602 let e: BusinessError = error as BusinessError; 7603 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7604 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7605 } 7606 } 7607 ``` 7608 7609### addDeathRecipient<sup>(deprecated)</sup> 7610 7611>**NOTE**<br>This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9-1) instead. 7612 7613addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7614 7615Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 7616 7617**System capability**: SystemCapability.Communication.IPC.Core 7618 7619**Parameters** 7620 7621| Name | Type | Mandatory| Description | 7622| --------- | --------------------------------- | ---- | --------------------------------- | 7623| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 7624| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7625 7626**Return value** 7627 7628| Type | Description | 7629| ------- | ---------------------------------------- | 7630| boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 7631 7632**Example** 7633 7634 ```ts 7635 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7636 // import { featureAbility } from '@kit.AbilityKit'; 7637 import { Want, common } from '@kit.AbilityKit'; 7638 import { hilog } from '@kit.PerformanceAnalysisKit'; 7639 7640 let proxy: rpc.IRemoteObject | undefined; 7641 let connect: common.ConnectOptions = { 7642 onConnect: (elementName, remoteProxy) => { 7643 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7644 proxy = remoteProxy; 7645 }, 7646 onDisconnect: (elementName) => { 7647 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7648 }, 7649 onFailed: () => { 7650 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7651 } 7652 }; 7653 let want: Want = { 7654 bundleName: "com.ohos.server", 7655 abilityName: "com.ohos.server.EntryAbility", 7656 }; 7657 7658 // Use this method to connect to the ability for the FA model. 7659 // FA.connectAbility(want,connect); 7660 7661 // Save the connection ID, which will be used for the subsequent service disconnection. 7662 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7663 // Save the connection ID, which will be used for the subsequent service disconnection. 7664 let connectionId = context.connectServiceExtensionAbility(want, connect); 7665 ``` 7666 7667The 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. 7668 7669 ```ts 7670 import { hilog } from '@kit.PerformanceAnalysisKit'; 7671 7672 class MyDeathRecipient implements rpc.DeathRecipient { 7673 onRemoteDied() { 7674 hilog.info(0x0000, 'testTag', 'server died'); 7675 } 7676 } 7677 let deathRecipient = new MyDeathRecipient(); 7678 if (proxy != undefined) { 7679 proxy.addDeathRecipient(deathRecipient, 0); 7680 } 7681 ``` 7682 7683### unregisterDeathRecipient<sup>9+</sup> 7684 7685unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7686 7687Unregisters the callback used to receive death notifications of the remote object. 7688 7689**System capability**: SystemCapability.Communication.IPC.Core 7690 7691**Parameters** 7692 7693| Name | Type | Mandatory| Description | 7694| --------- | --------------------------------- | ---- | -------------- | 7695| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 7696| flags | number | Yes | Flag of the death notification.| 7697 7698**Error codes** 7699 7700For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7701 7702| ID| Error Message| 7703| -------- | -------- | 7704| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The callback used to receive remote object death notifications is empty. | 7705| 1900008 | The proxy or remote object is invalid. | 7706 7707**Example** 7708 7709 ```ts 7710 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7711 // import { featureAbility } from '@kit.AbilityKit'; 7712 import { Want, common } from '@kit.AbilityKit'; 7713 import { hilog } from '@kit.PerformanceAnalysisKit'; 7714 7715 let proxy: rpc.IRemoteObject | undefined; 7716 let connect: common.ConnectOptions = { 7717 onConnect: (elementName, remoteProxy) => { 7718 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7719 proxy = remoteProxy; 7720 }, 7721 onDisconnect: (elementName) => { 7722 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7723 }, 7724 onFailed: () => { 7725 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7726 } 7727 }; 7728 let want: Want = { 7729 bundleName: "com.ohos.server", 7730 abilityName: "com.ohos.server.EntryAbility", 7731 }; 7732 7733 // Use this method to connect to the ability for the FA model. 7734 // FA.connectAbility(want,connect); 7735 7736 // Save the connection ID, which will be used for the subsequent service disconnection. 7737 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7738 // Save the connection ID, which will be used for the subsequent service disconnection. 7739 let connectionId = context.connectServiceExtensionAbility(want, connect); 7740 ``` 7741 7742The 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. 7743 7744 ```ts 7745 import { hilog } from '@kit.PerformanceAnalysisKit'; 7746 import { BusinessError } from '@kit.BasicServicesKit'; 7747 7748 class MyDeathRecipient implements rpc.DeathRecipient { 7749 onRemoteDied() { 7750 hilog.info(0x0000, 'testTag', 'server died'); 7751 } 7752 } 7753 let deathRecipient = new MyDeathRecipient(); 7754 if (proxy != undefined) { 7755 try { 7756 proxy.registerDeathRecipient(deathRecipient, 0); 7757 proxy.unregisterDeathRecipient(deathRecipient, 0); 7758 } catch (error) { 7759 let e: BusinessError = error as BusinessError; 7760 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7761 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7762 } 7763 } 7764 ``` 7765 7766### removeDeathRecipient<sup>(deprecated)</sup> 7767 7768>**NOTE**<br>This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9-1) instead. 7769 7770removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7771 7772Removes the callback used to receive death notifications of the remote object. 7773 7774**System capability**: SystemCapability.Communication.IPC.Core 7775 7776**Parameters** 7777 7778| Name | Type | Mandatory| Description | 7779| --------- | --------------------------------- | ---- | --------------------------------- | 7780| recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 7781| flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7782 7783**Return value** 7784 7785| Type | Description | 7786| ------- | ---------------------------------------- | 7787| boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 7788 7789**Example** 7790 7791 ```ts 7792 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7793 // import { featureAbility } from '@kit.AbilityKit'; 7794 import { Want, common } from '@kit.AbilityKit'; 7795 import { hilog } from '@kit.PerformanceAnalysisKit'; 7796 7797 let proxy: rpc.IRemoteObject | undefined; 7798 let connect: common.ConnectOptions = { 7799 onConnect: (elementName, remoteProxy) => { 7800 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7801 proxy = remoteProxy; 7802 }, 7803 onDisconnect: (elementName) => { 7804 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7805 }, 7806 onFailed: () => { 7807 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7808 } 7809 }; 7810 let want: Want = { 7811 bundleName: "com.ohos.server", 7812 abilityName: "com.ohos.server.EntryAbility", 7813 }; 7814 7815 // Use this method to connect to the ability for the FA model. 7816 // FA.connectAbility(want,connect); 7817 7818 // Save the connection ID, which will be used for the subsequent service disconnection. 7819 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7820 // Save the connection ID, which will be used for the subsequent service disconnection. 7821 let connectionId = context.connectServiceExtensionAbility(want, connect); 7822 ``` 7823 7824The 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. 7825 7826 ```ts 7827 import { hilog } from '@kit.PerformanceAnalysisKit'; 7828 7829 class MyDeathRecipient implements rpc.DeathRecipient { 7830 onRemoteDied() { 7831 hilog.info(0x0000, 'testTag', 'server died'); 7832 } 7833 } 7834 let deathRecipient = new MyDeathRecipient(); 7835 if (proxy != undefined) { 7836 proxy.addDeathRecipient(deathRecipient, 0); 7837 proxy.removeDeathRecipient(deathRecipient, 0); 7838 } 7839 ``` 7840 7841### getDescriptor<sup>9+</sup> 7842 7843getDescriptor(): string 7844 7845Obtains the interface descriptor (which is a string) of this object. 7846 7847**System capability**: SystemCapability.Communication.IPC.Core 7848 7849**Return value** 7850 7851| Type | Description | 7852| ------ | ---------------- | 7853| string | Interface descriptor obtained.| 7854 7855**Error codes** 7856 7857For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7858 7859| ID| Error Message| 7860| -------- | -------- | 7861| 1900007 | communication failed. | 7862| 1900008 | The proxy or remote object is invalid. | 7863 7864**Example** 7865 7866 ```ts 7867 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7868 // import { featureAbility } from '@kit.AbilityKit'; 7869 import { Want, common } from '@kit.AbilityKit'; 7870 import { hilog } from '@kit.PerformanceAnalysisKit'; 7871 7872 let proxy: rpc.IRemoteObject | undefined; 7873 let connect: common.ConnectOptions = { 7874 onConnect: (elementName, remoteProxy) => { 7875 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7876 proxy = remoteProxy; 7877 }, 7878 onDisconnect: (elementName) => { 7879 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7880 }, 7881 onFailed: () => { 7882 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7883 } 7884 }; 7885 let want: Want = { 7886 bundleName: "com.ohos.server", 7887 abilityName: "com.ohos.server.EntryAbility", 7888 }; 7889 7890 // Use this method to connect to the ability for the FA model. 7891 // FA.connectAbility(want,connect); 7892 7893 // Save the connection ID, which will be used for the subsequent service disconnection. 7894 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7895 // Save the connection ID, which will be used for the subsequent service disconnection. 7896 let connectionId = context.connectServiceExtensionAbility(want, connect); 7897 ``` 7898The 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. 7899 7900 ```ts 7901 import { hilog } from '@kit.PerformanceAnalysisKit'; 7902 import { BusinessError } from '@kit.BasicServicesKit'; 7903 7904 if (proxy != undefined) { 7905 try { 7906 let descriptor: string = proxy.getDescriptor(); 7907 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7908 } catch (error) { 7909 let e: BusinessError = error as BusinessError; 7910 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7911 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7912 } 7913 } 7914 ``` 7915 7916### getInterfaceDescriptor<sup>(deprecated)</sup> 7917 7918>**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-1) instead. 7919 7920getInterfaceDescriptor(): string 7921 7922Obtains the interface descriptor of this proxy object. 7923 7924**System capability**: SystemCapability.Communication.IPC.Core 7925 7926**Return value** 7927 7928| Type | Description | 7929| ------ | ------------------ | 7930| string | Interface descriptor obtained.| 7931 7932**Example** 7933 7934 ```ts 7935 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7936 // import { featureAbility } from '@kit.AbilityKit'; 7937 import { Want, common } from '@kit.AbilityKit'; 7938 import { hilog } from '@kit.PerformanceAnalysisKit'; 7939 7940 let proxy: rpc.IRemoteObject | undefined; 7941 let connect: common.ConnectOptions = { 7942 onConnect: (elementName, remoteProxy) => { 7943 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7944 proxy = remoteProxy; 7945 }, 7946 onDisconnect: (elementName) => { 7947 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7948 }, 7949 onFailed: () => { 7950 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7951 } 7952 }; 7953 let want: Want = { 7954 bundleName: "com.ohos.server", 7955 abilityName: "com.ohos.server.EntryAbility", 7956 }; 7957 7958 // Use this method to connect to the ability for the FA model. 7959 // FA.connectAbility(want,connect); 7960 7961 // Save the connection ID, which will be used for the subsequent service disconnection. 7962 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7963 // Save the connection ID, which will be used for the subsequent service disconnection. 7964 let connectionId = context.connectServiceExtensionAbility(want, connect); 7965 ``` 7966 7967The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getInterfaceDescriptor()** of the proxy object is called to obtain the interface descriptor of the current proxy object. 7968 7969 ```ts 7970 import { hilog } from '@kit.PerformanceAnalysisKit'; 7971 7972 if (proxy != undefined) { 7973 let descriptor: string = proxy.getInterfaceDescriptor(); 7974 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7975 } 7976 ``` 7977 7978### isObjectDead 7979 7980isObjectDead(): boolean 7981 7982Checks whether the **RemoteObject** is dead. 7983 7984**System capability**: SystemCapability.Communication.IPC.Core 7985 7986**Return value** 7987 7988| Type | Description | 7989| ------- | ------------------------------------------------- | 7990| boolean | Returns **true** if **RemoteObject** is dead; returns **false** otherwise.| 7991 7992**Example** 7993 7994 ```ts 7995 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7996 // import { featureAbility } from '@kit.AbilityKit'; 7997 import { Want, common } from '@kit.AbilityKit'; 7998 import { hilog } from '@kit.PerformanceAnalysisKit'; 7999 8000 let proxy: rpc.IRemoteObject | undefined; 8001 let connect: common.ConnectOptions = { 8002 onConnect: (elementName, remoteProxy) => { 8003 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8004 proxy = remoteProxy; 8005 }, 8006 onDisconnect: (elementName) => { 8007 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8008 }, 8009 onFailed: () => { 8010 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8011 } 8012 }; 8013 let want: Want = { 8014 bundleName: "com.ohos.server", 8015 abilityName: "com.ohos.server.EntryAbility", 8016 }; 8017 8018 // Use this method to connect to the ability for the FA model. 8019 // FA.connectAbility(want,connect); 8020 8021 // Save the connection ID, which will be used for the subsequent service disconnection. 8022 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8023 // Save the connection ID, which will be used for the subsequent service disconnection. 8024 let connectionId = context.connectServiceExtensionAbility(want, connect); 8025 ``` 8026 8027The 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. 8028 8029 ```ts 8030 import { hilog } from '@kit.PerformanceAnalysisKit'; 8031 8032 if (proxy != undefined) { 8033 let isDead: boolean = proxy.isObjectDead(); 8034 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8035 } 8036 ``` 8037 8038## MessageOption 8039 8040Defines the options used to construct the **MessageOption** object. 8041 8042**System capability**: SystemCapability.Communication.IPC.Core 8043 8044| Name | Value | Description | 8045| ------------- | --------- | ----------------------------------------------------------- | 8046| TF_SYNC | 0 (0x00) | Synchronous call. | 8047| TF_ASYNC | 1 (0x01) | Asynchronous call. | 8048| TF_ACCEPT_FDS | 16 (0x10) | Indication to **sendMessageRequest<sup>9+</sup>** for returning the file descriptor.| 8049| TF_WAIT_TIME | 8 (0x8) | RPC wait time, in seconds. This parameter cannot be used in IPC. | 8050 8051### constructor<sup>9+</sup> 8052 8053constructor(async?: boolean) 8054 8055A constructor used to create a **MessageOption** object. 8056 8057**System capability**: SystemCapability.Communication.IPC.Core 8058 8059**Parameters** 8060 8061| Name| Type | Mandatory| Description | 8062| ------ | ------- | ---- | ------------------------------------------------------------ | 8063| async | boolean | No | Whether to execute the call asynchronously. The value **true** means to execute the call asynchronously; the value **false** means to execute the call synchronously. The default value is **synchronous**.| 8064 8065**Example** 8066 8067 ```ts 8068 class TestRemoteObject extends rpc.MessageOption { 8069 constructor(async: boolean) { 8070 super(async); 8071 } 8072 } 8073 ``` 8074 8075### constructor 8076 8077constructor(syncFlags?: number, waitTime?: number) 8078 8079A constructor used to create a **MessageOption** object. 8080 8081**System capability**: SystemCapability.Communication.IPC.Core 8082 8083**Parameters** 8084 8085| Name | Type | Mandatory| Description | 8086| --------- | ------ | ---- | --------------------------------------------- | 8087| syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 8088| waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 8089 8090**Example** 8091 8092 ```ts 8093 class TestRemoteObject extends rpc.MessageOption { 8094 constructor(syncFlags?: number,waitTime?: number) { 8095 super(syncFlags,waitTime); 8096 } 8097 } 8098 ``` 8099### isAsync<sup>9+</sup> 8100 8101isAsync(): boolean 8102 8103Checks whether **SendMessageRequest** is called synchronously or asynchronously. 8104 8105**System capability**: SystemCapability.Communication.IPC.Core 8106 8107**Return value** 8108 8109| Type | Description | 8110| ------- | ---------------------------------------- | 8111| boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 8112 8113**Example** 8114 8115 ```ts 8116 let option = new rpc.MessageOption(); 8117 option.isAsync(); 8118 ``` 8119 8120### setAsync<sup>9+</sup> 8121 8122setAsync(async: boolean): void 8123 8124Sets the calling flag in **SendMessageRequest**. 8125 8126**System capability**: SystemCapability.Communication.IPC.Core 8127 8128**Parameters** 8129 8130| Name| Type | Mandatory| Description | 8131| ------ | ------- | ---- | ------------------------------------------------- | 8132| async | boolean | Yes | Whether to execute the call asynchronously. The value **true** means to execute the call asynchronously; the value **false** means to execute the call synchronously.| 8133 8134**Example** 8135 8136 ```ts 8137 import { hilog } from '@kit.PerformanceAnalysisKit'; 8138 8139 let option = new rpc.MessageOption(); 8140 option.setAsync(true); 8141 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8142 ``` 8143 8144### getFlags 8145 8146getFlags(): number 8147 8148Obtains the call flag, which can be synchronous or asynchronous. 8149 8150**System capability**: SystemCapability.Communication.IPC.Core 8151 8152**Return value** 8153 8154| Type | Description | 8155| ------ | ------------------------------------ | 8156| number | Call mode obtained.| 8157 8158**Example** 8159 8160 ```ts 8161 import { hilog } from '@kit.PerformanceAnalysisKit'; 8162 8163 try { 8164 let option = new rpc.MessageOption(); 8165 hilog.info(0x0000, 'testTag', 'create object successfully'); 8166 let flog = option.getFlags(); 8167 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8168 option.setFlags(1) 8169 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8170 let flog2 = option.getFlags(); 8171 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8172 } catch (error) { 8173 hilog.error(0x0000, 'testTag', 'error ' + error); 8174 } 8175 ``` 8176 8177### setFlags 8178 8179setFlags(flags: number): void 8180 8181Sets the call flag, which can be synchronous or asynchronous. 8182 8183**System capability**: SystemCapability.Communication.IPC.Core 8184 8185**Parameters** 8186 8187| Name| Type | Mandatory| Description | 8188| ------ | ------ | ---- | ------------------------ | 8189| flags | number | Yes | Call flag to set.| 8190 8191**Example** 8192 8193 ```ts 8194 import { hilog } from '@kit.PerformanceAnalysisKit'; 8195 8196 try { 8197 let option = new rpc.MessageOption(); 8198 option.setFlags(1) 8199 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8200 let flog = option.getFlags(); 8201 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8202 } catch (error) { 8203 hilog.error(0x0000, 'testTag', 'error ' + error); 8204 } 8205 ``` 8206 8207### getWaitTime 8208 8209getWaitTime(): number 8210 8211Obtains the maximum wait time for this RPC call. 8212 8213**System capability**: SystemCapability.Communication.IPC.Core 8214 8215**Return value** 8216 8217| Type | Description | 8218| ------ | ----------------- | 8219| number | Maximum wait time obtained.| 8220 8221**Example** 8222 8223 ```ts 8224 import { hilog } from '@kit.PerformanceAnalysisKit'; 8225 8226 try { 8227 let option = new rpc.MessageOption(); 8228 let time = option.getWaitTime(); 8229 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8230 option.setWaitTime(16); 8231 let time2 = option.getWaitTime(); 8232 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8233 } catch (error) { 8234 hilog.error(0x0000, 'testTag', 'error ' + error); 8235 } 8236 ``` 8237 8238### setWaitTime 8239 8240setWaitTime(waitTime: number): void 8241 8242Sets the maximum wait time for this RPC call. 8243 8244**System capability**: SystemCapability.Communication.IPC.Core 8245 8246**Parameters** 8247 8248| Name | Type | Mandatory| Description | 8249| -------- | ------ | ---- | --------------------- | 8250| waitTime | number | Yes | Maximum wait time to set. The upper limit is 3000 seconds.| 8251 8252**Example** 8253 8254 ```ts 8255 import { hilog } from '@kit.PerformanceAnalysisKit'; 8256 8257 try { 8258 let option = new rpc.MessageOption(); 8259 option.setWaitTime(16); 8260 let time = option.getWaitTime(); 8261 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8262 } catch (error) { 8263 hilog.error(0x0000, 'testTag', 'error ' + error); 8264 } 8265 ``` 8266 8267## IPCSkeleton 8268 8269Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 8270 8271### getContextObject 8272 8273static getContextObject(): IRemoteObject 8274 8275Obtains the system capability manager. This API is a static method. 8276 8277**System capability**: SystemCapability.Communication.IPC.Core 8278 8279**Return value** 8280 8281| Type | Description | 8282| ------------------------------- | -------------------- | 8283| [IRemoteObject](#iremoteobject) | System capability manager obtained.| 8284 8285**Example** 8286 8287 ```ts 8288 import { hilog } from '@kit.PerformanceAnalysisKit'; 8289 8290 let samgr = rpc.IPCSkeleton.getContextObject(); 8291 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8292 ``` 8293 8294### getCallingPid 8295 8296static getCallingPid(): number 8297 8298Obtains 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. 8299 8300**System capability**: SystemCapability.Communication.IPC.Core 8301 8302**Return value** 8303 8304| Type | Description | 8305| ------ | ----------------- | 8306| number | PID of the caller.| 8307 8308**Example** 8309 8310 ```ts 8311 import { hilog } from '@kit.PerformanceAnalysisKit'; 8312 8313 class Stub extends rpc.RemoteObject { 8314 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8315 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8316 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8317 return true; 8318 } 8319 } 8320 ``` 8321 8322### getCallingUid 8323 8324static getCallingUid(): number 8325 8326Obtains 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. 8327 8328**System capability**: SystemCapability.Communication.IPC.Core 8329 8330**Return value** 8331 8332| Type | Description | 8333| ------ | ----------------- | 8334| number | UID of the caller.| 8335 8336**Example** 8337 8338 ```ts 8339 import { hilog } from '@kit.PerformanceAnalysisKit'; 8340 8341 class Stub extends rpc.RemoteObject { 8342 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8343 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8344 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8345 return true; 8346 } 8347 } 8348 ``` 8349 8350### getCallingTokenId<sup>8+</sup> 8351 8352static getCallingTokenId(): number 8353 8354Obtains the caller's token ID, which is used to verify the caller identity. 8355 8356**System capability**: SystemCapability.Communication.IPC.Core 8357 8358**Return value** 8359 8360| Type | Description | 8361| ------ | --------------------- | 8362| number | Token ID of the caller obtained.| 8363 8364**Example** 8365 8366 ```ts 8367 import { hilog } from '@kit.PerformanceAnalysisKit'; 8368 8369 class Stub extends rpc.RemoteObject { 8370 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8371 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8372 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8373 return true; 8374 } 8375 } 8376 ``` 8377 8378### getCallingDeviceID 8379 8380static getCallingDeviceID(): string 8381 8382Obtains the ID of the device hosting the caller's process. This API is a static method. 8383 8384**System capability**: SystemCapability.Communication.IPC.Core 8385 8386**Return value** 8387 8388| Type | Description | 8389| ------ | ---------------------------- | 8390| string | Device ID obtained.| 8391 8392**Example** 8393 8394 ```ts 8395 import { hilog } from '@kit.PerformanceAnalysisKit'; 8396 8397 class Stub extends rpc.RemoteObject { 8398 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8399 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8400 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8401 return true; 8402 } 8403 } 8404 ``` 8405 8406### getLocalDeviceID 8407 8408static getLocalDeviceID(): string 8409 8410Obtains the local device ID. This API is a static method. 8411 8412**System capability**: SystemCapability.Communication.IPC.Core 8413 8414**Return value** 8415 8416| Type | Description | 8417| ------ | ------------------ | 8418| string | Local device ID obtained.| 8419 8420**Example** 8421 8422 ```ts 8423 import { hilog } from '@kit.PerformanceAnalysisKit'; 8424 8425 class Stub extends rpc.RemoteObject { 8426 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8427 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8428 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8429 return true; 8430 } 8431 } 8432 ``` 8433 8434### isLocalCalling 8435 8436static isLocalCalling(): boolean 8437 8438Checks whether the peer process is a process of the local device. This API is a static method. 8439 8440**System capability**: SystemCapability.Communication.IPC.Core 8441 8442**Return value** 8443 8444| Type | Description | 8445| ------- | -------------------------------------------------- | 8446| boolean | Returns **true** if the local and peer processes are on the same device; returns **false** otherwise.| 8447 8448**Example** 8449 8450 ```ts 8451 import { hilog } from '@kit.PerformanceAnalysisKit'; 8452 8453 class Stub extends rpc.RemoteObject { 8454 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8455 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8456 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8457 return true; 8458 } 8459 } 8460 ``` 8461 8462### flushCmdBuffer<sup>9+</sup> 8463 8464static flushCmdBuffer(object: IRemoteObject): void 8465 8466Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8467 8468**System capability**: SystemCapability.Communication.IPC.Core 8469 8470**Parameters** 8471 8472| Name| Type | Mandatory| Description | 8473| ------ | ------------------------------- | ---- | ------------------- | 8474| object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8475 8476**Error codes** 8477 8478For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8479 8480| ID| Error Message| 8481| -------- | -------- | 8482| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 8483 8484**Example** 8485 8486 ```ts 8487 import { hilog } from '@kit.PerformanceAnalysisKit'; 8488 import { BusinessError } from '@kit.BasicServicesKit'; 8489 8490 class TestRemoteObject extends rpc.RemoteObject { 8491 constructor(descriptor: string) { 8492 super(descriptor); 8493 } 8494 } 8495 let remoteObject = new TestRemoteObject("aaa"); 8496 try { 8497 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8498 } catch (error) { 8499 let e: BusinessError = error as BusinessError; 8500 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8501 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8502 } 8503 ``` 8504 8505### flushCommands<sup>(deprecated)</sup> 8506 8507>**NOTE**<br>This API is deprecated since API version 9. Use [flushCmdBuffer](#flushcmdbuffer9) instead. 8508 8509static flushCommands(object: IRemoteObject): number 8510 8511Flushes all suspended commands from the specified **RemoteProxy** to the corresponding **RemoteObject**. This API is a static method. You are advised to call this API before performing any sensitive operation. 8512 8513**System capability**: SystemCapability.Communication.IPC.Core 8514 8515**Parameters** 8516 8517| Name| Type | Mandatory| Description | 8518| ------ | ------------------------------- | ---- | ------------------- | 8519| object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8520 8521**Return value** 8522 8523| Type | Description | 8524| ------ | --------------------------------------------------------------------------------- | 8525| 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.| 8526 8527**Example** 8528 8529 ```ts 8530 import { hilog } from '@kit.PerformanceAnalysisKit'; 8531 8532 class MyDeathRecipient implements rpc.DeathRecipient { 8533 onRemoteDied() { 8534 hilog.info(0x0000, 'testTag', 'server died'); 8535 } 8536 } 8537 class TestRemoteObject extends rpc.RemoteObject { 8538 constructor(descriptor: string) { 8539 super(descriptor); 8540 } 8541 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8542 return true; 8543 } 8544 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8545 return true; 8546 } 8547 isObjectDead(): boolean { 8548 return false; 8549 } 8550 } 8551 let remoteObject = new TestRemoteObject("aaa"); 8552 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8553 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8554 ``` 8555 8556### resetCallingIdentity 8557 8558static resetCallingIdentity(): string 8559 8560Resets the UID and PID of the remote user to those of the local user. This API is a static method and is used in scenarios such as identity authentication. 8561 8562**System capability**: SystemCapability.Communication.IPC.Core 8563 8564**Return value** 8565 8566| Type | Description | 8567| ------ | ------------------------------------ | 8568| string | String containing the UID and PID of the remote user.| 8569 8570**Example** 8571 8572 ```ts 8573 import { hilog } from '@kit.PerformanceAnalysisKit'; 8574 8575 class Stub extends rpc.RemoteObject { 8576 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8577 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8578 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8579 return true; 8580 } 8581 } 8582 ``` 8583 8584### restoreCallingIdentity<sup>9+</sup> 8585 8586static restoreCallingIdentity(identity: string): void 8587 8588Restores the UID and PID of the remote user. This API is a static method. It is usually called after **resetCallingIdentity**, and the UID and PID of the remote user returned by **resetCallingIdentity** are required. 8589 8590**System capability**: SystemCapability.Communication.IPC.Core 8591 8592**Parameters** 8593 8594| Name | Type | Mandatory| Description | 8595| -------- | ------ | ---- | ------------------------------------------------------------------ | 8596| identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8597 8598**Error codes** 8599 8600For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8601 8602| ID| Error Message| 8603| -------- | -------- | 8604| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 8605 8606**Example** 8607 8608 ```ts 8609 import { hilog } from '@kit.PerformanceAnalysisKit'; 8610 8611 class Stub extends rpc.RemoteObject { 8612 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8613 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8614 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8615 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8616 return true; 8617 } 8618 } 8619 ``` 8620 8621### setCallingIdentity<sup>(deprecated)</sup> 8622 8623>**NOTE**<br>This API is deprecated since API version 9. Use [restoreCallingIdentity](#restorecallingidentity9) instead. 8624 8625static setCallingIdentity(identity: string): boolean 8626 8627Sets the UID and PID of the remote user. This API is a static method. It is usually called after **resetCallingIdentity**, and the UID and PID of the remote user returned by **resetCallingIdentity** are required. 8628 8629**System capability**: SystemCapability.Communication.IPC.Core 8630 8631**Parameters** 8632 8633| Name | Type | Mandatory| Description | 8634| -------- | ------ | ---- | ------------------------------------------------------------------ | 8635| identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8636 8637**Return value** 8638 8639| Type | Description | 8640| ------- | ---------------------------------| 8641| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8642 8643**Example** 8644 8645 ```ts 8646 import { hilog } from '@kit.PerformanceAnalysisKit'; 8647 8648 class Stub extends rpc.RemoteObject { 8649 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8650 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8651 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8652 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8653 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8654 return true; 8655 } 8656 } 8657 ``` 8658 8659## RemoteObject 8660 8661Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 8662 8663### constructor 8664 8665constructor(descriptor: string) 8666 8667A constructor used to create a **RemoteObject** object. 8668 8669**System capability**: SystemCapability.Communication.IPC.Core 8670 8671**Parameters** 8672 8673| Name | Type | Mandatory| Description | 8674| ---------- | ------ | ---- | ------------ | 8675| descriptor | string | Yes | Interface descriptor.| 8676 8677### sendRequest<sup>(deprecated)</sup> 8678 8679>**NOTE**<br>This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8680 8681sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8682 8683Sends 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. 8684 8685**System capability**: SystemCapability.Communication.IPC.Core 8686 8687**Parameters** 8688 8689| Name | Type | Mandatory| Description | 8690| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8691| 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.| 8692| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8693| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8694| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8695 8696**Return value** 8697 8698| Type | Description | 8699| ------- | -------------------------------- | 8700| boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 8701 8702**Example** 8703 8704 ```ts 8705 import { hilog } from '@kit.PerformanceAnalysisKit'; 8706 8707 class MyDeathRecipient implements rpc.DeathRecipient { 8708 onRemoteDied() { 8709 hilog.info(0x0000, 'testTag', 'server died'); 8710 } 8711 } 8712 class TestRemoteObject extends rpc.RemoteObject { 8713 constructor(descriptor: string) { 8714 super(descriptor); 8715 } 8716 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8717 return true; 8718 } 8719 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8720 return true; 8721 } 8722 isObjectDead(): boolean { 8723 return false; 8724 } 8725 } 8726 let testRemoteObject = new TestRemoteObject("testObject"); 8727 let option = new rpc.MessageOption(); 8728 let data = rpc.MessageParcel.create(); 8729 let reply = rpc.MessageParcel.create(); 8730 data.writeInt(1); 8731 data.writeString("hello"); 8732 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8733 if (ret) { 8734 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8735 let msg = reply.readString(); 8736 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8737 } else { 8738 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8739 } 8740 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8741 data.reclaim(); 8742 reply.reclaim(); 8743 ``` 8744 8745### sendMessageRequest<sup>9+</sup> 8746 8747sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8748 8749Sends 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. 8750 8751**System capability**: SystemCapability.Communication.IPC.Core 8752 8753**Parameters** 8754 8755| Name | Type | Mandatory| Description | 8756| ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8757| 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.| 8758| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8759| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8760| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8761 8762**Return value** 8763 8764| Type | Description | 8765| ----------------------------------------------- | ----------------------------------------- | 8766| Promise<[RequestResult](#requestresult9)> | Promise used to return a **RequestResult** instance.| 8767 8768 8769**Error codes** 8770 8771For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8772 8773| ID| Error Message| 8774| -------- | -------- | 8775| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 8776 8777**Example** 8778 8779 ```ts 8780 import { hilog } from '@kit.PerformanceAnalysisKit'; 8781 8782 class TestRemoteObject extends rpc.RemoteObject { 8783 constructor(descriptor: string) { 8784 super(descriptor); 8785 } 8786 } 8787 let testRemoteObject = new TestRemoteObject("testObject"); 8788 let option = new rpc.MessageOption(); 8789 let data = rpc.MessageSequence.create(); 8790 let reply = rpc.MessageSequence.create(); 8791 data.writeInt(1); 8792 data.writeString("hello"); 8793 testRemoteObject.sendMessageRequest(1, data, reply, option) 8794 .then((result: rpc.RequestResult) => { 8795 if (result.errCode === 0) { 8796 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8797 let num = result.reply.readInt(); 8798 let msg = result.reply.readString(); 8799 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8800 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8801 } else { 8802 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8803 } 8804 }).catch((e: Error) => { 8805 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8806 }).finally (() => { 8807 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8808 data.reclaim(); 8809 reply.reclaim(); 8810 }); 8811 ``` 8812 8813### sendRequest<sup>(deprecated)</sup> 8814 8815>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8816 8817sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8818 8819Sends 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. 8820 8821**System capability**: SystemCapability.Communication.IPC.Core 8822 8823**Parameters** 8824 8825| Name | Type | Mandatory| Description | 8826| ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8827| 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.| 8828| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8829| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8830| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8831 8832**Return value** 8833 8834| Type | Description | 8835| ------------------------------------------------------------ | --------------------------------------------- | 8836| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 8837 8838**Example** 8839 8840 ```ts 8841 import { hilog } from '@kit.PerformanceAnalysisKit'; 8842 8843 class MyDeathRecipient implements rpc.DeathRecipient { 8844 onRemoteDied() { 8845 hilog.info(0x0000, 'testTag', 'server died'); 8846 } 8847 } 8848 class TestRemoteObject extends rpc.RemoteObject { 8849 constructor(descriptor: string) { 8850 super(descriptor); 8851 } 8852 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8853 return true; 8854 } 8855 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8856 return true; 8857 } 8858 isObjectDead(): boolean { 8859 return false; 8860 } 8861 } 8862 let testRemoteObject = new TestRemoteObject("testObject"); 8863 let option = new rpc.MessageOption(); 8864 let data = rpc.MessageParcel.create(); 8865 let reply = rpc.MessageParcel.create(); 8866 data.writeInt(1); 8867 data.writeString("hello"); 8868 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8869 let b = a as Promise<rpc.SendRequestResult>; 8870 b.then((result: rpc.SendRequestResult) => { 8871 if (result.errCode === 0) { 8872 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8873 let num = result.reply.readInt(); 8874 let msg = result.reply.readString(); 8875 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8876 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8877 } else { 8878 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8879 } 8880 }).catch((e: Error) => { 8881 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8882 }).finally (() => { 8883 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8884 data.reclaim(); 8885 reply.reclaim(); 8886 }); 8887 ``` 8888 8889### sendMessageRequest<sup>9+</sup> 8890 8891sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8892 8893Sends 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. 8894 8895**System capability**: SystemCapability.Communication.IPC.Core 8896 8897**Parameters** 8898 8899| Name | Type | Mandatory| Description | 8900| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8901| 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.| 8902| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8903| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8904| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8905| callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 8906 8907 8908**Error codes** 8909 8910For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8911 8912| ID| Error Message| 8913| -------- | -------- | 8914| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain the passed object instance. | 8915 8916**Example** 8917 8918 ```ts 8919 import { hilog } from '@kit.PerformanceAnalysisKit'; 8920 import { BusinessError } from '@kit.BasicServicesKit'; 8921 8922 class TestRemoteObject extends rpc.RemoteObject { 8923 constructor(descriptor: string) { 8924 super(descriptor); 8925 } 8926 } 8927 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 8928 if (result.errCode === 0) { 8929 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8930 let num = result.reply.readInt(); 8931 let msg = result.reply.readString(); 8932 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8933 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8934 } else { 8935 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8936 } 8937 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8938 result.data.reclaim(); 8939 result.reply.reclaim(); 8940 } 8941 let testRemoteObject = new TestRemoteObject("testObject"); 8942 let option = new rpc.MessageOption(); 8943 let data = rpc.MessageSequence.create(); 8944 let reply = rpc.MessageSequence.create(); 8945 data.writeInt(1); 8946 data.writeString("hello"); 8947 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 8948 ``` 8949 8950### sendRequest<sup>(deprecated)</sup> 8951 8952>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-5) instead. 8953 8954sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 8955 8956Sends 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. 8957 8958**System capability**: SystemCapability.Communication.IPC.Core 8959 8960**Parameters** 8961 8962| Name | Type | Mandatory| Description | 8963| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 8964| 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.| 8965| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8966| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8967| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8968| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 8969 8970**Example** 8971 8972 ```ts 8973 import { hilog } from '@kit.PerformanceAnalysisKit'; 8974 import { BusinessError } from '@kit.BasicServicesKit'; 8975 8976 class MyDeathRecipient implements rpc.DeathRecipient { 8977 onRemoteDied() { 8978 hilog.info(0x0000, 'testTag', 'server died'); 8979 } 8980 } 8981 class TestRemoteObject extends rpc.RemoteObject { 8982 constructor(descriptor: string) { 8983 super(descriptor); 8984 } 8985 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8986 return true; 8987 } 8988 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8989 return true; 8990 } 8991 isObjectDead(): boolean { 8992 return false; 8993 } 8994 } 8995 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 8996 if (result.errCode === 0) { 8997 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8998 let num = result.reply.readInt(); 8999 let msg = result.reply.readString(); 9000 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 9001 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9002 } else { 9003 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9004 } 9005 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9006 result.data.reclaim(); 9007 result.reply.reclaim(); 9008 } 9009 let testRemoteObject = new TestRemoteObject("testObject"); 9010 let option = new rpc.MessageOption(); 9011 let data = rpc.MessageParcel.create(); 9012 let reply = rpc.MessageParcel.create(); 9013 data.writeInt(1); 9014 data.writeString("hello"); 9015 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 9016 ``` 9017 9018### onRemoteMessageRequest<sup>9+</sup> 9019 9020onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 9021 9022> **NOTE** 9023> 9024>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 9025>* If both **onRemoteRequest()** and **onRemoteMessageRequest()** are overloaded, only the onRemoteMessageRequest() takes effect. 9026 9027Called to return a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 9028 9029**System capability**: SystemCapability.Communication.IPC.Core 9030 9031**Parameters** 9032 9033| Name| Type | Mandatory| Description | 9034| ------ | ------------------------------------ | ---- | ----------------------------------------- | 9035| code | number | Yes | Service request code sent by the remote end. | 9036| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 9037| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 9038| options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 9039 9040**Return value** 9041 9042| Type | Description | 9043| ----------------- | ----------------------------------------------------------------------------------------------- | 9044| 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.| 9045| Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 9046 9047**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 9048 9049 ```ts 9050 import { hilog } from '@kit.PerformanceAnalysisKit'; 9051 9052 class TestRemoteObject extends rpc.RemoteObject { 9053 constructor(descriptor: string) { 9054 super(descriptor); 9055 } 9056 9057 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9058 if (code === 1) { 9059 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9060 return true; 9061 } else { 9062 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9063 return false; 9064 } 9065 } 9066 } 9067 ``` 9068 9069 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 9070 9071 ```ts 9072 import { hilog } from '@kit.PerformanceAnalysisKit'; 9073 9074 class TestRemoteObject extends rpc.RemoteObject { 9075 constructor(descriptor: string) { 9076 super(descriptor); 9077 } 9078 9079 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9080 if (code === 1) { 9081 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9082 } else { 9083 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9084 return false; 9085 } 9086 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9087 setTimeout(resolve, 100); 9088 }) 9089 return true; 9090 } 9091 } 9092 ``` 9093 9094**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 9095 9096 ```ts 9097 import { hilog } from '@kit.PerformanceAnalysisKit'; 9098 9099 class TestRemoteObject extends rpc.RemoteObject { 9100 constructor(descriptor: string) { 9101 super(descriptor); 9102 } 9103 9104 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9105 if (code === 1) { 9106 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9107 return true; 9108 } else { 9109 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9110 return false; 9111 } 9112 } 9113 // Only onRemoteMessageRequest is executed. 9114 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9115 if (code === 1) { 9116 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9117 } else { 9118 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9119 return false; 9120 } 9121 return true; 9122 } 9123 } 9124 ``` 9125 9126 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 9127 9128 ```ts 9129 import { hilog } from '@kit.PerformanceAnalysisKit'; 9130 class TestRemoteObject extends rpc.RemoteObject { 9131 constructor(descriptor: string) { 9132 super(descriptor); 9133 } 9134 9135 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9136 if (code === 1) { 9137 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9138 return true; 9139 } else { 9140 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9141 return false; 9142 } 9143 } 9144 // Only onRemoteMessageRequest is executed. 9145 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9146 if (code === 1) { 9147 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9148 } else { 9149 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9150 return false; 9151 } 9152 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9153 setTimeout(resolve, 100); 9154 }) 9155 return true; 9156 } 9157 } 9158 ``` 9159 9160### onRemoteRequest<sup>(deprecated)</sup> 9161 9162>**NOTE**<br>This API is deprecated since API version 9. Use [onRemoteMessageRequest](#onremotemessagerequest9) instead. 9163 9164onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9165 9166Called to return a response to **sendRequest()**. The server processes the request and returns a response in this function. 9167 9168**System capability**: SystemCapability.Communication.IPC.Core 9169 9170**Parameters** 9171 9172| Name| Type | Mandatory| Description | 9173| ------ | ----------------------------------------- | ---- | --------------------------------------- | 9174| code | number | Yes | Service request code sent by the remote end. | 9175| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 9176| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 9177| options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 9178 9179**Return value** 9180 9181| Type | Description | 9182| ------- | -------------------------------- | 9183| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 9184 9185**Example** 9186 9187 ```ts 9188 import { hilog } from '@kit.PerformanceAnalysisKit'; 9189 9190 class MyDeathRecipient implements rpc.DeathRecipient { 9191 onRemoteDied() { 9192 hilog.info(0x0000, 'testTag', 'server died'); 9193 } 9194 } 9195 class TestRemoteObject extends rpc.RemoteObject { 9196 constructor(descriptor: string) { 9197 super(descriptor); 9198 } 9199 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9200 return true; 9201 } 9202 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9203 return true; 9204 } 9205 isObjectDead(): boolean { 9206 return false; 9207 } 9208 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9209 if (code === 1) { 9210 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9211 return true; 9212 } else { 9213 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9214 return false; 9215 } 9216 } 9217 } 9218 ``` 9219 9220### getCallingUid 9221 9222getCallingUid(): number 9223 9224Obtains the UID of the remote process. 9225 9226**System capability**: SystemCapability.Communication.IPC.Core 9227 9228**Return value** 9229| Type | Description | 9230| ------ | ----------------------- | 9231| number | UID of the remote process obtained.| 9232 9233**Example** 9234 9235 ```ts 9236 import { hilog } from '@kit.PerformanceAnalysisKit'; 9237 9238 class TestRemoteObject extends rpc.RemoteObject { 9239 constructor(descriptor: string) { 9240 super(descriptor); 9241 } 9242 } 9243 let testRemoteObject = new TestRemoteObject("testObject"); 9244 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9245 ``` 9246 9247### getCallingPid 9248 9249getCallingPid(): number 9250 9251Obtains the PID of the remote process. 9252 9253**System capability**: SystemCapability.Communication.IPC.Core 9254 9255**Return value** 9256 9257| Type | Description | 9258| ------ | ----------------------- | 9259| number | PID of the remote process obtained.| 9260 9261**Example** 9262 9263 ```ts 9264 import { hilog } from '@kit.PerformanceAnalysisKit'; 9265 9266 class TestRemoteObject extends rpc.RemoteObject { 9267 constructor(descriptor: string) { 9268 super(descriptor); 9269 } 9270 } 9271 let testRemoteObject = new TestRemoteObject("testObject"); 9272 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9273 ``` 9274 9275### getLocalInterface<sup>9+</sup> 9276 9277getLocalInterface(descriptor: string): IRemoteBroker 9278 9279Obtains the string of the interface descriptor. 9280 9281**System capability**: SystemCapability.Communication.IPC.Core 9282 9283**Parameters** 9284 9285| Name | Type | Mandatory| Description | 9286| ---------- | ------ | ---- | -------------------- | 9287| descriptor | string | Yes | Interface descriptor.| 9288 9289**Return value** 9290 9291| Type | Description | 9292| ------------- | --------------------------------------------- | 9293| IRemoteBroker | **IRemoteBroker** object bound to the specified interface token.| 9294 9295**Error codes** 9296 9297For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9298 9299| ID| Error Message| 9300| -------- | -------- | 9301| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9302 9303**Example** 9304 9305 ```ts 9306 import { hilog } from '@kit.PerformanceAnalysisKit'; 9307 import { BusinessError } from '@kit.BasicServicesKit'; 9308 9309 class MyDeathRecipient implements rpc.DeathRecipient { 9310 onRemoteDied() { 9311 hilog.info(0x0000, 'testTag', 'server died'); 9312 } 9313 } 9314 class TestRemoteObject extends rpc.RemoteObject { 9315 constructor(descriptor: string) { 9316 super(descriptor); 9317 this.modifyLocalInterface(this, descriptor); 9318 } 9319 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9320 // Implement the method logic based on service requirements. 9321 } 9322 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9323 // Implement the method logic based on service requirements. 9324 } 9325 isObjectDead(): boolean { 9326 return false; 9327 } 9328 asObject(): rpc.IRemoteObject { 9329 return this; 9330 } 9331 } 9332 let testRemoteObject = new TestRemoteObject("testObject"); 9333 try { 9334 testRemoteObject.getLocalInterface("testObject"); 9335 } catch (error) { 9336 let e: BusinessError = error as BusinessError; 9337 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9338 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9339 } 9340 ``` 9341 9342### queryLocalInterface<sup>(deprecated)</sup> 9343 9344>**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-2) instead. 9345 9346queryLocalInterface(descriptor: string): IRemoteBroker 9347 9348Checks whether the remote object corresponding to the specified interface token exists. 9349 9350**System capability**: SystemCapability.Communication.IPC.Core 9351 9352**Parameters** 9353 9354| Name | Type | Mandatory| Description | 9355| ---------- | ------ | ---- | ---------------------- | 9356| descriptor | string | Yes | Interface descriptor.| 9357 9358**Return value** 9359 9360| Type | Description | 9361| ------------- | ------------------------------------------------------------------ | 9362| IRemoteBroker | Returns the remote object if a match is found; returns **Null** otherwise.| 9363 9364**Example** 9365 9366 ```ts 9367 import { hilog } from '@kit.PerformanceAnalysisKit'; 9368 9369 class MyDeathRecipient implements rpc.DeathRecipient { 9370 onRemoteDied() { 9371 hilog.info(0x0000, 'testTag', 'server died'); 9372 } 9373 } 9374 class TestRemoteObject extends rpc.RemoteObject { 9375 constructor(descriptor: string) { 9376 super(descriptor); 9377 this.attachLocalInterface(this, descriptor); 9378 } 9379 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9380 return true; 9381 } 9382 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9383 return true; 9384 } 9385 isObjectDead(): boolean { 9386 return false; 9387 } 9388 asObject(): rpc.IRemoteObject { 9389 return this; 9390 } 9391 } 9392 let testRemoteObject = new TestRemoteObject("testObject"); 9393 testRemoteObject.queryLocalInterface("testObject"); 9394 ``` 9395 9396### getDescriptor<sup>9+</sup> 9397 9398getDescriptor(): string 9399 9400Obtains the interface descriptor of this object. The interface descriptor is a string. 9401 9402**System capability**: SystemCapability.Communication.IPC.Core 9403 9404**Return value** 9405 9406| Type | Description | 9407| ------ | ---------------- | 9408| string | Interface descriptor obtained.| 9409 9410**Error codes** 9411 9412For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9413 9414| ID| Error Message| 9415| -------- | -------- | 9416| 1900008 | The proxy or remote object is invalid. | 9417 9418**Example** 9419 9420 ```ts 9421 import { hilog } from '@kit.PerformanceAnalysisKit'; 9422 import { BusinessError } from '@kit.BasicServicesKit'; 9423 9424 class MyDeathRecipient implements rpc.DeathRecipient { 9425 onRemoteDied() { 9426 hilog.info(0x0000, 'testTag', 'server died'); 9427 } 9428 } 9429 class TestRemoteObject extends rpc.RemoteObject { 9430 constructor(descriptor: string) { 9431 super(descriptor); 9432 } 9433 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9434 // Implement the method logic based on service requirements. 9435 } 9436 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9437 // Implement the method logic based on service requirements. 9438 } 9439 isObjectDead(): boolean { 9440 return false; 9441 } 9442 } 9443 let testRemoteObject = new TestRemoteObject("testObject"); 9444 try { 9445 let descriptor = testRemoteObject.getDescriptor(); 9446 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9447 } catch (error) { 9448 let e: BusinessError = error as BusinessError; 9449 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9450 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9451 } 9452 ``` 9453 9454### getInterfaceDescriptor<sup>(deprecated)</sup> 9455 9456>**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-2) instead. 9457 9458getInterfaceDescriptor(): string 9459 9460Obtains the interface descriptor. 9461 9462**System capability**: SystemCapability.Communication.IPC.Core 9463 9464**Return value** 9465 9466| Type | Description | 9467| ------ | ---------------- | 9468| string | Interface descriptor obtained.| 9469 9470**Example** 9471 9472 ```ts 9473 import { hilog } from '@kit.PerformanceAnalysisKit'; 9474 9475 class MyDeathRecipient implements rpc.DeathRecipient { 9476 onRemoteDied() { 9477 hilog.info(0x0000, 'testTag', 'server died'); 9478 } 9479 } 9480 class TestRemoteObject extends rpc.RemoteObject { 9481 constructor(descriptor: string) { 9482 super(descriptor); 9483 } 9484 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9485 return true; 9486 } 9487 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9488 return true; 9489 } 9490 isObjectDead(): boolean { 9491 return false; 9492 } 9493 } 9494 let testRemoteObject = new TestRemoteObject("testObject"); 9495 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9496 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9497 ``` 9498 9499### modifyLocalInterface<sup>9+</sup> 9500 9501modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9502 9503Binds an interface descriptor to an **IRemoteBroker** object. 9504 9505**System capability**: SystemCapability.Communication.IPC.Core 9506 9507**Parameters** 9508 9509| Name | Type | Mandatory| Description | 9510| -------------- | ------------------------------- | ---- | ------------------------------------- | 9511| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9512| descriptor | string | Yes | Interface descriptor.| 9513 9514**Error codes** 9515 9516For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9517 9518| ID| Error Message| 9519| -------- | -------- | 9520| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9521 9522**Example** 9523 9524 ```ts 9525 import { hilog } from '@kit.PerformanceAnalysisKit'; 9526 import { BusinessError } from '@kit.BasicServicesKit'; 9527 9528 class MyDeathRecipient implements rpc.DeathRecipient { 9529 onRemoteDied() { 9530 hilog.info(0x0000, 'testTag', 'server died'); 9531 } 9532 } 9533 class TestRemoteObject extends rpc.RemoteObject { 9534 constructor(descriptor: string) { 9535 super(descriptor); 9536 try { 9537 this.modifyLocalInterface(this, descriptor); 9538 } catch (error) { 9539 let e: BusinessError = error as BusinessError; 9540 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9541 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9542 } 9543 } 9544 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9545 // Implement the method logic based on service requirements. 9546 } 9547 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9548 // Implement the method logic based on service requirements. 9549 } 9550 isObjectDead(): boolean { 9551 return false; 9552 } 9553 asObject(): rpc.IRemoteObject { 9554 return this; 9555 } 9556 } 9557 let testRemoteObject = new TestRemoteObject("testObject"); 9558 ``` 9559 9560### attachLocalInterface<sup>(deprecated)</sup> 9561 9562>**NOTE**<br>This API is deprecated since API version 9. Use [modifyLocalInterface](#modifylocalinterface9) instead. 9563 9564attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9565 9566Binds an interface descriptor to an **IRemoteBroker** object. 9567 9568**System capability**: SystemCapability.Communication.IPC.Core 9569 9570**Parameters** 9571 9572| Name | Type | Mandatory| Description | 9573| -------------- | ------------------------------- | ---- | ------------------------------------- | 9574| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9575| descriptor | string | Yes | Interface descriptor.| 9576 9577**Example** 9578 9579 ```ts 9580 import { hilog } from '@kit.PerformanceAnalysisKit'; 9581 9582 class MyDeathRecipient implements rpc.DeathRecipient { 9583 onRemoteDied() { 9584 hilog.info(0x0000, 'testTag', 'server died'); 9585 } 9586 } 9587 class TestRemoteObject extends rpc.RemoteObject { 9588 constructor(descriptor: string) { 9589 super(descriptor); 9590 this.attachLocalInterface(this, descriptor); 9591 } 9592 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9593 return true; 9594 } 9595 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9596 return true; 9597 } 9598 isObjectDead(): boolean { 9599 return false; 9600 } 9601 asObject(): rpc.IRemoteObject { 9602 return this; 9603 } 9604 } 9605 let testRemoteObject = new TestRemoteObject("testObject"); 9606 ``` 9607 9608## Ashmem<sup>8+</sup> 9609 9610Provides 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. 9611 9612**System capability**: SystemCapability.Communication.IPC.Core 9613 9614The table below describes the protection types of the mapped memory. 9615 9616| Name | Value | Description | 9617| ---------- | --- | ------------------ | 9618| PROT_EXEC | 4 | The mapped memory is executable. | 9619| PROT_NONE | 0 | The mapped memory is inaccessible.| 9620| PROT_READ | 1 | The mapped memory is readable. | 9621| PROT_WRITE | 2 | The mapped memory is writeable. | 9622 9623### create<sup>9+</sup> 9624 9625static create(name: string, size: number): Ashmem 9626 9627Creates an **Ashmem** object with the specified name and size. This API is a static method. 9628 9629**System capability**: SystemCapability.Communication.IPC.Core 9630 9631**Parameters** 9632 9633| Name| Type | Mandatory| Description | 9634| ------ | ------ | ---- | ---------------------------- | 9635| name | string | Yes | Name of the **Ashmem** object to create. | 9636| size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9637 9638**Return value** 9639 9640| Type | Description | 9641| ------------------ | ---------------------------------------------- | 9642| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9643 9644**Error codes** 9645 9646For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9647 9648| ID| Error Message| 9649| -------- | -------- | 9650| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The Ashmem name passed is empty; <br> 4.The Ashmem size passed is less than or equal to 0. | 9651 9652**Example** 9653 9654 ```ts 9655 import { hilog } from '@kit.PerformanceAnalysisKit'; 9656 import { BusinessError } from '@kit.BasicServicesKit'; 9657 9658 let ashmem: rpc.Ashmem | undefined = undefined; 9659 try { 9660 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9661 let size = ashmem.getAshmemSize(); 9662 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9663 } catch (error) { 9664 let e: BusinessError = error as BusinessError; 9665 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9666 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9667 } 9668 ``` 9669 9670### createAshmem<sup>(deprecated)</sup> 9671 9672>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9) instead. 9673 9674static createAshmem(name: string, size: number): Ashmem 9675 9676Creates an **Ashmem** object with the specified name and size. This API is a static method. 9677 9678**System capability**: SystemCapability.Communication.IPC.Core 9679 9680**Parameters** 9681 9682| Name| Type | Mandatory| Description | 9683| ------ | ------ | ---- | ---------------------------- | 9684| name | string | Yes | Name of the **Ashmem** object to create. | 9685| size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9686 9687**Return value** 9688 9689| Type | Description | 9690| ------------------ | ---------------------------------------------- | 9691| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9692 9693**Example** 9694 9695 ```ts 9696 import { hilog } from '@kit.PerformanceAnalysisKit'; 9697 9698 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9699 let size = ashmem.getAshmemSize(); 9700 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9701 ``` 9702 9703### create<sup>9+</sup> 9704 9705static create(ashmem: Ashmem): Ashmem 9706 9707Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9708 9709**System capability**: SystemCapability.Communication.IPC.Core 9710 9711**Parameters** 9712 9713| Name| Type | Mandatory| Description | 9714| ------ | ------------------ | ---- | -------------------- | 9715| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9716 9717**Return value** 9718 9719| Type | Description | 9720| ------------------ | ---------------------- | 9721| [Ashmem](#ashmem8) | **Ashmem** object created.| 9722 9723**Error codes** 9724 9725For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9726 9727| ID| Error Message| 9728| -------- | -------- | 9729| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The passed parameter is not an Ahmem object; <br> 3.The ashmem instance for obtaining packaging is empty. | 9730 9731**Example** 9732 9733 ```ts 9734 import { hilog } from '@kit.PerformanceAnalysisKit'; 9735 import { BusinessError } from '@kit.BasicServicesKit'; 9736 9737 try { 9738 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9739 let ashmem2 = rpc.Ashmem.create(ashmem); 9740 let size = ashmem2.getAshmemSize(); 9741 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9742 } catch (error) { 9743 let e: BusinessError = error as BusinessError; 9744 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9745 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9746 } 9747 ``` 9748 9749### createAshmemFromExisting<sup>(deprecated)</sup> 9750 9751>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9-1) instead. 9752 9753static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9754 9755Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9756 9757**System capability**: SystemCapability.Communication.IPC.Core 9758 9759**Parameters** 9760 9761| Name| Type | Mandatory| Description | 9762| ------ | ------------------ | ---- | -------------------- | 9763| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9764 9765**Return value** 9766 9767| Type | Description | 9768| ------------------ | ---------------------- | 9769| [Ashmem](#ashmem8) | **Ashmem** object created.| 9770 9771**Example** 9772 9773 ```ts 9774 import { hilog } from '@kit.PerformanceAnalysisKit'; 9775 9776 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9777 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9778 let size = ashmem2.getAshmemSize(); 9779 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9780 ``` 9781 9782### closeAshmem<sup>8+</sup> 9783 9784closeAshmem(): void 9785 9786Closes this **Ashmem** object. 9787 9788**System capability**: SystemCapability.Communication.IPC.Core 9789 9790**Example** 9791 9792 ```ts 9793 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9794 ashmem.closeAshmem(); 9795 ``` 9796 9797### unmapAshmem<sup>8+</sup> 9798 9799unmapAshmem(): void 9800 9801Deletes the mappings for the specified address range of this **Ashmem** object. 9802 9803**System capability**: SystemCapability.Communication.IPC.Core 9804 9805**Example** 9806 9807 ```ts 9808 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9809 ashmem.unmapAshmem(); 9810 ``` 9811 9812### getAshmemSize<sup>8+</sup> 9813 9814getAshmemSize(): number 9815 9816Obtains the memory size of this **Ashmem** object. 9817 9818**System capability**: SystemCapability.Communication.IPC.Core 9819 9820**Return value** 9821 9822| Type | Description | 9823| ------ | -------------------------- | 9824| number | **Ashmem** size obtained.| 9825 9826**Example** 9827 9828 ```ts 9829 import { hilog } from '@kit.PerformanceAnalysisKit'; 9830 9831 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9832 let size = ashmem.getAshmemSize(); 9833 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9834 ``` 9835 9836### mapTypedAshmem<sup>9+</sup> 9837 9838mapTypedAshmem(mapType: number): void 9839 9840Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9841 9842**System capability**: SystemCapability.Communication.IPC.Core 9843 9844**Parameters** 9845 9846| Name | Type | Mandatory| Description | 9847| ------- | ------ | ---- | ------------------------------ | 9848| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9849 9850**Error codes** 9851 9852For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9853 9854| ID| Error Message| 9855| -------- | -------- | 9856| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The passed mapType exceeds the maximum protection level. | 9857| 1900001 | Failed to call mmap. | 9858 9859**Example** 9860 9861 ```ts 9862 import { hilog } from '@kit.PerformanceAnalysisKit'; 9863 import { BusinessError } from '@kit.BasicServicesKit'; 9864 9865 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9866 try { 9867 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9868 } catch (error) { 9869 let e: BusinessError = error as BusinessError; 9870 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9871 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9872 } 9873 ``` 9874 9875### mapAshmem<sup>(deprecated)</sup> 9876 9877>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapTypedAshmem](#maptypedashmem9) instead. 9878 9879mapAshmem(mapType: number): boolean 9880 9881Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9882 9883**System capability**: SystemCapability.Communication.IPC.Core 9884 9885**Parameters** 9886 9887| Name | Type | Mandatory| Description | 9888| ------- | ------ | ---- | ------------------------------ | 9889| mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9890 9891**Return value** 9892 9893| Type | Description | 9894| ------- | -------------------------------- | 9895| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9896 9897**Example** 9898 9899 ```ts 9900 import { hilog } from '@kit.PerformanceAnalysisKit'; 9901 9902 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9903 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9904 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 9905 ``` 9906 9907### mapReadWriteAshmem<sup>9+</sup> 9908 9909mapReadWriteAshmem(): void 9910 9911Maps the shared file to the readable and writable virtual address space of the process. 9912 9913**System capability**: SystemCapability.Communication.IPC.Core 9914 9915**Error codes** 9916 9917For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9918 9919| ID| Error Message| 9920| -------- | -------- | 9921| 1900001 | Failed to call mmap. | 9922 9923**Example** 9924 9925 ```ts 9926 import { hilog } from '@kit.PerformanceAnalysisKit'; 9927 import { BusinessError } from '@kit.BasicServicesKit'; 9928 9929 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9930 try { 9931 ashmem.mapReadWriteAshmem(); 9932 } catch (error) { 9933 let e: BusinessError = error as BusinessError; 9934 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9935 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9936 } 9937 ``` 9938 9939### mapReadAndWriteAshmem<sup>(deprecated)</sup> 9940 9941>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapReadWriteAshmem](#mapreadwriteashmem9) instead. 9942 9943mapReadAndWriteAshmem(): boolean 9944 9945Maps the shared file to the readable and writable virtual address space of the process. 9946 9947**System capability**: SystemCapability.Communication.IPC.Core 9948 9949**Return value** 9950 9951| Type | Description | 9952| ------- | -------------------------------- | 9953| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9954 9955**Example** 9956 9957 ```ts 9958 import { hilog } from '@kit.PerformanceAnalysisKit'; 9959 9960 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9961 let mapResult = ashmem.mapReadAndWriteAshmem(); 9962 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 9963 ``` 9964 9965### mapReadonlyAshmem<sup>9+</sup> 9966 9967mapReadonlyAshmem(): void 9968 9969Maps the shared file to the read-only virtual address space of the process. 9970 9971**System capability**: SystemCapability.Communication.IPC.Core 9972 9973**Error codes** 9974 9975For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9976 9977| ID| Error Message| 9978| -------- | -------- | 9979| 1900001 | Failed to call mmap. | 9980 9981**Example** 9982 9983 ```ts 9984 import { hilog } from '@kit.PerformanceAnalysisKit'; 9985 import { BusinessError } from '@kit.BasicServicesKit'; 9986 9987 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9988 try { 9989 ashmem.mapReadonlyAshmem(); 9990 } catch (error) { 9991 let e: BusinessError = error as BusinessError; 9992 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 9993 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 9994 } 9995 ``` 9996 9997### mapReadOnlyAshmem<sup>(deprecated)</sup> 9998 9999>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapReadonlyAshmem](#mapreadonlyashmem9) instead. 10000 10001mapReadOnlyAshmem(): boolean 10002 10003Maps the shared file to the read-only virtual address space of the process. 10004 10005**System capability**: SystemCapability.Communication.IPC.Core 10006 10007**Return value** 10008 10009| Type | Description | 10010| ------- | -------------------------------- | 10011| boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 10012 10013**Example** 10014 10015 ```ts 10016 import { hilog } from '@kit.PerformanceAnalysisKit'; 10017 10018 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10019 let mapResult = ashmem.mapReadOnlyAshmem(); 10020 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 10021 ``` 10022 10023### setProtectionType<sup>9+</sup> 10024 10025setProtectionType(protectionType: number): void 10026 10027Sets the protection level of the memory region to which the shared file is mapped. 10028 10029**System capability**: SystemCapability.Communication.IPC.Core 10030 10031**Parameters** 10032 10033| Name | Type | Mandatory| Description | 10034| -------------- | ------ | ---- | ------------------ | 10035| protectionType | number | Yes | Protection type to set.| 10036 10037**Error codes** 10038 10039For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10040 10041| ID| Error Message| 10042| -------- | -------- | 10043| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10044| 1900002 | Failed to call ioctl. | 10045 10046**Example** 10047 10048 ```ts 10049 import { hilog } from '@kit.PerformanceAnalysisKit'; 10050 import { BusinessError } from '@kit.BasicServicesKit'; 10051 10052 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10053 try { 10054 ashmem.setProtection(ashmem.PROT_READ); 10055 } catch (error) { 10056 let e: BusinessError = error as BusinessError; 10057 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10058 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10059 } 10060 ``` 10061 10062### setProtection<sup>(deprecated)</sup> 10063 10064>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [setProtectionType](#setprotectiontype9) instead. 10065 10066setProtection(protectionType: number): boolean 10067 10068Sets the protection level of the memory region to which the shared file is mapped. 10069 10070**System capability**: SystemCapability.Communication.IPC.Core 10071 10072**Parameters** 10073 10074| Name | Type | Mandatory| Description | 10075| -------------- | ------ | ---- | ------------------ | 10076| protectionType | number | Yes | Protection type to set.| 10077 10078**Return value** 10079 10080| Type | Description | 10081| ------- | -------------------------------- | 10082| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 10083 10084**Example** 10085 10086 ```ts 10087 import { hilog } from '@kit.PerformanceAnalysisKit'; 10088 10089 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10090 let result = ashmem.setProtection(ashmem.PROT_READ); 10091 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10092 ``` 10093 10094### writeDataToAshmem<sup>11+</sup> 10095 10096writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10097 10098Writes data to the shared file associated with this **Ashmem** object. 10099 10100**System capability**: SystemCapability.Communication.IPC.Core 10101 10102**Parameters** 10103 10104| Name| Type | Mandatory| Description | 10105| ------ | -------- | ---- | -------------------------------------------------- | 10106| buf | ArrayBuffer | Yes | Data to write. | 10107| size | number | Yes | Size of the data to write. | 10108| offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10109 10110**Error codes** 10111 10112For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10113 10114| ID| Error Message| 10115| -------- | -------- | 10116| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.Failed to obtain arrayBuffer information. | 10117| 1900003 | Failed to write data to the shared memory. | 10118 10119**Example** 10120 10121 ```ts 10122 import { hilog } from '@kit.PerformanceAnalysisKit'; 10123 import { BusinessError } from '@kit.BasicServicesKit'; 10124 10125 let buffer = new ArrayBuffer(1024); 10126 let int32View = new Int32Array(buffer); 10127 for (let i = 0; i < int32View.length; i++) { 10128 int32View[i] = i * 2 + 1; 10129 } 10130 let size = buffer.byteLength; 10131 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10132 ashmem.mapReadWriteAshmem(); 10133 try { 10134 ashmem.writeDataToAshmem(buffer, size, 0); 10135 } catch (error) { 10136 let e: BusinessError = error as BusinessError; 10137 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10138 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10139 } 10140 ``` 10141 10142### writeAshmem<sup>(deprecated)</sup> 10143 10144>**NOTE**<br>This API is supported since API version 9 and deprecated since API version 11. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10145 10146writeAshmem(buf: number[], size: number, offset: number): void 10147 10148Writes data to the shared file associated with this **Ashmem** object. 10149 10150**System capability**: SystemCapability.Communication.IPC.Core 10151 10152**Parameters** 10153 10154| Name| Type | Mandatory| Description | 10155| ------ | -------- | ---- | -------------------------------------------------- | 10156| buf | number[] | Yes | Data to write. | 10157| size | number | Yes | Size of the data to write. | 10158| offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10159 10160**Error codes** 10161 10162For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10163 10164| ID| Error Message| 10165| -------- | -------- | 10166| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The element does not exist in the array. | 10167| 1900003 | Failed to write data to the shared memory. | 10168 10169**Example** 10170 10171 ```ts 10172 import { hilog } from '@kit.PerformanceAnalysisKit'; 10173 import { BusinessError } from '@kit.BasicServicesKit'; 10174 10175 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10176 ashmem.mapReadWriteAshmem(); 10177 let ByteArrayVar = [1, 2, 3, 4, 5]; 10178 try { 10179 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10180 } catch (error) { 10181 let e: BusinessError = error as BusinessError; 10182 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10183 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10184 } 10185 ``` 10186 10187### writeToAshmem<sup>(deprecated)</sup> 10188 10189>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10190 10191writeToAshmem(buf: number[], size: number, offset: number): boolean 10192 10193Writes data to the shared file associated with this **Ashmem** object. 10194 10195**System capability**: SystemCapability.Communication.IPC.Core 10196 10197**Parameters** 10198 10199| Name| Type | Mandatory| Description | 10200| ------ | -------- | ---- | -------------------------------------------------- | 10201| buf | number[] | Yes | Data to write. | 10202| size | number | Yes | Size of the data to write. | 10203| offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10204 10205**Return value** 10206 10207| Type | Description | 10208| ------- | ----------------------------------------------------------------------------- | 10209| boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 10210 10211**Example** 10212 10213 ```ts 10214 import { hilog } from '@kit.PerformanceAnalysisKit'; 10215 10216 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10217 let mapResult = ashmem.mapReadAndWriteAshmem(); 10218 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10219 let ByteArrayVar = [1, 2, 3, 4, 5]; 10220 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10221 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10222 ``` 10223 10224### readDataFromAshmem<sup>11+</sup> 10225 10226readDataFromAshmem(size: number, offset: number): ArrayBuffer 10227 10228Reads data from the shared file associated with this **Ashmem** object. 10229 10230**System capability**: SystemCapability.Communication.IPC.Core 10231 10232**Parameters** 10233 10234| Name| Type | Mandatory| Description | 10235| ------ | ------ | ---- | -------------------------------------------------- | 10236| size | number | Yes | Size of the data to read. | 10237| offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10238 10239**Return value** 10240 10241| Type | Description | 10242| -------- | ---------------- | 10243| ArrayBuffer | Data read.| 10244 10245**Error codes** 10246 10247For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10248 10249| ID| Error Message| 10250| -------- | -------- | 10251| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10252| 1900004 | Failed to read data from the shared memory. | 10253 10254**Example** 10255 10256 ```ts 10257 import { hilog } from '@kit.PerformanceAnalysisKit'; 10258 import { BusinessError } from '@kit.BasicServicesKit'; 10259 10260 let buffer = new ArrayBuffer(1024); 10261 let int32View = new Int32Array(buffer); 10262 for (let i = 0; i < int32View.length; i++) { 10263 int32View[i] = i * 2 + 1; 10264 } 10265 let size = buffer.byteLength; 10266 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10267 ashmem.mapReadWriteAshmem(); 10268 try { 10269 ashmem.writeDataToAshmem(buffer, size, 0); 10270 } catch (error) { 10271 let e: BusinessError = error as BusinessError; 10272 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10273 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10274 } 10275 try { 10276 let readResult = ashmem.readDataFromAshmem(size, 0); 10277 let readInt32View = new Int32Array(readResult); 10278 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10279 } catch (error) { 10280 let e: BusinessError = error as BusinessError; 10281 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10282 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10283 } 10284 ``` 10285 10286### readAshmem<sup>(deprecated)</sup> 10287 10288>**NOTE**<br>This API is supported since API version 9 and deprecated since API version 11. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10289 10290readAshmem(size: number, offset: number): number[] 10291 10292Reads data from the shared file associated with this **Ashmem** object. 10293 10294**System capability**: SystemCapability.Communication.IPC.Core 10295 10296**Parameters** 10297 10298| Name| Type | Mandatory| Description | 10299| ------ | ------ | ---- | -------------------------------------------------- | 10300| size | number | Yes | Size of the data to read. | 10301| offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10302 10303**Return value** 10304 10305| Type | Description | 10306| -------- | ---------------- | 10307| number[] | Data read.| 10308 10309**Error codes** 10310 10311For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10312 10313| ID| Error Message| 10314| -------- | -------- | 10315| 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10316| 1900004 | Failed to read data from the shared memory. | 10317 10318**Example** 10319 10320 ```ts 10321 import { hilog } from '@kit.PerformanceAnalysisKit'; 10322 import { BusinessError } from '@kit.BasicServicesKit'; 10323 10324 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10325 ashmem.mapReadWriteAshmem(); 10326 let ByteArrayVar = [1, 2, 3, 4, 5]; 10327 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10328 try { 10329 let readResult = ashmem.readAshmem(5, 0); 10330 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10331 } catch (error) { 10332 let e: BusinessError = error as BusinessError; 10333 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10334 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10335 } 10336 ``` 10337 10338### readFromAshmem<sup>(deprecated)</sup> 10339 10340>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10341 10342readFromAshmem(size: number, offset: number): number[] 10343 10344Reads data from the shared file associated with this **Ashmem** object. 10345 10346**System capability**: SystemCapability.Communication.IPC.Core 10347 10348**Parameters** 10349 10350| Name| Type | Mandatory| Description | 10351| ------ | ------ | ---- | -------------------------------------------------- | 10352| size | number | Yes | Size of the data to read. | 10353| offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10354 10355**Return value** 10356 10357| Type | Description | 10358| -------- | ---------------- | 10359| number[] | Data read.| 10360 10361**Example** 10362 10363 ``` ts 10364 import { hilog } from '@kit.PerformanceAnalysisKit'; 10365 10366 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10367 let mapResult = ashmem.mapReadAndWriteAshmem(); 10368 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10369 let ByteArrayVar = [1, 2, 3, 4, 5]; 10370 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10371 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10372 let readResult = ashmem.readFromAshmem(5, 0); 10373 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10374 ``` 10375