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 63 Provides APIs for reading and writing data in specific format. During RPC or IPC, the sender can use the **write()** method provided by **MessageSequence** to write data in specific format to a **MessageSequence** object. The receiver can use the **read()** method provided by **MessageSequence** to read data in specific format from a **MessageSequence** object. The data formats include basic data types and arrays, IPC objects, interface tokens, and custom sequenceable objects. 64 65### create 66 67 static create(): MessageSequence 68 69 Creates 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 // When the MessageSequence object is no longer used, the service calls the reclaim method to release resources. 88 data.reclaim(); 89 ``` 90 91### reclaim 92 93reclaim(): void 94 95Reclaims the **MessageSequence** object that is no longer used. 96 97**System capability**: SystemCapability.Communication.IPC.Core 98 99**Example** 100 101 ```ts 102 let reply = rpc.MessageSequence.create(); 103 reply.reclaim(); 104 ``` 105 106### writeRemoteObject 107 108writeRemoteObject(object: IRemoteObject): void 109 110Serializes the remote object and writes it to the [MessageSequence](#messagesequence9) object. 111 112**System capability**: SystemCapability.Communication.IPC.Core 113 114**Parameters** 115 116 | Name| Type | Mandatory| Description | 117 | ------ | ------------------------------- | ---- | ----------------------------------------- | 118 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageSequence** object.| 119 120**Error codes** 121 122For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 123 124 | ID| Error Message| 125 | -------- | -------- | 126 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 127 | 1900008 | The proxy or remote object is invalid. | 128 | 1900009 | Failed to write data to the message sequence. | 129 130**Example** 131 132 ```ts 133 import { hilog } from '@kit.PerformanceAnalysisKit'; 134 import { BusinessError } from '@kit.BasicServicesKit'; 135 136 class TestRemoteObject extends rpc.RemoteObject { 137 constructor(descriptor: string) { 138 super(descriptor); 139 } 140 } 141 let data = rpc.MessageSequence.create(); 142 let testRemoteObject = new TestRemoteObject("testObject"); 143 try { 144 data.writeRemoteObject(testRemoteObject); 145 } catch (error) { 146 let e: BusinessError = error as BusinessError; 147 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 148 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 149 } 150 ``` 151 152### readRemoteObject 153 154readRemoteObject(): IRemoteObject 155 156Reads 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. 157 158**System capability**: SystemCapability.Communication.IPC.Core 159 160**Return value** 161 162 | Type | Description | 163 | ------------------------------- | ------------------ | 164 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 165 166**Error codes** 167 168For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 169 170 | ID| Error Message| 171 | -------- | -------- | 172 | 1900008 | The proxy or remote object is invalid. | 173 | 1900010 | Failed to read data from the message sequence. | 174 175**Example** 176 177 ```ts 178 import { hilog } from '@kit.PerformanceAnalysisKit'; 179 import { BusinessError } from '@kit.BasicServicesKit'; 180 181 class TestRemoteObject extends rpc.RemoteObject { 182 constructor(descriptor: string) { 183 super(descriptor); 184 } 185 } 186 let data = rpc.MessageSequence.create(); 187 let testRemoteObject = new TestRemoteObject("testObject"); 188 try { 189 data.writeRemoteObject(testRemoteObject); 190 let proxy = data.readRemoteObject(); 191 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObject is ' + proxy); 192 } catch (error) { 193 let e: BusinessError = error as BusinessError; 194 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorCode ' + e.code); 195 hilog.error(0x0000, 'testTag', 'Rpc write remote object fail, errorMessage ' + e.message); 196 } 197 ``` 198 199### writeInterfaceToken 200 201writeInterfaceToken(token: string): void 202 203Writes an interface token to this **MessageSequence** object. The remote object can use this interface token to verify the communication. 204 205**System capability**: SystemCapability.Communication.IPC.Core 206 207**Parameters** 208 209 | Name| Type | Mandatory| Description | 210 | ------ | ------ | ---- | ------------------ | 211 | token | string | Yes | Interface token to write.| 212 213**Error codes** 214 215For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 216 217 | ID| Error Message| 218 | -------- | -------- | 219 | 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. | 220 | 1900009 | Failed to write data to the message sequence. | 221 222**Example** 223 224 ```ts 225 import { hilog } from '@kit.PerformanceAnalysisKit'; 226 import { BusinessError } from '@kit.BasicServicesKit'; 227 228 let data = rpc.MessageSequence.create(); 229 try { 230 data.writeInterfaceToken("aaa"); 231 } catch (error) { 232 let e: BusinessError = error as BusinessError; 233 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorCode ' + e.code); 234 hilog.error(0x0000, 'testTag', 'rpc write interface fail, errorMessage ' + e.message); 235 } 236 ``` 237 238### readInterfaceToken 239 240readInterfaceToken(): string 241 242Reads 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. 243 244**System capability**: SystemCapability.Communication.IPC.Core 245 246**Return value** 247 248 | Type | Description | 249 | ------ | ------------------------ | 250 | string | Interface token obtained.| 251 252**Error codes** 253 254For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 255 256 | ID| Error Message| 257 | -------- | -------- | 258 | 1900010 | Failed to read data from the message sequence. | 259 260**Example** 261 262```ts 263import { hilog } from '@kit.PerformanceAnalysisKit'; 264import { BusinessError } from '@kit.BasicServicesKit'; 265 266class Stub extends rpc.RemoteObject { 267 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 268 try { 269 let interfaceToken = data.readInterfaceToken(); 270 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 271 } catch (error) { 272 let e: BusinessError = error as BusinessError; 273 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorCode ' + e.code); 274 hilog.error(0x0000, 'testTag', 'RpcServer: read interfaceToken failed, errorMessage ' + e.message); 275 } 276 return true; 277 } 278} 279``` 280 281### getSize 282 283getSize(): number 284 285Obtains the data size of this **MessageSequence** object. 286 287**System capability**: SystemCapability.Communication.IPC.Core 288 289**Return value** 290 291 | Type | Description | 292 | ------ | ----------------------------------------------- | 293 | number | Size of the **MessageSequence** instance obtained, in bytes.| 294 295**Example** 296 297 ```ts 298 import { hilog } from '@kit.PerformanceAnalysisKit'; 299 300 let data = rpc.MessageSequence.create(); 301 let size = data.getSize(); 302 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 303 ``` 304 305### getCapacity 306 307getCapacity(): number 308 309Obtains the capacity of this **MessageSequence** object. 310 311**System capability**: SystemCapability.Communication.IPC.Core 312 313**Return value** 314 315 | Type | Description | 316 | ------ | ----- | 317 | number | Capacity of the obtained **MessageSequence** object, in bytes.| 318 319**Example** 320 321 ```ts 322 import { hilog } from '@kit.PerformanceAnalysisKit'; 323 324 let data = rpc.MessageSequence.create(); 325 let result = data.getCapacity(); 326 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 327 ``` 328 329### setSize 330 331setSize(size: number): void 332 333Sets the size of the data contained in this **MessageSequence** object. 334 335**System capability**: SystemCapability.Communication.IPC.Core 336 337**Parameters** 338 339 | Name| Type | Mandatory| Description | 340 | ------ | ------ | ---- | ------ | 341 | size | number | Yes | Data size to set, in bytes.| 342 343**Error codes** 344 345For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 346 347 | ID| Error Message| 348 | -------- | -------- | 349 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 350 351**Example** 352 353 ```ts 354 import { hilog } from '@kit.PerformanceAnalysisKit'; 355 import { BusinessError } from '@kit.BasicServicesKit'; 356 357 let data = rpc.MessageSequence.create(); 358 data.writeString('Hello World'); 359 try { 360 data.setSize(16); 361 } catch (error) { 362 let e: BusinessError = error as BusinessError; 363 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorCode ' + e.code); 364 hilog.error(0x0000, 'testTag', 'rpc set size of MessageSequence fail, errorMessage ' + e.message); 365 } 366 ``` 367 368### setCapacity 369 370setCapacity(size: number): void 371 372Sets the storage capacity of this **MessageSequence** object. 373 374**System capability**: SystemCapability.Communication.IPC.Core 375 376**Parameters** 377 378 | Name| Type | Mandatory| Description | 379 | ------ | ------ | ---- | --------------------------------------------- | 380 | size | number | Yes | Storage capacity of the **MessageSequence** object to set, in bytes.| 381 382**Error codes** 383 384For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 385 386 | ID| Error Message| 387 | -------- | -------- | 388 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 389 | 1900011 | Memory allocation failed | 390 391**Example** 392 393 ```ts 394 import { hilog } from '@kit.PerformanceAnalysisKit'; 395 import { BusinessError } from '@kit.BasicServicesKit'; 396 397 let data = rpc.MessageSequence.create(); 398 try { 399 data.setCapacity(100); 400 } catch (error) { 401 let e: BusinessError = error as BusinessError; 402 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorCode ' + e.code); 403 hilog.error(0x0000, 'testTag', 'rpc memory alloc fail, errorMessage ' + e.message); 404 } 405 ``` 406 407### getWritableBytes 408 409getWritableBytes(): number 410 411Obtains the writable capacity (in bytes) of this **MessageSequence** object. 412 413**System capability**: SystemCapability.Communication.IPC.Core 414 415**Return value** 416 417 | Type | Description | 418 | ------ | ------ | 419 | number | Writable capacity of the **MessageSequence** instance, in bytes.| 420 421**Example** 422 423```ts 424import { hilog } from '@kit.PerformanceAnalysisKit'; 425 426class Stub extends rpc.RemoteObject { 427 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 428 let getWritableBytes = data.getWritableBytes(); 429 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 430 return true; 431 } 432} 433``` 434 435### getReadableBytes 436 437getReadableBytes(): number 438 439Obtains the readable capacity of this **MessageSequence** object. 440 441**System capability**: SystemCapability.Communication.IPC.Core 442 443**Return value** 444 445 | Type | Description | 446 | ------ | ------- | 447 | number | Readable capacity of the **MessageSequence** instance, in bytes.| 448 449**Example** 450 451```ts 452import { hilog } from '@kit.PerformanceAnalysisKit'; 453 454class Stub extends rpc.RemoteObject { 455 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 456 let result = data.getReadableBytes(); 457 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 458 return true; 459 } 460} 461``` 462 463### getReadPosition 464 465getReadPosition(): number 466 467Obtains the read position of this **MessageSequence** object. 468 469**System capability**: SystemCapability.Communication.IPC.Core 470 471**Return value** 472 473 | Type | Description | 474 | ------ | ------ | 475 | number | Read position obtained.| 476 477**Example** 478 479 ```ts 480 import { hilog } from '@kit.PerformanceAnalysisKit'; 481 482 let data = rpc.MessageSequence.create(); 483 let readPos = data.getReadPosition(); 484 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 485 ``` 486 487### getWritePosition 488 489getWritePosition(): number 490 491Obtains the write position of this **MessageSequence** object. 492 493**System capability**: SystemCapability.Communication.IPC.Core 494 495**Return value** 496 497 | Type | Description | 498 | ------ | ----- | 499 | number | Write position obtained.| 500 501**Example** 502 503 ```ts 504 import { hilog } from '@kit.PerformanceAnalysisKit'; 505 506 let data = rpc.MessageSequence.create(); 507 data.writeInt(10); 508 let bwPos = data.getWritePosition(); 509 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 510 ``` 511 512### rewindRead 513 514rewindRead(pos: number): void 515 516Moves the read pointer to the specified position. 517 518**System capability**: SystemCapability.Communication.IPC.Core 519 520**Parameters** 521 522 | Name| Type | Mandatory| Description | 523 | ------ | ------ | ---- | ------- | 524 | pos | number | Yes | Position from which data is to read.| 525 526**Error codes** 527 528For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 529 530 | ID| Error Message| 531 | -------- | -------- | 532 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 533 534**Example** 535 536 ```ts 537 import { hilog } from '@kit.PerformanceAnalysisKit'; 538 import { BusinessError } from '@kit.BasicServicesKit'; 539 540 let data = rpc.MessageSequence.create(); 541 data.writeInt(12); 542 data.writeString("sequence"); 543 let number = data.readInt(); 544 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 545 try { 546 data.rewindRead(0); 547 } catch (error) { 548 let e: BusinessError = error as BusinessError; 549 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorCode ' + e.code); 550 hilog.error(0x0000, 'testTag', 'rpc rewind read data fail, errorMessage ' + e.message); 551 } 552 let number2 = data.readInt(); 553 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 554 ``` 555 556### rewindWrite 557 558rewindWrite(pos: number): void 559 560Moves the write pointer to the specified position. 561 562**System capability**: SystemCapability.Communication.IPC.Core 563 564**Parameters** 565 566 | Name| Type | Mandatory| Description | 567 | ------ | ------ | ---- | ----- | 568 | pos | number | Yes | Position from which data is to write.| 569 570**Error codes** 571 572For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 573 574 | ID| Error Message| 575 | -------- | -------- | 576 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 577 578**Example** 579 580 ```ts 581 import { hilog } from '@kit.PerformanceAnalysisKit'; 582 import { BusinessError } from '@kit.BasicServicesKit'; 583 584 let data = rpc.MessageSequence.create(); 585 data.writeInt(4); 586 try { 587 data.rewindWrite(0); 588 } catch (error) { 589 let e: BusinessError = error as BusinessError; 590 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorCode ' + e.code); 591 hilog.error(0x0000, 'testTag', 'rpc rewindWrite fail, errorMessage ' + e.message); 592 } 593 data.writeInt(5); 594 let number = data.readInt(); 595 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is: ' + number); 596 ``` 597 598### writeByte 599 600writeByte(val: number): void 601 602Writes a byte value to this **MessageSequence** object. 603 604**System capability**: SystemCapability.Communication.IPC.Core 605 606**Parameters** 607 608 | Name| Type | Mandatory| Description | 609 | ------ | ------ | ---- | ----- | 610 | val | number | Yes | Byte value to write.| 611 612**Error codes** 613 614For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 615 616 | ID| Error Message| 617 | -------- | ------- | 618 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 619 | 1900009 | Failed to write data to the message sequence. | 620 621**Example** 622 623 ```ts 624 import { hilog } from '@kit.PerformanceAnalysisKit'; 625 import { BusinessError } from '@kit.BasicServicesKit'; 626 627 let data = rpc.MessageSequence.create(); 628 try { 629 data.writeByte(2); 630 } catch (error) { 631 let e: BusinessError = error as BusinessError; 632 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 633 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 634 } 635 ``` 636 637### readByte 638 639readByte(): number 640 641Reads the byte value from this **MessageSequence** object. 642 643**System capability**: SystemCapability.Communication.IPC.Core 644 645**Return value** 646 647 | Type | Description | 648 | ------ | ----- | 649 | number | Byte value read.| 650 651**Error codes** 652 653For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 654 655 | ID| Error Message| 656 | ------- | -------- | 657 | 1900010 | Failed to read data from the message sequence. | 658 659**Example** 660 661 ```ts 662 import { hilog } from '@kit.PerformanceAnalysisKit'; 663 import { BusinessError } from '@kit.BasicServicesKit'; 664 665 let data = rpc.MessageSequence.create(); 666 try { 667 data.writeByte(2); 668 } catch (error) { 669 let e: BusinessError = error as BusinessError; 670 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorCode ' + e.code); 671 hilog.error(0x0000, 'testTag', 'rpc write byte fail, errorMessage ' + e.message); 672 } 673 try { 674 let ret = data.readByte(); 675 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is: ' + ret); 676 } catch (error) { 677 let e: BusinessError = error as BusinessError; 678 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorCode ' + e.code); 679 hilog.error(0x0000, 'testTag', 'rpc read byte fail, errorMessage ' + e.message); 680 } 681 ``` 682 683### writeShort 684 685writeShort(val: number): void 686 687Writes a short integer to this **MessageSequence** object. 688 689**System capability**: SystemCapability.Communication.IPC.Core 690 691**Parameters** 692 693 | Name| Type | Mandatory| Description| 694 | ------ | ------ | --- | --- | 695 | val | number | Yes | Short integer to write.| 696 697**Error codes** 698 699For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 700 701 | ID| Error Message| 702 | -------- | -------- | 703 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 704 | 1900009 | Failed to write data to the message sequence. | 705 706**Example** 707 708 ```ts 709 import { hilog } from '@kit.PerformanceAnalysisKit'; 710 import { BusinessError } from '@kit.BasicServicesKit'; 711 712 let data = rpc.MessageSequence.create(); 713 try { 714 data.writeShort(8); 715 } catch (error) { 716 let e: BusinessError = error as BusinessError; 717 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 718 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 719 } 720 ``` 721 722### readShort 723 724readShort(): number 725 726Reads the short integer from this **MessageSequence** object. 727 728**System capability**: SystemCapability.Communication.IPC.Core 729 730**Return value** 731 732 | Type | Description | 733 | ------ | -------------- | 734 | number | Short integer read.| 735 736**Error codes** 737 738For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 739 740 | ID| Error Message| 741 | -------- | -------- | 742 | 1900010 | Failed to read data from the message sequence. | 743 744**Example** 745 746 ```ts 747 import { hilog } from '@kit.PerformanceAnalysisKit'; 748 import { BusinessError } from '@kit.BasicServicesKit'; 749 750 let data = rpc.MessageSequence.create(); 751 try { 752 data.writeShort(8); 753 } catch (error) { 754 let e: BusinessError = error as BusinessError; 755 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorCode ' + e.code); 756 hilog.error(0x0000, 'testTag', 'rpc write short fail, errorMessage ' + e.message); 757 } 758 try { 759 let ret = data.readShort(); 760 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 761 } catch (error) { 762 let e: BusinessError = error as BusinessError; 763 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorCode ' + e.code); 764 hilog.error(0x0000, 'testTag', 'rpc read short fail, errorMessage ' + e.message); 765 } 766 ``` 767 768### writeInt 769 770writeInt(val: number): void 771 772Writes an integer to this **MessageSequence** object. 773 774**System capability**: SystemCapability.Communication.IPC.Core 775 776**Parameters** 777 778 | Name| Type | Mandatory| Description | 779 | ------ | ------ | ---- | ---------------- | 780 | val | number | Yes | Integer to write.| 781 782**Error codes** 783 784For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 785 786 | ID| Error Message| 787 | -------- | -------- | 788 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 789 | 1900009 | Failed to write data to the message sequence. | 790 791**Example** 792 793 ```ts 794 import { hilog } from '@kit.PerformanceAnalysisKit'; 795 import { BusinessError } from '@kit.BasicServicesKit'; 796 797 let data = rpc.MessageSequence.create(); 798 try { 799 data.writeInt(10); 800 } catch (error) { 801 let e: BusinessError = error as BusinessError; 802 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 803 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 804 } 805 ``` 806 807### readInt 808 809readInt(): number 810 811Reads the integer from this **MessageSequence** object. 812 813**System capability**: SystemCapability.Communication.IPC.Core 814 815**Return value** 816 817 | Type | Description | 818 | ------ | ------------ | 819 | number | Integer read.| 820 821**Error codes** 822 823For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 824 825 | ID| Error Message| 826 | -------- | -------- | 827 | 1900010 | Failed to read data from the message sequence. | 828 829**Example** 830 831 ```ts 832 import { hilog } from '@kit.PerformanceAnalysisKit'; 833 import { BusinessError } from '@kit.BasicServicesKit'; 834 835 let data = rpc.MessageSequence.create(); 836 try { 837 data.writeInt(10); 838 } catch (error) { 839 let e: BusinessError = error as BusinessError; 840 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorCode ' + e.code); 841 hilog.error(0x0000, 'testTag', 'rpc write int fail, errorMessage ' + e.message); 842 } 843 try { 844 let ret = data.readInt(); 845 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 846 } catch (error) { 847 let e: BusinessError = error as BusinessError; 848 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorCode ' + e.code); 849 hilog.error(0x0000, 'testTag', 'rpc read int fail, errorMessage ' + e.message); 850 } 851 ``` 852 853### writeLong 854 855writeLong(val: number): void 856 857Writes a long integer to this **MessageSequence** object. 858 859**System capability**: SystemCapability.Communication.IPC.Core 860 861**Parameters** 862 863 | Name| Type | Mandatory| Description | 864 | ------ | ------ | ---- | ---------------- | 865 | val | number | Yes | Long integer to write.| 866 867**Error codes** 868 869For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 870 871 | ID| Error Message| 872 | -------- | -------- | 873 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 874 | 1900009 | Failed to write data to the message sequence. | 875 876**Example** 877 878 ```ts 879 import { hilog } from '@kit.PerformanceAnalysisKit'; 880 import { BusinessError } from '@kit.BasicServicesKit'; 881 882 let data = rpc.MessageSequence.create(); 883 try { 884 data.writeLong(10000); 885 } catch (error) { 886 let e: BusinessError = error as BusinessError; 887 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 888 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 889 } 890 ``` 891 892### readLong 893 894readLong(): number 895 896Reads the long integer from this **MessageSequence** object. 897 898**System capability**: SystemCapability.Communication.IPC.Core 899 900**Return value** 901 902 | Type | Description | 903 | ------ | -------------- | 904 | number | Long integer read.| 905 906**Error codes** 907 908For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 909 910 | ID| Error Message| 911 | -------- | -------- | 912 | 1900010 | Failed to read data from the message sequence. | 913 914**Example** 915 916 ```ts 917 import { hilog } from '@kit.PerformanceAnalysisKit'; 918 import { BusinessError } from '@kit.BasicServicesKit'; 919 920 let data = rpc.MessageSequence.create(); 921 try { 922 data.writeLong(10000); 923 } catch (error) { 924 let e: BusinessError = error as BusinessError; 925 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorCode ' + e.code); 926 hilog.error(0x0000, 'testTag', 'rpc write long fail, errorMessage ' + e.message); 927 } 928 try { 929 let ret = data.readLong(); 930 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 931 } catch (error) { 932 let e: BusinessError = error as BusinessError; 933 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorCode ' + e.code); 934 hilog.error(0x0000, 'testTag', 'rpc read long fail, errorMessage ' + e.message); 935 } 936 ``` 937 938### writeFloat 939 940writeFloat(val: number): void 941 942Writes a floating-point number to this **MessageSequence** object. 943 944**System capability**: SystemCapability.Communication.IPC.Core 945 946**Parameters** 947 948 | Name| Type | Mandatory| Description | 949 | ------ | ------ | ---- | ----- | 950 | val | number | Yes | Floating-point number to write.| 951 952**Error codes** 953 954For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 955 956 | ID| Error Message| 957 | -------- | -------- | 958 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 959 | 1900009 | Failed to write data to the message sequence. | 960 961**Example** 962 963 ```ts 964 import { hilog } from '@kit.PerformanceAnalysisKit'; 965 import { BusinessError } from '@kit.BasicServicesKit'; 966 967 let data = rpc.MessageSequence.create(); 968 try { 969 data.writeFloat(1.2); 970 } catch (error) { 971 let e: BusinessError = error as BusinessError; 972 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 973 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 974 } 975 ``` 976 977### readFloat 978 979readFloat(): number 980 981Reads the floating-point number from this **MessageSequence** object. 982 983**System capability**: SystemCapability.Communication.IPC.Core 984 985**Return value** 986 987 | Type | Description | 988 | ------ | ------------ | 989 | number | Floating-point number read.| 990 991**Error codes** 992 993For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 994 995 | ID| Error Message| 996 | -------- | -------- | 997 | 1900010 | Failed to read data from the message sequence. | 998 999**Example** 1000 1001 ```ts 1002 import { hilog } from '@kit.PerformanceAnalysisKit'; 1003 import { BusinessError } from '@kit.BasicServicesKit'; 1004 1005 let data = rpc.MessageSequence.create(); 1006 try { 1007 data.writeFloat(1.2); 1008 } catch (error) { 1009 let e: BusinessError = error as BusinessError; 1010 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorCode ' + e.code); 1011 hilog.error(0x0000, 'testTag', 'rpc write float fail, errorMessage ' + e.message); 1012 } 1013 try { 1014 let ret = data.readFloat(); 1015 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 1016 } catch (error) { 1017 let e: BusinessError = error as BusinessError; 1018 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorCode ' + e.code); 1019 hilog.error(0x0000, 'testTag', 'rpc read float fail, errorMessage ' + e.message); 1020 } 1021 ``` 1022 1023### writeDouble 1024 1025writeDouble(val: number): void 1026 1027Writes a double-precision floating-point number to this **MessageSequence** object. 1028 1029**System capability**: SystemCapability.Communication.IPC.Core 1030 1031**Parameters** 1032 1033 | Name| Type | Mandatory| Description | 1034 | ------ | ------ | ---- | ---------------------- | 1035 | val | number | Yes | Double-precision floating-point number to write.| 1036 1037**Error codes** 1038 1039For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1040 1041 | ID| Error Message| 1042 | -------- | -------- | 1043 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1044 | 1900009 | Failed to write data to the message sequence. | 1045 1046**Example** 1047 1048 ```ts 1049 import { hilog } from '@kit.PerformanceAnalysisKit'; 1050 import { BusinessError } from '@kit.BasicServicesKit'; 1051 1052 let data = rpc.MessageSequence.create(); 1053 try { 1054 data.writeDouble(10.2); 1055 } catch (error) { 1056 let e: BusinessError = error as BusinessError; 1057 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1058 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1059 } 1060 ``` 1061 1062### readDouble 1063 1064readDouble(): number 1065 1066Reads the double-precision floating-point number from this **MessageSequence** object. 1067 1068**System capability**: SystemCapability.Communication.IPC.Core 1069 1070**Return value** 1071 1072 | Type | Description | 1073 | ------ | ------------------ | 1074 | number | Double-precision floating-point number read.| 1075 1076**Error codes** 1077 1078For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1079 1080 | ID| Error Message| 1081 | -------- | -------- | 1082 | 1900010 | Failed to read data from the message sequence. | 1083 1084**Example** 1085 1086 ```ts 1087 import { hilog } from '@kit.PerformanceAnalysisKit'; 1088 import { BusinessError } from '@kit.BasicServicesKit'; 1089 1090 let data = rpc.MessageSequence.create(); 1091 try { 1092 data.writeDouble(10.2); 1093 } catch (error) { 1094 let e: BusinessError = error as BusinessError; 1095 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorCode ' + e.code); 1096 hilog.error(0x0000, 'testTag', 'rpc write double fail, errorMessage ' + e.message); 1097 } 1098 try { 1099 let ret = data.readDouble(); 1100 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 1101 } catch (error) { 1102 let e: BusinessError = error as BusinessError; 1103 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorCode ' + e.code); 1104 hilog.error(0x0000, 'testTag', 'rpc read double fail, errorMessage ' + e.message); 1105 } 1106 ``` 1107 1108### writeBoolean 1109 1110writeBoolean(val: boolean): void 1111 1112Writes a Boolean value to this **MessageSequence** object. 1113 1114**System capability**: SystemCapability.Communication.IPC.Core 1115 1116**Parameters** 1117 1118 | Name| Type | Mandatory| Description | 1119 | ------ | ------- | ---- | ---------------- | 1120 | val | boolean | Yes | Boolean value to write.| 1121 1122**Error codes** 1123 1124For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1125 1126 | ID| Error Message| 1127 | -------- | -------- | 1128 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1129 | 1900009 | Failed to write data to the message sequence. | 1130 1131**Example** 1132 1133 ```ts 1134 import { hilog } from '@kit.PerformanceAnalysisKit'; 1135 import { BusinessError } from '@kit.BasicServicesKit'; 1136 1137 let data = rpc.MessageSequence.create(); 1138 try { 1139 data.writeBoolean(false); 1140 } catch (error) { 1141 let e: BusinessError = error as BusinessError; 1142 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1143 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1144 } 1145 ``` 1146 1147### readBoolean 1148 1149readBoolean(): boolean 1150 1151Reads the Boolean value from this **MessageSequence** object. 1152 1153**System capability**: SystemCapability.Communication.IPC.Core 1154 1155**Return value** 1156 1157 | Type | Description | 1158 | ------- | -------------------- | 1159 | boolean | Boolean value read.| 1160 1161**Error codes** 1162 1163For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1164 1165 | ID| Error Message| 1166 | -------- | -------- | 1167 | 1900010 | Failed to read data from the message sequence. | 1168 1169**Example** 1170 1171 ```ts 1172 import { hilog } from '@kit.PerformanceAnalysisKit'; 1173 import { BusinessError } from '@kit.BasicServicesKit'; 1174 1175 let data = rpc.MessageSequence.create(); 1176 try { 1177 data.writeBoolean(false); 1178 } catch (error) { 1179 let e: BusinessError = error as BusinessError; 1180 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorCode ' + e.code); 1181 hilog.error(0x0000, 'testTag', 'rpc write boolean fail, errorMessage ' + e.message); 1182 } 1183 try { 1184 let ret = data.readBoolean(); 1185 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 1186 } catch (error) { 1187 let e: BusinessError = error as BusinessError; 1188 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorCode ' + e.code); 1189 hilog.error(0x0000, 'testTag', 'rpc read boolean fail, errorMessage ' + e.message); 1190 } 1191 ``` 1192 1193### writeChar 1194 1195writeChar(val: number): void 1196 1197Writes a character to this **MessageSequence** object. 1198 1199**System capability**: SystemCapability.Communication.IPC.Core 1200 1201**Parameters** 1202 1203 | Name| Type | Mandatory| Description | 1204 | ------ | ------ | ---- | -------------------- | 1205 | val | number | Yes | Single character to write.| 1206 1207**Error codes** 1208 1209For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1210 1211 | ID| Error Message| 1212 | -------- | -------- | 1213 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1214 | 1900009 | Failed to write data to the message sequence. | 1215 1216**Example** 1217 1218 ```ts 1219 import { hilog } from '@kit.PerformanceAnalysisKit'; 1220 import { BusinessError } from '@kit.BasicServicesKit'; 1221 1222 let data = rpc.MessageSequence.create(); 1223 try { 1224 data.writeChar(97); 1225 } catch (error) { 1226 let e: BusinessError = error as BusinessError; 1227 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1228 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1229 } 1230 ``` 1231 1232### readChar 1233 1234readChar(): number 1235 1236Reads the character from this **MessageSequence** object. 1237 1238**System capability**: SystemCapability.Communication.IPC.Core 1239 1240**Return value** 1241 1242 | Type | Description| 1243 | ------ | ---- | 1244 | number | Character read.| 1245 1246**Error codes** 1247 1248For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1249 1250 | ID| Error Message| 1251 | -------- | -------- | 1252 | 1900010 | Failed to read data from the message sequence. | 1253 1254**Example** 1255 1256 ```ts 1257 import { hilog } from '@kit.PerformanceAnalysisKit'; 1258 import { BusinessError } from '@kit.BasicServicesKit'; 1259 1260 let data = rpc.MessageSequence.create(); 1261 try { 1262 data.writeChar(97); 1263 } catch (error) { 1264 let e: BusinessError = error as BusinessError; 1265 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorCode ' + e.code); 1266 hilog.error(0x0000, 'testTag', 'rpc write char fail, errorMessage ' + e.message); 1267 } 1268 try { 1269 let ret = data.readChar(); 1270 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 1271 } catch (error) { 1272 let e: BusinessError = error as BusinessError; 1273 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorCode ' + e.code); 1274 hilog.error(0x0000, 'testTag', 'rpc read char fail, errorMessage ' + e.message); 1275 } 1276 ``` 1277 1278### writeString 1279 1280writeString(val: string): void 1281 1282Writes a string to this **MessageSequence** object. 1283 1284**System capability**: SystemCapability.Communication.IPC.Core 1285 1286**Parameters** 1287 1288 | Name| Type | Mandatory| Description | 1289 | ------ | ------ | ---- | ----------------------------------------- | 1290 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 1291 1292**Error codes** 1293 1294For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1295 1296 | ID| Error Message| 1297 | -------- | -------- | 1298 | 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. | 1299 | 1900009 | Failed to write data to the message sequence. | 1300 1301**Example** 1302 1303 ```ts 1304 import { hilog } from '@kit.PerformanceAnalysisKit'; 1305 import { BusinessError } from '@kit.BasicServicesKit'; 1306 1307 let data = rpc.MessageSequence.create(); 1308 try { 1309 data.writeString('abc'); 1310 } catch (error) { 1311 let e: BusinessError = error as BusinessError; 1312 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1313 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1314 } 1315 ``` 1316 1317### readString 1318 1319readString(): string 1320 1321Reads the string from this **MessageSequence** object. 1322 1323**System capability**: SystemCapability.Communication.IPC.Core 1324 1325**Return value** 1326 1327 | Type | Description | 1328 | ------ | -------------- | 1329 | string | String read.| 1330 1331**Error codes** 1332 1333For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1334 1335 | ID| Error Message| 1336 | -------- | -------- | 1337 | 1900010 | Failed to read data from the message sequence. | 1338 1339**Example** 1340 1341 ```ts 1342 import { hilog } from '@kit.PerformanceAnalysisKit'; 1343 import { BusinessError } from '@kit.BasicServicesKit'; 1344 1345 let data = rpc.MessageSequence.create(); 1346 try { 1347 data.writeString('abc'); 1348 } catch (error) { 1349 let e: BusinessError = error as BusinessError; 1350 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorCode ' + e.code); 1351 hilog.error(0x0000, 'testTag', 'rpc write string fail, errorMessage ' + e.message); 1352 } 1353 try { 1354 let ret = data.readString(); 1355 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 1356 } catch (error) { 1357 let e: BusinessError = error as BusinessError; 1358 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorCode ' + e.code); 1359 hilog.error(0x0000, 'testTag', 'rpc read string fail, errorMessage ' + e.message); 1360 } 1361 ``` 1362 1363### writeParcelable 1364 1365writeParcelable(val: Parcelable): void 1366 1367Writes a **Parcelable** object to this **MessageSequence** object. 1368 1369**System capability**: SystemCapability.Communication.IPC.Core 1370 1371**Parameters** 1372 1373| Name| Type| Mandatory| Description| 1374| ------ | --------- | ---- | ------ | 1375| val | [Parcelable](#parcelable9) | Yes | **Parcelable** object to write.| 1376 1377**Error codes** 1378 1379For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1380 1381 | ID| Error Message| 1382 | -------- | -------- | 1383 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 1384 | 1900009 | Failed to write data to the message sequence. | 1385 1386**Example** 1387 1388 ```ts 1389 import { hilog } from '@kit.PerformanceAnalysisKit'; 1390 import { BusinessError } from '@kit.BasicServicesKit'; 1391 1392 class MyParcelable implements rpc.Parcelable { 1393 num: number = 0; 1394 str: string = ''; 1395 constructor( num: number, str: string) { 1396 this.num = num; 1397 this.str = str; 1398 } 1399 marshalling(messageSequence: rpc.MessageSequence): boolean { 1400 messageSequence.writeInt(this.num); 1401 messageSequence.writeString(this.str); 1402 return true; 1403 } 1404 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1405 this.num = messageSequence.readInt(); 1406 this.str = messageSequence.readString(); 1407 return true; 1408 } 1409 } 1410 let parcelable = new MyParcelable(1, "aaa"); 1411 let data = rpc.MessageSequence.create(); 1412 try { 1413 data.writeParcelable(parcelable); 1414 } catch (error) { 1415 let e: BusinessError = error as BusinessError; 1416 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorCode ' + e.code); 1417 hilog.error(0x0000, 'testTag', 'rpc write parcelable fail, errorMessage ' + e.message); 1418 } 1419 ``` 1420 1421### readParcelable 1422 1423readParcelable(dataIn: Parcelable): void 1424 1425Reads a **Parcelable** object from this **MessageSequence** object to the specified object (**dataIn**). 1426 1427**System capability**: SystemCapability.Communication.IPC.Core 1428 1429**Parameters** 1430 1431| Name| Type | Mandatory| Description | 1432| ------ | -------------------------- | ---- | ----------------------------------------- | 1433| dataIn | [Parcelable](#parcelable9) | Yes | **Parcelable** object to read.| 1434 1435**Error codes** 1436 1437For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1438 1439 | ID| Error Message| 1440 | -------- | -------- | 1441 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect. | 1442 | 1900010 | Failed to read data from the message sequence. | 1443 | 1900012 | Failed to call the JS callback function. | 1444 1445**Example** 1446 1447 ```ts 1448 import { hilog } from '@kit.PerformanceAnalysisKit'; 1449 import { BusinessError } from '@kit.BasicServicesKit'; 1450 1451 class MyParcelable implements rpc.Parcelable { 1452 num: number = 0; 1453 str: string = ''; 1454 constructor(num: number, str: string) { 1455 this.num = num; 1456 this.str = str; 1457 } 1458 marshalling(messageSequence: rpc.MessageSequence): boolean { 1459 messageSequence.writeInt(this.num); 1460 messageSequence.writeString(this.str); 1461 return true; 1462 } 1463 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 1464 this.num = messageSequence.readInt(); 1465 this.str = messageSequence.readString(); 1466 return true; 1467 } 1468 } 1469 let parcelable = new MyParcelable(1, "aaa"); 1470 let data = rpc.MessageSequence.create(); 1471 data.writeParcelable(parcelable); 1472 let ret = new MyParcelable(0, ""); 1473 try { 1474 data.readParcelable(ret); 1475 }catch (error) { 1476 let e: BusinessError = error as BusinessError; 1477 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorCode ' + e.code); 1478 hilog.error(0x0000, 'testTag', 'rpc read parcelable fail, errorMessage ' + e.message); 1479 } 1480 ``` 1481 1482### writeByteArray 1483 1484writeByteArray(byteArray: number[]): void 1485 1486Writes a byte array to this **MessageSequence** object. 1487 1488**System capability**: SystemCapability.Communication.IPC.Core 1489 1490**Parameters** 1491 1492 | Name | Type | Mandatory| Description | 1493 | --------- | -------- | ---- | ------------------ | 1494 | byteArray | number[] | Yes | Byte array to write.| 1495 1496**Error codes** 1497 1498For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1499 1500 | ID| Error Message| 1501 | -------- | -------- | 1502 | 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. | 1503 | 1900009 | Failed to write data to the message sequence. | 1504 1505**Example** 1506 1507 ```ts 1508 import { hilog } from '@kit.PerformanceAnalysisKit'; 1509 import { BusinessError } from '@kit.BasicServicesKit'; 1510 1511 let data = rpc.MessageSequence.create(); 1512 let ByteArrayVar = [1, 2, 3, 4, 5]; 1513 try { 1514 data.writeByteArray(ByteArrayVar); 1515 } catch (error) { 1516 let e: BusinessError = error as BusinessError; 1517 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1518 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1519 } 1520 ``` 1521 1522### readByteArray 1523 1524readByteArray(dataIn: number[]): void 1525 1526Reads a byte array from this **MessageSequence** object. 1527 1528**System capability**: SystemCapability.Communication.IPC.Core 1529 1530**Parameters** 1531 1532 | Name| Type | Mandatory| Description | 1533 | ------ | -------- | ---- | ------------------ | 1534 | dataIn | number[] | Yes | Byte array to read.| 1535 1536**Error codes** 1537 1538For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1539 1540 | ID| Error Message| 1541 | -------- | -------- | 1542 | 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. | 1543 | 1900010 | Failed to read data from the message sequence. | 1544 1545**Example** 1546 1547 ```ts 1548 import { hilog } from '@kit.PerformanceAnalysisKit'; 1549 import { BusinessError } from '@kit.BasicServicesKit'; 1550 1551 let data = rpc.MessageSequence.create(); 1552 let ByteArrayVar = [1, 2, 3, 4, 5]; 1553 try { 1554 data.writeByteArray(ByteArrayVar); 1555 } catch (error) { 1556 let e: BusinessError = error as BusinessError; 1557 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1558 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1559 } 1560 try { 1561 let array: Array<number> = new Array(5); 1562 data.readByteArray(array); 1563 } catch (error) { 1564 let e: BusinessError = error as BusinessError; 1565 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1566 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1567 } 1568 ``` 1569 1570### readByteArray 1571 1572readByteArray(): number[] 1573 1574Reads the byte array from this **MessageSequence** object. 1575 1576**System capability**: SystemCapability.Communication.IPC.Core 1577 1578**Return value** 1579 1580 | Type | Description | 1581 | -------- | -------------- | 1582 | number[] | Byte array read.| 1583 1584**Error codes** 1585 1586For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1587 1588 | ID| Error Message| 1589 | -------- | -------- | 1590 | 401 | check param failed | 1591 | 1900010 | Failed to read data from the message sequence. | 1592 1593**Example** 1594 1595 ```ts 1596 import { hilog } from '@kit.PerformanceAnalysisKit'; 1597 import { BusinessError } from '@kit.BasicServicesKit'; 1598 1599 let data = rpc.MessageSequence.create(); 1600 let byteArrayVar = [1, 2, 3, 4, 5]; 1601 try { 1602 data.writeByteArray(byteArrayVar); 1603 } catch (error) { 1604 let e: BusinessError = error as BusinessError; 1605 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorCode ' + e.code); 1606 hilog.error(0x0000, 'testTag', 'rpc write byteArray fail, errorMessage ' + e.message); 1607 } 1608 try { 1609 let array = data.readByteArray(); 1610 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 1611 } catch (error) { 1612 let e: BusinessError = error as BusinessError; 1613 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorCode ' + e.code); 1614 hilog.error(0x0000, 'testTag', 'rpc read byteArray fail, errorMessage ' + e.message); 1615 } 1616 ``` 1617 1618### writeShortArray 1619 1620writeShortArray(shortArray: number[]): void 1621 1622Writes a short array to this **MessageSequence** object. 1623 1624**System capability**: SystemCapability.Communication.IPC.Core 1625 1626**Parameters** 1627 1628 | Name | Type | Mandatory| Description | 1629 | ---------- | -------- | ---- | -------------------- | 1630 | shortArray | number[] | Yes | Short array to write.| 1631 1632**Error codes** 1633 1634For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1635 1636 | ID| Error Message| 1637 | -------- | -------- | 1638 | 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. | 1639 | 1900009 | Failed to write data to the message sequence. | 1640 1641**Example** 1642 1643 ```ts 1644 import { hilog } from '@kit.PerformanceAnalysisKit'; 1645 import { BusinessError } from '@kit.BasicServicesKit'; 1646 1647 let data = rpc.MessageSequence.create(); 1648 try { 1649 data.writeShortArray([11, 12, 13]); 1650 } catch (error) { 1651 let e: BusinessError = error as BusinessError; 1652 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1653 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1654 } 1655 ``` 1656 1657### readShortArray 1658 1659readShortArray(dataIn: number[]): void 1660 1661Reads a short array from this **MessageSequence** object. 1662 1663**System capability**: SystemCapability.Communication.IPC.Core 1664 1665**Parameters** 1666 1667 | Name| Type | Mandatory| Description | 1668 | ------ | -------- | ---- | -------------------- | 1669 | dataIn | number[] | Yes | Short array to read.| 1670 1671**Error codes** 1672 1673For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1674 1675 | ID| Error Message| 1676 | -------- | -------- | 1677 | 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. | 1678 | 1900010 | Failed to read data from the message sequence. | 1679 1680**Example** 1681 1682 ```ts 1683 import { hilog } from '@kit.PerformanceAnalysisKit'; 1684 import { BusinessError } from '@kit.BasicServicesKit'; 1685 1686 let data = rpc.MessageSequence.create(); 1687 try { 1688 data.writeShortArray([11, 12, 13]); 1689 } catch (error) { 1690 let e: BusinessError = error as BusinessError; 1691 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1692 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1693 } 1694 try { 1695 let array: Array<number> = new Array(3); 1696 data.readShortArray(array); 1697 } catch (error) { 1698 let e: BusinessError = error as BusinessError; 1699 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1700 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1701 } 1702 ``` 1703 1704### readShortArray 1705 1706readShortArray(): number[] 1707 1708Reads the short array from this **MessageSequence** object. 1709 1710**System capability**: SystemCapability.Communication.IPC.Core 1711 1712**Return value** 1713 1714 | Type | Description | 1715 | -------- | ---------------- | 1716 | number[] | Short array read.| 1717 1718**Error codes** 1719 1720For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1721 1722 | ID| Error Message| 1723 | -------- | -------- | 1724 | 1900010 | Failed to read data from the message sequence. | 1725 1726**Example** 1727 1728 ```ts 1729 import { hilog } from '@kit.PerformanceAnalysisKit'; 1730 import { BusinessError } from '@kit.BasicServicesKit'; 1731 1732 let data = rpc.MessageSequence.create(); 1733 try { 1734 data.writeShortArray([11, 12, 13]); 1735 } catch (error) { 1736 let e: BusinessError = error as BusinessError; 1737 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorCode ' + e.code); 1738 hilog.error(0x0000, 'testTag', 'rpc write shortArray fail, errorMessage ' + e.message); 1739 } 1740 try { 1741 let array = data.readShortArray(); 1742 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 1743 } catch (error) { 1744 let e: BusinessError = error as BusinessError; 1745 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorCode ' + e.code); 1746 hilog.error(0x0000, 'testTag', 'rpc read shortArray fail, errorMessage ' + e.message); 1747 } 1748 ``` 1749 1750### writeIntArray 1751 1752writeIntArray(intArray: number[]): void 1753 1754Writes an integer array to this **MessageSequence** object. 1755 1756**System capability**: SystemCapability.Communication.IPC.Core 1757 1758**Parameters** 1759 1760 | Name | Type | Mandatory| Description | 1761 | -------- | -------- | ---- | ------------------ | 1762 | intArray | number[] | Yes | Integer array to write.| 1763 1764**Error codes** 1765 1766For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1767 1768 | ID| Error Message| 1769 | -------- | -------- | 1770 | 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. | 1771 | 1900009 | Failed to write data to the message sequence. | 1772 1773**Example** 1774 1775 ```ts 1776 import { hilog } from '@kit.PerformanceAnalysisKit'; 1777 import { BusinessError } from '@kit.BasicServicesKit'; 1778 1779 let data = rpc.MessageSequence.create(); 1780 try { 1781 data.writeIntArray([100, 111, 112]); 1782 } catch (error) { 1783 let e: BusinessError = error as BusinessError; 1784 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1785 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1786 } 1787 ``` 1788 1789### readIntArray 1790 1791readIntArray(dataIn: number[]): void 1792 1793Reads an integer array from this **MessageSequence** object. 1794 1795**System capability**: SystemCapability.Communication.IPC.Core 1796 1797**Parameters** 1798 1799 | Name| Type | Mandatory| Description | 1800 | ------ | -------- | ---- | ------------------ | 1801 | dataIn | number[] | Yes | Integer array to read.| 1802 1803**Error codes** 1804 1805For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1806 1807 | ID| Error Message| 1808 | -------- | -------- | 1809 | 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. | 1810 | 1900010 | Failed to read data from the message sequence. | 1811 1812**Example** 1813 1814 ```ts 1815 import { hilog } from '@kit.PerformanceAnalysisKit'; 1816 import { BusinessError } from '@kit.BasicServicesKit'; 1817 1818 let data = rpc.MessageSequence.create(); 1819 try { 1820 data.writeIntArray([100, 111, 112]); 1821 } catch (error) { 1822 let e: BusinessError = error as BusinessError; 1823 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1824 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1825 } 1826 let array: Array<number> = new Array(3); 1827 try { 1828 data.readIntArray(array); 1829 } catch (error) { 1830 let e: BusinessError = error as BusinessError; 1831 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1832 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1833 } 1834 ``` 1835 1836### readIntArray 1837 1838readIntArray(): number[] 1839 1840Reads the integer array from this **MessageSequence** object. 1841 1842**System capability**: SystemCapability.Communication.IPC.Core 1843 1844**Return value** 1845 1846 | Type | Description | 1847 | -------- | -------------- | 1848 | number[] | Integer array read.| 1849 1850**Error codes** 1851 1852For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1853 1854 | ID| Error Message| 1855 | -------- | -------- | 1856 | 1900010 | Failed to read data from the message sequence. | 1857 1858**Example** 1859 1860 ```ts 1861 import { hilog } from '@kit.PerformanceAnalysisKit'; 1862 import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864 let data = rpc.MessageSequence.create(); 1865 try { 1866 data.writeIntArray([100, 111, 112]); 1867 } catch (error) { 1868 let e: BusinessError = error as BusinessError; 1869 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorCode ' + e.code); 1870 hilog.error(0x0000, 'testTag', 'rpc write intArray fail, errorMessage ' + e.message); 1871 } 1872 try { 1873 let array = data.readIntArray(); 1874 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 1875 } catch (error) { 1876 let e: BusinessError = error as BusinessError; 1877 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorCode ' + e.code); 1878 hilog.error(0x0000, 'testTag', 'rpc read intArray fail, errorMessage ' + e.message); 1879 } 1880 ``` 1881 1882### writeLongArray 1883 1884writeLongArray(longArray: number[]): void 1885 1886Writes a long array to this **MessageSequence** object. 1887 1888**System capability**: SystemCapability.Communication.IPC.Core 1889 1890**Parameters** 1891 1892 | Name | Type | Mandatory| Description | 1893 | --------- | -------- | ---- | -------------------- | 1894 | longArray | number[] | Yes | Long array to write.| 1895 1896**Error codes** 1897 1898For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1899 1900 | ID| Error Message| 1901 | -------- | -------- | 1902 | 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. | 1903 | 1900009 | Failed to write data to the message sequence. | 1904 1905**Example** 1906 1907 ```ts 1908 import { hilog } from '@kit.PerformanceAnalysisKit'; 1909 import { BusinessError } from '@kit.BasicServicesKit'; 1910 1911 let data = rpc.MessageSequence.create(); 1912 try { 1913 data.writeLongArray([1111, 1112, 1113]); 1914 }catch (error){ 1915 let e: BusinessError = error as BusinessError; 1916 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1917 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1918 } 1919 ``` 1920 1921### readLongArray 1922 1923readLongArray(dataIn: number[]): void 1924 1925Reads a long array from this **MessageSequence** object. 1926 1927**System capability**: SystemCapability.Communication.IPC.Core 1928 1929**Parameters** 1930 1931 | Name| Type | Mandatory| Description | 1932 | ------ | -------- | ---- | -------------------- | 1933 | dataIn | number[] | Yes | Long array to read.| 1934 1935**Error codes** 1936 1937For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1938 1939 | ID| Error Message| 1940 | -------- | -------- | 1941 | 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. | 1942 | 1900010 | Failed to read data from the message sequence. | 1943 1944**Example** 1945 1946 ```ts 1947 import { hilog } from '@kit.PerformanceAnalysisKit'; 1948 import { BusinessError } from '@kit.BasicServicesKit'; 1949 1950 let data = rpc.MessageSequence.create(); 1951 try { 1952 data.writeLongArray([1111, 1112, 1113]); 1953 } catch (error) { 1954 let e: BusinessError = error as BusinessError; 1955 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 1956 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 1957 } 1958 let array: Array<number> = new Array(3); 1959 try { 1960 data.readLongArray(array); 1961 } catch (error) { 1962 let e: BusinessError = error as BusinessError; 1963 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 1964 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 1965 } 1966 ``` 1967 1968### readLongArray 1969 1970readLongArray(): number[] 1971 1972Reads the long integer array from this **MessageSequence** object. 1973 1974**System capability**: SystemCapability.Communication.IPC.Core 1975 1976**Return value** 1977 1978 | Type | Description | 1979 | -------- | ---------------- | 1980 | number[] | Long array read.| 1981 1982**Error codes** 1983 1984For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 1985 1986 | ID| Error Message| 1987 | -------- | -------- | 1988 | 1900010 | Failed to read data from the message sequence. | 1989 1990**Example** 1991 1992 ```ts 1993 import { hilog } from '@kit.PerformanceAnalysisKit'; 1994 import { BusinessError } from '@kit.BasicServicesKit'; 1995 1996 let data = rpc.MessageSequence.create(); 1997 try { 1998 data.writeLongArray([1111, 1112, 1113]); 1999 } catch (error) { 2000 let e: BusinessError = error as BusinessError; 2001 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorCode ' + e.code); 2002 hilog.error(0x0000, 'testTag', 'rpc write longArray fail, errorMessage ' + e.message); 2003 } 2004 try { 2005 let array = data.readLongArray(); 2006 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 2007 } catch (error) { 2008 let e: BusinessError = error as BusinessError; 2009 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorCode ' + e.code); 2010 hilog.error(0x0000, 'testTag', 'rpc read longArray fail, errorMessage ' + e.message); 2011 } 2012 ``` 2013 2014### writeFloatArray 2015 2016writeFloatArray(floatArray: number[]): void 2017 2018Writes a floating-point array to this **MessageSequence** object. 2019 2020**System capability**: SystemCapability.Communication.IPC.Core 2021 2022**Parameters** 2023 2024 | Name | Type | Mandatory| Description | 2025 | ---------- | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2026 | 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.| 2027 2028**Error codes** 2029 2030For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2031 2032 | ID| Error Message| 2033 | -------- | -------- | 2034 | 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. | 2035 | 1900009 | Failed to write data to the message sequence. | 2036 2037**Example** 2038 2039 ```ts 2040 import { hilog } from '@kit.PerformanceAnalysisKit'; 2041 import { BusinessError } from '@kit.BasicServicesKit'; 2042 2043 let data = rpc.MessageSequence.create(); 2044 try { 2045 data.writeFloatArray([1.2, 1.3, 1.4]); 2046 } catch (error) { 2047 let e: BusinessError = error as BusinessError; 2048 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2049 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2050 } 2051 ``` 2052 2053### readFloatArray 2054 2055readFloatArray(dataIn: number[]): void 2056 2057Reads a floating-point array from this **MessageSequence** object. 2058 2059**System capability**: SystemCapability.Communication.IPC.Core 2060 2061**Parameters** 2062 2063 | Name| Type | Mandatory| Description | 2064 | ------ | -------- | ---- | ----------------------------------------------------------------------------------------------------------------------- | 2065 | 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.| 2066 2067**Error codes** 2068 2069For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2070 2071 | ID| Error Message| 2072 | -------- | -------- | 2073 | 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. | 2074 | 1900010 | Failed to read data from the message sequence. | 2075 2076**Example** 2077 2078 ```ts 2079 import { hilog } from '@kit.PerformanceAnalysisKit'; 2080 import { BusinessError } from '@kit.BasicServicesKit'; 2081 2082 let data = rpc.MessageSequence.create(); 2083 try { 2084 data.writeFloatArray([1.2, 1.3, 1.4]); 2085 }catch (error){ 2086 let e: BusinessError = error as BusinessError; 2087 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2088 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2089 } 2090 let array: Array<number> = new Array(3); 2091 try { 2092 data.readFloatArray(array); 2093 } catch (error) { 2094 let e: BusinessError = error as BusinessError; 2095 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2096 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2097 } 2098 ``` 2099 2100### readFloatArray 2101 2102readFloatArray(): number[] 2103 2104Reads the floating-point array from this **MessageSequence** object. 2105 2106**System capability**: SystemCapability.Communication.IPC.Core 2107 2108**Return value** 2109 2110 | Type | Description | 2111 | -------- | -------------- | 2112 | number[] | Floating-point array read.| 2113 2114**Error codes** 2115 2116For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2117 2118 | ID| Error Message| 2119 | -------- | -------- | 2120 | 1900010 | Failed to read data from the message sequence. | 2121 2122**Example** 2123 2124 ```ts 2125 import { hilog } from '@kit.PerformanceAnalysisKit'; 2126 import { BusinessError } from '@kit.BasicServicesKit'; 2127 2128 let data = rpc.MessageSequence.create(); 2129 try { 2130 data.writeFloatArray([1.2, 1.3, 1.4]); 2131 } catch (error) { 2132 let e: BusinessError = error as BusinessError; 2133 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorCode ' + e.code); 2134 hilog.error(0x0000, 'testTag', 'rpc write floatArray fail, errorMessage ' + e.message); 2135 } 2136 try { 2137 let array = data.readFloatArray(); 2138 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 2139 } catch (error) { 2140 let e: BusinessError = error as BusinessError; 2141 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorCode ' + e.code); 2142 hilog.error(0x0000, 'testTag', 'rpc read floatArray fail, errorMessage ' + e.message); 2143 } 2144 ``` 2145 2146### writeDoubleArray 2147 2148writeDoubleArray(doubleArray: number[]): void 2149 2150Writes a double-precision floating-point array to this **MessageSequence** object. 2151 2152**System capability**: SystemCapability.Communication.IPC.Core 2153 2154**Parameters** 2155 2156 | Name | Type | Mandatory| Description | 2157 | ----------- | -------- | ---- | ------------------------ | 2158 | doubleArray | number[] | Yes | Double-precision floating-point array to write.| 2159 2160**Error codes** 2161 2162For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2163 2164 | ID| Error Message| 2165 | -------- | -------- | 2166 | 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. | 2167 | 1900009 | Failed to write data to the message sequence. | 2168 2169**Example** 2170 2171 ```ts 2172 import { hilog } from '@kit.PerformanceAnalysisKit'; 2173 import { BusinessError } from '@kit.BasicServicesKit'; 2174 2175 let data = rpc.MessageSequence.create(); 2176 try { 2177 data.writeDoubleArray([11.1, 12.2, 13.3]); 2178 } catch (error) { 2179 let e: BusinessError = error as BusinessError; 2180 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2181 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2182 } 2183 ``` 2184 2185### readDoubleArray 2186 2187readDoubleArray(dataIn: number[]): void 2188 2189Reads a double-precision floating-point array from this **MessageSequence** object. 2190 2191**System capability**: SystemCapability.Communication.IPC.Core 2192 2193**Parameters** 2194 2195 | Name| Type | Mandatory| Description | 2196 | ------ | -------- | ---- | ------------------------ | 2197 | dataIn | number[] | Yes | Double-precision floating-point array to read.| 2198 2199**Error codes** 2200 2201For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2202 2203 | ID| Error Message| 2204 | -------- | -------- | 2205 | 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. | 2206 | 1900010 | Failed to read data from the message sequence. | 2207 2208**Example** 2209 2210 ```ts 2211 import { hilog } from '@kit.PerformanceAnalysisKit'; 2212 import { BusinessError } from '@kit.BasicServicesKit'; 2213 2214 let data = rpc.MessageSequence.create(); 2215 try { 2216 data.writeDoubleArray([11.1, 12.2, 13.3]); 2217 } catch (error) { 2218 let e: BusinessError = error as BusinessError; 2219 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2220 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2221 } 2222 let array: Array<number> = new Array(3); 2223 try { 2224 data.readDoubleArray(array); 2225 } catch (error) { 2226 let e: BusinessError = error as BusinessError; 2227 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2228 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2229 } 2230 ``` 2231 2232### readDoubleArray 2233 2234readDoubleArray(): number[] 2235 2236Reads the double-precision floating-point array from this **MessageSequence** object. 2237 2238**System capability**: SystemCapability.Communication.IPC.Core 2239 2240**Return value** 2241 2242 | Type | Description | 2243 | -------- | -------------------- | 2244 | number[] | Double-precision floating-point array read.| 2245 2246**Error codes** 2247 2248For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2249 2250 | ID| Error Message| 2251 | -------- | -------- | 2252 | 1900010 | Failed to read data from the message sequence. | 2253 2254**Example** 2255 2256 ```ts 2257 import { hilog } from '@kit.PerformanceAnalysisKit'; 2258 import { BusinessError } from '@kit.BasicServicesKit'; 2259 2260 let data = rpc.MessageSequence.create(); 2261 try { 2262 data.writeDoubleArray([11.1, 12.2, 13.3]); 2263 } catch (error) { 2264 let e: BusinessError = error as BusinessError; 2265 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorCode ' + e.code); 2266 hilog.error(0x0000, 'testTag', 'rpc write doubleArray fail, errorMessage ' + e.message); 2267 } 2268 try { 2269 let array = data.readDoubleArray(); 2270 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 2271 } catch (error) { 2272 let e: BusinessError = error as BusinessError; 2273 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorCode ' + e.code); 2274 hilog.error(0x0000, 'testTag', 'rpc read doubleArray fail, errorMessage ' + e.message); 2275 } 2276 ``` 2277 2278### writeBooleanArray 2279 2280writeBooleanArray(booleanArray: boolean[]): void 2281 2282Writes a Boolean array to this **MessageSequence** object. 2283 2284**System capability**: SystemCapability.Communication.IPC.Core 2285 2286**Parameters** 2287 2288 | Name | Type | Mandatory| Description | 2289 | ------------ | --------- | ---- | ------------------ | 2290 | booleanArray | boolean[] | Yes | Boolean array to write.| 2291 2292**Error codes** 2293 2294For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2295 2296 | ID| Error Message| 2297 | -------- | -------- | 2298 | 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. | 2299 | 1900009 | Failed to write data to the message sequence. | 2300 2301**Example** 2302 2303 ```ts 2304 import { hilog } from '@kit.PerformanceAnalysisKit'; 2305 import { BusinessError } from '@kit.BasicServicesKit'; 2306 2307 let data = rpc.MessageSequence.create(); 2308 try { 2309 data.writeBooleanArray([false, true, false]); 2310 } catch (error) { 2311 let e: BusinessError = error as BusinessError; 2312 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2313 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2314 } 2315 ``` 2316 2317### readBooleanArray 2318 2319readBooleanArray(dataIn: boolean[]): void 2320 2321Reads a Boolean array from this **MessageSequence** object. 2322 2323**System capability**: SystemCapability.Communication.IPC.Core 2324 2325**Parameters** 2326 2327 | Name| Type | Mandatory| Description | 2328 | ------ | --------- | ---- | ------------------ | 2329 | dataIn | boolean[] | Yes | Boolean array to read.| 2330 2331**Error codes** 2332 2333For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2334 2335 | ID| Error Message| 2336 | -------- | -------- | 2337 | 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. | 2338 | 1900010 | Failed to read data from the message sequence. | 2339 2340**Example** 2341 2342 ```ts 2343 import { hilog } from '@kit.PerformanceAnalysisKit'; 2344 import { BusinessError } from '@kit.BasicServicesKit'; 2345 2346 let data = rpc.MessageSequence.create(); 2347 try { 2348 data.writeBooleanArray([false, true, false]); 2349 } catch (error) { 2350 let e: BusinessError = error as BusinessError; 2351 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2352 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2353 } 2354 let array: Array<boolean> = new Array(3); 2355 try { 2356 data.readBooleanArray(array); 2357 } catch (error) { 2358 let e: BusinessError = error as BusinessError; 2359 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2360 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2361 } 2362 ``` 2363 2364### readBooleanArray 2365 2366readBooleanArray(): boolean[] 2367 2368Reads the Boolean array from this **MessageSequence** object. 2369 2370**System capability**: SystemCapability.Communication.IPC.Core 2371 2372**Return value** 2373 2374 | Type | Description | 2375 | --------- | -------------- | 2376 | boolean[] | Boolean array read.| 2377 2378**Error codes** 2379 2380For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2381 2382 | ID| Error Message| 2383 | -------- | -------- | 2384 | 1900010 | Failed to read data from the message sequence. | 2385 2386**Example** 2387 2388 ```ts 2389 import { hilog } from '@kit.PerformanceAnalysisKit'; 2390 import { BusinessError } from '@kit.BasicServicesKit'; 2391 2392 let data = rpc.MessageSequence.create(); 2393 try { 2394 data.writeBooleanArray([false, true, false]); 2395 } catch (error) { 2396 let e: BusinessError = error as BusinessError; 2397 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorCode ' + e.code); 2398 hilog.error(0x0000, 'testTag', 'rpc write booleanArray fail, errorMessage ' + e.message); 2399 } 2400 try { 2401 let array = data.readBooleanArray(); 2402 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 2403 } catch (error) { 2404 let e: BusinessError = error as BusinessError; 2405 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorCode ' + e.code); 2406 hilog.error(0x0000, 'testTag', 'rpc read booleanArray fail, errorMessage ' + e.message); 2407 } 2408 ``` 2409 2410### writeCharArray 2411 2412writeCharArray(charArray: number[]): void 2413 2414Writes a character array to this **MessageSequence** object. 2415 2416**System capability**: SystemCapability.Communication.IPC.Core 2417 2418**Parameters** 2419 2420 | Name | Type | Mandatory| Description | 2421 | --------- | -------- | ---- | ---------------------- | 2422 | charArray | number[] | Yes | Character array to write.| 2423 2424**Error codes** 2425 2426For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2427 2428 | ID| Error Message| 2429 | -------- | -------- | 2430 | 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. | 2431 | 1900009 | Failed to write data to the message sequence. | 2432 2433**Example** 2434 2435 ```ts 2436 import { hilog } from '@kit.PerformanceAnalysisKit'; 2437 import { BusinessError } from '@kit.BasicServicesKit'; 2438 2439 let data = rpc.MessageSequence.create(); 2440 try { 2441 data.writeCharArray([97, 98, 88]); 2442 } catch (error) { 2443 let e: BusinessError = error as BusinessError; 2444 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2445 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2446 } 2447 ``` 2448 2449### readCharArray 2450 2451readCharArray(dataIn: number[]): void 2452 2453Reads a character array from this **MessageSequence** object. 2454 2455**System capability**: SystemCapability.Communication.IPC.Core 2456 2457**Parameters** 2458 2459 | Name| Type | Mandatory| Description | 2460 | ------ | -------- | ---- | ---------------------- | 2461 | dataIn | number[] | Yes | Character array to read.| 2462 2463**Error codes** 2464 2465For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2466 2467 | ID| Error Message| 2468 | -------- | -------- | 2469 | 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. | 2470 | 1900010 | Failed to read data from the message sequence. | 2471 2472**Example** 2473 2474 ```ts 2475 import { hilog } from '@kit.PerformanceAnalysisKit'; 2476 import { BusinessError } from '@kit.BasicServicesKit'; 2477 2478 let data = rpc.MessageSequence.create(); 2479 try { 2480 data.writeCharArray([97, 98, 88]); 2481 } catch (error) { 2482 let e: BusinessError = error as BusinessError; 2483 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2484 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2485 } 2486 let array: Array<number> = new Array(3); 2487 try { 2488 data.readCharArray(array); 2489 } catch (error) { 2490 let e: BusinessError = error as BusinessError; 2491 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2492 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2493 } 2494 ``` 2495 2496### readCharArray 2497 2498readCharArray(): number[] 2499 2500Reads the character array from this **MessageSequence** object. 2501 2502**System capability**: SystemCapability.Communication.IPC.Core 2503 2504**Return value** 2505 2506 | Type | Description | 2507 | -------- | ------------------ | 2508 | number[] | Character array read.| 2509 2510**Error codes** 2511 2512For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2513 2514 | ID| Error Message| 2515 | -------- | -------- | 2516 | 1900010 | Failed to read data from the message sequence. | 2517 2518**Example** 2519 2520 ```ts 2521 import { hilog } from '@kit.PerformanceAnalysisKit'; 2522 import { BusinessError } from '@kit.BasicServicesKit'; 2523 2524 let data = rpc.MessageSequence.create(); 2525 try { 2526 data.writeCharArray([97, 98, 88]); 2527 } catch (error) { 2528 let e: BusinessError = error as BusinessError; 2529 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorCode ' + e.code); 2530 hilog.error(0x0000, 'testTag', 'rpc write charArray fail, errorMessage ' + e.message); 2531 } 2532 try { 2533 let array = data.readCharArray(); 2534 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 2535 } catch (error) { 2536 let e: BusinessError = error as BusinessError; 2537 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorCode ' + e.code); 2538 hilog.error(0x0000, 'testTag', 'rpc read charArray fail, errorMessage ' + e.message); 2539 } 2540 ``` 2541 2542### writeStringArray 2543 2544writeStringArray(stringArray: string[]): void 2545 2546Writes a string array to this **MessageSequence** object. 2547 2548**System capability**: SystemCapability.Communication.IPC.Core 2549 2550**Parameters** 2551 2552 | Name | Type | Mandatory| Description | 2553 | ----------- | -------- | ---- | ------------------------------------------------------- | 2554 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 2555 2556**Error codes** 2557 2558For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2559 2560 | ID| Error Message| 2561 | -------- | -------- | 2562 | 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. | 2563 | 1900009 | Failed to write data to the message sequence. | 2564 2565**Example** 2566 2567 ```ts 2568 import { hilog } from '@kit.PerformanceAnalysisKit'; 2569 import { BusinessError } from '@kit.BasicServicesKit'; 2570 2571 let data = rpc.MessageSequence.create(); 2572 try { 2573 data.writeStringArray(["abc", "def"]); 2574 } catch (error) { 2575 let e: BusinessError = error as BusinessError; 2576 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2577 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2578 } 2579 ``` 2580 2581### readStringArray 2582 2583readStringArray(dataIn: string[]): void 2584 2585Reads a string array from this **MessageSequence** object. 2586 2587**System capability**: SystemCapability.Communication.IPC.Core 2588 2589**Parameters** 2590 2591 | Name| Type | Mandatory| Description | 2592 | ------ | -------- | ---- | -------------------- | 2593 | dataIn | string[] | Yes | String array to read.| 2594 2595**Error codes** 2596 2597For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2598 2599 | ID| Error Message| 2600 | -------- | -------- | 2601 | 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. | 2602 | 1900010 | Failed to read data from the message sequence. | 2603 2604**Example** 2605 2606 ```ts 2607 import { hilog } from '@kit.PerformanceAnalysisKit'; 2608 import { BusinessError } from '@kit.BasicServicesKit'; 2609 2610 let data = rpc.MessageSequence.create(); 2611 try { 2612 data.writeStringArray(["abc", "def"]); 2613 } catch (error) { 2614 let e: BusinessError = error as BusinessError; 2615 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2616 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2617 } 2618 let array: Array<string> = new Array(2); 2619 try { 2620 data.readStringArray(array); 2621 } catch (error) { 2622 let e: BusinessError = error as BusinessError; 2623 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2624 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2625 } 2626 ``` 2627 2628### readStringArray 2629 2630readStringArray(): string[] 2631 2632Reads the string array from this **MessageSequence** object. 2633 2634**System capability**: SystemCapability.Communication.IPC.Core 2635 2636**Return value** 2637 2638 | Type | Description | 2639 | -------- | ---------------- | 2640 | string[] | String array read.| 2641 2642**Error codes** 2643 2644For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2645 2646 | ID| Error Message| 2647 | -------- | -------- | 2648 | 1900010 | Failed to read data from the message sequence. | 2649 2650**Example** 2651 2652 ```ts 2653 import { hilog } from '@kit.PerformanceAnalysisKit'; 2654 import { BusinessError } from '@kit.BasicServicesKit'; 2655 2656 let data = rpc.MessageSequence.create(); 2657 try { 2658 data.writeStringArray(["abc", "def"]); 2659 } catch (error) { 2660 let e: BusinessError = error as BusinessError; 2661 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorCode ' + e.code); 2662 hilog.error(0x0000, 'testTag', 'rpc write stringArray fail, errorMessage ' + e.message); 2663 } 2664 try { 2665 let array = data.readStringArray(); 2666 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 2667 } catch (error) { 2668 let e: BusinessError = error as BusinessError; 2669 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorCode ' + e.code); 2670 hilog.error(0x0000, 'testTag', 'rpc read stringArray fail, errorMessage ' + e.message); 2671 } 2672 ``` 2673 2674### writeNoException 2675 2676writeNoException(): void 2677 2678Writes information to this **MessageSequence** object indicating that no exception occurred. 2679 2680**System capability**: SystemCapability.Communication.IPC.Core 2681 2682**Error codes** 2683 2684For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2685 2686 | ID| Error Message| 2687 | -------- | -------- | 2688 | 1900009 | Failed to write data to the message sequence. | 2689 2690**Example** 2691 2692 ```ts 2693 import { hilog } from '@kit.PerformanceAnalysisKit'; 2694 import { BusinessError } from '@kit.BasicServicesKit'; 2695 2696 class TestRemoteObject extends rpc.RemoteObject { 2697 constructor(descriptor: string) { 2698 super(descriptor); 2699 } 2700 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 2701 if (code === 1) { 2702 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteMessageRequest called'); 2703 try { 2704 reply.writeNoException(); 2705 } catch (error) { 2706 let e: BusinessError = error as BusinessError; 2707 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorCode ' + e.code); 2708 hilog.error(0x0000, 'testTag', 'rpc write no exception fail, errorMessage ' + e.message); 2709 } 2710 return true; 2711 } else { 2712 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 2713 return false; 2714 } 2715 } 2716 } 2717 ``` 2718 2719### readException 2720 2721readException(): void 2722 2723Reads the exception information from this **MessageSequence** object. 2724 2725**System capability**: SystemCapability.Communication.IPC.Core 2726 2727**Error codes** 2728 2729For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2730 2731 | ID| Error Message| 2732 | -------- | -------- | 2733 | 1900010 | Failed to read data from the message sequence. | 2734 2735**Example** 2736 2737 ```ts 2738 // If the FA model is used, import featureAbility from @kit.AbilityKit. 2739 // import { featureAbility } from '@kit.AbilityKit'; 2740 import { Want, common } from '@kit.AbilityKit'; 2741 import { hilog } from '@kit.PerformanceAnalysisKit'; 2742 2743 let proxy: rpc.IRemoteObject | undefined; 2744 let connect: common.ConnectOptions = { 2745 onConnect: (elementName, remoteProxy) => { 2746 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 2747 proxy = remoteProxy; 2748 }, 2749 onDisconnect: (elementName) => { 2750 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 2751 }, 2752 onFailed: () => { 2753 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 2754 } 2755 }; 2756 let want: Want = { 2757 bundleName: "com.ohos.server", 2758 abilityName: "com.ohos.server.EntryAbility", 2759 }; 2760 2761 // Use this method to connect to the ability for the FA model. 2762 // FA.connectAbility(want,connect); 2763 2764 // Save the connection ID, which will be used for the subsequent service disconnection. 2765 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 2766 // Save the connection ID, which will be used for the subsequent service disconnection. 2767 let connectionId = context.connectServiceExtensionAbility(want, connect); 2768 ``` 2769 2770 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 2771 2772 ```ts 2773 import { BusinessError } from '@kit.BasicServicesKit'; 2774 import { hilog } from '@kit.PerformanceAnalysisKit'; 2775 2776 let option = new rpc.MessageOption(); 2777 let data = rpc.MessageSequence.create(); 2778 let reply = rpc.MessageSequence.create(); 2779 data.writeNoException(); 2780 data.writeInt(6); 2781 if (proxy != undefined) { 2782 proxy.sendMessageRequest(1, data, reply, option) 2783 .then((result: rpc.RequestResult) => { 2784 if (result.errCode === 0) { 2785 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 2786 try { 2787 result.reply.readException(); 2788 } catch (error) { 2789 let e: BusinessError = error as BusinessError; 2790 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorCode ' + e.code); 2791 hilog.error(0x0000, 'testTag', 'rpc read exception fail, errorMessage ' + e.message); 2792 } 2793 let num = result.reply.readInt(); 2794 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 2795 } else { 2796 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 2797 } 2798 }).catch((e: Error) => { 2799 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest got exception: ' + e.message); 2800 }).finally (() => { 2801 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 2802 data.reclaim(); 2803 reply.reclaim(); 2804 }); 2805 } 2806 ``` 2807 2808### writeParcelableArray 2809 2810writeParcelableArray(parcelableArray: Parcelable[]): void 2811 2812Writes a **Parcelable** array to this **MessageSequence** object. 2813 2814**System capability**: SystemCapability.Communication.IPC.Core 2815 2816**Parameters** 2817 2818| Name | Type | Mandatory| Description | 2819| --------------- | ------------ | ---- | -------------------------- | 2820| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to write.| 2821 2822**Error codes** 2823 2824For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2825 2826 | ID| Error Message| 2827 | -------- | -------- | 2828 | 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. | 2829 | 1900009 | Failed to write data to the message sequence. | 2830 2831**Example** 2832 2833 ```ts 2834 import { hilog } from '@kit.PerformanceAnalysisKit'; 2835 import { BusinessError } from '@kit.BasicServicesKit'; 2836 2837 class MyParcelable implements rpc.Parcelable { 2838 num: number = 0; 2839 str: string = ''; 2840 constructor(num: number, str: string) { 2841 this.num = num; 2842 this.str = str; 2843 } 2844 marshalling(messageSequence: rpc.MessageSequence): boolean { 2845 messageSequence.writeInt(this.num); 2846 messageSequence.writeString(this.str); 2847 return true; 2848 } 2849 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2850 this.num = messageSequence.readInt(); 2851 this.str = messageSequence.readString(); 2852 return true; 2853 } 2854 } 2855 let parcelable = new MyParcelable(1, "aaa"); 2856 let parcelable2 = new MyParcelable(2, "bbb"); 2857 let parcelable3 = new MyParcelable(3, "ccc"); 2858 let a = [parcelable, parcelable2, parcelable3]; 2859 let data = rpc.MessageSequence.create(); 2860 try { 2861 data.writeParcelableArray(a); 2862 } catch (error) { 2863 let e: BusinessError = error as BusinessError; 2864 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorCode ' + e.code); 2865 hilog.error(0x0000, 'testTag', 'rpc write parcelable array fail, errorMessage ' + e.message); 2866 } 2867 ``` 2868 2869### readParcelableArray 2870 2871readParcelableArray(parcelableArray: Parcelable[]): void 2872 2873Reads a **Parcelable** array from this **MessageSequence** object. 2874 2875**System capability**: SystemCapability.Communication.IPC.Core 2876 2877**Parameters** 2878 2879| Name | Type | Mandatory| Description | 2880| --------------- | ------------ | ---- | -------------------------- | 2881| parcelableArray | [Parcelable](#parcelable9)[] | Yes | **Parcelable** array to read.| 2882 2883**Error codes** 2884 2885For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2886 2887 | ID| Error Message| 2888 | -------- | -------- | 2889 | 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. | 2890 | 1900010 | Failed to read data from the message sequence. | 2891 | 1900012 | Failed to call the JS callback function. | 2892 2893**Example** 2894 2895 ```ts 2896 import { hilog } from '@kit.PerformanceAnalysisKit'; 2897 import { BusinessError } from '@kit.BasicServicesKit'; 2898 2899 class MyParcelable implements rpc.Parcelable { 2900 num: number = 0; 2901 str: string = ''; 2902 constructor(num: number, str: string) { 2903 this.num = num; 2904 this.str = str; 2905 } 2906 marshalling(messageSequence: rpc.MessageSequence): boolean { 2907 messageSequence.writeInt(this.num); 2908 messageSequence.writeString(this.str); 2909 return true; 2910 } 2911 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 2912 this.num = messageSequence.readInt(); 2913 this.str = messageSequence.readString(); 2914 return true; 2915 } 2916 } 2917 let parcelable = new MyParcelable(1, "aaa"); 2918 let parcelable2 = new MyParcelable(2, "bbb"); 2919 let parcelable3 = new MyParcelable(3, "ccc"); 2920 let a = [parcelable, parcelable2, parcelable3]; 2921 let data = rpc.MessageSequence.create(); 2922 data.writeParcelableArray(a); 2923 let b = [new MyParcelable(0, ""), new MyParcelable(0, ""), new MyParcelable(0, "")]; 2924 try { 2925 data.readParcelableArray(b); 2926 } catch (error) { 2927 let e: BusinessError = error as BusinessError; 2928 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorCode ' + e.code); 2929 hilog.error(0x0000, 'testTag', 'rpc read parcelable array fail, errorMessage ' + e.message); 2930 } 2931 ``` 2932 2933### writeRemoteObjectArray 2934 2935writeRemoteObjectArray(objectArray: IRemoteObject[]): void 2936 2937Writes an array of **IRemoteObject** objects to this **MessageSequence** object. 2938 2939**System capability**: SystemCapability.Communication.IPC.Core 2940 2941**Parameters** 2942 2943| Name | Type | Mandatory| Description | 2944| ----------- | --------------- | ---- | ---------------------------------------------- | 2945| objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 2946 2947**Error codes** 2948 2949For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 2950 2951 | ID| Error Message| 2952 | -------- | -------- | 2953 | 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. | 2954 | 1900009 | Failed to write data to the message sequence. | 2955 2956**Example** 2957 2958 ```ts 2959 import { hilog } from '@kit.PerformanceAnalysisKit'; 2960 import { BusinessError } from '@kit.BasicServicesKit'; 2961 2962 class TestRemoteObject extends rpc.RemoteObject { 2963 constructor(descriptor: string) { 2964 super(descriptor); 2965 this.modifyLocalInterface(this, descriptor); 2966 } 2967 2968 asObject(): rpc.IRemoteObject { 2969 return this; 2970 } 2971 } 2972 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 2973 let data = rpc.MessageSequence.create(); 2974 try { 2975 data.writeRemoteObjectArray(a); 2976 } catch (error) { 2977 let e: BusinessError = error as BusinessError; 2978 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorCode ' + e.code); 2979 hilog.error(0x0000, 'testTag', 'rpc write remote object array fail, errorMessage ' + e.message); 2980 } 2981 ``` 2982 2983### readRemoteObjectArray 2984 2985readRemoteObjectArray(objects: IRemoteObject[]): void 2986 2987Reads an array of **IRemoteObject** objects from this **MessageSequence** object. 2988 2989**System capability**: SystemCapability.Communication.IPC.Core 2990 2991**Parameters** 2992 2993| Name | Type | Mandatory| Description | 2994| ------- | --------------- | ---- | ---------------------------------------------- | 2995| objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 2996 2997**Error codes** 2998 2999For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3000 3001 | ID| Error Message| 3002 | -------- | -------- | 3003 | 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. | 3004 | 1900010 | Failed to read data from the message sequence. | 3005 3006**Example** 3007 3008 ```ts 3009 import { hilog } from '@kit.PerformanceAnalysisKit'; 3010 import { BusinessError } from '@kit.BasicServicesKit'; 3011 3012 class TestRemoteObject extends rpc.RemoteObject { 3013 constructor(descriptor: string) { 3014 super(descriptor); 3015 this.modifyLocalInterface(this, descriptor); 3016 } 3017 3018 asObject(): rpc.IRemoteObject { 3019 return this; 3020 } 3021 } 3022 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3023 let data = rpc.MessageSequence.create(); 3024 data.writeRemoteObjectArray(a); 3025 let b: Array<rpc.IRemoteObject> = new Array(3); 3026 try { 3027 data.readRemoteObjectArray(b); 3028 } catch (error) { 3029 let e: BusinessError = error as BusinessError; 3030 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3031 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3032 } 3033 ``` 3034 3035### readRemoteObjectArray 3036 3037readRemoteObjectArray(): IRemoteObject[] 3038 3039Reads the **IRemoteObject** object array from this **MessageSequence** object. 3040 3041**System capability**: SystemCapability.Communication.IPC.Core 3042 3043**Return value** 3044 3045| Type | Description | 3046| --------------- | --------------------------- | 3047| [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 3048 3049**Error codes** 3050 3051For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3052 3053 | ID| Error Message| 3054 | -------- | -------- | 3055 | 1900010 | Failed to read data from the message sequence. | 3056 3057**Example** 3058 3059 ```ts 3060 import { hilog } from '@kit.PerformanceAnalysisKit'; 3061 import { BusinessError } from '@kit.BasicServicesKit'; 3062 3063 class TestRemoteObject extends rpc.RemoteObject { 3064 constructor(descriptor: string) { 3065 super(descriptor); 3066 this.modifyLocalInterface(this, descriptor); 3067 } 3068 3069 asObject(): rpc.IRemoteObject { 3070 return this; 3071 } 3072 } 3073 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 3074 let data = rpc.MessageSequence.create(); 3075 data.writeRemoteObjectArray(a); 3076 try { 3077 let b = data.readRemoteObjectArray(); 3078 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 3079 } catch (error) { 3080 let e: BusinessError = error as BusinessError; 3081 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorCode ' + e.code); 3082 hilog.error(0x0000, 'testTag', 'rpc read remote object array fail, errorMessage ' + e.message); 3083 } 3084 ``` 3085 3086### closeFileDescriptor 3087 3088static closeFileDescriptor(fd: number): void 3089 3090Closes a file descriptor. This API is a static method. 3091 3092**System capability**: SystemCapability.Communication.IPC.Core 3093 3094**Parameters** 3095 3096 | Name| Type | Mandatory| Description | 3097 | ------ | ------ | ---- | -------------------- | 3098 | fd | number | Yes | File descriptor to close.| 3099 3100**Error codes** 3101 3102For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3103 3104 | ID| Error Message| 3105 | -------- | -------- | 3106 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3107 3108**Example** 3109 3110 ```ts 3111 import { fileIo } from '@kit.CoreFileKit'; 3112 import { hilog } from '@kit.PerformanceAnalysisKit'; 3113 import { BusinessError } from '@kit.BasicServicesKit'; 3114 3115 let filePath = "path/to/file"; 3116 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3117 try { 3118 rpc.MessageSequence.closeFileDescriptor(file.fd); 3119 } catch (error) { 3120 let e: BusinessError = error as BusinessError; 3121 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorCode ' + e.code); 3122 hilog.error(0x0000, 'testTag', 'rpc close file descriptor fail, errorMessage ' + e.message); 3123 } 3124 ``` 3125 3126### dupFileDescriptor 3127 3128static dupFileDescriptor(fd: number): number 3129 3130Duplicates a file descriptor. This API is a static method. 3131 3132**System capability**: SystemCapability.Communication.IPC.Core 3133 3134**Parameters** 3135 3136 | Name| Type | Mandatory| Description | 3137 | ------ | ------ | ---- | ------------------------ | 3138 | fd | number | Yes | File descriptor to duplicate.| 3139 3140**Return value** 3141 3142 | Type | Description | 3143 | ------ | -------------------- | 3144 | number | New file descriptor.| 3145 3146**Error codes** 3147 3148For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3149 3150 | ID| Error Message| 3151 | -------- | -------- | 3152 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3153 | 1900013 | Failed to call dup. | 3154 3155**Example** 3156 3157 ```ts 3158 import { fileIo } from '@kit.CoreFileKit'; 3159 import { hilog } from '@kit.PerformanceAnalysisKit'; 3160 import { BusinessError } from '@kit.BasicServicesKit'; 3161 3162 let filePath = "path/to/file"; 3163 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3164 try { 3165 rpc.MessageSequence.dupFileDescriptor(file.fd); 3166 } catch (error) { 3167 let e: BusinessError = error as BusinessError; 3168 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorCode ' + e.code); 3169 hilog.error(0x0000, 'testTag', 'rpc dup file descriptor fail, errorMessage ' + e.message); 3170 } 3171 ``` 3172 3173### containFileDescriptors 3174 3175containFileDescriptors(): boolean 3176 3177Checks whether this **MessageSequence** object contains file descriptors. 3178 3179**System capability**: SystemCapability.Communication.IPC.Core 3180 3181**Return value** 3182 3183 | Type | Description | 3184 | ------- | -------------------------------------------------------------------- | 3185 | boolean | Returns **true** if the **MessageSequence** object contains file descriptors; returns **false** otherwise.| 3186 3187**Example** 3188 3189 ```ts 3190 import { fileIo } from '@kit.CoreFileKit'; 3191 import { hilog } from '@kit.PerformanceAnalysisKit'; 3192 import { BusinessError } from '@kit.BasicServicesKit'; 3193 3194 let sequence = new rpc.MessageSequence(); 3195 let filePath = "path/to/file"; 3196 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3197 try { 3198 sequence.writeFileDescriptor(file.fd); 3199 } catch (error) { 3200 let e: BusinessError = error as BusinessError; 3201 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3202 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3203 } 3204 try { 3205 let containFD = sequence.containFileDescriptors(); 3206 hilog.info(0x0000, 'testTag', 'RpcTest: sequence after write fd containFd result is ' + containFD); 3207 } catch (error) { 3208 let e: BusinessError = error as BusinessError; 3209 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorCode ' + e.code); 3210 hilog.error(0x0000, 'testTag', 'rpc contain file descriptor fail, errorMessage ' + e.message); 3211 } 3212 ``` 3213 3214### writeFileDescriptor 3215 3216writeFileDescriptor(fd: number): void 3217 3218Writes a file descriptor to this **MessageSequence** object. 3219 3220**System capability**: SystemCapability.Communication.IPC.Core 3221 3222**Parameters** 3223 3224 | Name| Type | Mandatory| Description | 3225 | ------ | ------ | ---- | ------------ | 3226 | fd | number | Yes | File descriptor to write.| 3227 3228**Error codes** 3229 3230For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3231 3232 | ID| Error Message| 3233 | -------- | -------- | 3234 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3235 | 1900009 | Failed to write data to the message sequence. | 3236 3237**Example** 3238 3239 ```ts 3240 import { fileIo } from '@kit.CoreFileKit'; 3241 import { hilog } from '@kit.PerformanceAnalysisKit'; 3242 import { BusinessError } from '@kit.BasicServicesKit'; 3243 3244 let sequence = new rpc.MessageSequence(); 3245 let filePath = "path/to/file"; 3246 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3247 try { 3248 sequence.writeFileDescriptor(file.fd); 3249 } catch (error) { 3250 let e: BusinessError = error as BusinessError; 3251 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3252 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3253 } 3254 ``` 3255 3256### readFileDescriptor 3257 3258readFileDescriptor(): number 3259 3260Reads the file descriptor from this **MessageSequence** object. 3261 3262**System capability**: SystemCapability.Communication.IPC.Core 3263 3264**Return value** 3265 3266 | Type | Description | 3267 | ------ | ---------------- | 3268 | number | File descriptor read.| 3269 3270**Error codes** 3271 3272For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3273 3274 | ID| Error Message| 3275 | -------- | -------- | 3276 | 1900010 | Failed to read data from the message sequence. | 3277 3278**Example** 3279 3280 ```ts 3281 import { fileIo } from '@kit.CoreFileKit'; 3282 import { hilog } from '@kit.PerformanceAnalysisKit'; 3283 import { BusinessError } from '@kit.BasicServicesKit'; 3284 3285 let sequence = new rpc.MessageSequence(); 3286 let filePath = "path/to/file"; 3287 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 3288 try { 3289 sequence.writeFileDescriptor(file.fd); 3290 } catch (error) { 3291 let e: BusinessError = error as BusinessError; 3292 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorCode ' + e.code); 3293 hilog.error(0x0000, 'testTag', 'rpc write file descriptor fail, errorMessage ' + e.message); 3294 } 3295 try { 3296 let readFD = sequence.readFileDescriptor(); 3297 hilog.info(0x0000, 'testTag', 'RpcClient: readFileDescriptor is ' + readFD); 3298 } catch (error) { 3299 let e: BusinessError = error as BusinessError; 3300 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorCode ' + e.code); 3301 hilog.error(0x0000, 'testTag', 'rpc read file descriptor fail, errorMessage ' + e.message); 3302 } 3303 ``` 3304 3305### writeAshmem 3306 3307writeAshmem(ashmem: Ashmem): void 3308 3309Writes an anonymous shared object to this **MessageSequence** object. 3310 3311**System capability**: SystemCapability.Communication.IPC.Core 3312 3313**Parameters** 3314 3315| Name| Type | Mandatory| Description | 3316| ------ | ------ | ---- | ------------------------------------- | 3317| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 3318 3319**Error codes** 3320 3321For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3322 3323 | ID| Error Message| 3324 | -------- | ------- | 3325 | 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. | 3326 | 1900003 | Failed to write data to the shared memory. | 3327 3328**Example** 3329 3330 ```ts 3331 import { hilog } from '@kit.PerformanceAnalysisKit'; 3332 import { BusinessError } from '@kit.BasicServicesKit'; 3333 3334 let sequence = new rpc.MessageSequence(); 3335 let ashmem: rpc.Ashmem | undefined = undefined; 3336 try { 3337 ashmem = rpc.Ashmem.create("ashmem", 1024); 3338 try { 3339 sequence.writeAshmem(ashmem); 3340 } catch (error) { 3341 let e: BusinessError = error as BusinessError; 3342 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3343 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3344 } 3345 } catch (error) { 3346 let e: BusinessError = error as BusinessError; 3347 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3348 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3349 } 3350 ``` 3351 3352### readAshmem 3353 3354readAshmem(): Ashmem 3355 3356Reads the anonymous shared object from this **MessageSequence** object. 3357 3358**System capability**: SystemCapability.Communication.IPC.Core 3359 3360**Return value** 3361 3362| Type | Description | 3363| ------ | ------------------ | 3364| [Ashmem](#ashmem8) | Anonymous share object obtained.| 3365 3366**Error codes** 3367 3368For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3369 3370 | ID| Error Message| 3371 | -------- | -------- | 3372 | 401 | check param failed | 3373 | 1900004 | Failed to read data from the shared memory. | 3374 3375**Example** 3376 3377 ```ts 3378 import { hilog } from '@kit.PerformanceAnalysisKit'; 3379 import { BusinessError } from '@kit.BasicServicesKit'; 3380 3381 let sequence = new rpc.MessageSequence(); 3382 let ashmem: rpc.Ashmem | undefined = undefined; 3383 try { 3384 ashmem = rpc.Ashmem.create("ashmem", 1024); 3385 try { 3386 sequence.writeAshmem(ashmem); 3387 } catch (error) { 3388 let e: BusinessError = error as BusinessError; 3389 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorCode ' + e.code); 3390 hilog.error(0x0000, 'testTag', 'rpc write ashmem fail, errorMessage ' + e.message); 3391 } 3392 } catch (error) { 3393 let e: BusinessError = error as BusinessError; 3394 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorCode ' + e.code); 3395 hilog.error(0x0000, 'testTag', 'rpc create ashmem fail, errorMessage ' + e.message); 3396 } 3397 try { 3398 sequence.readAshmem(); 3399 } catch (error) { 3400 let e: BusinessError = error as BusinessError; 3401 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorCode ' + e.code); 3402 hilog.error(0x0000, 'testTag', 'rpc read ashmem fail, errorMessage ' + e.message); 3403 } 3404 ``` 3405 3406### getRawDataCapacity 3407 3408getRawDataCapacity(): number 3409 3410Obtains the maximum amount of raw data that can be held by this **MessageSequence** object. 3411 3412**System capability**: SystemCapability.Communication.IPC.Core 3413 3414**Return value** 3415 3416 | Type | Description | 3417 | ------ | ------------------------------------------------------------ | 3418 | number | Maximum amount of raw data that **MessageSequence** can hold, that is, 128 MB.| 3419 3420**Example** 3421 3422 ```ts 3423 import { hilog } from '@kit.PerformanceAnalysisKit'; 3424 3425 let sequence = new rpc.MessageSequence(); 3426 let result = sequence.getRawDataCapacity(); 3427 hilog.info(0x0000, 'testTag', 'RpcTest: sequence get RawDataCapacity result is ' + result); 3428 ``` 3429 3430### writeRawData<sup>(deprecated)</sup> 3431 3432writeRawData(rawData: number[], size: number): void 3433 3434Writes raw data to this **MessageSequence** object. 3435 3436> **NOTE**<br/> 3437> 3438> >**NOTE**<br>This API is deprecated since API version 11. Use [writeRawDataBuffer](#writerawdatabuffer11) instead. 3439> 3440> This API cannot be called for multiple times in one parcel communication. 3441> When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration. 3442 3443**System capability**: SystemCapability.Communication.IPC.Core 3444 3445**Parameters** 3446 3447 | Name | Type | Mandatory| Description | 3448 | ------- | -------- | ---- | ---------------------------------- | 3449 | rawData | number[] | Yes | Raw data to write. The size cannot exceed 128 MB.| 3450 | size | number | Yes | Size of the raw data, in bytes.| 3451 3452**Error codes** 3453 3454For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3455 3456 | ID| Error Message| 3457 | -------- | -------- | 3458 | 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. | 3459 | 1900009 | Failed to write data to the message sequence. | 3460 3461**Example** 3462 3463 ```ts 3464 import { hilog } from '@kit.PerformanceAnalysisKit'; 3465 import { BusinessError } from '@kit.BasicServicesKit'; 3466 3467 let sequence = new rpc.MessageSequence(); 3468 let arr = [1, 2, 3, 4, 5]; 3469 try { 3470 sequence.writeRawData(arr, arr.length); 3471 } catch (error) { 3472 let e: BusinessError = error as BusinessError; 3473 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3474 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3475 } 3476 ``` 3477 3478### writeRawDataBuffer<sup>11+</sup> 3479 3480writeRawDataBuffer(rawData: ArrayBuffer, size: number): void 3481 3482Writes raw data to this **MessageSequence** object. 3483 3484> **NOTE**<br/> 3485> 3486> This API cannot be called for multiple times in one parcel communication. 3487> When the data volume is large (greater than 32 KB), the shared memory is used to transmit data. In this case, pay attention to the SELinux configuration. 3488 3489**System capability**: SystemCapability.Communication.IPC.Core 3490 3491**Parameters** 3492 3493 | Name | Type | Mandatory| Description | 3494 | ------- | ----------- | ---- | ------------------------------------ | 3495 | rawData | ArrayBuffer | Yes | Raw data to write. The size cannot exceed 128 MB.| 3496 | size | number | Yes | Size of the raw data, in bytes. | 3497 3498**Error codes** 3499 3500For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3501 3502 | ID| Error Message| 3503 | -------- | -------- | 3504 | 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. | 3505 | 1900009 | Failed to write data to the message sequence. | 3506 3507**Example** 3508 3509 ```ts 3510 import { hilog } from '@kit.PerformanceAnalysisKit'; 3511 import { BusinessError } from '@kit.BasicServicesKit'; 3512 3513 let buffer = new ArrayBuffer(64 * 1024); 3514 let int32View = new Int32Array(buffer); 3515 for (let i = 0; i < int32View.length; i++) { 3516 int32View[i] = i * 2 + 1; 3517 } 3518 let size = buffer.byteLength; 3519 let sequence = new rpc.MessageSequence(); 3520 try { 3521 sequence.writeRawDataBuffer(buffer, size); 3522 } catch (error) { 3523 let e: BusinessError = error as BusinessError; 3524 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3525 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3526 } 3527 ``` 3528 3529### readRawData<sup>(deprecated)</sup> 3530 3531readRawData(size: number): number[] 3532 3533Reads raw data from this **MessageSequence** object. 3534 3535> **NOTE**<br/> 3536> 3537> >**NOTE**<br>This API is deprecated since API version 11. Use [readRawDataBuffer](#readrawdatabuffer11) instead. 3538 3539 3540**System capability**: SystemCapability.Communication.IPC.Core 3541 3542**Parameters** 3543 3544 | Name| Type | Mandatory| Description | 3545 | ------ | ------ | ---- | ------------------------ | 3546 | size | number | Yes | Size of the raw data to read.| 3547 3548**Return value** 3549 3550 | Type | Description | 3551 | -------- | ------------------------------ | 3552 | number[] | Raw data obtained, in bytes.| 3553 3554**Error codes** 3555 3556For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3557 3558 | ID| Error Message| 3559 | -------- | -------- | 3560 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3561 | 1900010 | Failed to read data from the message sequence. | 3562 3563**Example** 3564 3565 ```ts 3566 import { hilog } from '@kit.PerformanceAnalysisKit'; 3567 import { BusinessError } from '@kit.BasicServicesKit'; 3568 3569 let sequence = new rpc.MessageSequence(); 3570 let arr = [1, 2, 3, 4, 5]; 3571 try { 3572 sequence.writeRawData(arr, arr.length); 3573 } catch (error) { 3574 let e: BusinessError = error as BusinessError; 3575 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3576 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3577 } 3578 try { 3579 let result = sequence.readRawData(5); 3580 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + result); 3581 } catch (error) { 3582 let e: BusinessError = error as BusinessError; 3583 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3584 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3585 } 3586 ``` 3587 3588### readRawDataBuffer<sup>11+</sup> 3589 3590readRawDataBuffer(size: number): ArrayBuffer 3591 3592Reads raw data from this **MessageSequence** object. 3593 3594**System capability**: SystemCapability.Communication.IPC.Core 3595 3596**Parameters** 3597 3598 | Name| Type | Mandatory| Description | 3599 | ------ | ------ | ---- | ------------------------ | 3600 | size | number | Yes | Size of the raw data to read.| 3601 3602**Return value** 3603 3604 | Type | Description | 3605 | -------- | ------------------------------ | 3606 | ArrayBuffer | Raw data obtained, in bytes.| 3607 3608**Error codes** 3609 3610For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3611 3612 | ID| Error Message| 3613 | -------- | -------- | 3614 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 3615 | 1900010 | Failed to read data from the message sequence. | 3616 3617**Example** 3618 3619 ```ts 3620 import { hilog } from '@kit.PerformanceAnalysisKit'; 3621 import { BusinessError } from '@kit.BasicServicesKit'; 3622 3623 let buffer = new ArrayBuffer(64 * 1024); 3624 let int32View = new Int32Array(buffer); 3625 for (let i = 0; i < int32View.length; i++) { 3626 int32View[i] = i * 2 + 1; 3627 } 3628 let size = buffer.byteLength; 3629 let sequence = new rpc.MessageSequence(); 3630 try { 3631 sequence.writeRawDataBuffer(buffer, size); 3632 } catch (error) { 3633 let e: BusinessError = error as BusinessError; 3634 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorCode ' + e.code); 3635 hilog.error(0x0000, 'testTag', 'rpc write rawdata fail, errorMessage ' + e.message); 3636 } 3637 try { 3638 let result = sequence.readRawDataBuffer(size); 3639 let readInt32View = new Int32Array(result); 3640 hilog.info(0x0000, 'testTag', 'RpcTest: sequence read raw data result is ' + readInt32View); 3641 } catch (error) { 3642 let e: BusinessError = error as BusinessError; 3643 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorCode ' + e.code); 3644 hilog.error(0x0000, 'testTag', 'rpc read rawdata fail, errorMessage ' + e.message); 3645 } 3646 ``` 3647 3648### writeArrayBuffer<sup>12+</sup> 3649 3650writeArrayBuffer(buf: ArrayBuffer, typeCode: TypeCode): void 3651 3652Writes data of the ArrayBuffer type to this **MessageSequence** object. 3653 3654**System capability**: SystemCapability.Communication.IPC.Core 3655 3656**Parameters** 3657 3658 | Name | Type | Mandatory| Description | 3659 | --------- | ------------------------- | ---- | --------------------------- | 3660 | buf | ArrayBuffer | Yes | Data to write. | 3661 | 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.| 3662 3663**Error codes** 3664 3665For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3666 3667 | ID| Error Message| 3668 | -------- | -------- | 3669 | 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. | 3670 | 1900009 | Failed to write data to the message sequence. | 3671 3672**Example** 3673 3674 ```ts 3675 // In this example, the value of TypeCode is Int16Array. 3676 import { hilog } from '@kit.PerformanceAnalysisKit'; 3677 import { BusinessError } from '@kit.BasicServicesKit'; 3678 3679 const data = rpc.MessageSequence.create(); 3680 3681 let buffer = new ArrayBuffer(10); 3682 let int16View = new Int16Array(buffer); 3683 for (let i = 0; i < int16View.length; i++) { 3684 int16View[i] = i * 2 + 1; 3685 } 3686 3687 try { 3688 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3689 } catch (error) { 3690 let e: BusinessError = error as BusinessError; 3691 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3692 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3693 } 3694 ``` 3695 3696### readArrayBuffer<sup>12+</sup> 3697 3698readArrayBuffer(typeCode: TypeCode): ArrayBuffer 3699 3700Reads data of the ArrayBuffer type from this **MessageSequence**. 3701 3702**System capability**: SystemCapability.Communication.IPC.Core 3703 3704**Parameters** 3705 3706 | Name | Type | Mandatory| Description | 3707 | -------- | ----------------------- | ---- | ------------------------| 3708 | 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. | 3709 3710**Return value** 3711 3712 | Type | Description | 3713 | -------- | -------------------------------------------- | 3714 | ArrayBuffer | Data of the ArrayBuffer type read, in bytes.| 3715 3716**Error codes** 3717 3718For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 3719 3720 | ID| Error Message| 3721 | -------- | -------- | 3722 | 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; | 3723 | 1900010 | Failed to read data from the message sequence. | 3724 3725**Example** 3726 3727 ```ts 3728 // In this example, the value of TypeCode is Int16Array. 3729 import { hilog } from '@kit.PerformanceAnalysisKit'; 3730 import { BusinessError } from '@kit.BasicServicesKit'; 3731 3732 const data = rpc.MessageSequence.create(); 3733 3734 let buffer = new ArrayBuffer(10); 3735 let int16View = new Int16Array(buffer); 3736 for (let i = 0; i < int16View.length; i++) { 3737 int16View[i] = i * 2 + 1; 3738 } 3739 3740 try { 3741 data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); 3742 } catch (error) { 3743 let e: BusinessError = error as BusinessError; 3744 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorCode ' + e.code); 3745 hilog.error(0x0000, 'testTag', 'rpc write ArrayBuffe fail, errorMessage ' + e.message); 3746 } 3747 try { 3748 let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); 3749 let readInt16View = new Int16Array(result); 3750 hilog.info(0x0000, 'testTag', 'RpcTest: read ArrayBuffer result is ' + readInt16View); 3751 } catch (error) { 3752 let e: BusinessError = error as BusinessError; 3753 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorCode ' + e.code); 3754 hilog.error(0x0000, 'testTag', 'rpc read ArrayBuffer fail, errorMessage ' + e.message); 3755 } 3756 ``` 3757 3758## MessageParcel<sup>(deprecated)</sup> 3759 3760Provides 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. 3761 3762> **NOTE**<br/> 3763> 3764> >**NOTE**<br>This API is deprecated since API version 9. Use [MessageSequence](#messagesequence9) instead. 3765 3766### create 3767 3768static create(): MessageParcel 3769 3770Creates a **MessageParcel** object. This method is a static method. 3771 3772**System capability**: SystemCapability.Communication.IPC.Core 3773 3774**Return value** 3775 3776 | Type | Description | 3777 | ------------- | ----------------------------- | 3778 | [MessageParcel](#messageparceldeprecated) | **MessageParcel** object created.| 3779 3780**Example** 3781 3782 ```ts 3783 import { hilog } from '@kit.PerformanceAnalysisKit'; 3784 3785 let data = rpc.MessageParcel.create(); 3786 hilog.info(0x0000, 'testTag', 'RpcClient: data is ' + data); 3787 3788 // When the MessageParcel object is no longer used, the service calls the reclaim method to release resources. 3789 data.reclaim(); 3790 ``` 3791 3792### reclaim 3793 3794reclaim(): void 3795 3796Reclaims the **MessageParcel** object that is no longer used. 3797 3798**System capability**: SystemCapability.Communication.IPC.Core 3799 3800**Example** 3801 3802 ```ts 3803 let reply = rpc.MessageParcel.create(); 3804 reply.reclaim(); 3805 ``` 3806 3807### writeRemoteObject 3808 3809writeRemoteObject(object: IRemoteObject): boolean 3810 3811Serializes a remote object and writes it to this **MessageParcel** object. 3812 3813**System capability**: SystemCapability.Communication.IPC.Core 3814 3815**Parameters** 3816 3817 | Name| Type | Mandatory| Description | 3818 | ------ | ------------------------------- | ---- | --------------------------------------- | 3819 | object | [IRemoteObject](#iremoteobject) | Yes | Remote object to serialize and write to the **MessageParcel** object.| 3820 3821**Return value** 3822 3823 | Type | Description | 3824 | ------- | ----------------------------------------- | 3825 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3826 3827**Example** 3828 3829 ```ts 3830 import { hilog } from '@kit.PerformanceAnalysisKit'; 3831 3832 class MyDeathRecipient implements rpc.DeathRecipient { 3833 onRemoteDied() { 3834 hilog.info(0x0000, 'testTag', 'server died'); 3835 } 3836 } 3837 class TestRemoteObject extends rpc.RemoteObject { 3838 constructor(descriptor: string) { 3839 super(descriptor); 3840 } 3841 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3842 return true; 3843 } 3844 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3845 return true; 3846 } 3847 isObjectDead(): boolean { 3848 return false; 3849 } 3850 } 3851 let data = rpc.MessageParcel.create(); 3852 let testRemoteObject = new TestRemoteObject("testObject"); 3853 data.writeRemoteObject(testRemoteObject); 3854 ``` 3855 3856### readRemoteObject 3857 3858readRemoteObject(): IRemoteObject 3859 3860Reads 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. 3861 3862**System capability**: SystemCapability.Communication.IPC.Core 3863 3864**Return value** 3865 3866 | Type | Description | 3867 | ------------------------------- | ------------------ | 3868 | [IRemoteObject](#iremoteobject) | Remote object obtained.| 3869 3870**Example** 3871 3872 ```ts 3873 import { hilog } from '@kit.PerformanceAnalysisKit'; 3874 3875 class MyDeathRecipient implements rpc.DeathRecipient { 3876 onRemoteDied() { 3877 hilog.info(0x0000, 'testTag', 'server died'); 3878 } 3879 } 3880 class TestRemoteObject extends rpc.RemoteObject { 3881 constructor(descriptor: string) { 3882 super(descriptor); 3883 } 3884 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3885 return true; 3886 } 3887 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 3888 return true; 3889 } 3890 isObjectDead(): boolean { 3891 return false; 3892 } 3893 } 3894 let data = rpc.MessageParcel.create(); 3895 let testRemoteObject = new TestRemoteObject("testObject"); 3896 data.writeRemoteObject(testRemoteObject); 3897 let proxy = data.readRemoteObject(); 3898 hilog.info(0x0000, 'testTag', 'readRemoteObject is ' + proxy); 3899 ``` 3900 3901### writeInterfaceToken 3902 3903writeInterfaceToken(token: string): boolean 3904 3905Writes an interface token to this **MessageParcel** object. The remote object can use this interface token to verify the communication. 3906 3907**System capability**: SystemCapability.Communication.IPC.Core 3908 3909**Parameters** 3910 3911 | Name| Type | Mandatory| Description | 3912 | ------ | ------ | ---- | ------------------ | 3913 | token | string | Yes | Interface token to write.| 3914 3915**Return value** 3916 3917 | Type | Description | 3918 | ------- | ----------------------------------------- | 3919 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3920 3921**Example** 3922 3923 ```ts 3924 import { hilog } from '@kit.PerformanceAnalysisKit'; 3925 3926 let data = rpc.MessageParcel.create(); 3927 let result = data.writeInterfaceToken("aaa"); 3928 hilog.info(0x0000, 'testTag', 'RpcServer: writeInterfaceToken is ' + result); 3929 ``` 3930 3931### readInterfaceToken 3932 3933readInterfaceToken(): string 3934 3935Reads 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. 3936 3937**System capability**: SystemCapability.Communication.IPC.Core 3938 3939**Return value** 3940 3941 | Type | Description | 3942 | ------ | ------------------------ | 3943 | string | Interface token obtained.| 3944 3945**Example** 3946 3947 ```ts 3948 import { hilog } from '@kit.PerformanceAnalysisKit'; 3949 3950 class Stub extends rpc.RemoteObject { 3951 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 3952 let interfaceToken = data.readInterfaceToken(); 3953 hilog.info(0x0000, 'testTag', 'RpcServer: interfaceToken is ' + interfaceToken); 3954 return true; 3955 } 3956 } 3957 ``` 3958 3959### getSize 3960 3961getSize(): number 3962 3963Obtains the data size of this **MessageParcel** object. 3964 3965**System capability**: SystemCapability.Communication.IPC.Core 3966 3967**Return value** 3968 3969 | Type | Description | 3970 | ------ | --------------------------------------------- | 3971 | number | Size of the **MessageParcel** object obtained, in bytes.| 3972 3973**Example** 3974 3975 ```ts 3976 import { hilog } from '@kit.PerformanceAnalysisKit'; 3977 3978 let data = rpc.MessageParcel.create(); 3979 let size = data.getSize(); 3980 hilog.info(0x0000, 'testTag', 'RpcClient: size is ' + size); 3981 ``` 3982 3983### getCapacity 3984 3985getCapacity(): number 3986 3987Obtains the capacity of this **MessageParcel** object. 3988 3989**System capability**: SystemCapability.Communication.IPC.Core 3990 3991**Return value** 3992 3993 | Type | Description | 3994 | ------ | --------------------------------------------- | 3995 | number | **MessageParcel** capacity obtained, in bytes.| 3996 3997**Example** 3998 3999 ```ts 4000 import { hilog } from '@kit.PerformanceAnalysisKit'; 4001 4002 let data = rpc.MessageParcel.create(); 4003 let result = data.getCapacity(); 4004 hilog.info(0x0000, 'testTag', 'RpcClient: capacity is ' + result); 4005 ``` 4006 4007### setSize 4008 4009setSize(size: number): boolean 4010 4011Sets the size of data contained in this **MessageParcel** object. 4012 4013**System capability**: SystemCapability.Communication.IPC.Core 4014 4015**Parameters** 4016 4017 | Name| Type | Mandatory| Description | 4018 | ------ | ------ | ---- | ------------------------------------------- | 4019 | size | number | Yes | Data size to set, in bytes.| 4020 4021**Return value** 4022 4023 | Type | Description | 4024 | ------- | --------------------------------- | 4025 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4026 4027**Example** 4028 4029 ```ts 4030 import { hilog } from '@kit.PerformanceAnalysisKit'; 4031 4032 let data = rpc.MessageParcel.create(); 4033 let setSize = data.setSize(16); 4034 hilog.info(0x0000, 'testTag', 'RpcClient: setSize is ' + setSize); 4035 ``` 4036 4037### setCapacity 4038 4039setCapacity(size: number): boolean 4040 4041Sets the storage capacity of this **MessageParcel** object. 4042 4043**System capability**: SystemCapability.Communication.IPC.Core 4044 4045**Parameters** 4046 4047 | Name| Type | Mandatory| Description | 4048 | ------ | ------ | ---- | ------------------------------------------- | 4049 | size | number | Yes | Storage capacity to set, in bytes.| 4050 4051**Return value** 4052 4053 | Type | Description | 4054 | ------- | --------------------------------- | 4055 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4056 4057**Example** 4058 4059 ```ts 4060 import { hilog } from '@kit.PerformanceAnalysisKit'; 4061 4062 let data = rpc.MessageParcel.create(); 4063 let result = data.setCapacity(100); 4064 hilog.info(0x0000, 'testTag', 'RpcClient: setCapacity is ' + result); 4065 ``` 4066 4067### getWritableBytes 4068 4069getWritableBytes(): number 4070 4071Obtains the writable capacity of this **MessageParcel** object. 4072 4073**System capability**: SystemCapability.Communication.IPC.Core 4074 4075**Return value** 4076 4077 | Type | Description | 4078 | ------ | --------------------------------------------------- | 4079 | number | **MessageParcel** writable capacity obtained, in bytes.| 4080 4081**Example** 4082 4083 ```ts 4084 import { hilog } from '@kit.PerformanceAnalysisKit'; 4085 4086 class Stub extends rpc.RemoteObject { 4087 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4088 let getWritableBytes = data.getWritableBytes(); 4089 hilog.info(0x0000, 'testTag', 'RpcServer: getWritableBytes is ' + getWritableBytes); 4090 return true; 4091 } 4092 } 4093 ``` 4094 4095### getReadableBytes 4096 4097getReadableBytes(): number 4098 4099Obtains the readable capacity of this **MessageParcel** object. 4100 4101**System capability**: SystemCapability.Communication.IPC.Core 4102 4103**Return value** 4104 4105 | Type | Description | 4106 | ------ | --------------------------------------------------- | 4107 | number | **MessageParcel** object readable capacity, in bytes.| 4108 4109**Example** 4110 4111 ```ts 4112 import { hilog } from '@kit.PerformanceAnalysisKit'; 4113 4114 class Stub extends rpc.RemoteObject { 4115 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 4116 let result = data.getReadableBytes(); 4117 hilog.info(0x0000, 'testTag', 'RpcServer: getReadableBytes is ' + result); 4118 return true; 4119 } 4120 } 4121 ``` 4122 4123### getReadPosition 4124 4125getReadPosition(): number 4126 4127Obtains the read position of this **MessageParcel** object. 4128 4129**System capability**: SystemCapability.Communication.IPC.Core 4130 4131**Return value** 4132 4133 | Type | Description | 4134 | ------ | --------------------------------------- | 4135 | number | Current read position of the **MessageParcel** object.| 4136 4137**Example** 4138 4139 ```ts 4140 import { hilog } from '@kit.PerformanceAnalysisKit'; 4141 4142 let data = rpc.MessageParcel.create(); 4143 let readPos = data.getReadPosition(); 4144 hilog.info(0x0000, 'testTag', 'RpcClient: readPos is ' + readPos); 4145 ``` 4146 4147### getWritePosition 4148 4149getWritePosition(): number 4150 4151Obtains the write position of this **MessageParcel** object. 4152 4153**System capability**: SystemCapability.Communication.IPC.Core 4154 4155**Return value** 4156 4157 | Type | Description | 4158 | ------ | --------------------------------------- | 4159 | number | Current write position of the **MessageParcel** object.| 4160 4161**Example** 4162 4163 ```ts 4164 import { hilog } from '@kit.PerformanceAnalysisKit'; 4165 4166 let data = rpc.MessageParcel.create(); 4167 data.writeInt(10); 4168 let bwPos = data.getWritePosition(); 4169 hilog.info(0x0000, 'testTag', 'RpcClient: bwPos is ' + bwPos); 4170 ``` 4171 4172### rewindRead 4173 4174rewindRead(pos: number): boolean 4175 4176Moves the read pointer to the specified position. 4177 4178**System capability**: SystemCapability.Communication.IPC.Core 4179 4180**Parameters** 4181 4182 | Name| Type | Mandatory| Description | 4183 | ------ | ------ | ---- | ------------------------ | 4184 | pos | number | Yes | Position from which data is to read.| 4185 4186**Return value** 4187 4188 | Type | Description | 4189 | ------- | ------------------------------------------------- | 4190 | boolean | Returns **true** if the read position changes; returns **false** otherwise.| 4191 4192**Example** 4193 4194 ```ts 4195 import { hilog } from '@kit.PerformanceAnalysisKit'; 4196 4197 let data = rpc.MessageParcel.create(); 4198 data.writeInt(12); 4199 data.writeString("parcel"); 4200 let number = data.readInt(); 4201 hilog.info(0x0000, 'testTag', 'RpcClient: number is ' + number); 4202 data.rewindRead(0); 4203 let number2 = data.readInt(); 4204 hilog.info(0x0000, 'testTag', 'RpcClient: rewindRead is ' + number2); 4205 ``` 4206 4207### rewindWrite 4208 4209rewindWrite(pos: number): boolean 4210 4211Moves the write pointer to the specified position. 4212 4213**System capability**: SystemCapability.Communication.IPC.Core 4214 4215**Parameters** 4216 4217 | Name| Type | Mandatory| Description | 4218 | ------ | ------ | ---- | ------------------------ | 4219 | pos | number | Yes | Position from which data is to write.| 4220 4221**Return value** 4222 4223 | Type | Description | 4224 | ------- | --------------------------------------------- | 4225 | boolean | Returns **true** if the write position changes; returns **false** otherwise.| 4226 4227**Example** 4228 4229 ```ts 4230 import { hilog } from '@kit.PerformanceAnalysisKit'; 4231 4232 let data = rpc.MessageParcel.create(); 4233 data.writeInt(4); 4234 data.rewindWrite(0); 4235 data.writeInt(5); 4236 let number = data.readInt(); 4237 hilog.info(0x0000, 'testTag', 'RpcClient: rewindWrite is ' + number); 4238 ``` 4239 4240### writeByte 4241 4242writeByte(val: number): boolean 4243 4244Writes a Byte value to this **MessageParcel** object. 4245 4246**System capability**: SystemCapability.Communication.IPC.Core 4247 4248**Parameters** 4249 4250 | Name| Type | Mandatory| Description | 4251 | ------ | ------ | ---- | ---------------- | 4252 | val | number | Yes | Byte value to write.| 4253 4254**Return value** 4255 4256 | Type | Description | 4257 | ------- | ----------------------------- | 4258 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4259 4260**Example** 4261 4262 ```ts 4263 import { hilog } from '@kit.PerformanceAnalysisKit'; 4264 4265 let data = rpc.MessageParcel.create(); 4266 let result = data.writeByte(2); 4267 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4268 ``` 4269 4270### readByte 4271 4272readByte(): number 4273 4274Reads the Byte value from this **MessageParcel** object. 4275 4276**System capability**: SystemCapability.Communication.IPC.Core 4277 4278**Return value** 4279 4280 | Type | Description | 4281 | ------ | ------------ | 4282 | number | Byte value read.| 4283 4284**Example** 4285 4286 ```ts 4287 import { hilog } from '@kit.PerformanceAnalysisKit'; 4288 4289 let data = rpc.MessageParcel.create(); 4290 let result = data.writeByte(2); 4291 hilog.info(0x0000, 'testTag', 'RpcClient: writeByte is ' + result); 4292 let ret = data.readByte(); 4293 hilog.info(0x0000, 'testTag', 'RpcClient: readByte is ' + ret); 4294 ``` 4295 4296### writeShort 4297 4298writeShort(val: number): boolean 4299 4300Writes a Short int value to this **MessageParcel** object. 4301 4302**System capability**: SystemCapability.Communication.IPC.Core 4303 4304**Parameters** 4305 4306 | Name| Type | Mandatory| Description | 4307 | ------ | ------ | ---- | ------------------ | 4308 | val | number | Yes | Short int value to write.| 4309 4310**Return value** 4311 4312 | Type | Description | 4313 | ------- | ----------------------------- | 4314 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4315 4316**Example** 4317 4318 ```ts 4319 import { hilog } from '@kit.PerformanceAnalysisKit'; 4320 4321 let data = rpc.MessageParcel.create(); 4322 let result = data.writeShort(8); 4323 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4324 ``` 4325 4326### readShort 4327 4328readShort(): number 4329 4330Reads the Short int value from this **MessageParcel** object. 4331 4332**System capability**: SystemCapability.Communication.IPC.Core 4333 4334**Return value** 4335 4336 | Type | Description | 4337 | ------ | -------------- | 4338 | number | Short int value read.| 4339 4340**Example** 4341 4342 ```ts 4343 import { hilog } from '@kit.PerformanceAnalysisKit'; 4344 4345 let data = rpc.MessageParcel.create(); 4346 let result = data.writeShort(8); 4347 hilog.info(0x0000, 'testTag', 'RpcClient: writeShort is ' + result); 4348 let ret = data.readShort(); 4349 hilog.info(0x0000, 'testTag', 'RpcClient: readShort is ' + ret); 4350 ``` 4351 4352### writeInt 4353 4354writeInt(val: number): boolean 4355 4356Writes an Int value to this **MessageParcel** object. 4357 4358**System capability**: SystemCapability.Communication.IPC.Core 4359 4360**Parameters** 4361 4362 | Name| Type | Mandatory| Description | 4363 | ------ | ------ | ---- | ---------------- | 4364 | val | number | Yes | Int value to write.| 4365 4366**Return value** 4367 4368 | Type | Description | 4369 | ------- | ----------------------------- | 4370 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4371 4372**Example** 4373 4374 ```ts 4375 import { hilog } from '@kit.PerformanceAnalysisKit'; 4376 4377 let data = rpc.MessageParcel.create(); 4378 let result = data.writeInt(10); 4379 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4380 ``` 4381 4382### readInt 4383 4384readInt(): number 4385 4386Reads the Int value from this **MessageParcel** object. 4387 4388**System capability**: SystemCapability.Communication.IPC.Core 4389 4390**Return value** 4391 4392 | Type | Description | 4393 | ------ | ------------ | 4394 | number | Int value read.| 4395 4396**Example** 4397 4398 ```ts 4399 import { hilog } from '@kit.PerformanceAnalysisKit'; 4400 4401 let data = rpc.MessageParcel.create(); 4402 let result = data.writeInt(10); 4403 hilog.info(0x0000, 'testTag', 'RpcClient: writeInt is ' + result); 4404 let ret = data.readInt(); 4405 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + ret); 4406 ``` 4407 4408### writeLong 4409 4410writeLong(val: number): boolean 4411 4412Writes a Long int value to this **MessageParcel** object. 4413 4414**System capability**: SystemCapability.Communication.IPC.Core 4415 4416**Parameters** 4417 4418 | Name| Type | Mandatory| Description | 4419 | ------ | ------ | ---- | ---------------- | 4420 | val | number | Yes | Long int value to write.| 4421 4422**Return value** 4423 4424 | Type | Description | 4425 | ------- | --------------------------------- | 4426 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4427 4428**Example** 4429 4430 ```ts 4431 import { hilog } from '@kit.PerformanceAnalysisKit'; 4432 4433 let data = rpc.MessageParcel.create(); 4434 let result = data.writeLong(10000); 4435 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4436 ``` 4437 4438### readLong 4439 4440readLong(): number 4441 4442Reads the Long int value from this **MessageParcel** object. 4443 4444**System capability**: SystemCapability.Communication.IPC.Core 4445 4446**Return value** 4447 4448 | Type | Description | 4449 | ------ | -------------- | 4450 | number | Long integer read.| 4451 4452**Example** 4453 4454 ```ts 4455 import { hilog } from '@kit.PerformanceAnalysisKit'; 4456 4457 let data = rpc.MessageParcel.create(); 4458 let result = data.writeLong(10000); 4459 hilog.info(0x0000, 'testTag', 'RpcClient: writeLong is ' + result); 4460 let ret = data.readLong(); 4461 hilog.info(0x0000, 'testTag', 'RpcClient: readLong is ' + ret); 4462 ``` 4463 4464### writeFloat 4465 4466writeFloat(val: number): boolean 4467 4468Writes a Float value to this **MessageParcel** object. 4469 4470**System capability**: SystemCapability.Communication.IPC.Core 4471 4472**Parameters** 4473 4474 | Name| Type | Mandatory| Description | 4475 | ------ | ------ | ---- | ---------------- | 4476 | val | number | Yes | Float value to write.| 4477 4478**Return value** 4479 4480 | Type | Description | 4481 | ------- | --------------------------------- | 4482 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4483 4484**Example** 4485 4486 ```ts 4487 import { hilog } from '@kit.PerformanceAnalysisKit'; 4488 4489 let data = rpc.MessageParcel.create(); 4490 let result = data.writeFloat(1.2); 4491 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4492 ``` 4493 4494### readFloat 4495 4496readFloat(): number 4497 4498Reads the Float value from this **MessageParcel** object. 4499 4500**System capability**: SystemCapability.Communication.IPC.Core 4501 4502**Return value** 4503 4504 | Type | Description | 4505 | ------ | ------------ | 4506 | number | Float value read.| 4507 4508**Example** 4509 4510 ```ts 4511 import { hilog } from '@kit.PerformanceAnalysisKit'; 4512 4513 let data = rpc.MessageParcel.create(); 4514 let result = data.writeFloat(1.2); 4515 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloat is ' + result); 4516 let ret = data.readFloat(); 4517 hilog.info(0x0000, 'testTag', 'RpcClient: readFloat is ' + ret); 4518 ``` 4519 4520### writeDouble 4521 4522writeDouble(val: number): boolean 4523 4524Writes a Double value to this **MessageParcel** object. 4525 4526**System capability**: SystemCapability.Communication.IPC.Core 4527 4528**Parameters** 4529 4530 | Name| Type | Mandatory| Description | 4531 | ------ | ------ | ---- | ---------------------- | 4532 | val | number | Yes | Double value to write.| 4533 4534**Return value** 4535 4536 | Type | Description | 4537 | ------- | --------------------------------- | 4538 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4539 4540**Example** 4541 4542 ```ts 4543 import { hilog } from '@kit.PerformanceAnalysisKit'; 4544 4545 let data = rpc.MessageParcel.create(); 4546 let result = data.writeDouble(10.2); 4547 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4548 ``` 4549 4550### readDouble 4551 4552readDouble(): number 4553 4554Reads the Double value from this **MessageParcel** object. 4555 4556**System capability**: SystemCapability.Communication.IPC.Core 4557 4558**Return value** 4559 4560 | Type | Description | 4561 | ------ | ------------------ | 4562 | number | Double value read.| 4563 4564**Example** 4565 4566 ```ts 4567 import { hilog } from '@kit.PerformanceAnalysisKit'; 4568 4569 let data = rpc.MessageParcel.create(); 4570 let result = data.writeDouble(10.2); 4571 hilog.info(0x0000, 'testTag', 'RpcClient: writeDouble is ' + result); 4572 let ret = data.readDouble(); 4573 hilog.info(0x0000, 'testTag', 'RpcClient: readDouble is ' + ret); 4574 ``` 4575 4576### writeBoolean 4577 4578writeBoolean(val: boolean): boolean 4579 4580Writes a Boolean value to this **MessageParcel** object. 4581 4582**System capability**: SystemCapability.Communication.IPC.Core 4583 4584**Parameters** 4585 4586 | Name| Type | Mandatory| Description | 4587 | ------ | ------- | ---- | ---------------- | 4588 | val | boolean | Yes | Boolean value to write.| 4589 4590**Return value** 4591 4592 | Type | Description | 4593 | ------- | --------------------------------- | 4594 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4595 4596**Example** 4597 4598 ```ts 4599 import { hilog } from '@kit.PerformanceAnalysisKit'; 4600 4601 let data = rpc.MessageParcel.create(); 4602 let result = data.writeBoolean(false); 4603 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4604 ``` 4605 4606### readBoolean 4607 4608readBoolean(): boolean 4609 4610Reads the Boolean value from this **MessageParcel** object. 4611 4612**System capability**: SystemCapability.Communication.IPC.Core 4613 4614**Return value** 4615 4616 | Type | Description | 4617 | ------- | -------------------- | 4618 | boolean | Boolean value read.| 4619 4620**Example** 4621 4622 ```ts 4623 import { hilog } from '@kit.PerformanceAnalysisKit'; 4624 4625 let data = rpc.MessageParcel.create(); 4626 let result = data.writeBoolean(false); 4627 hilog.info(0x0000, 'testTag', 'RpcClient: writeBoolean is ' + result); 4628 let ret = data.readBoolean(); 4629 hilog.info(0x0000, 'testTag', 'RpcClient: readBoolean is ' + ret); 4630 ``` 4631 4632### writeChar 4633 4634writeChar(val: number): boolean 4635 4636Writes a Char value to this **MessageParcel** object. 4637 4638**System capability**: SystemCapability.Communication.IPC.Core 4639 4640**Parameters** 4641 4642 | Name| Type | Mandatory| Description | 4643 | ------ | ------ | ---- | -------------------- | 4644 | val | number | Yes | Char value to write.| 4645 4646**Return value** 4647 4648 | Type | Description | 4649 | ------- | ----------------------------- | 4650 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4651 4652**Example** 4653 4654 ```ts 4655 import { hilog } from '@kit.PerformanceAnalysisKit'; 4656 4657 let data = rpc.MessageParcel.create(); 4658 let result = data.writeChar(97); 4659 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4660 ``` 4661 4662### readChar 4663 4664readChar(): number 4665 4666Reads the single character value from this **MessageParcel** object. 4667 4668**System capability**: SystemCapability.Communication.IPC.Core 4669 4670**Return value** 4671 4672 | Type | Description | 4673 | ------ | ---------------- | 4674 | number | Char value read.| 4675 4676**Example** 4677 4678 ```ts 4679 import { hilog } from '@kit.PerformanceAnalysisKit'; 4680 4681 let data = rpc.MessageParcel.create(); 4682 let result = data.writeChar(97); 4683 hilog.info(0x0000, 'testTag', 'RpcClient: writeChar is ' + result); 4684 let ret = data.readChar(); 4685 hilog.info(0x0000, 'testTag', 'RpcClient: readChar is ' + ret); 4686 ``` 4687 4688### writeString 4689 4690writeString(val: string): boolean 4691 4692Writes a string to this **MessageParcel** object. 4693 4694**System capability**: SystemCapability.Communication.IPC.Core 4695 4696**Parameters** 4697 4698 | Name| Type | Mandatory| Description | 4699 | ------ | ------ | ---- | ----------------------------------------- | 4700 | val | string | Yes | String to write. The length of the string must be less than 40960 bytes.| 4701 4702**Return value** 4703 4704 | Type | Description | 4705 | ------- | --------------------------------- | 4706 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4707 4708**Example** 4709 4710 ```ts 4711 import { hilog } from '@kit.PerformanceAnalysisKit'; 4712 4713 let data = rpc.MessageParcel.create(); 4714 let result = data.writeString('abc'); 4715 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4716 ``` 4717 4718### readString 4719 4720readString(): string 4721 4722Reads the string from this **MessageParcel** object. 4723 4724**System capability**: SystemCapability.Communication.IPC.Core 4725 4726**Return value** 4727 4728 | Type | Description | 4729 | ------ | -------------- | 4730 | string | String read.| 4731 4732**Example** 4733 4734 ```ts 4735 import { hilog } from '@kit.PerformanceAnalysisKit'; 4736 4737 let data = rpc.MessageParcel.create(); 4738 let result = data.writeString('abc'); 4739 hilog.info(0x0000, 'testTag', 'RpcClient: writeString is ' + result); 4740 let ret = data.readString(); 4741 hilog.info(0x0000, 'testTag', 'RpcClient: readString is ' + ret); 4742 ``` 4743 4744### writeSequenceable 4745 4746writeSequenceable(val: Sequenceable): boolean 4747 4748Writes a sequenceable object to this **MessageParcel** object. 4749 4750**System capability**: SystemCapability.Communication.IPC.Core 4751 4752**Parameters** 4753 4754 | Name| Type | Mandatory| Description | 4755 | ------ | ----------------------------- | ---- | -------------------- | 4756 | val | [Sequenceable](#sequenceabledeprecated) | Yes | Sequenceable object to write.| 4757 4758**Return value** 4759 4760 | Type | Description | 4761 | ------- | -------------------------------- | 4762 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4763 4764**Example** 4765 4766 ```ts 4767 import { hilog } from '@kit.PerformanceAnalysisKit'; 4768 4769 class MySequenceable implements rpc.Sequenceable { 4770 num: number = 0; 4771 str: string = ''; 4772 constructor(num: number, str: string) { 4773 this.num = num; 4774 this.str = str; 4775 } 4776 marshalling(messageParcel: rpc.MessageParcel): boolean { 4777 messageParcel.writeInt(this.num); 4778 messageParcel.writeString(this.str); 4779 return true; 4780 } 4781 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4782 this.num = messageParcel.readInt(); 4783 this.str = messageParcel.readString(); 4784 return true; 4785 } 4786 } 4787 let sequenceable = new MySequenceable(1, "aaa"); 4788 let data = rpc.MessageParcel.create(); 4789 let result = data.writeSequenceable(sequenceable); 4790 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4791 ``` 4792 4793### readSequenceable 4794 4795readSequenceable(dataIn: Sequenceable): boolean 4796 4797Reads member variables from this **MessageParcel** object. 4798 4799**System capability**: SystemCapability.Communication.IPC.Core 4800 4801**Parameters** 4802 4803 | Name| Type | Mandatory | Description | 4804 | ------ | ----------------------------- | ------- | ---------------------------------------------- | 4805 | dataIn | [Sequenceable](#sequenceabledeprecated) | Yes | Object that reads member variables from the **MessageParcel** object.| 4806 4807**Return value** 4808 4809 | Type | Description | 4810 | ------- | ---------------------------------------- | 4811 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4812 4813**Example** 4814 4815 ```ts 4816 import { hilog } from '@kit.PerformanceAnalysisKit'; 4817 4818 class MySequenceable implements rpc.Sequenceable { 4819 num: number = 0; 4820 str: string = ''; 4821 constructor(num: number, str: string) { 4822 this.num = num; 4823 this.str = str; 4824 } 4825 marshalling(messageParcel: rpc.MessageParcel): boolean { 4826 messageParcel.writeInt(this.num); 4827 messageParcel.writeString(this.str); 4828 return true; 4829 } 4830 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 4831 this.num = messageParcel.readInt(); 4832 this.str = messageParcel.readString(); 4833 return true; 4834 } 4835 } 4836 let sequenceable = new MySequenceable(1, "aaa"); 4837 let data = rpc.MessageParcel.create(); 4838 let result = data.writeSequenceable(sequenceable); 4839 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 4840 let ret = new MySequenceable(0, ""); 4841 let result2 = data.readSequenceable(ret); 4842 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 4843 ``` 4844 4845### writeByteArray 4846 4847writeByteArray(byteArray: number[]): boolean 4848 4849Writes a byte array to this **MessageParcel** object. 4850 4851**System capability**: SystemCapability.Communication.IPC.Core 4852 4853**Parameters** 4854 4855 | Name | Type | Mandatory| Description | 4856 | --------- | -------- | ---- | ------------------ | 4857 | byteArray | number[] | Yes | Byte array to write.| 4858 4859**Return value** 4860 4861 | Type | Description | 4862 | ------- | -------------------------------- | 4863 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4864 4865**Example** 4866 4867 ```ts 4868 import { hilog } from '@kit.PerformanceAnalysisKit'; 4869 4870 let data = rpc.MessageParcel.create(); 4871 let ByteArrayVar = [1, 2, 3, 4, 5]; 4872 let result = data.writeByteArray(ByteArrayVar); 4873 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4874 ``` 4875 4876### readByteArray 4877 4878readByteArray(dataIn: number[]): void 4879 4880Reads a byte array from this **MessageParcel** object. 4881 4882**System capability**: SystemCapability.Communication.IPC.Core 4883 4884**Parameters** 4885 4886 | Name| Type | Mandatory| Description | 4887 | ------ | -------- | ---- | ------------------ | 4888 | dataIn | number[] | Yes | Byte array to read.| 4889 4890**Example** 4891 4892 ```ts 4893 import { hilog } from '@kit.PerformanceAnalysisKit'; 4894 4895 let data = rpc.MessageParcel.create(); 4896 let ByteArrayVar = [1, 2, 3, 4, 5]; 4897 let result = data.writeByteArray(ByteArrayVar); 4898 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4899 let array: Array<number> = new Array(5); 4900 data.readByteArray(array); 4901 ``` 4902 4903### readByteArray 4904 4905readByteArray(): number[] 4906 4907Reads the byte array from this **MessageParcel** object. 4908 4909**System capability**: SystemCapability.Communication.IPC.Core 4910 4911**Return value** 4912 4913 | Type | Description | 4914 | -------- | -------------- | 4915 | number[] | Byte array read.| 4916 4917**Example** 4918 4919 ```ts 4920 import { hilog } from '@kit.PerformanceAnalysisKit'; 4921 4922 let data = rpc.MessageParcel.create(); 4923 let ByteArrayVar = [1, 2, 3, 4, 5]; 4924 let result = data.writeByteArray(ByteArrayVar); 4925 hilog.info(0x0000, 'testTag', 'RpcClient: writeByteArray is ' + result); 4926 let array = data.readByteArray(); 4927 hilog.info(0x0000, 'testTag', 'RpcClient: readByteArray is ' + array); 4928 ``` 4929 4930### writeShortArray 4931 4932writeShortArray(shortArray: number[]): boolean 4933 4934Writes a short array to this **MessageParcel** object. 4935 4936**System capability**: SystemCapability.Communication.IPC.Core 4937 4938**Parameters** 4939 4940 | Name | Type | Mandatory| Description | 4941 | ---------- | -------- | ---- | -------------------- | 4942 | shortArray | number[] | Yes | Short array to write.| 4943 4944**Return value** 4945 4946 | Type | Description | 4947 | ------- | -------------------------------- | 4948 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 4949 4950**Example** 4951 4952 ```ts 4953 import { hilog } from '@kit.PerformanceAnalysisKit'; 4954 4955 let data = rpc.MessageParcel.create(); 4956 let result = data.writeShortArray([11, 12, 13]); 4957 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4958 ``` 4959 4960### readShortArray 4961 4962readShortArray(dataIn: number[]): void 4963 4964Reads a short array from this **MessageParcel** object. 4965 4966**System capability**: SystemCapability.Communication.IPC.Core 4967 4968**Parameters** 4969 4970 | Name| Type | Mandatory| Description | 4971 | ------ | -------- | ---- | -------------------- | 4972 | dataIn | number[] | Yes | Short array to read.| 4973 4974**Example** 4975 4976 ```ts 4977 import { hilog } from '@kit.PerformanceAnalysisKit'; 4978 4979 let data = rpc.MessageParcel.create(); 4980 let result = data.writeShortArray([11, 12, 13]); 4981 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 4982 let array: Array<number> = new Array(3); 4983 data.readShortArray(array); 4984 ``` 4985 4986### readShortArray 4987 4988readShortArray(): number[] 4989 4990Reads the short array from this **MessageParcel** object. 4991 4992**System capability**: SystemCapability.Communication.IPC.Core 4993 4994**Return value** 4995 4996 | Type | Description | 4997 | -------- | ---------------- | 4998 | number[] | Short array read.| 4999 5000**Example** 5001 5002 ```ts 5003 import { hilog } from '@kit.PerformanceAnalysisKit'; 5004 5005 let data = rpc.MessageParcel.create(); 5006 let result = data.writeShortArray([11, 12, 13]); 5007 hilog.info(0x0000, 'testTag', 'RpcClient: writeShortArray is ' + result); 5008 let array = data.readShortArray(); 5009 hilog.info(0x0000, 'testTag', 'RpcClient: readShortArray is ' + array); 5010 ``` 5011 5012### writeIntArray 5013 5014writeIntArray(intArray: number[]): boolean 5015 5016Writes an integer array to this **MessageParcel** object. 5017 5018**System capability**: SystemCapability.Communication.IPC.Core 5019 5020**Parameters** 5021 5022 | Name | Type | Mandatory| Description | 5023 | -------- | -------- | ---- | ------------------ | 5024 | intArray | number[] | Yes | Integer array to write.| 5025 5026**Return value** 5027 5028 | Type | Description | 5029 | ------- | -------------------------------- | 5030 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5031 5032**Example** 5033 5034 ```ts 5035 import { hilog } from '@kit.PerformanceAnalysisKit'; 5036 5037 let data = rpc.MessageParcel.create(); 5038 let result = data.writeIntArray([100, 111, 112]); 5039 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5040 ``` 5041 5042### readIntArray 5043 5044readIntArray(dataIn: number[]): void 5045 5046Reads an integer array from this **MessageParcel** object. 5047 5048**System capability**: SystemCapability.Communication.IPC.Core 5049 5050**Parameters** 5051 5052 | Name| Type | Mandatory| Description | 5053 | ------ | -------- | ---- | ------------------ | 5054 | dataIn | number[] | Yes | Integer array to read.| 5055 5056**Example** 5057 5058 ```ts 5059 import { hilog } from '@kit.PerformanceAnalysisKit'; 5060 5061 let data = rpc.MessageParcel.create(); 5062 let result = data.writeIntArray([100, 111, 112]); 5063 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5064 let array: Array<number> = new Array(3); 5065 data.readIntArray(array); 5066 ``` 5067 5068### readIntArray 5069 5070readIntArray(): number[] 5071 5072Reads the integer array from this **MessageParcel** object. 5073 5074**System capability**: SystemCapability.Communication.IPC.Core 5075 5076**Return value** 5077 5078 | Type | Description | 5079 | -------- | -------------- | 5080 | number[] | Integer array read.| 5081 5082**Example** 5083 5084 ```ts 5085 import { hilog } from '@kit.PerformanceAnalysisKit'; 5086 5087 let data = rpc.MessageParcel.create(); 5088 let result = data.writeIntArray([100, 111, 112]); 5089 hilog.info(0x0000, 'testTag', 'RpcClient: writeIntArray is ' + result); 5090 let array = data.readIntArray(); 5091 hilog.info(0x0000, 'testTag', 'RpcClient: readIntArray is ' + array); 5092 ``` 5093 5094### writeLongArray 5095 5096writeLongArray(longArray: number[]): boolean 5097 5098Writes a long array to this **MessageParcel** object. 5099 5100**System capability**: SystemCapability.Communication.IPC.Core 5101 5102**Parameters** 5103 5104 | Name | Type | Mandatory| Description | 5105 | --------- | -------- | ---- | -------------------- | 5106 | longArray | number[] | Yes | Long array to write.| 5107 5108**Return value** 5109 5110 | Type | Description | 5111 | ------- | ----------------------------- | 5112 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5113 5114**Example** 5115 5116 ```ts 5117 import { hilog } from '@kit.PerformanceAnalysisKit'; 5118 5119 let data = rpc.MessageParcel.create(); 5120 let result = data.writeLongArray([1111, 1112, 1113]); 5121 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5122 ``` 5123 5124### readLongArray 5125 5126readLongArray(dataIn: number[]): void 5127 5128Reads a long array from this **MessageParcel** object. 5129 5130**System capability**: SystemCapability.Communication.IPC.Core 5131 5132**Parameters** 5133 5134 | Name| Type | Mandatory| Description | 5135 | ------ | -------- | ---- | -------------------- | 5136 | dataIn | number[] | Yes | Long array to read.| 5137 5138**Example** 5139 5140 ```ts 5141 import { hilog } from '@kit.PerformanceAnalysisKit'; 5142 5143 let data = rpc.MessageParcel.create(); 5144 let result = data.writeLongArray([1111, 1112, 1113]); 5145 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5146 let array: Array<number> = new Array(3); 5147 data.readLongArray(array); 5148 ``` 5149 5150### readLongArray 5151 5152readLongArray(): number[] 5153 5154Reads the long array from this **MessageParcel** object. 5155 5156**System capability**: SystemCapability.Communication.IPC.Core 5157 5158**Return value** 5159 5160 | Type | Description | 5161 | -------- | ---------------- | 5162 | number[] | Long array read.| 5163 5164**Example** 5165 5166 ```ts 5167 import { hilog } from '@kit.PerformanceAnalysisKit'; 5168 5169 let data = rpc.MessageParcel.create(); 5170 let result = data.writeLongArray([1111, 1112, 1113]); 5171 hilog.info(0x0000, 'testTag', 'RpcClient: writeLongArray is ' + result); 5172 let array = data.readLongArray(); 5173 hilog.info(0x0000, 'testTag', 'RpcClient: readLongArray is ' + array); 5174 ``` 5175 5176### writeFloatArray 5177 5178writeFloatArray(floatArray: number[]): boolean 5179 5180Writes a FloatArray to this **MessageParcel** object. 5181 5182**System capability**: SystemCapability.Communication.IPC.Core 5183 5184**Parameters** 5185 5186 | Name| Type| Mandatory| Description | 5187 | ---------- | -------- | ---- | --- | 5188 | 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.| 5189 5190**Return value** 5191 5192 | Type | Description | 5193 | ------- | -------------------------------- | 5194 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5195 5196**Example** 5197 5198 ```ts 5199 import { hilog } from '@kit.PerformanceAnalysisKit'; 5200 5201 let data = rpc.MessageParcel.create(); 5202 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5203 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5204 ``` 5205 5206### readFloatArray 5207 5208readFloatArray(dataIn: number[]): void 5209 5210Reads a FloatArray from this **MessageParcel** object. 5211 5212**System capability**: SystemCapability.Communication.IPC.Core 5213 5214**Parameters** 5215 5216 | Name| Type | Mandatory| Description | 5217 | ------ | -------- | ---- | ------ | 5218 | 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.| 5219 5220**Example** 5221 5222 ```ts 5223 import { hilog } from '@kit.PerformanceAnalysisKit'; 5224 5225 let data = rpc.MessageParcel.create(); 5226 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5227 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5228 let array: Array<number> = new Array(3); 5229 data.readFloatArray(array); 5230 ``` 5231 5232### readFloatArray 5233 5234readFloatArray(): number[] 5235 5236Reads the FloatArray from this **MessageParcel** object. 5237 5238**System capability**: SystemCapability.Communication.IPC.Core 5239 5240**Return value** 5241 5242 | Type | Description | 5243 | -------- | -------------- | 5244 | number[] | FloatArray read.| 5245 5246**Example** 5247 5248 ```ts 5249 import { hilog } from '@kit.PerformanceAnalysisKit'; 5250 5251 let data = rpc.MessageParcel.create(); 5252 let result = data.writeFloatArray([1.2, 1.3, 1.4]); 5253 hilog.info(0x0000, 'testTag', 'RpcClient: writeFloatArray is ' + result); 5254 let array = data.readFloatArray(); 5255 hilog.info(0x0000, 'testTag', 'RpcClient: readFloatArray is ' + array); 5256 ``` 5257 5258### writeDoubleArray 5259 5260writeDoubleArray(doubleArray: number[]): boolean 5261 5262Writes a DoubleArray to this **MessageParcel** object. 5263 5264**System capability**: SystemCapability.Communication.IPC.Core 5265 5266**Parameters** 5267 5268 | Name | Type | Mandatory| Description | 5269 | ----------- | -------- | ---- | ------------------------ | 5270 | doubleArray | number[] | Yes | DoubleArray to write.| 5271 5272**Return value** 5273 5274 | Type | Description | 5275 | ------- | -------------------------------- | 5276 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5277 5278**Example** 5279 5280 ```ts 5281 import { hilog } from '@kit.PerformanceAnalysisKit'; 5282 5283 let data = rpc.MessageParcel.create(); 5284 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5285 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5286 ``` 5287 5288### readDoubleArray 5289 5290readDoubleArray(dataIn: number[]): void 5291 5292Reads a DoubleArray from this **MessageParcel** object. 5293 5294**System capability**: SystemCapability.Communication.IPC.Core 5295 5296**Parameters** 5297 5298 | Name| Type | Mandatory| Description | 5299 | ------ | -------- | ---- | ------------------------ | 5300 | dataIn | number[] | Yes | DoubleArray to read.| 5301 5302**Example** 5303 5304 ```ts 5305 import { hilog } from '@kit.PerformanceAnalysisKit'; 5306 5307 let data = rpc.MessageParcel.create(); 5308 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5309 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5310 let array: Array<number> = new Array(3); 5311 data.readDoubleArray(array); 5312 ``` 5313 5314### readDoubleArray 5315 5316readDoubleArray(): number[] 5317 5318Reads the DoubleArray from this **MessageParcel** object. 5319 5320**System capability**: SystemCapability.Communication.IPC.Core 5321 5322**Return value** 5323 5324 | Type | Description | 5325 | -------- | -------------------- | 5326 | number[] | DoubleArray read.| 5327 5328**Example** 5329 5330 ```ts 5331 import { hilog } from '@kit.PerformanceAnalysisKit'; 5332 5333 let data = rpc.MessageParcel.create(); 5334 let result = data.writeDoubleArray([11.1, 12.2, 13.3]); 5335 hilog.info(0x0000, 'testTag', 'RpcClient: writeDoubleArray is ' + result); 5336 let array = data.readDoubleArray(); 5337 hilog.info(0x0000, 'testTag', 'RpcClient: readDoubleArray is ' + array); 5338 ``` 5339 5340### writeBooleanArray 5341 5342writeBooleanArray(booleanArray: boolean[]): boolean 5343 5344Writes a Boolean array to this **MessageParcel** object. 5345 5346**System capability**: SystemCapability.Communication.IPC.Core 5347 5348**Parameters** 5349 5350 | Name | Type | Mandatory| Description | 5351 | ------------ | --------- | ---- | ------------------ | 5352 | booleanArray | boolean[] | Yes | Boolean array to write.| 5353 5354**Return value** 5355 5356 | Type | Description | 5357 | ------- | -------------------------------- | 5358 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5359 5360**Example** 5361 5362 ```ts 5363 import { hilog } from '@kit.PerformanceAnalysisKit'; 5364 5365 let data = rpc.MessageParcel.create(); 5366 let result = data.writeBooleanArray([false, true, false]); 5367 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5368 ``` 5369 5370### readBooleanArray 5371 5372readBooleanArray(dataIn: boolean[]): void 5373 5374Reads a Boolean array from this **MessageParcel** object. 5375 5376**System capability**: SystemCapability.Communication.IPC.Core 5377 5378**Parameters** 5379 5380 | Name| Type | Mandatory| Description | 5381 | ------ | --------- | ---- | ------------------ | 5382 | dataIn | boolean[] | Yes | Boolean array to read.| 5383 5384**Example** 5385 5386 ```ts 5387 import { hilog } from '@kit.PerformanceAnalysisKit'; 5388 5389 let data = rpc.MessageParcel.create(); 5390 let result = data.writeBooleanArray([false, true, false]); 5391 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5392 let array: Array<boolean> = new Array(3); 5393 data.readBooleanArray(array); 5394 ``` 5395 5396### readBooleanArray 5397 5398readBooleanArray(): boolean[] 5399 5400Reads the Boolean array from this **MessageParcel** object. 5401 5402**System capability**: SystemCapability.Communication.IPC.Core 5403 5404**Return value** 5405 5406 | Type | Description | 5407 | --------- | -------------- | 5408 | boolean[] | Boolean array read.| 5409 5410**Example** 5411 5412 ```ts 5413 import { hilog } from '@kit.PerformanceAnalysisKit'; 5414 5415 let data = rpc.MessageParcel.create(); 5416 let result = data.writeBooleanArray([false, true, false]); 5417 hilog.info(0x0000, 'testTag', 'RpcClient: writeBooleanArray is ' + result); 5418 let array = data.readBooleanArray(); 5419 hilog.info(0x0000, 'testTag', 'RpcClient: readBooleanArray is ' + array); 5420 ``` 5421 5422### writeCharArray 5423 5424writeCharArray(charArray: number[]): boolean 5425 5426Writes a character array to this **MessageParcel** object. 5427 5428**System capability**: SystemCapability.Communication.IPC.Core 5429 5430**Parameters** 5431 5432 | Name | Type | Mandatory| Description | 5433 | --------- | -------- | ---- | ---------------------- | 5434 | charArray | number[] | Yes | Character array to write.| 5435 5436**Return value** 5437 5438 | Type | Description | 5439 | ------- | -------------------------------- | 5440 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5441 5442**Example** 5443 5444 ```ts 5445 import { hilog } from '@kit.PerformanceAnalysisKit'; 5446 5447 let data = rpc.MessageParcel.create(); 5448 let result = data.writeCharArray([97, 98, 88]); 5449 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5450 ``` 5451 5452### readCharArray 5453 5454readCharArray(dataIn: number[]): void 5455 5456Reads a character array from this **MessageParcel** object. 5457 5458**System capability**: SystemCapability.Communication.IPC.Core 5459 5460**Parameters** 5461 5462 | Name| Type | Mandatory| Description | 5463 | ------ | -------- | ---- | ---------------------- | 5464 | dataIn | number[] | Yes | Character array to read.| 5465 5466**Example** 5467 5468 ```ts 5469 import { hilog } from '@kit.PerformanceAnalysisKit'; 5470 5471 let data = rpc.MessageParcel.create(); 5472 let result = data.writeCharArray([97, 98, 99]); 5473 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5474 let array: Array<number> = new Array(3); 5475 data.readCharArray(array); 5476 ``` 5477 5478### readCharArray 5479 5480readCharArray(): number[] 5481 5482Reads the character array from this **MessageParcel** object. 5483 5484**System capability**: SystemCapability.Communication.IPC.Core 5485 5486**Return value** 5487 5488 | Type | Description | 5489 | -------- | ------------------ | 5490 | number[] | Character array read.| 5491 5492**Example** 5493 5494 ```ts 5495 import { hilog } from '@kit.PerformanceAnalysisKit'; 5496 5497 let data = rpc.MessageParcel.create(); 5498 let result = data.writeCharArray([97, 98, 99]); 5499 hilog.info(0x0000, 'testTag', 'RpcClient: writeCharArray is ' + result); 5500 let array = data.readCharArray(); 5501 hilog.info(0x0000, 'testTag', 'RpcClient: readCharArray is ' + array); 5502 ``` 5503 5504### writeStringArray 5505 5506writeStringArray(stringArray: string[]): boolean 5507 5508Writes a string array to this **MessageParcel** object. 5509 5510**System capability**: SystemCapability.Communication.IPC.Core 5511 5512**Parameters** 5513 5514 | Name | Type | Mandatory| Description | 5515 | ----------- | -------- | ---- | ---------------- | 5516 | stringArray | string[] | Yes | String array to write. The length of a single element in the array must be less than 40960 bytes.| 5517 5518**Return value** 5519 5520 | Type | Description| 5521 | ------- | -------------------------------- | 5522 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5523 5524**Example** 5525 5526 ```ts 5527 import { hilog } from '@kit.PerformanceAnalysisKit'; 5528 5529 let data = rpc.MessageParcel.create(); 5530 let result = data.writeStringArray(["abc", "def"]); 5531 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5532 ``` 5533 5534### readStringArray 5535 5536readStringArray(dataIn: string[]): void 5537 5538Reads a string array from this **MessageParcel** object. 5539 5540**System capability**: SystemCapability.Communication.IPC.Core 5541 5542**Parameters** 5543 5544 | Name| Type | Mandatory| Description | 5545 | ------ | -------- | ---- | -------------------- | 5546 | dataIn | string[] | Yes | String array to read.| 5547 5548**Example** 5549 5550 ```ts 5551 import { hilog } from '@kit.PerformanceAnalysisKit'; 5552 5553 let data = rpc.MessageParcel.create(); 5554 let result = data.writeStringArray(["abc", "def"]); 5555 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5556 let array: Array<string> = new Array(2); 5557 data.readStringArray(array); 5558 ``` 5559 5560### readStringArray 5561 5562readStringArray(): string[] 5563 5564Reads the string array from this **MessageParcel** object. 5565 5566**System capability**: SystemCapability.Communication.IPC.Core 5567 5568**Return value** 5569 5570 | Type | Description | 5571 | -------- | ---------------- | 5572 | string[] | String array read.| 5573 5574**Example** 5575 5576 ```ts 5577 import { hilog } from '@kit.PerformanceAnalysisKit'; 5578 5579 let data = rpc.MessageParcel.create(); 5580 let result = data.writeStringArray(["abc", "def"]); 5581 hilog.info(0x0000, 'testTag', 'RpcClient: writeStringArray is ' + result); 5582 let array = data.readStringArray(); 5583 hilog.info(0x0000, 'testTag', 'RpcClient: readStringArray is ' + array); 5584 ``` 5585 5586### writeNoException<sup>8+</sup> 5587 5588writeNoException(): void 5589 5590Writes information to this **MessageParcel** object indicating that no exception occurred. 5591 5592**System capability**: SystemCapability.Communication.IPC.Core 5593 5594**Example** 5595 5596 ```ts 5597 import { hilog } from '@kit.PerformanceAnalysisKit'; 5598 5599 class MyDeathRecipient implements rpc.DeathRecipient { 5600 onRemoteDied() { 5601 hilog.info(0x0000, 'testTag', 'server died'); 5602 } 5603 } 5604 class TestRemoteObject extends rpc.RemoteObject { 5605 constructor(descriptor: string) { 5606 super(descriptor); 5607 } 5608 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5609 return true; 5610 } 5611 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5612 return true; 5613 } 5614 isObjectDead(): boolean { 5615 return false; 5616 } 5617 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 5618 if (code === 1) { 5619 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 5620 reply.writeNoException(); 5621 return true; 5622 } else { 5623 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 5624 return false; 5625 } 5626 } 5627 } 5628 ``` 5629 5630### readException<sup>8+</sup> 5631 5632readException(): void 5633 5634Reads the exception information from this **MessageParcel** object. 5635 5636**System capability**: SystemCapability.Communication.IPC.Core 5637 5638**Example** 5639 5640 ```ts 5641 // If the FA model is used, import featureAbility from @kit.AbilityKit. 5642 // import { featureAbility } from '@kit.AbilityKit'; 5643 import { Want, common } from '@kit.AbilityKit'; 5644 import { hilog } from '@kit.PerformanceAnalysisKit'; 5645 5646 let proxy: rpc.IRemoteObject | undefined; 5647 let connect: common.ConnectOptions = { 5648 onConnect: (elementName, remoteProxy) => { 5649 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 5650 proxy = remoteProxy; 5651 }, 5652 onDisconnect: (elementName) => { 5653 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 5654 }, 5655 onFailed: () => { 5656 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 5657 } 5658 }; 5659 let want: Want = { 5660 bundleName: "com.ohos.server", 5661 abilityName: "com.ohos.server.EntryAbility", 5662 }; 5663 5664 // Use this method to connect to the ability for the FA model. 5665 // FA.connectAbility(want,connect); 5666 5667 // Save the connection ID, which will be used for the subsequent service disconnection. 5668 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 5669 // Save the connection ID, which will be used for the subsequent service disconnection. 5670 let connectionId = context.connectServiceExtensionAbility(want, connect); 5671 ``` 5672 5673 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 5674 5675 ```ts 5676 import { hilog } from '@kit.PerformanceAnalysisKit'; 5677 5678 let option = new rpc.MessageOption(); 5679 let data = rpc.MessageParcel.create(); 5680 let reply = rpc.MessageParcel.create(); 5681 data.writeNoException(); 5682 data.writeString('hello'); 5683 if (proxy != undefined) { 5684 let a = proxy.sendRequest(1, data, reply, option) as Object; 5685 let b = a as Promise<rpc.SendRequestResult>; 5686 b.then((result: rpc.SendRequestResult) => { 5687 if (result.errCode === 0) { 5688 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 5689 result.reply.readException(); 5690 let msg = result.reply.readString(); 5691 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 5692 } else { 5693 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 5694 } 5695 }).catch((e: Error) => { 5696 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest got exception: ' + e.message); 5697 }).finally (() => { 5698 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 5699 data.reclaim(); 5700 reply.reclaim(); 5701 }); 5702 } 5703 ``` 5704 5705### writeSequenceableArray 5706 5707writeSequenceableArray(sequenceableArray: Sequenceable[]): boolean 5708 5709Writes a sequenceable array to this **MessageParcel** object. 5710 5711**System capability**: SystemCapability.Communication.IPC.Core 5712 5713**Parameters** 5714 5715| Name | Type | Mandatory| Description | 5716| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5717| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to write.| 5718 5719**Return value** 5720 5721 | Type | Description | 5722 | ------- | -------------------------------- | 5723 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5724 5725**Example** 5726 5727 ```ts 5728 import { hilog } from '@kit.PerformanceAnalysisKit'; 5729 5730 class MySequenceable implements rpc.Sequenceable { 5731 num: number = 0; 5732 str: string = ''; 5733 constructor(num: number, str: string) { 5734 this.num = num; 5735 this.str = str; 5736 } 5737 marshalling(messageParcel: rpc.MessageParcel): boolean { 5738 messageParcel.writeInt(this.num); 5739 messageParcel.writeString(this.str); 5740 return true; 5741 } 5742 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5743 this.num = messageParcel.readInt(); 5744 this.str = messageParcel.readString(); 5745 return true; 5746 } 5747 } 5748 let sequenceable = new MySequenceable(1, "aaa"); 5749 let sequenceable2 = new MySequenceable(2, "bbb"); 5750 let sequenceable3 = new MySequenceable(3, "ccc"); 5751 let a = [sequenceable, sequenceable2, sequenceable3]; 5752 let data = rpc.MessageParcel.create(); 5753 let result = data.writeSequenceableArray(a); 5754 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5755 ``` 5756 5757### readSequenceableArray<sup>8+</sup> 5758 5759readSequenceableArray(sequenceableArray: Sequenceable[]): void 5760 5761Reads a sequenceable array from this **MessageParcel** object. 5762 5763**System capability**: SystemCapability.Communication.IPC.Core 5764 5765**Parameters** 5766 5767| Name | Type | Mandatory| Description | 5768| ----------------- | ----------------------------------------- | ---- | -------------------------- | 5769| sequenceableArray | [Sequenceable](#sequenceabledeprecated)[] | Yes | Sequenceable array to read.| 5770 5771**Example** 5772 5773 ```ts 5774 import { hilog } from '@kit.PerformanceAnalysisKit'; 5775 5776 class MySequenceable implements rpc.Sequenceable { 5777 num: number = 0; 5778 str: string = ''; 5779 constructor(num: number, str: string) { 5780 this.num = num; 5781 this.str = str; 5782 } 5783 marshalling(messageParcel: rpc.MessageParcel): boolean { 5784 messageParcel.writeInt(this.num); 5785 messageParcel.writeString(this.str); 5786 return true; 5787 } 5788 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 5789 this.num = messageParcel.readInt(); 5790 this.str = messageParcel.readString(); 5791 return true; 5792 } 5793 } 5794 let sequenceable = new MySequenceable(1, "aaa"); 5795 let sequenceable2 = new MySequenceable(2, "bbb"); 5796 let sequenceable3 = new MySequenceable(3, "ccc"); 5797 let a = [sequenceable, sequenceable2, sequenceable3]; 5798 let data = rpc.MessageParcel.create(); 5799 let result = data.writeSequenceableArray(a); 5800 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceableArray is ' + result); 5801 let b = [new MySequenceable(0, ""), new MySequenceable(0, ""), new MySequenceable(0, "")]; 5802 data.readSequenceableArray(b); 5803 ``` 5804 5805### writeRemoteObjectArray<sup>8+</sup> 5806 5807writeRemoteObjectArray(objectArray: IRemoteObject[]): boolean 5808 5809Writes an array of **IRemoteObject** objects to this **MessageParcel** object. 5810 5811**System capability**: SystemCapability.Communication.IPC.Core 5812 5813**Parameters** 5814 5815 | Name | Type | Mandatory| Description | 5816 | ----------- | --------------- | ---- | ----- | 5817 | objectArray | [IRemoteObject](#iremoteobject)[] | Yes | Array of **IRemoteObject** objects to write.| 5818 5819**Return value** 5820 5821 | Type | Description | 5822 | ------- | -------------------------------- | 5823 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 5824 5825**Example** 5826 5827 ```ts 5828 import { hilog } from '@kit.PerformanceAnalysisKit'; 5829 5830 class MyDeathRecipient implements rpc.DeathRecipient { 5831 onRemoteDied() { 5832 hilog.info(0x0000, 'testTag', 'server died'); 5833 } 5834 } 5835 class TestRemoteObject extends rpc.RemoteObject { 5836 constructor(descriptor: string) { 5837 super(descriptor); 5838 this.attachLocalInterface(this, descriptor); 5839 } 5840 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5841 return true; 5842 } 5843 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5844 return true; 5845 } 5846 isObjectDead(): boolean { 5847 return false; 5848 } 5849 asObject(): rpc.IRemoteObject { 5850 return this; 5851 } 5852 } 5853 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5854 let data = rpc.MessageParcel.create(); 5855 let result = data.writeRemoteObjectArray(a); 5856 hilog.info(0x0000, 'testTag', 'RpcClient: writeRemoteObjectArray is ' + result); 5857 ``` 5858 5859### readRemoteObjectArray<sup>8+</sup> 5860 5861readRemoteObjectArray(objects: IRemoteObject[]): void 5862 5863Reads an **IRemoteObject** array from this **MessageParcel** object. 5864 5865**System capability**: SystemCapability.Communication.IPC.Core 5866 5867**Parameters** 5868 5869 | Name | Type | Mandatory| Description | 5870 | ------- | --------------- | ---- | --------- | 5871 | objects | [IRemoteObject](#iremoteobject)[] | Yes | **IRemoteObject** array to read.| 5872 5873**Example** 5874 5875 ```ts 5876 import { hilog } from '@kit.PerformanceAnalysisKit'; 5877 5878 class MyDeathRecipient implements rpc.DeathRecipient { 5879 onRemoteDied() { 5880 hilog.info(0x0000, 'testTag', 'server died'); 5881 } 5882 } 5883 class TestRemoteObject extends rpc.RemoteObject { 5884 constructor(descriptor: string) { 5885 super(descriptor); 5886 this.attachLocalInterface(this, descriptor); 5887 } 5888 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5889 return true; 5890 } 5891 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5892 return true; 5893 } 5894 isObjectDead(): boolean { 5895 return false; 5896 } 5897 asObject(): rpc.IRemoteObject { 5898 return this; 5899 } 5900 } 5901 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5902 let data = rpc.MessageParcel.create(); 5903 data.writeRemoteObjectArray(a); 5904 let b: Array<rpc.IRemoteObject> = new Array(3); 5905 data.readRemoteObjectArray(b); 5906 ``` 5907 5908### readRemoteObjectArray<sup>8+</sup> 5909 5910readRemoteObjectArray(): IRemoteObject[] 5911 5912Reads the **IRemoteObject** array from this **MessageParcel** object. 5913 5914**System capability**: SystemCapability.Communication.IPC.Core 5915 5916**Return value** 5917 5918 | Type | Description | 5919 | --------------- | --------------------------- | 5920 | [IRemoteObject](#iremoteobject)[] | **IRemoteObject** object array obtained.| 5921 5922**Example** 5923 5924 ```ts 5925 import { hilog } from '@kit.PerformanceAnalysisKit'; 5926 5927 class MyDeathRecipient implements rpc.DeathRecipient { 5928 onRemoteDied() { 5929 hilog.info(0x0000, 'testTag', 'server died'); 5930 } 5931 } 5932 class TestRemoteObject extends rpc.RemoteObject { 5933 constructor(descriptor: string) { 5934 super(descriptor); 5935 this.attachLocalInterface(this, descriptor); 5936 } 5937 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5938 return true; 5939 } 5940 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 5941 return true; 5942 } 5943 isObjectDead(): boolean { 5944 return false; 5945 } 5946 asObject(): rpc.IRemoteObject { 5947 return this; 5948 } 5949 } 5950 let a = [new TestRemoteObject("testObject1"), new TestRemoteObject("testObject2"), new TestRemoteObject("testObject3")]; 5951 let data = rpc.MessageParcel.create(); 5952 let result = data.writeRemoteObjectArray(a); 5953 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + result); 5954 let b = data.readRemoteObjectArray(); 5955 hilog.info(0x0000, 'testTag', 'RpcClient: readRemoteObjectArray is ' + b); 5956 ``` 5957 5958### closeFileDescriptor<sup>8+</sup> 5959 5960static closeFileDescriptor(fd: number): void 5961 5962Closes a file descriptor. This API is a static method. 5963 5964**System capability**: SystemCapability.Communication.IPC.Core 5965 5966**Parameters** 5967 5968 | Name| Type | Mandatory| Description | 5969 | ------ | ------ | ---- | -------------------- | 5970 | fd | number | Yes | File descriptor to close.| 5971 5972**Example** 5973 5974 ```ts 5975 import { fileIo } from '@kit.CoreFileKit'; 5976 5977 let filePath = "path/to/file"; 5978 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 5979 rpc.MessageParcel.closeFileDescriptor(file.fd); 5980 ``` 5981 5982### dupFileDescriptor<sup>8+</sup> 5983 5984static dupFileDescriptor(fd: number) :number 5985 5986Duplicates a file descriptor. This API is a static method. 5987 5988**System capability**: SystemCapability.Communication.IPC.Core 5989 5990**Parameters** 5991 5992 | Name| Type | Mandatory| Description | 5993 | ------ | ------ | ---- | ------------------------ | 5994 | fd | number | Yes | File descriptor to duplicate.| 5995 5996**Return value** 5997 5998 | Type | Description | 5999 | ------ | -------------------- | 6000 | number | New file descriptor.| 6001 6002**Example** 6003 6004 ```ts 6005 import { fileIo } from '@kit.CoreFileKit'; 6006 6007 let filePath = "path/to/file"; 6008 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6009 rpc.MessageParcel.dupFileDescriptor(file.fd); 6010 ``` 6011 6012### containFileDescriptors<sup>8+</sup> 6013 6014containFileDescriptors(): boolean 6015 6016Checks whether this **MessageParcel** object contains file descriptors. 6017 6018**System capability**: SystemCapability.Communication.IPC.Core 6019 6020**Return value** 6021 6022 | Type | Description | 6023 | ------- | --------------------------------------------- | 6024 | boolean |Returns **true** if the **MessageParcel** object contains file descriptors; returns **false** otherwise.| 6025 6026**Example** 6027 6028 ```ts 6029 import { fileIo } from '@kit.CoreFileKit'; 6030 import { hilog } from '@kit.PerformanceAnalysisKit'; 6031 6032 let parcel = new rpc.MessageParcel(); 6033 let filePath = "path/to/file"; 6034 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6035 let writeResult = parcel.writeFileDescriptor(file.fd); 6036 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6037 let containFD = parcel.containFileDescriptors(); 6038 hilog.info(0x0000, 'testTag', 'RpcTest: parcel after write fd containFd result is ' + containFD); 6039 ``` 6040 6041### writeFileDescriptor<sup>8+</sup> 6042 6043writeFileDescriptor(fd: number): boolean 6044 6045Writes a file descriptor to this **MessageParcel** object. 6046 6047**System capability**: SystemCapability.Communication.IPC.Core 6048 6049**Parameters** 6050 6051 | Name| Type | Mandatory| Description | 6052 | ------ | ------ | ---- | ------------ | 6053 | fd | number | Yes | File descriptor to write.| 6054 6055**Return value** 6056 6057 | Type | Description | 6058 | ------- | -------------------------------- | 6059 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6060 6061**Example** 6062 6063 ```ts 6064 import { fileIo } from '@kit.CoreFileKit'; 6065 import { hilog } from '@kit.PerformanceAnalysisKit'; 6066 6067 let parcel = new rpc.MessageParcel(); 6068 let filePath = "path/to/file"; 6069 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6070 let writeResult = parcel.writeFileDescriptor(file.fd); 6071 hilog.info(0x0000, 'testTag', 'RpcTest: parcel writeFd result is ' + writeResult); 6072 ``` 6073 6074### readFileDescriptor<sup>8+</sup> 6075 6076readFileDescriptor(): number 6077 6078Reads the file descriptor from this **MessageParcel** object. 6079 6080**System capability**: SystemCapability.Communication.IPC.Core 6081 6082**Return value** 6083 6084 | Type | Description | 6085 | ------ | ---------------- | 6086 | number | File descriptor read.| 6087 6088**Example** 6089 6090 ```ts 6091 import { fileIo } from '@kit.CoreFileKit'; 6092 import { hilog } from '@kit.PerformanceAnalysisKit'; 6093 6094 let parcel = new rpc.MessageParcel(); 6095 let filePath = "path/to/file"; 6096 let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 6097 parcel.writeFileDescriptor(file.fd); 6098 let readFD = parcel.readFileDescriptor(); 6099 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read fd is ' + readFD); 6100 ``` 6101 6102### writeAshmem<sup>8+</sup> 6103 6104writeAshmem(ashmem: Ashmem): boolean 6105 6106Writes an anonymous shared object to this **MessageParcel** object. 6107 6108**System capability**: SystemCapability.Communication.IPC.Core 6109 6110**Parameters** 6111 6112| Name| Type | Mandatory| Description | 6113| ------ | ------ | ---- | ----------------------------------- | 6114| ashmem | [Ashmem](#ashmem8) | Yes | Anonymous shared object to write.| 6115 6116**Return value** 6117 6118 | Type | Description | 6119 | ------- | -------------------------------- | 6120 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6121 6122**Example** 6123 6124 ```ts 6125 import { hilog } from '@kit.PerformanceAnalysisKit'; 6126 6127 let parcel = new rpc.MessageParcel(); 6128 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6129 let isWriteSuccess = parcel.writeAshmem(ashmem); 6130 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6131 ``` 6132 6133### readAshmem<sup>8+</sup> 6134 6135readAshmem(): Ashmem 6136 6137Reads the anonymous shared object from this **MessageParcel** object. 6138 6139**System capability**: SystemCapability.Communication.IPC.Core 6140 6141**Return value** 6142 6143| Type | Description | 6144| ------ | ------------------ | 6145| [Ashmem](#ashmem8) | Anonymous share object obtained.| 6146 6147**Example** 6148 6149 ```ts 6150 import { hilog } from '@kit.PerformanceAnalysisKit'; 6151 6152 let parcel = new rpc.MessageParcel(); 6153 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024); 6154 let isWriteSuccess = parcel.writeAshmem(ashmem); 6155 hilog.info(0x0000, 'testTag', 'RpcTest: write ashmem to result is ' + isWriteSuccess); 6156 let readAshmem = parcel.readAshmem(); 6157 hilog.info(0x0000, 'testTag', 'RpcTest: read ashmem to result is ' + readAshmem); 6158 ``` 6159 6160### getRawDataCapacity<sup>8+</sup> 6161 6162getRawDataCapacity(): number 6163 6164Obtains the maximum amount of raw data that can be held by this **MessageParcel** object. 6165 6166**System capability**: SystemCapability.Communication.IPC.Core 6167 6168**Return value** 6169 6170 | Type | Description | 6171 | ------ | ---------------------------------------------------------- | 6172 | number | Maximum amount of raw data that **MessageParcel** can hold, that is, 128 MB.| 6173 6174**Example** 6175 6176 ```ts 6177 import { hilog } from '@kit.PerformanceAnalysisKit'; 6178 6179 let parcel = new rpc.MessageParcel(); 6180 let result = parcel.getRawDataCapacity(); 6181 hilog.info(0x0000, 'testTag', 'RpcTest: parcel get RawDataCapacity result is ' + result); 6182 ``` 6183 6184### writeRawData<sup>8+</sup> 6185 6186writeRawData(rawData: number[], size: number): boolean 6187 6188Writes raw data to this **MessageParcel** object. 6189 6190**System capability**: SystemCapability.Communication.IPC.Core 6191 6192**Parameters** 6193 6194 | Name | Type | Mandatory| Description | 6195 | ------- | -------- | ---- | ---------------------------------- | 6196 | rawData | number[] | Yes | Raw data to write. | 6197 | size | number | Yes | Size of the raw data, in bytes.| 6198 6199**Return value** 6200 6201 | Type | Description | 6202 | ------- | -------------------------------- | 6203 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 6204 6205**Example** 6206 6207 ```ts 6208 import { hilog } from '@kit.PerformanceAnalysisKit'; 6209 6210 let parcel = new rpc.MessageParcel(); 6211 let arr = [1, 2, 3, 4, 5]; 6212 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6213 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6214 ``` 6215 6216### readRawData<sup>8+</sup> 6217 6218readRawData(size: number): number[] 6219 6220Reads raw data from this **MessageParcel** object. 6221 6222**System capability**: SystemCapability.Communication.IPC.Core 6223 6224**Parameters** 6225 6226 | Name| Type | Mandatory| Description | 6227 | ------ | ------ | ---- | ------------------------ | 6228 | size | number | Yes | Size of the raw data to read.| 6229 6230**Return value** 6231 6232 | Type | Description | 6233 | -------- | ------------------------------ | 6234 | number[] | Raw data obtained, in bytes.| 6235 6236**Example** 6237 6238 ```ts 6239 import { hilog } from '@kit.PerformanceAnalysisKit'; 6240 6241 let parcel = new rpc.MessageParcel(); 6242 let arr = [1, 2, 3, 4, 5]; 6243 let isWriteSuccess = parcel.writeRawData(arr, arr.length); 6244 hilog.info(0x0000, 'testTag', 'RpcTest: parcel write raw data result is ' + isWriteSuccess); 6245 let result = parcel.readRawData(5); 6246 hilog.info(0x0000, 'testTag', 'RpcTest: parcel read raw data result is ' + result); 6247 ``` 6248 6249## Parcelable<sup>9+</sup> 6250 6251Writes an object to a **MessageSequence** and reads it from the **MessageSequence** during IPC. 6252 6253### marshalling 6254 6255marshalling(dataOut: MessageSequence): boolean 6256 6257Marshals this **Parcelable** object into a **MessageSequence** object. 6258 6259**System capability**: SystemCapability.Communication.IPC.Core 6260 6261**Parameters** 6262 6263| Name | Type | Mandatory| Description | 6264| ------- | --------------- | ---- | ------------------------------------------- | 6265| dataOut |[MessageSequence](#messagesequence9)| Yes | **MessageSequence** object to which the **Parcelable** object is to be marshaled.| 6266 6267**Return value** 6268 6269 | Type | Description | 6270 | ------- | -------------------------------- | 6271 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6272 6273**Example** 6274 6275 ```ts 6276 import { hilog } from '@kit.PerformanceAnalysisKit'; 6277 6278 class MyParcelable implements rpc.Parcelable { 6279 num: number = 0; 6280 str: string = ''; 6281 constructor(num: number, str: string) { 6282 this.num = num; 6283 this.str = str; 6284 } 6285 marshalling(messageSequence: rpc.MessageSequence): boolean { 6286 messageSequence.writeInt(this.num); 6287 messageSequence.writeString(this.str); 6288 return true; 6289 } 6290 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6291 this.num = messageSequence.readInt(); 6292 this.str = messageSequence.readString(); 6293 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6294 return true; 6295 } 6296 } 6297 let parcelable = new MyParcelable(1, "aaa"); 6298 let data = rpc.MessageSequence.create(); 6299 data.writeParcelable(parcelable); 6300 let ret = new MyParcelable(0, ""); 6301 data.readParcelable(ret); 6302 ``` 6303 6304### unmarshalling 6305 6306unmarshalling(dataIn: MessageSequence): boolean 6307 6308Unmarshals this **Parcelable** object from a **MessageSequence** object. 6309 6310**System capability**: SystemCapability.Communication.IPC.Core 6311 6312**Parameters** 6313 6314| Name| Type | Mandatory| Description | 6315| ------ | --------------- | ---- | ----------------------------------------------- | 6316| dataIn | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object from which the **Parcelable** object is to be unmarshaled.| 6317 6318**Return value** 6319 6320 | Type | Description | 6321 | ------- | ---------------------------------------- | 6322 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6323 6324**Example** 6325 6326 ```ts 6327 import { hilog } from '@kit.PerformanceAnalysisKit'; 6328 6329 class MyParcelable implements rpc.Parcelable { 6330 num: number = 0; 6331 str: string = ''; 6332 constructor(num: number, str: string) { 6333 this.num = num; 6334 this.str = str; 6335 } 6336 marshalling(messageSequence: rpc.MessageSequence): boolean { 6337 messageSequence.writeInt(this.num); 6338 messageSequence.writeString(this.str); 6339 return true; 6340 } 6341 unmarshalling(messageSequence: rpc.MessageSequence): boolean { 6342 this.num = messageSequence.readInt(); 6343 this.str = messageSequence.readString(); 6344 hilog.info(0x0000, 'testTag', 'RpcClient: readInt is ' + this.num + ' readString is ' + this.str); 6345 return true; 6346 } 6347 } 6348 let parcelable = new MyParcelable(1, "aaa"); 6349 let data = rpc.MessageSequence.create(); 6350 data.writeParcelable(parcelable); 6351 let ret = new MyParcelable(0, ""); 6352 data.readParcelable(ret); 6353 ``` 6354 6355## Sequenceable<sup>(deprecated)</sup> 6356 6357Writes objects of classes to a **MessageParcel** and reads them from the **MessageParcel** during IPC. 6358 6359> **NOTE**<br/> 6360> 6361> >**NOTE**<br>This API is deprecated since API version 9. Use [Parcelable](#parcelable9) instead. 6362 6363### marshalling 6364 6365marshalling(dataOut: MessageParcel): boolean 6366 6367Marshals the sequenceable object into a **MessageParcel** object. 6368 6369**System capability**: SystemCapability.Communication.IPC.Core 6370 6371**Parameters** 6372 6373 | Name | Type | Mandatory| Description | 6374 | ------- | ----------------------------------------- | ---- | ----------------------------------------- | 6375 | dataOut | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object to which the sequenceable object is to be marshaled.| 6376 6377**Return value** 6378 6379 | Type | Description | 6380 | ------- | -------------------------------- | 6381 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6382 6383**Example** 6384 6385 ```ts 6386 import { hilog } from '@kit.PerformanceAnalysisKit'; 6387 6388 class MySequenceable implements rpc.Sequenceable { 6389 num: number = 0; 6390 str: string = ''; 6391 constructor(num: number, str: string) { 6392 this.num = num; 6393 this.str = str; 6394 } 6395 marshalling(messageParcel: rpc.MessageParcel): boolean { 6396 messageParcel.writeInt(this.num); 6397 messageParcel.writeString(this.str); 6398 return true; 6399 } 6400 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6401 this.num = messageParcel.readInt(); 6402 this.str = messageParcel.readString(); 6403 return true; 6404 } 6405 } 6406 let sequenceable = new MySequenceable(1, "aaa"); 6407 let data = rpc.MessageParcel.create(); 6408 let result = data.writeSequenceable(sequenceable); 6409 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6410 let ret = new MySequenceable(0, ""); 6411 let result2 = data.readSequenceable(ret); 6412 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6413 ``` 6414 6415### unmarshalling 6416 6417unmarshalling(dataIn: MessageParcel): boolean 6418 6419Unmarshals this sequenceable object from a **MessageParcel** object. 6420 6421**System capability**: SystemCapability.Communication.IPC.Core 6422 6423**Parameters** 6424 6425 | Name| Type | Mandatory| Description | 6426 | ------ | ----------------------------------------- | ---- | --------------------------------------------- | 6427 | dataIn | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object in which the sequenceable object is to be unmarshaled.| 6428 6429**Return value** 6430 6431 | Type | Description | 6432 | ------- | ---------------------------------------- | 6433 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 6434 6435**Example** 6436 6437 ```ts 6438 import { hilog } from '@kit.PerformanceAnalysisKit'; 6439 6440 class MySequenceable implements rpc.Sequenceable { 6441 num: number = 0; 6442 str: string = ''; 6443 constructor(num: number, str: string) { 6444 this.num = num; 6445 this.str = str; 6446 } 6447 marshalling(messageParcel: rpc.MessageParcel): boolean { 6448 messageParcel.writeInt(this.num); 6449 messageParcel.writeString(this.str); 6450 return true; 6451 } 6452 unmarshalling(messageParcel: rpc.MessageParcel): boolean { 6453 this.num = messageParcel.readInt(); 6454 this.str = messageParcel.readString(); 6455 return true; 6456 } 6457 } 6458 let sequenceable = new MySequenceable(1, "aaa"); 6459 let data = rpc.MessageParcel.create(); 6460 let result = data.writeSequenceable(sequenceable); 6461 hilog.info(0x0000, 'testTag', 'RpcClient: writeSequenceable is ' + result); 6462 let ret = new MySequenceable(0, ""); 6463 let result2 = data.readSequenceable(ret); 6464 hilog.info(0x0000, 'testTag', 'RpcClient: readSequenceable is ' + result2); 6465 ``` 6466 6467## IRemoteBroker 6468 6469Represents the holder of a remote proxy object. It is used to obtain a proxy object. 6470 6471### asObject 6472 6473asObject(): IRemoteObject 6474 6475Obtains a proxy or remote object. This API must be implemented by its derived classes. 6476 6477**System capability**: SystemCapability.Communication.IPC.Core 6478 6479**Return value** 6480 6481 | Type | Description | 6482 | ----- | ----- | 6483 | [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.| 6484 6485**Example** 6486 6487 ```ts 6488 class TestAbility extends rpc.RemoteObject { 6489 asObject() { 6490 return this; 6491 } 6492 } 6493 let remoteObject = new TestAbility("testObject").asObject(); 6494 ``` 6495 6496**Example** 6497 6498 ```ts 6499 // If the FA model is used, import featureAbility from @kit.AbilityKit. 6500 // import { featureAbility } from '@kit.AbilityKit'; 6501 import { Want, common } from '@kit.AbilityKit'; 6502 import { hilog } from '@kit.PerformanceAnalysisKit'; 6503 6504 let proxy: rpc.IRemoteObject | undefined; 6505 let connect: common.ConnectOptions = { 6506 onConnect: (elementName, remoteProxy) => { 6507 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6508 proxy = remoteProxy; 6509 }, 6510 onDisconnect: (elementName) => { 6511 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 6512 }, 6513 onFailed: () => { 6514 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 6515 } 6516 }; 6517 let want: Want = { 6518 bundleName: "com.ohos.server", 6519 abilityName: "com.ohos.server.EntryAbility", 6520 }; 6521 6522 // Use this method to connect to the ability for the FA model. 6523 // FA.connectAbility(want,connect); 6524 6525 // Save the connection ID, which will be used for the subsequent service disconnection. 6526 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 6527 // Save the connection ID, which will be used for the subsequent service disconnection. 6528 let connectionId = context.connectServiceExtensionAbility(want, connect); 6529 ``` 6530 6531 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **asObject()** of the proxy object is called to obtain the proxy or remote object. 6532 6533 ```ts 6534 class TestProxy { 6535 remote: rpc.IRemoteObject; 6536 constructor(remote: rpc.IRemoteObject) { 6537 this.remote = remote; 6538 } 6539 asObject() { 6540 return this.remote; 6541 } 6542 } 6543 if (proxy != undefined) { 6544 let iRemoteObject = new TestProxy(proxy).asObject(); 6545 } 6546 ``` 6547 6548## DeathRecipient 6549 6550Subscribes 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. 6551 6552### onRemoteDied 6553 6554onRemoteDied(): void 6555 6556Called to perform subsequent operations when a death notification of the remote object is received. 6557 6558**System capability**: SystemCapability.Communication.IPC.Core 6559 6560**Example** 6561 6562 ```ts 6563 import { hilog } from '@kit.PerformanceAnalysisKit'; 6564 6565 class MyDeathRecipient implements rpc.DeathRecipient { 6566 onRemoteDied() { 6567 hilog.info(0x0000, 'testTag', 'server died'); 6568 } 6569 } 6570 ``` 6571 6572## RequestResult<sup>9+</sup> 6573 6574Defines the response to the request. 6575 6576**System capability**: SystemCapability.Communication.IPC.Core 6577 6578| Name | Type | Readable| Writable| Description | 6579| ------- | --------------- | ---- | ---- |-------------------------------------- | 6580| errCode | number | Yes | No | Error code. | 6581| code | number | Yes | No | Message code. | 6582| data | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object sent to the remote process.| 6583| reply | [MessageSequence](#messagesequence9) | Yes | No | **MessageSequence** object returned by the remote process. | 6584 6585## SendRequestResult<sup>(deprecated)</sup> 6586 6587Defines the response to the request. 6588 6589> **NOTE**<br/> 6590> 6591> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [RequestResult](#requestresult9) instead. 6592 6593**System capability**: SystemCapability.Communication.IPC.Core 6594 6595 | Name | Type | Readable| Writable| Description | 6596 | ------- | ------------- | ---- | ---- | ----------------------------------- | 6597 | errCode | number | Yes | No | Error code. | 6598 | code | number | Yes | No | Message code. | 6599 | data | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object sent to the remote process.| 6600 | reply | [MessageParcel](#messageparceldeprecated) | Yes | No | **MessageParcel** object returned by the remote process. | 6601 6602## IRemoteObject 6603 6604Provides methods to query of obtain interface descriptors, add or delete death notifications, dump object status to specific files, and send messages. 6605 6606### getLocalInterface<sup>9+</sup> 6607 6608getLocalInterface(descriptor: string): IRemoteBroker 6609 6610Obtains the string of the interface descriptor. 6611 6612**System capability**: SystemCapability.Communication.IPC.Core 6613 6614**Parameters** 6615 6616 | Name | Type | Mandatory| Description | 6617 | ---------- | ------ | ---- | -------------------- | 6618 | descriptor | string | Yes | Interface descriptor.| 6619 6620**Return value** 6621 6622| Type | Description | 6623| ------------- | --------------------------------------------- | 6624| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6625 6626**Error codes** 6627 6628For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6629 6630 | ID| Error Message| 6631 | -------- | -------- | 6632 | 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. | 6633 6634### queryLocalInterface<sup>(deprecated)</sup> 6635 6636queryLocalInterface(descriptor: string): IRemoteBroker 6637 6638Queries the string of the interface descriptor. 6639 6640> **NOTE**<br/> 6641> 6642> >**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9) instead. 6643 6644**System capability**: SystemCapability.Communication.IPC.Core 6645 6646**Parameters** 6647 6648 | Name | Type | Mandatory| Description | 6649 | ---------- | ------ | ---- | -------------------- | 6650 | descriptor | string | Yes | Interface descriptor.| 6651 6652**Return value** 6653 6654| Type | Description | 6655| ------------- | --------------------------------------------- | 6656| [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 6657 6658### sendRequest<sup>(deprecated)</sup> 6659 6660sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6661 6662Sends 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. 6663 6664> **NOTE**<br/> 6665> 6666> This API is deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9) instead. 6667 6668**System capability**: SystemCapability.Communication.IPC.Core 6669 6670**Parameters** 6671 6672 | Name | Type | Mandatory| Description | 6673 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6674 | 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.| 6675 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6676 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6677 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6678 6679**Return value** 6680 6681 | Type | Description | 6682 | ------- | -------------------------------- | 6683 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6684 6685### sendMessageRequest<sup>9+</sup> 6686 6687sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 6688 6689Sends 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. 6690 6691**System capability**: SystemCapability.Communication.IPC.Core 6692 6693**Parameters** 6694 6695 | Name | Type | Mandatory| Description | 6696 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6697 | 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.| 6698 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6699 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6700 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6701 6702**Return value** 6703 6704 | Type | Description | 6705 | ---------------------------- | ----------------------------------------- | 6706 | Promise<[RequestResult](#requestresult9)> | Promise used to return a **requestResult** instance.| 6707 6708**Error codes** 6709 6710For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6711 6712 | ID| Error Message| 6713 | -------- | -------- | 6714 | 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. | 6715 6716### sendRequest<sup>(deprecated)</sup> 6717 6718sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 6719 6720Sends 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. 6721 6722> **NOTE**<br/> 6723> 6724> This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9) instead. 6725 6726**System capability**: SystemCapability.Communication.IPC.Core 6727 6728**Parameters** 6729 6730 | Name | Type | Mandatory| Description | 6731 | ------- | ---------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6732 | 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.| 6733 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6734 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6735 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6736 6737**Return value** 6738 6739| Type | Description | 6740| ------------------------------------------------------------ | --------------------------------------------- | 6741| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 6742 6743### sendMessageRequest<sup>9+</sup> 6744 6745sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 6746 6747Sends 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. 6748 6749**System capability**: SystemCapability.Communication.IPC.Core 6750 6751**Parameters** 6752 6753 | Name | Type | Mandatory| Description | 6754 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 6755 | 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.| 6756 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 6757 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 6758 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6759 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 6760 6761**Error codes** 6762 6763For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6764 6765 | ID| Error Message| 6766 | -------- | -------- | 6767 | 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. | 6768 6769### sendRequest<sup>(deprecated)</sup> 6770 6771sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 6772 6773Sends 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. 6774 6775> **NOTE**<br/> 6776> 6777> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-1) instead. 6778 6779**System capability**: SystemCapability.Communication.IPC.Core 6780 6781**Parameters** 6782 6783| Name | Type | Mandatory| Description | 6784| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6785| 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.| 6786| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6787| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6788| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6789| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 6790 6791### registerDeathRecipient<sup>9+</sup> 6792 6793registerDeathRecipient(recipient: DeathRecipient, flags: number): void 6794 6795Registers 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. 6796 6797**System capability**: SystemCapability.Communication.IPC.Core 6798 6799**Parameters** 6800 6801 | Name | Type | Mandatory| Description | 6802 | --------- | --------------------------------- | ---- | -------------- | 6803 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6804 | flags | number | Yes | Flag of the death notification.| 6805 6806**Error codes** 6807 6808For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6809 6810 | ID| Error Message| 6811 | -------- | -------- | 6812 | 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. | 6813 | 1900008 | The proxy or remote object is invalid. | 6814 6815### addDeathRecipient<sup>(deprecated)</sup> 6816 6817addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6818 6819Adds 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. 6820 6821> **NOTE**<br/> 6822> 6823> >**NOTE**<br>This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9) instead. 6824 6825**System capability**: SystemCapability.Communication.IPC.Core 6826 6827**Parameters** 6828 6829 | Name | Type | Mandatory| Description | 6830 | --------- | --------------------------------- | ---- | -------------- | 6831 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 6832 | flags | number | Yes | Flag of the death notification.| 6833 6834**Return value** 6835 6836 | Type | Description | 6837 | ------- | ---------------------------------------- | 6838 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 6839 6840### unregisterDeathRecipient<sup>9+</sup> 6841 6842unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 6843 6844Unregisters the callback used to receive death notifications of the remote object. 6845 6846**System capability**: SystemCapability.Communication.IPC.Core 6847 6848**Parameters** 6849 6850 | Name | Type | Mandatory| Description | 6851 | --------- | --------------------------------- | ---- | -------------- | 6852 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6853 | flags | number | Yes | Flag of the death notification.| 6854 6855**Error codes** 6856 6857For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6858 6859 | ID| Error Message| 6860 | -------- | -------- | 6861 | 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. | 6862 | 1900008 | The proxy or remote object is invalid. | 6863 6864### removeDeathRecipient<sup>(deprecated)</sup> 6865 6866removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 6867 6868Removes the callback used to receive death notifications of the remote object. 6869 6870> **NOTE**<br/> 6871> 6872> This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9) instead. 6873 6874**System capability**: SystemCapability.Communication.IPC.Core 6875 6876**Parameters** 6877 6878 | Name | Type | Mandatory| Description | 6879 | --------- | --------------------------------- | ---- | -------------- | 6880 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 6881 | flags | number | Yes | Flag of the death notification.| 6882 6883**Return value** 6884 6885 | Type | Description | 6886 | ------- | -----------------------------------------| 6887 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 6888 6889### getDescriptor<sup>9+</sup> 6890 6891getDescriptor(): string 6892 6893Obtains the interface descriptor (which is a string) of this object. 6894 6895**System capability**: SystemCapability.Communication.IPC.Core 6896 6897**Return value** 6898 6899 | Type | Description | 6900 | ------ | ---------------- | 6901 | string | Interface descriptor obtained.| 6902 6903**Error codes** 6904 6905For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 6906 6907 | ID| Error Message| 6908 | -------- | -------- | 6909 | 1900008 | The proxy or remote object is invalid. | 6910 6911### getInterfaceDescriptor<sup>(deprecated)</sup> 6912 6913getInterfaceDescriptor(): string 6914 6915Obtains the interface descriptor (which is a string) of this object. 6916 6917> **NOTE**<br/> 6918> 6919> >**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9) instead. 6920 6921**System capability**: SystemCapability.Communication.IPC.Core 6922 6923**Return value** 6924 6925 | Type | Description | 6926 | ------ | ---------------- | 6927 | string | Interface descriptor obtained.| 6928 6929### isObjectDead 6930 6931isObjectDead(): boolean 6932 6933Checks whether this object is dead. 6934 6935**System capability**: SystemCapability.Communication.IPC.Core 6936 6937**Return value** 6938 6939 | Type | Description | 6940 | ------- | ---------------------------------- | 6941 | boolean | Returns **true** if the object is dead; returns **false** otherwise.| 6942 6943## RemoteProxy 6944 6945Provides APIs to implement **IRemoteObject**. 6946 6947### Properties 6948 6949**System capability**: SystemCapability.Communication.IPC.Core 6950 6951 | Name | Type | Readable | Writable| Description | 6952 | --------------------- | -------| ------|------|------------------------------------------ | 6953 | PING_TRANSACTION | number | Yes | No | Internal instruction code used to test whether the IPC service is normal. | 6954 | DUMP_TRANSACTION | number | Yes | No | Internal instruction code used to obtain IPC service status information. | 6955 | INTERFACE_TRANSACTION | number | Yes | No | Internal instruction code used to obtain the remote interface token. | 6956 | MIN_TRANSACTION_ID | number | Yes | No | Minimum valid instruction code. | 6957 | MAX_TRANSACTION_ID | number | Yes | No | Maximum valid instruction code. | 6958 6959 6960### sendRequest<sup>(deprecated)</sup> 6961 6962sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 6963 6964Sends 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. 6965 6966> **NOTE**<br/> 6967> 6968> >**NOTE**<br>This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 6969 6970**System capability**: SystemCapability.Communication.IPC.Core 6971 6972**Parameters** 6973 6974 | Name | Type | Mandatory| Description | 6975 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 6976 | 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.| 6977 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 6978 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 6979 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 6980 6981**Return value** 6982 6983 | Type | Description | 6984 | ------- | ---------------------------------| 6985 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 6986 6987**Example** 6988 6989 ```ts 6990 // If the FA model is used, import featureAbility from @kit.AbilityKit. 6991 // import { featureAbility } from '@kit.AbilityKit'; 6992 import { Want, common } from '@kit.AbilityKit'; 6993 import { hilog } from '@kit.PerformanceAnalysisKit'; 6994 6995 let proxy: rpc.IRemoteObject | undefined; 6996 let connect: common.ConnectOptions = { 6997 onConnect: (elementName, remoteProxy) => { 6998 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 6999 proxy = remoteProxy; 7000 }, 7001 onDisconnect: (elementName) => { 7002 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7003 }, 7004 onFailed: () => { 7005 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7006 } 7007 }; 7008 let want: Want = { 7009 bundleName: "com.ohos.server", 7010 abilityName: "com.ohos.server.EntryAbility", 7011 }; 7012 7013 // Use this method to connect to the ability for the FA model. 7014 // FA.connectAbility(want,connect); 7015 7016 // Save the connection ID, which will be used for the subsequent service disconnection. 7017 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7018 // Save the connection ID, which will be used for the subsequent service disconnection. 7019 let connectionId = context.connectServiceExtensionAbility(want, connect); 7020 ``` 7021 7022 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7023 7024 ```ts 7025 import { hilog } from '@kit.PerformanceAnalysisKit'; 7026 7027 let option = new rpc.MessageOption(); 7028 let data = rpc.MessageParcel.create(); 7029 let reply = rpc.MessageParcel.create(); 7030 data.writeInt(1); 7031 data.writeString("hello"); 7032 if (proxy != undefined) { 7033 let ret: boolean = proxy.sendRequest(1, data, reply, option); 7034 if (ret) { 7035 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7036 let msg = reply.readString(); 7037 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7038 } else { 7039 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 7040 } 7041 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7042 data.reclaim(); 7043 reply.reclaim(); 7044 } 7045 ``` 7046 7047### sendMessageRequest<sup>9+</sup> 7048 7049sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 7050 7051Sends 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. 7052 7053**System capability**: SystemCapability.Communication.IPC.Core 7054 7055**Parameters** 7056 7057 | Name | Type | Mandatory| Description | 7058 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7059 | 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.| 7060 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7061 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7062 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7063 7064**Return value** 7065 7066 | Type | Description | 7067 | ---------------------------- | ----------------------------------------- | 7068 | Promise<[RequestResult](#requestresult9)> | Promise used to return a **requestResult** instance.| 7069 7070 7071**Error codes** 7072 7073For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7074 7075 | ID| Error Message| 7076 | -------- | -------- | 7077 | 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. | 7078 7079**Example** 7080 7081 ```ts 7082 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7083 // import { featureAbility } from '@kit.AbilityKit'; 7084 import { Want, common } from '@kit.AbilityKit'; 7085 import { hilog } from '@kit.PerformanceAnalysisKit'; 7086 7087 let proxy: rpc.IRemoteObject | undefined; 7088 let connect: common.ConnectOptions = { 7089 onConnect: (elementName, remoteProxy) => { 7090 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7091 proxy = remoteProxy; 7092 }, 7093 onDisconnect: (elementName) => { 7094 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7095 }, 7096 onFailed: () => { 7097 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7098 } 7099 }; 7100 let want: Want = { 7101 bundleName: "com.ohos.server", 7102 abilityName: "com.ohos.server.EntryAbility", 7103 }; 7104 7105 // Use this method to connect to the ability for the FA model. 7106 // FA.connectAbility(want,connect); 7107 7108 // Save the connection ID, which will be used for the subsequent service disconnection. 7109 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7110 // Save the connection ID, which will be used for the subsequent service disconnection. 7111 let connectionId = context.connectServiceExtensionAbility(want, connect); 7112 ``` 7113 7114 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 7115 7116 ```ts 7117 import { hilog } from '@kit.PerformanceAnalysisKit'; 7118 7119 let option = new rpc.MessageOption(); 7120 let data = rpc.MessageSequence.create(); 7121 let reply = rpc.MessageSequence.create(); 7122 data.writeInt(1); 7123 data.writeString("hello"); 7124 if (proxy != undefined) { 7125 proxy.sendMessageRequest(1, data, reply, option) 7126 .then((result: rpc.RequestResult) => { 7127 if (result.errCode === 0) { 7128 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7129 let num = result.reply.readInt(); 7130 let msg = result.reply.readString(); 7131 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7132 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7133 } else { 7134 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7135 } 7136 }).catch((e: Error) => { 7137 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 7138 }).finally (() => { 7139 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7140 data.reclaim(); 7141 reply.reclaim(); 7142 }); 7143 } 7144 ``` 7145 7146### sendRequest<sup>(deprecated)</sup> 7147 7148sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 7149 7150Sends 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. 7151 7152> **NOTE**<br/> 7153> 7154> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-2) instead. 7155 7156**System capability**: SystemCapability.Communication.IPC.Core 7157 7158**Parameters** 7159 7160 | Name | Type | Mandatory| Description | 7161 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 7162 | 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.| 7163 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7164 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7165 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7166 7167**Return value** 7168 7169| Type | Description | 7170| ------------------------------------------------------------ | --------------------------------------------- | 7171| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 7172 7173**Example** 7174 7175 ```ts 7176 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7177 // import { featureAbility } from '@kit.AbilityKit'; 7178 import { Want, common } from '@kit.AbilityKit'; 7179 import { hilog } from '@kit.PerformanceAnalysisKit'; 7180 7181 let proxy: rpc.IRemoteObject | undefined; 7182 let connect: common.ConnectOptions = { 7183 onConnect: (elementName, remoteProxy) => { 7184 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7185 proxy = remoteProxy; 7186 }, 7187 onDisconnect: (elementName) => { 7188 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7189 }, 7190 onFailed: () => { 7191 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7192 } 7193 }; 7194 let want: Want = { 7195 bundleName: "com.ohos.server", 7196 abilityName: "com.ohos.server.EntryAbility", 7197 }; 7198 7199 // Use this method to connect to the ability for the FA model. 7200 // FA.connectAbility(want,connect); 7201 7202 // Save the connection ID, which will be used for the subsequent service disconnection. 7203 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7204 // Save the connection ID, which will be used for the subsequent service disconnection. 7205 let connectionId = context.connectServiceExtensionAbility(want, connect); 7206 ``` 7207 7208 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7209 7210 ```ts 7211 import { hilog } from '@kit.PerformanceAnalysisKit'; 7212 7213 let option = new rpc.MessageOption(); 7214 let data = rpc.MessageParcel.create(); 7215 let reply = rpc.MessageParcel.create(); 7216 data.writeInt(1); 7217 data.writeString("hello"); 7218 if (proxy != undefined) { 7219 let a = proxy.sendRequest(1, data, reply, option) as Object; 7220 let b = a as Promise<rpc.SendRequestResult>; 7221 b.then((result: rpc.SendRequestResult) => { 7222 if (result.errCode === 0) { 7223 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7224 let num = result.reply.readInt(); 7225 let msg = result.reply.readString(); 7226 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7227 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7228 } else { 7229 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7230 } 7231 }).catch((e: Error) => { 7232 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 7233 }).finally (() => { 7234 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7235 data.reclaim(); 7236 reply.reclaim(); 7237 }); 7238 } 7239 ``` 7240 7241### sendMessageRequest<sup>9+</sup> 7242 7243sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 7244 7245Sends 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. 7246 7247**System capability**: SystemCapability.Communication.IPC.Core 7248 7249**Parameters** 7250 7251 | Name | Type | Mandatory| Description | 7252 | -------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 7253 | 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.| 7254 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 7255 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 7256 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7257 | callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 7258 7259 7260**Error codes** 7261 7262For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7263 7264 | ID| Error Message| 7265 | -------- | -------- | 7266 | 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. | 7267 7268**Example** 7269 7270 ```ts 7271 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7272 // import { featureAbility } from '@kit.AbilityKit'; 7273 import { Want, common } from '@kit.AbilityKit'; 7274 import { hilog } from '@kit.PerformanceAnalysisKit'; 7275 import { BusinessError } from '@kit.BasicServicesKit'; 7276 7277 let proxy: rpc.IRemoteObject | undefined; 7278 let connect: common.ConnectOptions = { 7279 onConnect: (elementName, remoteProxy) => { 7280 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7281 proxy = remoteProxy; 7282 }, 7283 onDisconnect: (elementName) => { 7284 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7285 }, 7286 onFailed: () => { 7287 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7288 } 7289 }; 7290 let want: Want = { 7291 bundleName: "com.ohos.server", 7292 abilityName: "com.ohos.server.EntryAbility", 7293 }; 7294 function sendMessageRequestCallback(err: BusinessError, result: rpc.RequestResult) { 7295 if (result.errCode === 0) { 7296 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 7297 let num = result.reply.readInt(); 7298 let msg = result.reply.readString(); 7299 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7300 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7301 } else { 7302 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 7303 } 7304 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 7305 result.data.reclaim(); 7306 result.reply.reclaim(); 7307} 7308 7309 // Use this method to connect to the ability for the FA model. 7310 // FA.connectAbility(want,connect); 7311 7312 // Save the connection ID, which will be used for the subsequent service disconnection. 7313 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7314 // Save the connection ID, which will be used for the subsequent service disconnection. 7315 let connectionId = context.connectServiceExtensionAbility(want, connect); 7316 ``` 7317 7318 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendMessageRequest()** of the proxy object is called to send a message. 7319 7320 ```ts 7321 import { hilog } from '@kit.PerformanceAnalysisKit'; 7322 import { BusinessError } from '@kit.BasicServicesKit'; 7323 7324 let option = new rpc.MessageOption(); 7325 let data = rpc.MessageSequence.create(); 7326 let reply = rpc.MessageSequence.create(); 7327 data.writeInt(1); 7328 data.writeString("hello"); 7329 if (proxy != undefined) { 7330 try { 7331 proxy.sendMessageRequest(1, data, reply, option, sendMessageRequestCallback); 7332 } catch (error) { 7333 let e: BusinessError = error as BusinessError; 7334 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorCode ' + e.code); 7335 hilog.error(0x0000, 'testTag', 'rpc sendMessageRequest fail, errorMessage ' + e.message); 7336 } 7337 } 7338 ``` 7339 7340### sendRequest<sup>(deprecated)</sup> 7341 7342sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 7343 7344Sends 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. 7345 7346> **NOTE**<br/> 7347> 7348> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-3) instead. 7349 7350**System capability**: SystemCapability.Communication.IPC.Core 7351 7352**Parameters** 7353 7354| Name | Type | Mandatory| Description | 7355| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7356| 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.| 7357| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 7358| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 7359| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 7360| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 7361 7362**Example** 7363 7364 ```ts 7365 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7366 // import { featureAbility } from '@kit.AbilityKit'; 7367 import { Want, common } from '@kit.AbilityKit'; 7368 import { hilog } from '@kit.PerformanceAnalysisKit'; 7369 import { BusinessError } from '@kit.BasicServicesKit'; 7370 7371 let proxy: rpc.IRemoteObject | undefined; 7372 let connect: common.ConnectOptions = { 7373 onConnect: (elementName, remoteProxy) => { 7374 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7375 proxy = remoteProxy; 7376 }, 7377 onDisconnect: (elementName) => { 7378 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7379 }, 7380 onFailed: () => { 7381 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7382 } 7383 }; 7384 let want: Want = { 7385 bundleName: "com.ohos.server", 7386 abilityName: "com.ohos.server.EntryAbility", 7387 }; 7388 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 7389 if (result.errCode === 0) { 7390 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 7391 let num = result.reply.readInt(); 7392 let msg = result.reply.readString(); 7393 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 7394 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 7395 } else { 7396 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 7397 } 7398 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 7399 result.data.reclaim(); 7400 result.reply.reclaim(); 7401} 7402 7403 // Use this method to connect to the ability for the FA model. 7404 // FA.connectAbility(want,connect); 7405 7406 // Save the connection ID, which will be used for the subsequent service disconnection. 7407 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7408 // Save the connection ID, which will be used for the subsequent service disconnection. 7409 let connectionId = context.connectServiceExtensionAbility(want, connect); 7410 ``` 7411 7412 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **sendRequest()** of the proxy object is called to send a message. 7413 7414 ```ts 7415 let option = new rpc.MessageOption(); 7416 let data = rpc.MessageParcel.create(); 7417 let reply = rpc.MessageParcel.create(); 7418 data.writeInt(1); 7419 data.writeString("hello"); 7420 if (proxy != undefined) { 7421 proxy.sendRequest(1, data, reply, option, sendRequestCallback); 7422 } 7423 ``` 7424 7425### getLocalInterface<sup>9+</sup> 7426 7427getLocalInterface(interface: string): IRemoteBroker 7428 7429Obtains the **LocalInterface** object of an interface token. 7430 7431**System capability**: SystemCapability.Communication.IPC.Core 7432 7433**Parameters** 7434 7435 | Name | Type | Mandatory| Description | 7436 | --------- | ------ | ---- | ---------------------- | 7437 | interface | string | Yes | Interface descriptor.| 7438 7439**Return value** 7440 7441| Type | Description | 7442| ------------------------------- | ------------------------------------------ | 7443| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7444 7445**Error codes** 7446 7447For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7448 7449 | ID| Error Message| 7450 | -------- | -------- | 7451 | 401 | check param failed | 7452 | 1900006 | Operation allowed only for the remote object. | 7453 7454**Example** 7455 7456 ```ts 7457 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7458 // import { featureAbility } from '@kit.AbilityKit'; 7459 import { Want, common } from '@kit.AbilityKit'; 7460 import { hilog } from '@kit.PerformanceAnalysisKit'; 7461 7462 let proxy: rpc.IRemoteObject | undefined; 7463 let connect: common.ConnectOptions = { 7464 onConnect: (elementName, remoteProxy) => { 7465 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7466 proxy = remoteProxy; 7467 }, 7468 onDisconnect: (elementName) => { 7469 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7470 }, 7471 onFailed: () => { 7472 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7473 } 7474 }; 7475 let want: Want = { 7476 bundleName: "com.ohos.server", 7477 abilityName: "com.ohos.server.EntryAbility", 7478 }; 7479 7480 // Use this method to connect to the ability for the FA model. 7481 // FA.connectAbility(want,connect); 7482 7483 // Save the connection ID, which will be used for the subsequent service disconnection. 7484 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7485 // Save the connection ID, which will be used for the subsequent service disconnection. 7486 let connectionId = context.connectServiceExtensionAbility(want, connect); 7487 ``` 7488 7489 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getLocalInterface()** of the proxy object is called to obtain the interface descriptor. 7490 7491 ```ts 7492 import { hilog } from '@kit.PerformanceAnalysisKit'; 7493 import { BusinessError } from '@kit.BasicServicesKit'; 7494 7495 if (proxy != undefined) { 7496 try { 7497 let broker: rpc.IRemoteBroker = proxy.getLocalInterface("testObject"); 7498 hilog.info(0x0000, 'testTag', 'RpcClient: getLocalInterface is ' + broker); 7499 } catch (error) { 7500 let e: BusinessError = error as BusinessError; 7501 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 7502 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 7503 } 7504 } 7505 ``` 7506 7507### queryLocalInterface<sup>(deprecated)</sup> 7508 7509queryLocalInterface(interface: string): IRemoteBroker 7510 7511Obtains the **LocalInterface** object of an interface token. 7512 7513> **NOTE**<br/> 7514> 7515> >**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-1) instead. 7516 7517**System capability**: SystemCapability.Communication.IPC.Core 7518 7519**Parameters** 7520 7521 | Name | Type | Mandatory| Description | 7522 | --------- | ------ | ---- | ---------------------- | 7523 | interface | string | Yes | Interface descriptor.| 7524 7525**Return value** 7526 7527| Type | Description | 7528| ------------------------------- | ------------------------------------------ | 7529| [IRemoteBroker](#iremotebroker) | Returns **Null** by default, which indicates a proxy interface.| 7530 7531**Example** 7532 7533 ```ts 7534 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7535 // import { featureAbility } from '@kit.AbilityKit'; 7536 import { Want, common } from '@kit.AbilityKit'; 7537 import { hilog } from '@kit.PerformanceAnalysisKit'; 7538 7539 let proxy: rpc.IRemoteObject | undefined; 7540 let connect: common.ConnectOptions = { 7541 onConnect: (elementName, remoteProxy) => { 7542 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7543 proxy = remoteProxy; 7544 }, 7545 onDisconnect: (elementName) => { 7546 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7547 }, 7548 onFailed: () => { 7549 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7550 } 7551 }; 7552 let want: Want = { 7553 bundleName: "com.ohos.server", 7554 abilityName: "com.ohos.server.EntryAbility", 7555 }; 7556 7557 // Use this method to connect to the ability for the FA model. 7558 // FA.connectAbility(want,connect); 7559 7560 // Save the connection ID, which will be used for the subsequent service disconnection. 7561 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7562 // Save the connection ID, which will be used for the subsequent service disconnection. 7563 let connectionId = context.connectServiceExtensionAbility(want, connect); 7564 ``` 7565 7566 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **queryLocalInterface()** of the proxy object is called to obtain the interface descriptor. 7567 7568 ```ts 7569 import { hilog } from '@kit.PerformanceAnalysisKit'; 7570 7571 if (proxy != undefined) { 7572 let broker: rpc.IRemoteBroker = proxy.queryLocalInterface("testObject"); 7573 hilog.info(0x0000, 'testTag', 'RpcClient: queryLocalInterface is ' + broker); 7574 } 7575 ``` 7576 7577### registerDeathRecipient<sup>9+</sup> 7578 7579registerDeathRecipient(recipient: DeathRecipient, flags: number): void 7580 7581Registers 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. 7582 7583**System capability**: SystemCapability.Communication.IPC.Core 7584 7585**Parameters** 7586 7587 | Name | Type | Mandatory| Description | 7588 | --------- | --------------------------------- | ---- | -------------- | 7589 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to register.| 7590 | flags | number | Yes | Flag of the death notification.| 7591 7592**Error codes** 7593 7594For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7595 7596 | ID| Error Message| 7597 | -------- | -------- | 7598 | 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. | 7599 | 1900008 | The proxy or remote object is invalid. | 7600 7601**Example** 7602 7603 ```ts 7604 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7605 // import { featureAbility } from '@kit.AbilityKit'; 7606 import { Want, common } from '@kit.AbilityKit'; 7607 import { hilog } from '@kit.PerformanceAnalysisKit'; 7608 7609 let proxy: rpc.IRemoteObject | undefined; 7610 let connect: common.ConnectOptions = { 7611 onConnect: (elementName, remoteProxy) => { 7612 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7613 proxy = remoteProxy; 7614 }, 7615 onDisconnect: (elementName) => { 7616 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7617 }, 7618 onFailed: () => { 7619 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7620 } 7621 }; 7622 let want: Want = { 7623 bundleName: "com.ohos.server", 7624 abilityName: "com.ohos.server.EntryAbility", 7625 }; 7626 7627 // Use this method to connect to the ability for the FA model. 7628 // FA.connectAbility(want,connect); 7629 7630 // Save the connection ID, which will be used for the subsequent service disconnection. 7631 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7632 // Save the connection ID, which will be used for the subsequent service disconnection. 7633 let connectionId = context.connectServiceExtensionAbility(want, connect); 7634 ``` 7635 7636 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **registerDeathRecipient()** of the proxy object is called to register a callback for receiving the death notification of the remote object. 7637 7638 ```ts 7639 import { hilog } from '@kit.PerformanceAnalysisKit'; 7640 import { BusinessError } from '@kit.BasicServicesKit'; 7641 7642 class MyDeathRecipient implements rpc.DeathRecipient { 7643 onRemoteDied() { 7644 hilog.info(0x0000, 'testTag', 'server died'); 7645 } 7646 } 7647 let deathRecipient = new MyDeathRecipient(); 7648 if (proxy != undefined) { 7649 try { 7650 proxy.registerDeathRecipient(deathRecipient, 0); 7651 } catch (error) { 7652 let e: BusinessError = error as BusinessError; 7653 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorCode ' + e.code); 7654 hilog.error(0x0000, 'testTag', 'proxy register deathRecipient fail, errorMessage ' + e.message); 7655 } 7656 } 7657 ``` 7658 7659### addDeathRecipient<sup>(deprecated)</sup> 7660 7661addDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7662 7663Adds a callback for receiving the death notifications of the remote object, including the death notifications of the remote proxy. 7664 7665> **NOTE**<br/> 7666> 7667> >**NOTE**<br>This API is deprecated since API version 9. Use [registerDeathRecipient](#registerdeathrecipient9-1) instead. 7668 7669**System capability**: SystemCapability.Communication.IPC.Core 7670 7671**Parameters** 7672 7673 | Name | Type | Mandatory| Description | 7674 | --------- | --------------------------------- | ---- | --------------------------------- | 7675 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to add. | 7676 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7677 7678**Return value** 7679 7680 | Type | Description | 7681 | ------- | ---------------------------------------- | 7682 | boolean | Returns **true** if the callback is added successfully; returns **false** otherwise.| 7683 7684**Example** 7685 7686 ```ts 7687 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7688 // import { featureAbility } from '@kit.AbilityKit'; 7689 import { Want, common } from '@kit.AbilityKit'; 7690 import { hilog } from '@kit.PerformanceAnalysisKit'; 7691 7692 let proxy: rpc.IRemoteObject | undefined; 7693 let connect: common.ConnectOptions = { 7694 onConnect: (elementName, remoteProxy) => { 7695 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7696 proxy = remoteProxy; 7697 }, 7698 onDisconnect: (elementName) => { 7699 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7700 }, 7701 onFailed: () => { 7702 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7703 } 7704 }; 7705 let want: Want = { 7706 bundleName: "com.ohos.server", 7707 abilityName: "com.ohos.server.EntryAbility", 7708 }; 7709 7710 // Use this method to connect to the ability for the FA model. 7711 // FA.connectAbility(want,connect); 7712 7713 // Save the connection ID, which will be used for the subsequent service disconnection. 7714 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7715 // Save the connection ID, which will be used for the subsequent service disconnection. 7716 let connectionId = context.connectServiceExtensionAbility(want, connect); 7717 ``` 7718 7719 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **addDeathRecipient()** of the proxy object is called to add a callback for receiving the death notification of the remove object. 7720 7721 ```ts 7722 import { hilog } from '@kit.PerformanceAnalysisKit'; 7723 7724 class MyDeathRecipient implements rpc.DeathRecipient { 7725 onRemoteDied() { 7726 hilog.info(0x0000, 'testTag', 'server died'); 7727 } 7728 } 7729 let deathRecipient = new MyDeathRecipient(); 7730 if (proxy != undefined) { 7731 proxy.addDeathRecipient(deathRecipient, 0); 7732 } 7733 ``` 7734 7735### unregisterDeathRecipient<sup>9+</sup> 7736 7737unregisterDeathRecipient(recipient: DeathRecipient, flags: number): void 7738 7739Unregisters the callback used to receive death notifications of the remote object. 7740 7741**System capability**: SystemCapability.Communication.IPC.Core 7742 7743**Parameters** 7744 7745 | Name | Type | Mandatory| Description | 7746 | --------- | --------------------------------- | ---- | -------------- | 7747 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to unregister.| 7748 | flags | number | Yes | Flag of the death notification.| 7749 7750**Error codes** 7751 7752For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7753 7754 | ID| Error Message| 7755 | -------- | -------- | 7756 | 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. | 7757 | 1900008 | The proxy or remote object is invalid. | 7758 7759**Example** 7760 7761 ```ts 7762 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7763 // import { featureAbility } from '@kit.AbilityKit'; 7764 import { Want, common } from '@kit.AbilityKit'; 7765 import { hilog } from '@kit.PerformanceAnalysisKit'; 7766 7767 let proxy: rpc.IRemoteObject | undefined; 7768 let connect: common.ConnectOptions = { 7769 onConnect: (elementName, remoteProxy) => { 7770 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7771 proxy = remoteProxy; 7772 }, 7773 onDisconnect: (elementName) => { 7774 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7775 }, 7776 onFailed: () => { 7777 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7778 } 7779 }; 7780 let want: Want = { 7781 bundleName: "com.ohos.server", 7782 abilityName: "com.ohos.server.EntryAbility", 7783 }; 7784 7785 // Use this method to connect to the ability for the FA model. 7786 // FA.connectAbility(want,connect); 7787 7788 // Save the connection ID, which will be used for the subsequent service disconnection. 7789 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7790 // Save the connection ID, which will be used for the subsequent service disconnection. 7791 let connectionId = context.connectServiceExtensionAbility(want, connect); 7792 ``` 7793 7794 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **unregisterDeathRecipient()** of the proxy object is called to unregister the callback for receiving the death notification of the remote object. 7795 7796 ```ts 7797 import { hilog } from '@kit.PerformanceAnalysisKit'; 7798 import { BusinessError } from '@kit.BasicServicesKit'; 7799 7800 class MyDeathRecipient implements rpc.DeathRecipient { 7801 onRemoteDied() { 7802 hilog.info(0x0000, 'testTag', 'server died'); 7803 } 7804 } 7805 let deathRecipient = new MyDeathRecipient(); 7806 if (proxy != undefined) { 7807 try { 7808 proxy.registerDeathRecipient(deathRecipient, 0); 7809 proxy.unregisterDeathRecipient(deathRecipient, 0); 7810 } catch (error) { 7811 let e: BusinessError = error as BusinessError; 7812 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorCode ' + e.code); 7813 hilog.error(0x0000, 'testTag', 'proxy unregister deathRecipient fail, errorMessage ' + e.message); 7814 } 7815 } 7816 ``` 7817 7818### removeDeathRecipient<sup>(deprecated)</sup> 7819 7820removeDeathRecipient(recipient: DeathRecipient, flags: number): boolean 7821 7822Removes the callback used to receive death notifications of the remote object. 7823 7824> **NOTE**<br/> 7825> 7826> >**NOTE**<br>This API is deprecated since API version 9. Use [unregisterDeathRecipient](#unregisterdeathrecipient9-1) instead. 7827 7828**System capability**: SystemCapability.Communication.IPC.Core 7829 7830**Parameters** 7831 7832 | Name | Type | Mandatory| Description | 7833 | --------- | --------------------------------- | ---- | --------------------------------- | 7834 | recipient | [DeathRecipient](#deathrecipient) | Yes | Callback to remove. | 7835 | flags | number | Yes | Flag of the death notification. This parameter is reserved. It is set to **0**.| 7836 7837**Return value** 7838 7839 | Type | Description | 7840 | ------- | ---------------------------------------- | 7841 | boolean | Returns **true** if the callback is removed; returns **false** otherwise.| 7842 7843**Example** 7844 7845 ```ts 7846 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7847 // import { featureAbility } from '@kit.AbilityKit'; 7848 import { Want, common } from '@kit.AbilityKit'; 7849 import { hilog } from '@kit.PerformanceAnalysisKit'; 7850 7851 let proxy: rpc.IRemoteObject | undefined; 7852 let connect: common.ConnectOptions = { 7853 onConnect: (elementName, remoteProxy) => { 7854 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7855 proxy = remoteProxy; 7856 }, 7857 onDisconnect: (elementName) => { 7858 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7859 }, 7860 onFailed: () => { 7861 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7862 } 7863 }; 7864 let want: Want = { 7865 bundleName: "com.ohos.server", 7866 abilityName: "com.ohos.server.EntryAbility", 7867 }; 7868 7869 // Use this method to connect to the ability for the FA model. 7870 // FA.connectAbility(want,connect); 7871 7872 // Save the connection ID, which will be used for the subsequent service disconnection. 7873 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7874 // Save the connection ID, which will be used for the subsequent service disconnection. 7875 let connectionId = context.connectServiceExtensionAbility(want, connect); 7876 ``` 7877 7878 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **removeDeathRecipient()** of the proxy object is called to remove the callback used to receive the death notification of the remote object. 7879 7880 ```ts 7881 import { hilog } from '@kit.PerformanceAnalysisKit'; 7882 7883 class MyDeathRecipient implements rpc.DeathRecipient { 7884 onRemoteDied() { 7885 hilog.info(0x0000, 'testTag', 'server died'); 7886 } 7887 } 7888 let deathRecipient = new MyDeathRecipient(); 7889 if (proxy != undefined) { 7890 proxy.addDeathRecipient(deathRecipient, 0); 7891 proxy.removeDeathRecipient(deathRecipient, 0); 7892 } 7893 ``` 7894 7895### getDescriptor<sup>9+</sup> 7896 7897getDescriptor(): string 7898 7899Obtains the interface descriptor (which is a string) of this object. 7900 7901**System capability**: SystemCapability.Communication.IPC.Core 7902 7903**Return value** 7904 7905 | Type | Description | 7906 | ------ | ---------------- | 7907 | string | Interface descriptor obtained.| 7908 7909**Error codes** 7910 7911For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 7912 7913 | ID| Error Message| 7914 | -------- | -------- | 7915 | 1900007 | communication failed. | 7916 | 1900008 | The proxy or remote object is invalid. | 7917 7918**Example** 7919 7920 ```ts 7921 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7922 // import { featureAbility } from '@kit.AbilityKit'; 7923 import { Want, common } from '@kit.AbilityKit'; 7924 import { hilog } from '@kit.PerformanceAnalysisKit'; 7925 7926 let proxy: rpc.IRemoteObject | undefined; 7927 let connect: common.ConnectOptions = { 7928 onConnect: (elementName, remoteProxy) => { 7929 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 7930 proxy = remoteProxy; 7931 }, 7932 onDisconnect: (elementName) => { 7933 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 7934 }, 7935 onFailed: () => { 7936 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 7937 } 7938 }; 7939 let want: Want = { 7940 bundleName: "com.ohos.server", 7941 abilityName: "com.ohos.server.EntryAbility", 7942 }; 7943 7944 // Use this method to connect to the ability for the FA model. 7945 // FA.connectAbility(want,connect); 7946 7947 // Save the connection ID, which will be used for the subsequent service disconnection. 7948 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 7949 // Save the connection ID, which will be used for the subsequent service disconnection. 7950 let connectionId = context.connectServiceExtensionAbility(want, connect); 7951 ``` 7952 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getDescriptor()** of the proxy object is called to obtain the interface descriptor of the object. 7953 7954 ```ts 7955 import { hilog } from '@kit.PerformanceAnalysisKit'; 7956 import { BusinessError } from '@kit.BasicServicesKit'; 7957 7958 if (proxy != undefined) { 7959 try { 7960 let descriptor: string = proxy.getDescriptor(); 7961 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 7962 } catch (error) { 7963 let e: BusinessError = error as BusinessError; 7964 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorCode ' + e.code); 7965 hilog.error(0x0000, 'testTag', 'rpc get interface descriptor fail, errorMessage ' + e.message); 7966 } 7967 } 7968 ``` 7969 7970### getInterfaceDescriptor<sup>(deprecated)</sup> 7971 7972getInterfaceDescriptor(): string 7973 7974Obtains the interface descriptor of this proxy object. 7975 7976> **NOTE**<br/> 7977> 7978> >**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-1) instead. 7979 7980**System capability**: SystemCapability.Communication.IPC.Core 7981 7982**Return value** 7983 7984 | Type | Description | 7985 | ------ | ------------------ | 7986 | string | Interface descriptor obtained.| 7987 7988**Example** 7989 7990 ```ts 7991 // If the FA model is used, import featureAbility from @kit.AbilityKit. 7992 // import { featureAbility } from '@kit.AbilityKit'; 7993 import { Want, common } from '@kit.AbilityKit'; 7994 import { hilog } from '@kit.PerformanceAnalysisKit'; 7995 7996 let proxy: rpc.IRemoteObject | undefined; 7997 let connect: common.ConnectOptions = { 7998 onConnect: (elementName, remoteProxy) => { 7999 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8000 proxy = remoteProxy; 8001 }, 8002 onDisconnect: (elementName) => { 8003 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8004 }, 8005 onFailed: () => { 8006 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8007 } 8008 }; 8009 let want: Want = { 8010 bundleName: "com.ohos.server", 8011 abilityName: "com.ohos.server.EntryAbility", 8012 }; 8013 8014 // Use this method to connect to the ability for the FA model. 8015 // FA.connectAbility(want,connect); 8016 8017 // Save the connection ID, which will be used for the subsequent service disconnection. 8018 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8019 // Save the connection ID, which will be used for the subsequent service disconnection. 8020 let connectionId = context.connectServiceExtensionAbility(want, connect); 8021 ``` 8022 8023 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **getInterfaceDescriptor()** of the proxy object is called to obtain the interface descriptor of the current proxy object. 8024 8025 ```ts 8026 import { hilog } from '@kit.PerformanceAnalysisKit'; 8027 8028 if (proxy != undefined) { 8029 let descriptor: string = proxy.getInterfaceDescriptor(); 8030 hilog.info(0x0000, 'testTag', 'RpcClient: descriptor is ' + descriptor); 8031 } 8032 ``` 8033 8034### isObjectDead 8035 8036isObjectDead(): boolean 8037 8038Checks whether the **RemoteObject** is dead. 8039 8040**System capability**: SystemCapability.Communication.IPC.Core 8041 8042**Return value** 8043 8044 | Type | Description | 8045 | ------- | ------------------------------------------------- | 8046 | boolean | Returns **true** if **RemoteObject** is dead; returns **false** otherwise.| 8047 8048**Example** 8049 8050 ```ts 8051 // If the FA model is used, import featureAbility from @kit.AbilityKit. 8052 // import { featureAbility } from '@kit.AbilityKit'; 8053 import { Want, common } from '@kit.AbilityKit'; 8054 import { hilog } from '@kit.PerformanceAnalysisKit'; 8055 8056 let proxy: rpc.IRemoteObject | undefined; 8057 let connect: common.ConnectOptions = { 8058 onConnect: (elementName, remoteProxy) => { 8059 hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called'); 8060 proxy = remoteProxy; 8061 }, 8062 onDisconnect: (elementName) => { 8063 hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect'); 8064 }, 8065 onFailed: () => { 8066 hilog.info(0x0000, 'testTag', 'RpcClient: onFailed'); 8067 } 8068 }; 8069 let want: Want = { 8070 bundleName: "com.ohos.server", 8071 abilityName: "com.ohos.server.EntryAbility", 8072 }; 8073 8074 // Use this method to connect to the ability for the FA model. 8075 // FA.connectAbility(want,connect); 8076 8077 // Save the connection ID, which will be used for the subsequent service disconnection. 8078 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext 8079 // Save the connection ID, which will be used for the subsequent service disconnection. 8080 let connectionId = context.connectServiceExtensionAbility(want, connect); 8081 ``` 8082 8083 The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **isObjectDead()** of the proxy object is called to check whether this object is dead. 8084 8085 ```ts 8086 import { hilog } from '@kit.PerformanceAnalysisKit'; 8087 8088 if (proxy != undefined) { 8089 let isDead: boolean = proxy.isObjectDead(); 8090 hilog.info(0x0000, 'testTag', 'RpcClient: isObjectDead is ' + isDead); 8091 } 8092 ``` 8093 8094## MessageOption 8095 8096Defines the options used to construct the **MessageOption** object. 8097 8098### Properties 8099 8100**System capability**: SystemCapability.Communication.IPC.Core 8101 8102 | Name | Type | Readable | Writable | Description | 8103 | ------------- | ------ | ----- | ----- | ------------------------------------------------------------------------ | 8104 | TF_SYNC | number | Yes | No | Synchronous call. | 8105 | TF_ASYNC | number | Yes | No | Asynchronous call. | 8106 | TF_ACCEPT_FDS | number | Yes | No | Indication to **sendMessageRequest<sup>9+</sup>** for passing the file descriptor. | 8107 | TF_WAIT_TIME | number | Yes | Yes | RPC wait time, in seconds. This parameter cannot be used in IPC. The default waiting time is 8 seconds. You are advised not to change the waiting time.| 8108 8109### constructor<sup>9+</sup> 8110 8111constructor(async?: boolean) 8112 8113A constructor used to create a **MessageOption** object. 8114 8115**System capability**: SystemCapability.Communication.IPC.Core 8116 8117**Parameters** 8118 8119| Name| Type | Mandatory| Description | 8120| ------ | ------- | ---- | ------------------------------------------------------------ | 8121| 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**.| 8122 8123**Example** 8124 8125 ```ts 8126 class TestRemoteObject extends rpc.MessageOption { 8127 constructor(async: boolean) { 8128 super(async); 8129 } 8130 } 8131 ``` 8132 8133### constructor 8134 8135constructor(syncFlags?: number, waitTime?: number) 8136 8137A constructor used to create a **MessageOption** object. 8138 8139**System capability**: SystemCapability.Communication.IPC.Core 8140 8141**Parameters** 8142 8143 | Name | Type | Mandatory| Description | 8144 | --------- | ------ | ---- | --------------------------------------------- | 8145 | syncFlags | number | No | Call flag, which can be synchronous or asynchronous. The default value is **synchronous**. | 8146 | waitTime | number | No | Maximum wait time for an RPC call. The default value is **TF_WAIT_TIME**.| 8147 8148**Example** 8149 8150 ```ts 8151 class TestRemoteObject extends rpc.MessageOption { 8152 constructor(syncFlags?: number,waitTime?: number) { 8153 super(syncFlags,waitTime); 8154 } 8155 } 8156 ``` 8157### isAsync<sup>9+</sup> 8158 8159isAsync(): boolean 8160 8161Checks whether **SendMessageRequest** is called synchronously or asynchronously. 8162 8163**System capability**: SystemCapability.Communication.IPC.Core 8164 8165**Return value** 8166 8167 | Type | Description | 8168 | ------- | ---------------------------------------- | 8169 | boolean | Returns **true** if **SendMessageRequest** is called asynchronously; returns **false** if it is called synchronously.| 8170 8171**Example** 8172 8173 ```ts 8174 let option = new rpc.MessageOption(); 8175 option.isAsync(); 8176 ``` 8177 8178### setAsync<sup>9+</sup> 8179 8180setAsync(async: boolean): void 8181 8182Sets whether **SendMessageRequest** is called synchronously or asynchronously. 8183 8184**System capability**: SystemCapability.Communication.IPC.Core 8185 8186**Parameters** 8187 8188| Name| Type | Mandatory| Description | 8189| ------ | ------- | ---- | ------------------------------------------------- | 8190| 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.| 8191 8192**Example** 8193 8194 ```ts 8195 import { hilog } from '@kit.PerformanceAnalysisKit'; 8196 8197 let option = new rpc.MessageOption(); 8198 option.setAsync(true); 8199 hilog.info(0x0000, 'testTag', 'Set asynchronization flag'); 8200 ``` 8201 8202### getFlags 8203 8204getFlags(): number 8205 8206Obtains the call flag, which can be synchronous or asynchronous. 8207 8208**System capability**: SystemCapability.Communication.IPC.Core 8209 8210**Return value** 8211 8212 | Type | Description | 8213 | ------ | ------------------------------------ | 8214 | number | Call mode obtained.| 8215 8216**Example** 8217 8218 ```ts 8219 import { hilog } from '@kit.PerformanceAnalysisKit'; 8220 8221 try { 8222 let option = new rpc.MessageOption(); 8223 hilog.info(0x0000, 'testTag', 'create object successfully'); 8224 let flog = option.getFlags(); 8225 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8226 option.setFlags(1) 8227 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8228 let flog2 = option.getFlags(); 8229 hilog.info(0x0000, 'testTag', 'run getFlags success, flog2 is ' + flog2); 8230 } catch (error) { 8231 hilog.error(0x0000, 'testTag', 'error ' + error); 8232 } 8233 ``` 8234 8235### setFlags 8236 8237setFlags(flags: number): void 8238 8239Sets the call flag, which can be synchronous or asynchronous. 8240 8241**System capability**: SystemCapability.Communication.IPC.Core 8242 8243**Parameters** 8244 8245 | Name| Type | Mandatory| Description | 8246 | ------ | ------ | ---- | ------------------------ | 8247 | flags | number | Yes | Call flag to set.| 8248 8249**Example** 8250 8251 ```ts 8252 import { hilog } from '@kit.PerformanceAnalysisKit'; 8253 8254 try { 8255 let option = new rpc.MessageOption(); 8256 option.setFlags(1) 8257 hilog.info(0x0000, 'testTag', 'run setFlags success'); 8258 let flog = option.getFlags(); 8259 hilog.info(0x0000, 'testTag', 'run getFlags success, flog is ' + flog); 8260 } catch (error) { 8261 hilog.error(0x0000, 'testTag', 'error ' + error); 8262 } 8263 ``` 8264 8265### getWaitTime 8266 8267getWaitTime(): number 8268 8269Obtains the maximum wait time for this RPC call. 8270 8271**System capability**: SystemCapability.Communication.IPC.Core 8272 8273**Return value** 8274 8275 | Type | Description | 8276 | ------ | ----------------- | 8277 | number | Maximum wait time obtained.| 8278 8279**Example** 8280 8281 ```ts 8282 import { hilog } from '@kit.PerformanceAnalysisKit'; 8283 8284 try { 8285 let option = new rpc.MessageOption(); 8286 let time = option.getWaitTime(); 8287 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8288 option.setWaitTime(16); 8289 let time2 = option.getWaitTime(); 8290 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time2); 8291 } catch (error) { 8292 hilog.error(0x0000, 'testTag', 'error ' + error); 8293 } 8294 ``` 8295 8296### setWaitTime 8297 8298setWaitTime(waitTime: number): void 8299 8300Sets the maximum wait time for this RPC call. 8301 8302**System capability**: SystemCapability.Communication.IPC.Core 8303 8304**Parameters** 8305 8306 | Name | Type | Mandatory| Description | 8307 | -------- | ------ | ---- | --------------------- | 8308 | waitTime | number | Yes | Maximum wait time to set. The upper limit is 3000 seconds.| 8309 8310**Example** 8311 8312 ```ts 8313 import { hilog } from '@kit.PerformanceAnalysisKit'; 8314 8315 try { 8316 let option = new rpc.MessageOption(); 8317 option.setWaitTime(16); 8318 let time = option.getWaitTime(); 8319 hilog.info(0x0000, 'testTag', 'run getWaitTime success, time is ' + time); 8320 } catch (error) { 8321 hilog.error(0x0000, 'testTag', 'error ' + error); 8322 } 8323 ``` 8324 8325## IPCSkeleton 8326 8327Obtains IPC context information, including the UID and PID, local and remote device IDs, and whether the method is invoked on the same device. 8328 8329### getContextObject 8330 8331static getContextObject(): IRemoteObject 8332 8333Obtains the system capability manager. This API is a static method. 8334 8335**System capability**: SystemCapability.Communication.IPC.Core 8336 8337**Return value** 8338 8339 | Type | Description | 8340 | ------------------------------- | -------------------- | 8341 | [IRemoteObject](#iremoteobject) | System capability manager obtained.| 8342 8343**Example** 8344 8345 ```ts 8346 import { hilog } from '@kit.PerformanceAnalysisKit'; 8347 8348 let samgr = rpc.IPCSkeleton.getContextObject(); 8349 hilog.info(0x0000, 'testTag', 'RpcServer: getContextObject result: ' + samgr); 8350 ``` 8351 8352### getCallingPid 8353 8354static getCallingPid(): number 8355 8356Obtains 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. 8357 8358**System capability**: SystemCapability.Communication.IPC.Core 8359 8360**Return value** 8361 8362 | Type | Description | 8363 | ------ | ----------------- | 8364 | number | PID of the caller.| 8365 8366**Example** 8367 8368 ```ts 8369 import { hilog } from '@kit.PerformanceAnalysisKit'; 8370 8371 class Stub extends rpc.RemoteObject { 8372 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8373 let callerPid = rpc.IPCSkeleton.getCallingPid(); 8374 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid result: ' + callerPid); 8375 return true; 8376 } 8377 } 8378 ``` 8379 8380### getCallingUid 8381 8382static getCallingUid(): number 8383 8384Obtains 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. 8385 8386**System capability**: SystemCapability.Communication.IPC.Core 8387 8388**Return value** 8389 8390 | Type | Description | 8391 | ------ | ----------------- | 8392 | number | UID of the caller.| 8393 8394**Example** 8395 8396 ```ts 8397 import { hilog } from '@kit.PerformanceAnalysisKit'; 8398 8399 class Stub extends rpc.RemoteObject { 8400 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8401 let callerUid = rpc.IPCSkeleton.getCallingUid(); 8402 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid result: ' + callerUid); 8403 return true; 8404 } 8405 } 8406 ``` 8407 8408### getCallingTokenId<sup>8+</sup> 8409 8410static getCallingTokenId(): number 8411 8412Obtains the caller's token ID, which is used to verify the caller identity. 8413 8414**System capability**: SystemCapability.Communication.IPC.Core 8415 8416**Return value** 8417 8418 | Type | Description | 8419 | ------ | --------------------- | 8420 | number | Token ID of the caller obtained.| 8421 8422**Example** 8423 8424 ```ts 8425 import { hilog } from '@kit.PerformanceAnalysisKit'; 8426 8427 class Stub extends rpc.RemoteObject { 8428 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8429 let callerTokenId = rpc.IPCSkeleton.getCallingTokenId(); 8430 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingTokenId result: ' + callerTokenId); 8431 return true; 8432 } 8433 } 8434 ``` 8435 8436### getCallingDeviceID 8437 8438static getCallingDeviceID(): string 8439 8440Obtains the ID of the device hosting the caller's process. This API is a static method. 8441 8442**System capability**: SystemCapability.Communication.IPC.Core 8443 8444**Return value** 8445 8446 | Type | Description | 8447 | ------ | ---------------------------- | 8448 | string | Device ID obtained.| 8449 8450**Example** 8451 8452 ```ts 8453 import { hilog } from '@kit.PerformanceAnalysisKit'; 8454 8455 class Stub extends rpc.RemoteObject { 8456 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8457 let callerDeviceID = rpc.IPCSkeleton.getCallingDeviceID(); 8458 hilog.info(0x0000, 'testTag', 'RpcServer: callerDeviceID is ' + callerDeviceID); 8459 return true; 8460 } 8461 } 8462 ``` 8463 8464### getLocalDeviceID 8465 8466static getLocalDeviceID(): string 8467 8468Obtains the local device ID. This API is a static method. 8469 8470**System capability**: SystemCapability.Communication.IPC.Core 8471 8472**Return value** 8473 8474 | Type | Description | 8475 | ------ | ------------------ | 8476 | string | Local device ID obtained.| 8477 8478**Example** 8479 8480 ```ts 8481 import { hilog } from '@kit.PerformanceAnalysisKit'; 8482 8483 class Stub extends rpc.RemoteObject { 8484 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8485 let localDeviceID = rpc.IPCSkeleton.getLocalDeviceID(); 8486 hilog.info(0x0000, 'testTag', 'RpcServer: localDeviceID is ' + localDeviceID); 8487 return true; 8488 } 8489 } 8490 ``` 8491 8492### isLocalCalling 8493 8494static isLocalCalling(): boolean 8495 8496Checks whether the peer process is a process of the local device. This API is a static method. 8497 8498**System capability**: SystemCapability.Communication.IPC.Core 8499 8500**Return value** 8501 8502 | Type | Description | 8503 | ------- | -------------------------------------------------- | 8504 | boolean | Returns **true** if the local and peer processes are on the same device; returns **false** otherwise.| 8505 8506**Example** 8507 8508 ```ts 8509 import { hilog } from '@kit.PerformanceAnalysisKit'; 8510 8511 class Stub extends rpc.RemoteObject { 8512 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8513 let isLocalCalling = rpc.IPCSkeleton.isLocalCalling(); 8514 hilog.info(0x0000, 'testTag', 'RpcServer: isLocalCalling is ' + isLocalCalling); 8515 return true; 8516 } 8517 } 8518 ``` 8519 8520### flushCmdBuffer<sup>9+</sup> 8521 8522static flushCmdBuffer(object: IRemoteObject): void 8523 8524Flushes 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. 8525 8526**System capability**: SystemCapability.Communication.IPC.Core 8527 8528**Parameters** 8529 8530 | Name| Type | Mandatory| Description | 8531 | ------ | ------------------------------- | ---- | ------------------- | 8532 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8533 8534**Error codes** 8535 8536For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8537 8538 | ID| Error Message| 8539 | -------- | -------- | 8540 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 8541 8542**Example** 8543 8544 ```ts 8545 import { hilog } from '@kit.PerformanceAnalysisKit'; 8546 import { BusinessError } from '@kit.BasicServicesKit'; 8547 8548 class TestRemoteObject extends rpc.RemoteObject { 8549 constructor(descriptor: string) { 8550 super(descriptor); 8551 } 8552 } 8553 let remoteObject = new TestRemoteObject("aaa"); 8554 try { 8555 rpc.IPCSkeleton.flushCmdBuffer(remoteObject); 8556 } catch (error) { 8557 let e: BusinessError = error as BusinessError; 8558 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorCode ' + e.code); 8559 hilog.error(0x0000, 'testTag', 'proxy flushCmdBuffer fail, errorMessage ' + e.message); 8560 } 8561 ``` 8562 8563### flushCommands<sup>(deprecated)</sup> 8564 8565static flushCommands(object: IRemoteObject): number 8566 8567Flushes 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. 8568 8569> **NOTE**<br/> 8570> 8571> >**NOTE**<br>This API is deprecated since API version 9. Use [flushCmdBuffer](#flushcmdbuffer9) instead. 8572 8573**System capability**: SystemCapability.Communication.IPC.Core 8574 8575**Parameters** 8576 8577 | Name| Type | Mandatory| Description | 8578 | ------ | ------------------------------- | ---- | ------------------- | 8579 | object | [IRemoteObject](#iremoteobject) | Yes | **RemoteProxy** specified. | 8580 8581**Return value** 8582 8583 | Type | Description | 8584 | ------ | --------------------------------------------------------------------------------- | 8585 | 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.| 8586 8587**Example** 8588 8589 ```ts 8590 import { hilog } from '@kit.PerformanceAnalysisKit'; 8591 8592 class MyDeathRecipient implements rpc.DeathRecipient { 8593 onRemoteDied() { 8594 hilog.info(0x0000, 'testTag', 'server died'); 8595 } 8596 } 8597 class TestRemoteObject extends rpc.RemoteObject { 8598 constructor(descriptor: string) { 8599 super(descriptor); 8600 } 8601 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8602 return true; 8603 } 8604 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8605 return true; 8606 } 8607 isObjectDead(): boolean { 8608 return false; 8609 } 8610 } 8611 let remoteObject = new TestRemoteObject("aaa"); 8612 let ret = rpc.IPCSkeleton.flushCommands(remoteObject); 8613 hilog.info(0x0000, 'testTag', 'RpcServer: flushCommands result: ' + ret); 8614 ``` 8615 8616### resetCallingIdentity 8617 8618static resetCallingIdentity(): string 8619 8620Resets 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. 8621 8622**System capability**: SystemCapability.Communication.IPC.Core 8623 8624**Return value** 8625 8626 | Type | Description | 8627 | ------ | ------------------------------------ | 8628 | string | String containing the UID and PID of the remote user.| 8629 8630**Example** 8631 8632 ```ts 8633 import { hilog } from '@kit.PerformanceAnalysisKit'; 8634 8635 class Stub extends rpc.RemoteObject { 8636 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8637 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8638 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8639 return true; 8640 } 8641 } 8642 ``` 8643 8644### restoreCallingIdentity<sup>9+</sup> 8645 8646static restoreCallingIdentity(identity: string): void 8647 8648Restores 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. 8649 8650**System capability**: SystemCapability.Communication.IPC.Core 8651 8652**Parameters** 8653 8654 | Name | Type | Mandatory| Description | 8655 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8656 | identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8657 8658**Error codes** 8659 8660For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8661 8662 | ID| Error Message| 8663 | -------- | -------- | 8664 | 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. | 8665 8666**Example** 8667 8668 ```ts 8669 import { hilog } from '@kit.PerformanceAnalysisKit'; 8670 8671 class Stub extends rpc.RemoteObject { 8672 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8673 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8674 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8675 rpc.IPCSkeleton.restoreCallingIdentity(callingIdentity); 8676 return true; 8677 } 8678 } 8679 ``` 8680 8681### setCallingIdentity<sup>(deprecated)</sup> 8682 8683static setCallingIdentity(identity: string): boolean 8684 8685Sets 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. 8686 8687> **NOTE**<br/> 8688> 8689> >**NOTE**<br>This API is deprecated since API version 9. Use [restoreCallingIdentity](#restorecallingidentity9) instead. 8690 8691**System capability**: SystemCapability.Communication.IPC.Core 8692 8693**Parameters** 8694 8695 | Name | Type | Mandatory| Description | 8696 | -------- | ------ | ---- | ------------------------------------------------------------------ | 8697 | identity | string | Yes | String containing the remote user's UID and PID, which are returned by **resetCallingIdentity**.| 8698 8699**Return value** 8700 8701 | Type | Description | 8702 | ------- | ---------------------------------| 8703 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 8704 8705**Example** 8706 8707 ```ts 8708 import { hilog } from '@kit.PerformanceAnalysisKit'; 8709 8710 class Stub extends rpc.RemoteObject { 8711 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 8712 let callingIdentity = rpc.IPCSkeleton.resetCallingIdentity(); 8713 hilog.info(0x0000, 'testTag', 'RpcServer: callingIdentity is ' + callingIdentity); 8714 let ret = rpc.IPCSkeleton.setCallingIdentity(callingIdentity); 8715 hilog.info(0x0000, 'testTag', 'RpcServer: setCallingIdentity is ' + ret); 8716 return true; 8717 } 8718 } 8719 ``` 8720 8721## RemoteObject 8722 8723Provides methods to implement **RemoteObject**. The service provider must inherit from this class. 8724 8725### constructor 8726 8727constructor(descriptor: string) 8728 8729A constructor used to create a **RemoteObject** object. 8730 8731**System capability**: SystemCapability.Communication.IPC.Core 8732 8733**Parameters** 8734 8735 | Name | Type | Mandatory| Description | 8736 | ---------- | ------ | ---- | ------------ | 8737 | descriptor | string | Yes | Interface descriptor.| 8738 8739**Example** 8740 8741 ```ts 8742 class TestRemoteObject extends rpc.RemoteObject { 8743 constructor(descriptor: string) { 8744 super(descriptor); 8745 } 8746 } 8747 ``` 8748### sendRequest<sup>(deprecated)</sup> 8749 8750sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 8751 8752Sends 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. 8753 8754> **NOTE**<br/> 8755> 8756> >**NOTE**<br>This API is deprecated since API version 8. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8757 8758**System capability**: SystemCapability.Communication.IPC.Core 8759 8760**Parameters** 8761 8762 | Name | Type | Mandatory| Description | 8763 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8764 | 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.| 8765 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8766 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8767 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8768 8769**Return value** 8770 8771 | Type | Description | 8772 | ------- | -------------------------------- | 8773 | boolean | Returns **true** if the message is sent successfully; returns **false** otherwise.| 8774 8775**Example** 8776 8777 ```ts 8778 import { hilog } from '@kit.PerformanceAnalysisKit'; 8779 8780 class MyDeathRecipient implements rpc.DeathRecipient { 8781 onRemoteDied() { 8782 hilog.info(0x0000, 'testTag', 'server died'); 8783 } 8784 } 8785 class TestRemoteObject extends rpc.RemoteObject { 8786 constructor(descriptor: string) { 8787 super(descriptor); 8788 } 8789 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8790 return true; 8791 } 8792 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8793 return true; 8794 } 8795 isObjectDead(): boolean { 8796 return false; 8797 } 8798 } 8799 let testRemoteObject = new TestRemoteObject("testObject"); 8800 let option = new rpc.MessageOption(); 8801 let data = rpc.MessageParcel.create(); 8802 let reply = rpc.MessageParcel.create(); 8803 data.writeInt(1); 8804 data.writeString("hello"); 8805 let ret: boolean = testRemoteObject.sendRequest(1, data, reply, option); 8806 if (ret) { 8807 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8808 let msg = reply.readString(); 8809 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8810 } else { 8811 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed'); 8812 } 8813 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8814 data.reclaim(); 8815 reply.reclaim(); 8816 ``` 8817 8818### sendMessageRequest<sup>9+</sup> 8819 8820sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): Promise<RequestResult> 8821 8822Sends 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. 8823 8824**System capability**: SystemCapability.Communication.IPC.Core 8825 8826**Parameters** 8827 8828 | Name | Type | Mandatory| Description | 8829 | ------- | ------------------------------------ | ---- | -------------------------------------------------------------------------------------- | 8830 | 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.| 8831 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8832 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8833 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8834 8835**Return value** 8836 8837| Type | Description | 8838| ----------------------------------------------- | ----------------------------------------- | 8839| Promise<[RequestResult](#requestresult9)> | Promise used to return a **RequestResult** instance.| 8840 8841 8842**Error codes** 8843 8844For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8845 8846 | ID| Error Message| 8847 | -------- | -------- | 8848 | 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. | 8849 8850**Example** 8851 8852 ```ts 8853 import { hilog } from '@kit.PerformanceAnalysisKit'; 8854 8855 class TestRemoteObject extends rpc.RemoteObject { 8856 constructor(descriptor: string) { 8857 super(descriptor); 8858 } 8859 } 8860 let testRemoteObject = new TestRemoteObject("testObject"); 8861 let option = new rpc.MessageOption(); 8862 let data = rpc.MessageSequence.create(); 8863 let reply = rpc.MessageSequence.create(); 8864 data.writeInt(1); 8865 data.writeString("hello"); 8866 testRemoteObject.sendMessageRequest(1, data, reply, option) 8867 .then((result: rpc.RequestResult) => { 8868 if (result.errCode === 0) { 8869 hilog.info(0x0000, 'testTag', 'sendMessageRequest got result'); 8870 let num = result.reply.readInt(); 8871 let msg = result.reply.readString(); 8872 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8873 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8874 } else { 8875 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, errCode: ' + result.errCode); 8876 } 8877 }).catch((e: Error) => { 8878 hilog.error(0x0000, 'testTag', 'RPCTest: sendMessageRequest failed, message: ' + e.message); 8879 }).finally (() => { 8880 hilog.info(0x0000, 'testTag', 'RPCTest: sendMessageRequest ends, reclaim parcel'); 8881 data.reclaim(); 8882 reply.reclaim(); 8883 }); 8884 ``` 8885 8886### sendRequest<sup>(deprecated)</sup> 8887 8888sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): Promise<SendRequestResult> 8889 8890Sends 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. 8891 8892> **NOTE**<br/> 8893> 8894> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-4) instead. 8895 8896**System capability**: SystemCapability.Communication.IPC.Core 8897 8898**Parameters** 8899 8900 | Name | Type | Mandatory| Description | 8901 | ------- | ----------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 8902 | 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.| 8903 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 8904 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 8905 | options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8906 8907**Return value** 8908 8909| Type | Description | 8910| ------------------------------------------------------------ | --------------------------------------------- | 8911| Promise<[SendRequestResult](#sendrequestresultdeprecated)> | Promise used to return a **sendRequestResult** instance.| 8912 8913**Example** 8914 8915 ```ts 8916 import { hilog } from '@kit.PerformanceAnalysisKit'; 8917 8918 class MyDeathRecipient implements rpc.DeathRecipient { 8919 onRemoteDied() { 8920 hilog.info(0x0000, 'testTag', 'server died'); 8921 } 8922 } 8923 class TestRemoteObject extends rpc.RemoteObject { 8924 constructor(descriptor: string) { 8925 super(descriptor); 8926 } 8927 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8928 return true; 8929 } 8930 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 8931 return true; 8932 } 8933 isObjectDead(): boolean { 8934 return false; 8935 } 8936 } 8937 let testRemoteObject = new TestRemoteObject("testObject"); 8938 let option = new rpc.MessageOption(); 8939 let data = rpc.MessageParcel.create(); 8940 let reply = rpc.MessageParcel.create(); 8941 data.writeInt(1); 8942 data.writeString("hello"); 8943 let a = testRemoteObject.sendRequest(1, data, reply, option) as Object; 8944 let b = a as Promise<rpc.SendRequestResult>; 8945 b.then((result: rpc.SendRequestResult) => { 8946 if (result.errCode === 0) { 8947 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 8948 let num = result.reply.readInt(); 8949 let msg = result.reply.readString(); 8950 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 8951 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 8952 } else { 8953 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 8954 } 8955 }).catch((e: Error) => { 8956 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, message: ' + e.message); 8957 }).finally (() => { 8958 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 8959 data.reclaim(); 8960 reply.reclaim(); 8961 }); 8962 ``` 8963 8964### sendMessageRequest<sup>9+</sup> 8965 8966sendMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption, callback: AsyncCallback<RequestResult>): void 8967 8968Sends 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. 8969 8970**System capability**: SystemCapability.Communication.IPC.Core 8971 8972**Parameters** 8973 8974| Name | Type | Mandatory| Description | 8975| ------------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 8976| 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.| 8977| data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object holding the data to send. | 8978| reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that receives the response. | 8979| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 8980| callback | AsyncCallback<[RequestResult](#requestresult9)> | Yes | Callback for receiving the sending result. | 8981 8982 8983**Error codes** 8984 8985For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 8986 8987 | ID| Error Message| 8988 | -------- | -------- | 8989 | 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. | 8990 8991**Example** 8992 8993 ```ts 8994 import { hilog } from '@kit.PerformanceAnalysisKit'; 8995 import { BusinessError } from '@kit.BasicServicesKit'; 8996 8997 class TestRemoteObject extends rpc.RemoteObject { 8998 constructor(descriptor: string) { 8999 super(descriptor); 9000 } 9001 } 9002 function sendRequestCallback(err: BusinessError, result: rpc.RequestResult) { 9003 if (result.errCode === 0) { 9004 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 9005 let num = result.reply.readInt(); 9006 let msg = result.reply.readString(); 9007 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 9008 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9009 } else { 9010 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9011 } 9012 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9013 result.data.reclaim(); 9014 result.reply.reclaim(); 9015 } 9016 let testRemoteObject = new TestRemoteObject("testObject"); 9017 let option = new rpc.MessageOption(); 9018 let data = rpc.MessageSequence.create(); 9019 let reply = rpc.MessageSequence.create(); 9020 data.writeInt(1); 9021 data.writeString("hello"); 9022 testRemoteObject.sendMessageRequest(1, data, reply, option, sendRequestCallback); 9023 ``` 9024 9025### sendRequest<sup>(deprecated)</sup> 9026 9027sendRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption, callback: AsyncCallback<SendRequestResult>): void 9028 9029Sends 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. 9030 9031> **NOTE**<br/> 9032> 9033> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [sendMessageRequest](#sendmessagerequest9-5) instead. 9034 9035**System capability**: SystemCapability.Communication.IPC.Core 9036 9037**Parameters** 9038 9039| Name | Type | Mandatory| Description | 9040| ------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 9041| 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.| 9042| data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object holding the data to send. | 9043| reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that receives the response. | 9044| options | [MessageOption](#messageoption) | Yes | Request sending mode, which can be synchronous (default) or asynchronous. | 9045| callback | AsyncCallback<[SendRequestResult](#sendrequestresultdeprecated)> | Yes | Callback for receiving the sending result. | 9046 9047**Example** 9048 9049 ```ts 9050 import { hilog } from '@kit.PerformanceAnalysisKit'; 9051 import { BusinessError } from '@kit.BasicServicesKit'; 9052 9053 class MyDeathRecipient implements rpc.DeathRecipient { 9054 onRemoteDied() { 9055 hilog.info(0x0000, 'testTag', 'server died'); 9056 } 9057 } 9058 class TestRemoteObject extends rpc.RemoteObject { 9059 constructor(descriptor: string) { 9060 super(descriptor); 9061 } 9062 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9063 return true; 9064 } 9065 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9066 return true; 9067 } 9068 isObjectDead(): boolean { 9069 return false; 9070 } 9071 } 9072 function sendRequestCallback(err: BusinessError, result: rpc.SendRequestResult) { 9073 if (result.errCode === 0) { 9074 hilog.info(0x0000, 'testTag', 'sendRequest got result'); 9075 let num = result.reply.readInt(); 9076 let msg = result.reply.readString(); 9077 hilog.info(0x0000, 'testTag', 'RPCTest: reply num: ' + num); 9078 hilog.info(0x0000, 'testTag', 'RPCTest: reply msg: ' + msg); 9079 } else { 9080 hilog.error(0x0000, 'testTag', 'RPCTest: sendRequest failed, errCode: ' + result.errCode); 9081 } 9082 hilog.info(0x0000, 'testTag', 'RPCTest: sendRequest ends, reclaim parcel'); 9083 result.data.reclaim(); 9084 result.reply.reclaim(); 9085 } 9086 let testRemoteObject = new TestRemoteObject("testObject"); 9087 let option = new rpc.MessageOption(); 9088 let data = rpc.MessageParcel.create(); 9089 let reply = rpc.MessageParcel.create(); 9090 data.writeInt(1); 9091 data.writeString("hello"); 9092 testRemoteObject.sendRequest(1, data, reply, option, sendRequestCallback); 9093 ``` 9094 9095### onRemoteMessageRequest<sup>9+</sup> 9096 9097onRemoteMessageRequest(code: number, data: MessageSequence, reply: MessageSequence, options: MessageOption): boolean | Promise\<boolean> 9098 9099> **NOTE**<br/> 9100> 9101>* You are advised to overload **onRemoteMessageRequest** preferentially, which implements synchronous and asynchronous message processing. 9102>* If both **onRemoteRequest()** and **onRemoteMessageRequest()** are overloaded, only the onRemoteMessageRequest() takes effect. 9103 9104Called to return a response to **sendMessageRequest()**. The server processes the request synchronously or asynchronously and returns the result in this API. 9105 9106**System capability**: SystemCapability.Communication.IPC.Core 9107 9108**Parameters** 9109 9110 | Name| Type | Mandatory| Description | 9111 | ------ | ------------------------------------ | ---- | ----------------------------------------- | 9112 | code | number | Yes | Service request code sent by the remote end. | 9113 | data | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object that holds the parameters called by the client.| 9114 | reply | [MessageSequence](#messagesequence9) | Yes | **MessageSequence** object to which the result is written. | 9115 | options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 9116 9117**Return value** 9118 9119 | Type | Description | 9120 | ----------------- | ----------------------------------------------------------------------------------------------- | 9121 | 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.| 9122 | Promise\<boolean> | Returns a promise object if the request is processed asynchronously in **onRemoteMessageRequest**. | 9123 9124**Example**: Overload **onRemoteMessageRequest** to process requests synchronously. 9125 9126 ```ts 9127 import { hilog } from '@kit.PerformanceAnalysisKit'; 9128 9129 class TestRemoteObject extends rpc.RemoteObject { 9130 constructor(descriptor: string) { 9131 super(descriptor); 9132 } 9133 9134 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9135 if (code === 1) { 9136 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9137 return true; 9138 } else { 9139 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9140 return false; 9141 } 9142 } 9143 } 9144 ``` 9145 9146 **Example**: Overload **onRemoteMessageRequest** to process requests asynchronously. 9147 9148 ```ts 9149 import { hilog } from '@kit.PerformanceAnalysisKit'; 9150 9151 class TestRemoteObject extends rpc.RemoteObject { 9152 constructor(descriptor: string) { 9153 super(descriptor); 9154 } 9155 9156 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9157 if (code === 1) { 9158 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9159 } else { 9160 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9161 return false; 9162 } 9163 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9164 setTimeout(resolve, 100); 9165 }) 9166 return true; 9167 } 9168 } 9169 ``` 9170 9171**Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests synchronously. 9172 9173 ```ts 9174 import { hilog } from '@kit.PerformanceAnalysisKit'; 9175 9176 class TestRemoteObject extends rpc.RemoteObject { 9177 constructor(descriptor: string) { 9178 super(descriptor); 9179 } 9180 9181 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9182 if (code === 1) { 9183 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteMessageRequest is called'); 9184 return true; 9185 } else { 9186 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9187 return false; 9188 } 9189 } 9190 // Only onRemoteMessageRequest is executed. 9191 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> { 9192 if (code === 1) { 9193 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9194 } else { 9195 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9196 return false; 9197 } 9198 return true; 9199 } 9200 } 9201 ``` 9202 9203 **Example**: Overload **onRemoteMessageRequest** and **onRemoteRequest** to process requests asynchronously. 9204 9205 ```ts 9206 import { hilog } from '@kit.PerformanceAnalysisKit'; 9207 class TestRemoteObject extends rpc.RemoteObject { 9208 constructor(descriptor: string) { 9209 super(descriptor); 9210 } 9211 9212 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9213 if (code === 1) { 9214 hilog.info(0x0000, 'testTag', 'RpcServer: sync onRemoteRequest is called'); 9215 return true; 9216 } else { 9217 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9218 return false; 9219 } 9220 } 9221 // Only onRemoteMessageRequest is executed. 9222 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): Promise<boolean> { 9223 if (code === 1) { 9224 hilog.info(0x0000, 'testTag', 'RpcServer: async onRemoteMessageRequest is called'); 9225 } else { 9226 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9227 return false; 9228 } 9229 await new Promise((resolve: (data: rpc.RequestResult) => void) => { 9230 setTimeout(resolve, 100); 9231 }) 9232 return true; 9233 } 9234 } 9235 ``` 9236 9237### onRemoteRequest<sup>(deprecated)</sup> 9238 9239onRemoteRequest(code: number, data: MessageParcel, reply: MessageParcel, options: MessageOption): boolean 9240 9241Called to return a response to **sendRequest()**. The server processes the request and returns a response in this function. 9242 9243> **NOTE**<br/> 9244> 9245> >**NOTE**<br>This API is deprecated since API version 9. Use [onRemoteMessageRequest](#onremotemessagerequest9) instead. 9246 9247**System capability**: SystemCapability.Communication.IPC.Core 9248 9249**Parameters** 9250 9251 | Name| Type | Mandatory| Description | 9252 | ------ | ----------------------------------------- | ---- | --------------------------------------- | 9253 | code | number | Yes | Service request code sent by the remote end. | 9254 | data | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object that holds the parameters called by the client.| 9255 | reply | [MessageParcel](#messageparceldeprecated) | Yes | **MessageParcel** object carrying the result. | 9256 | options | [MessageOption](#messageoption) | Yes | Whether the operation is synchronous or asynchronous. | 9257 9258**Return value** 9259 9260 | Type | Description | 9261 | ------- | -------------------------------- | 9262 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 9263 9264**Example** 9265 9266 ```ts 9267 import { hilog } from '@kit.PerformanceAnalysisKit'; 9268 9269 class MyDeathRecipient implements rpc.DeathRecipient { 9270 onRemoteDied() { 9271 hilog.info(0x0000, 'testTag', 'server died'); 9272 } 9273 } 9274 class TestRemoteObject extends rpc.RemoteObject { 9275 constructor(descriptor: string) { 9276 super(descriptor); 9277 } 9278 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9279 return true; 9280 } 9281 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9282 return true; 9283 } 9284 isObjectDead(): boolean { 9285 return false; 9286 } 9287 onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 9288 if (code === 1) { 9289 hilog.info(0x0000, 'testTag', 'RpcServer: onRemoteRequest called'); 9290 return true; 9291 } else { 9292 hilog.error(0x0000, 'testTag', 'RpcServer: unknown code: ' + code); 9293 return false; 9294 } 9295 } 9296 } 9297 ``` 9298 9299### getCallingUid 9300 9301getCallingUid(): number 9302 9303Obtains the UID of the remote process. 9304 9305**System capability**: SystemCapability.Communication.IPC.Core 9306 9307**Return value** 9308 | Type | Description | 9309 | ------ | ----------------------- | 9310 | number | UID of the remote process obtained.| 9311 9312**Example** 9313 9314 ```ts 9315 import { hilog } from '@kit.PerformanceAnalysisKit'; 9316 9317 class TestRemoteObject extends rpc.RemoteObject { 9318 constructor(descriptor: string) { 9319 super(descriptor); 9320 } 9321 } 9322 let testRemoteObject = new TestRemoteObject("testObject"); 9323 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingUid: ' + testRemoteObject.getCallingUid()); 9324 ``` 9325 9326### getCallingPid 9327 9328getCallingPid(): number 9329 9330Obtains the PID of the remote process. 9331 9332**System capability**: SystemCapability.Communication.IPC.Core 9333 9334**Return value** 9335 9336 | Type | Description | 9337 | ------ | ----------------------- | 9338 | number | PID of the remote process obtained.| 9339 9340**Example** 9341 9342 ```ts 9343 import { hilog } from '@kit.PerformanceAnalysisKit'; 9344 9345 class TestRemoteObject extends rpc.RemoteObject { 9346 constructor(descriptor: string) { 9347 super(descriptor); 9348 } 9349 } 9350 let testRemoteObject = new TestRemoteObject("testObject"); 9351 hilog.info(0x0000, 'testTag', 'RpcServer: getCallingPid: ' + testRemoteObject.getCallingPid()); 9352 ``` 9353 9354### getLocalInterface<sup>9+</sup> 9355 9356getLocalInterface(descriptor: string): IRemoteBroker 9357 9358Obtains the string of the interface descriptor. 9359 9360**System capability**: SystemCapability.Communication.IPC.Core 9361 9362**Parameters** 9363 9364 | Name | Type | Mandatory| Description | 9365 | ---------- | ------ | ---- | -------------------- | 9366 | descriptor | string | Yes | Interface descriptor.| 9367 9368**Return value** 9369 9370 | Type | Description | 9371 | ------------- | --------------------------------------------- | 9372 | [IRemoteBroker](#iremotebroker) | **IRemoteBroker** object bound to the specified interface token.| 9373 9374**Error codes** 9375 9376For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9377 9378 | ID| Error Message| 9379 | -------- | -------- | 9380 | 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. | 9381 9382**Example** 9383 9384 ```ts 9385 import { hilog } from '@kit.PerformanceAnalysisKit'; 9386 import { BusinessError } from '@kit.BasicServicesKit'; 9387 9388 class MyDeathRecipient implements rpc.DeathRecipient { 9389 onRemoteDied() { 9390 hilog.info(0x0000, 'testTag', 'server died'); 9391 } 9392 } 9393 class TestRemoteObject extends rpc.RemoteObject { 9394 constructor(descriptor: string) { 9395 super(descriptor); 9396 this.modifyLocalInterface(this, descriptor); 9397 } 9398 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9399 // Implement the method logic based on service requirements. 9400 } 9401 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9402 // Implement the method logic based on service requirements. 9403 } 9404 isObjectDead(): boolean { 9405 return false; 9406 } 9407 asObject(): rpc.IRemoteObject { 9408 return this; 9409 } 9410 } 9411 let testRemoteObject = new TestRemoteObject("testObject"); 9412 try { 9413 testRemoteObject.getLocalInterface("testObject"); 9414 } catch (error) { 9415 let e: BusinessError = error as BusinessError; 9416 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9417 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9418 } 9419 ``` 9420 9421### queryLocalInterface<sup>(deprecated)</sup> 9422 9423queryLocalInterface(descriptor: string): IRemoteBroker 9424 9425Checks whether the remote object corresponding to the specified interface token exists. 9426 9427> **NOTE**<br/> 9428> 9429> >**NOTE**<br>This API is deprecated since API version 9. Use [getLocalInterface](#getlocalinterface9-2) instead. 9430 9431**System capability**: SystemCapability.Communication.IPC.Core 9432 9433**Parameters** 9434 9435 | Name | Type | Mandatory| Description | 9436 | ---------- | ------ | ---- | ---------------------- | 9437 | descriptor | string | Yes | Interface descriptor.| 9438 9439**Return value** 9440 9441 | Type | Description | 9442 | ------------- | ------------------------------------------------------------------ | 9443 | [IRemoteBroker](#iremotebroker) | Returns the remote object if a match is found; returns **Null** otherwise.| 9444 9445**Example** 9446 9447 ```ts 9448 import { hilog } from '@kit.PerformanceAnalysisKit'; 9449 9450 class MyDeathRecipient implements rpc.DeathRecipient { 9451 onRemoteDied() { 9452 hilog.info(0x0000, 'testTag', 'server died'); 9453 } 9454 } 9455 class TestRemoteObject extends rpc.RemoteObject { 9456 constructor(descriptor: string) { 9457 super(descriptor); 9458 this.attachLocalInterface(this, descriptor); 9459 } 9460 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9461 return true; 9462 } 9463 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9464 return true; 9465 } 9466 isObjectDead(): boolean { 9467 return false; 9468 } 9469 asObject(): rpc.IRemoteObject { 9470 return this; 9471 } 9472 } 9473 let testRemoteObject = new TestRemoteObject("testObject"); 9474 testRemoteObject.queryLocalInterface("testObject"); 9475 ``` 9476 9477### getDescriptor<sup>9+</sup> 9478 9479getDescriptor(): string 9480 9481Obtains the interface descriptor of this object. The interface descriptor is a string. 9482 9483**System capability**: SystemCapability.Communication.IPC.Core 9484 9485**Return value** 9486 9487 | Type | Description | 9488 | ------ | ---------------- | 9489 | string | Interface descriptor obtained.| 9490 9491**Error codes** 9492 9493For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9494 9495 | ID| Error Message| 9496 | -------- | -------- | 9497 | 1900008 | The proxy or remote object is invalid. | 9498 9499**Example** 9500 9501 ```ts 9502 import { hilog } from '@kit.PerformanceAnalysisKit'; 9503 import { BusinessError } from '@kit.BasicServicesKit'; 9504 9505 class MyDeathRecipient implements rpc.DeathRecipient { 9506 onRemoteDied() { 9507 hilog.info(0x0000, 'testTag', 'server died'); 9508 } 9509 } 9510 class TestRemoteObject extends rpc.RemoteObject { 9511 constructor(descriptor: string) { 9512 super(descriptor); 9513 } 9514 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9515 // Implement the method logic based on service requirements. 9516 } 9517 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9518 // Implement the method logic based on service requirements. 9519 } 9520 isObjectDead(): boolean { 9521 return false; 9522 } 9523 } 9524 let testRemoteObject = new TestRemoteObject("testObject"); 9525 try { 9526 let descriptor = testRemoteObject.getDescriptor(); 9527 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is ' + descriptor); 9528 } catch (error) { 9529 let e: BusinessError = error as BusinessError; 9530 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorCode ' + e.code); 9531 hilog.error(0x0000, 'testTag', 'rpc get local interface fail, errorMessage ' + e.message); 9532 } 9533 ``` 9534 9535### getInterfaceDescriptor<sup>(deprecated)</sup> 9536 9537getInterfaceDescriptor(): string 9538 9539Obtains the interface descriptor. 9540 9541> **NOTE**<br/> 9542> 9543> >**NOTE**<br>This API is deprecated since API version 9. Use [getDescriptor](#getdescriptor9-2) instead. 9544 9545**System capability**: SystemCapability.Communication.IPC.Core 9546 9547**Return value** 9548 9549 | Type | Description | 9550 | ------ | ---------------- | 9551 | string | Interface descriptor obtained.| 9552 9553**Example** 9554 9555 ```ts 9556 import { hilog } from '@kit.PerformanceAnalysisKit'; 9557 9558 class MyDeathRecipient implements rpc.DeathRecipient { 9559 onRemoteDied() { 9560 hilog.info(0x0000, 'testTag', 'server died'); 9561 } 9562 } 9563 class TestRemoteObject extends rpc.RemoteObject { 9564 constructor(descriptor: string) { 9565 super(descriptor); 9566 } 9567 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9568 return true; 9569 } 9570 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9571 return true; 9572 } 9573 isObjectDead(): boolean { 9574 return false; 9575 } 9576 } 9577 let testRemoteObject = new TestRemoteObject("testObject"); 9578 let descriptor = testRemoteObject.getInterfaceDescriptor(); 9579 hilog.info(0x0000, 'testTag', 'RpcServer: descriptor is: ' + descriptor); 9580 ``` 9581 9582### modifyLocalInterface<sup>9+</sup> 9583 9584modifyLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9585 9586Binds an interface descriptor to an **IRemoteBroker** object. 9587 9588**System capability**: SystemCapability.Communication.IPC.Core 9589 9590**Parameters** 9591 9592| Name | Type | Mandatory| Description | 9593| -------------- | ------------------------------- | ---- | ------------------------------------- | 9594| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9595| descriptor | string | Yes | Interface descriptor.| 9596 9597**Error codes** 9598 9599For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9600 9601 | ID| Error Message| 9602 | -------- | -------- | 9603 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match; <br> 3.The string length exceeds 40960 bytes; <br> 4.The number of bytes copied to the buffer is different from the length of the obtained string. | 9604 9605**Example** 9606 9607 ```ts 9608 import { hilog } from '@kit.PerformanceAnalysisKit'; 9609 import { BusinessError } from '@kit.BasicServicesKit'; 9610 9611 class MyDeathRecipient implements rpc.DeathRecipient { 9612 onRemoteDied() { 9613 hilog.info(0x0000, 'testTag', 'server died'); 9614 } 9615 } 9616 class TestRemoteObject extends rpc.RemoteObject { 9617 constructor(descriptor: string) { 9618 super(descriptor); 9619 try { 9620 this.modifyLocalInterface(this, descriptor); 9621 } catch (error) { 9622 let e: BusinessError = error as BusinessError; 9623 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorCode ' + e.code); 9624 hilog.error(0x0000, 'testTag', ' rpc attach local interface fail, errorMessage ' + e.message); 9625 } 9626 } 9627 registerDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9628 // Implement the method logic based on service requirements. 9629 } 9630 unregisterDeathRecipient(recipient: MyDeathRecipient, flags: number) { 9631 // Implement the method logic based on service requirements. 9632 } 9633 isObjectDead(): boolean { 9634 return false; 9635 } 9636 asObject(): rpc.IRemoteObject { 9637 return this; 9638 } 9639 } 9640 let testRemoteObject = new TestRemoteObject("testObject"); 9641 ``` 9642 9643### attachLocalInterface<sup>(deprecated)</sup> 9644 9645attachLocalInterface(localInterface: IRemoteBroker, descriptor: string): void 9646 9647Binds an interface descriptor to an **IRemoteBroker** object. 9648 9649> **NOTE**<br/> 9650> 9651> >**NOTE**<br>This API is deprecated since API version 9. Use [modifyLocalInterface](#modifylocalinterface9) instead. 9652 9653**System capability**: SystemCapability.Communication.IPC.Core 9654 9655**Parameters** 9656 9657| Name | Type | Mandatory| Description | 9658| -------------- | ------------------------------- | ---- | ------------------------------------- | 9659| localInterface | [IRemoteBroker](#iremotebroker) | Yes | **IRemoteBroker** object. | 9660| descriptor | string | Yes | Interface descriptor.| 9661 9662**Example** 9663 9664 ```ts 9665 import { hilog } from '@kit.PerformanceAnalysisKit'; 9666 9667 class MyDeathRecipient implements rpc.DeathRecipient { 9668 onRemoteDied() { 9669 hilog.info(0x0000, 'testTag', 'server died'); 9670 } 9671 } 9672 class TestRemoteObject extends rpc.RemoteObject { 9673 constructor(descriptor: string) { 9674 super(descriptor); 9675 this.attachLocalInterface(this, descriptor); 9676 } 9677 addDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9678 return true; 9679 } 9680 removeDeathRecipient(recipient: MyDeathRecipient, flags: number): boolean { 9681 return true; 9682 } 9683 isObjectDead(): boolean { 9684 return false; 9685 } 9686 asObject(): rpc.IRemoteObject { 9687 return this; 9688 } 9689 } 9690 let testRemoteObject = new TestRemoteObject("testObject"); 9691 ``` 9692 9693## Ashmem<sup>8+</sup> 9694 9695Provides 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. 9696The shared memory applies only to cross-process communication within the local device. 9697 9698### Properties 9699 9700**System capability**: SystemCapability.Communication.IPC.Core 9701 9702 | Name | Type | Readable | Writable | Description | 9703 | ---------- | ------ | ----- | ----- |----------------------------------------- | 9704 | PROT_EXEC | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is executable. | 9705 | PROT_NONE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory cannot be accessed.| 9706 | PROT_READ | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. | 9707 | PROT_WRITE | number | Yes | No | Mapped memory protection type, indicating that the mapped memory is readable. | 9708 9709### create<sup>9+</sup> 9710 9711static create(name: string, size: number): Ashmem 9712 9713Creates an **Ashmem** object with the specified name and size. This API is a static method. 9714 9715**System capability**: SystemCapability.Communication.IPC.Core 9716 9717**Parameters** 9718 9719 | Name| Type | Mandatory| Description | 9720 | ------ | ------ | ---- | ---------------------------- | 9721 | name | string | Yes | Name of the **Ashmem** object to create. | 9722 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9723 9724**Return value** 9725 9726| Type | Description | 9727| ------------------ | ---------------------------------------------- | 9728| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9729 9730**Error codes** 9731 9732For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9733 9734 | ID| Error Message| 9735 | -------- | -------- | 9736 | 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. | 9737 9738**Example** 9739 9740 ```ts 9741 import { hilog } from '@kit.PerformanceAnalysisKit'; 9742 import { BusinessError } from '@kit.BasicServicesKit'; 9743 9744 let ashmem: rpc.Ashmem | undefined = undefined; 9745 try { 9746 ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9747 let size = ashmem.getAshmemSize(); 9748 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem + ' size is ' + size); 9749 } catch (error) { 9750 let e: BusinessError = error as BusinessError; 9751 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorCode ' + e.code); 9752 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem fail, errorMessage ' + e.message); 9753 } 9754 ``` 9755 9756### createAshmem<sup>(deprecated)</sup> 9757 9758static createAshmem(name: string, size: number): Ashmem 9759 9760Creates an **Ashmem** object with the specified name and size. This API is a static method. 9761 9762> **NOTE**<br/> 9763> 9764> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9) instead. 9765 9766**System capability**: SystemCapability.Communication.IPC.Core 9767 9768**Parameters** 9769 9770 | Name| Type | Mandatory| Description | 9771 | ------ | ------ | ---- | ---------------------------- | 9772 | name | string | Yes | Name of the **Ashmem** object to create. | 9773 | size | number | Yes | Size (in bytes) of the **Ashmem** object to create.| 9774 9775**Return value** 9776 9777| Type | Description | 9778| ------------------ | ---------------------------------------------- | 9779| [Ashmem](#ashmem8) | Returns the **Ashmem** object if it is created successfully; returns null otherwise.| 9780 9781**Example** 9782 9783 ```ts 9784 import { hilog } from '@kit.PerformanceAnalysisKit'; 9785 9786 let ashmem = rpc.Ashmem.createAshmem("ashmem", 1024*1024); 9787 let size = ashmem.getAshmemSize(); 9788 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmem: ' + ashmem + ' size is ' + size); 9789 ``` 9790 9791### create<sup>9+</sup> 9792 9793static create(ashmem: Ashmem): Ashmem 9794 9795Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9796 9797**System capability**: SystemCapability.Communication.IPC.Core 9798 9799**Parameters** 9800 9801| Name| Type | Mandatory| Description | 9802| ------ | ------------------ | ---- | -------------------- | 9803| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9804 9805**Return value** 9806 9807| Type | Description | 9808| ------------------ | ---------------------- | 9809| [Ashmem](#ashmem8) | **Ashmem** object created.| 9810 9811**Error codes** 9812 9813For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9814 9815 | ID| Error Message| 9816 | -------- | -------- | 9817 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The passed parameter is not an Ahmem object; <br> 3.The ashmem instance for obtaining packaging is empty. | 9818 9819**Example** 9820 9821 ```ts 9822 import { hilog } from '@kit.PerformanceAnalysisKit'; 9823 import { BusinessError } from '@kit.BasicServicesKit'; 9824 9825 try { 9826 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9827 let ashmem2 = rpc.Ashmem.create(ashmem); 9828 let size = ashmem2.getAshmemSize(); 9829 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by create: ' + ashmem2 + ' size is ' + size); 9830 } catch (error) { 9831 let e: BusinessError = error as BusinessError; 9832 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorCode ' + e.code); 9833 hilog.error(0x0000, 'testTag', 'Rpc creat ashmem from existing fail, errorMessage ' + e.message); 9834 } 9835 ``` 9836 9837### createAshmemFromExisting<sup>(deprecated)</sup> 9838 9839static createAshmemFromExisting(ashmem: Ashmem): Ashmem 9840 9841Creates an **Ashmem** object by copying the file descriptor of an existing **Ashmem** object. The two **Ashmem** objects point to the same shared memory region. 9842 9843> **NOTE**<br/> 9844> 9845> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [create](#create9-1) instead. 9846 9847**System capability**: SystemCapability.Communication.IPC.Core 9848 9849**Parameters** 9850 9851| Name| Type | Mandatory| Description | 9852| ------ | ------------------ | ---- | -------------------- | 9853| ashmem | [Ashmem](#ashmem8) | Yes | Existing **Ashmem** object.| 9854 9855**Return value** 9856 9857| Type | Description | 9858| ------------------ | ---------------------- | 9859| [Ashmem](#ashmem8) | **Ashmem** object created.| 9860 9861**Example** 9862 9863 ```ts 9864 import { hilog } from '@kit.PerformanceAnalysisKit'; 9865 9866 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9867 let ashmem2 = rpc.Ashmem.createAshmemFromExisting(ashmem); 9868 let size = ashmem2.getAshmemSize(); 9869 hilog.info(0x0000, 'testTag', 'RpcTest: get ashemm by createAshmemFromExisting: ' + ashmem2 + ' size is ' + size); 9870 ``` 9871 9872### closeAshmem<sup>8+</sup> 9873 9874closeAshmem(): void 9875 9876Closes this **Ashmem** object. 9877 9878> **NOTE**<br/> 9879> 9880> Before closing the **Ashmem** object, you need to remove the address mapping. 9881 9882**System capability**: SystemCapability.Communication.IPC.Core 9883 9884**Example** 9885 9886 ```ts 9887 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9888 ashmem.closeAshmem(); 9889 ``` 9890 9891### unmapAshmem<sup>8+</sup> 9892 9893unmapAshmem(): void 9894 9895Deletes the mappings for the specified address range of this **Ashmem** object. 9896 9897**System capability**: SystemCapability.Communication.IPC.Core 9898 9899**Example** 9900 9901 ```ts 9902 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9903 ashmem.unmapAshmem(); 9904 ``` 9905 9906### getAshmemSize<sup>8+</sup> 9907 9908getAshmemSize(): number 9909 9910Obtains the memory size of this **Ashmem** object. 9911 9912**System capability**: SystemCapability.Communication.IPC.Core 9913 9914**Return value** 9915 9916 | Type | Description | 9917 | ------ | -------------------------- | 9918 | number | **Ashmem** size obtained.| 9919 9920**Example** 9921 9922 ```ts 9923 import { hilog } from '@kit.PerformanceAnalysisKit'; 9924 9925 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9926 let size = ashmem.getAshmemSize(); 9927 hilog.info(0x0000, 'testTag', 'RpcTest: get ashmem is ' + ashmem + ' size is ' + size); 9928 ``` 9929 9930### mapTypedAshmem<sup>9+</sup> 9931 9932mapTypedAshmem(mapType: number): void 9933 9934Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9935 9936**System capability**: SystemCapability.Communication.IPC.Core 9937 9938**Parameters** 9939 9940 | Name | Type | Mandatory| Description | 9941 | ------- | ------ | ---- | ------------------------------ | 9942 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9943 9944**Error codes** 9945 9946For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 9947 9948 | ID| Error Message| 9949 | -------- | -------- | 9950 | 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. | 9951 | 1900001 | Failed to call mmap. | 9952 9953**Example** 9954 9955 ```ts 9956 import { hilog } from '@kit.PerformanceAnalysisKit'; 9957 import { BusinessError } from '@kit.BasicServicesKit'; 9958 9959 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9960 try { 9961 ashmem.mapTypedAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 9962 } catch (error) { 9963 let e: BusinessError = error as BusinessError; 9964 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorCode ' + e.code); 9965 hilog.error(0x0000, 'testTag', 'Rpc map ashmem fail, errorMessage ' + e.message); 9966 } 9967 ``` 9968 9969### mapAshmem<sup>(deprecated)</sup> 9970 9971mapAshmem(mapType: number): boolean 9972 9973Creates the shared file mapping on the virtual address space of this process. The size of the mapping region is specified by this **Ashmem** object. 9974 9975> **NOTE**<br/> 9976> 9977> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapTypedAshmem](#maptypedashmem9) instead. 9978 9979**System capability**: SystemCapability.Communication.IPC.Core 9980 9981**Parameters** 9982 9983 | Name | Type | Mandatory| Description | 9984 | ------- | ------ | ---- | ------------------------------ | 9985 | mapType | number | Yes | Protection level of the memory region to which the shared file is mapped.| 9986 9987**Return value** 9988 9989 | Type | Description | 9990 | ------- | -------------------------------- | 9991 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 9992 9993**Example** 9994 9995 ```ts 9996 import { hilog } from '@kit.PerformanceAnalysisKit'; 9997 9998 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 9999 let mapReadAndWrite = ashmem.mapAshmem(ashmem.PROT_READ | ashmem.PROT_WRITE); 10000 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapReadAndWrite); 10001 ``` 10002 10003### mapReadWriteAshmem<sup>9+</sup> 10004 10005mapReadWriteAshmem(): void 10006 10007Maps the shared file to the readable and writable virtual address space of the process. 10008 10009**System capability**: SystemCapability.Communication.IPC.Core 10010 10011**Error codes** 10012 10013For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10014 10015 | ID| Error Message| 10016 | -------- | -------- | 10017 | 1900001 | Failed to call mmap. | 10018 10019**Example** 10020 10021 ```ts 10022 import { hilog } from '@kit.PerformanceAnalysisKit'; 10023 import { BusinessError } from '@kit.BasicServicesKit'; 10024 10025 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10026 try { 10027 ashmem.mapReadWriteAshmem(); 10028 } catch (error) { 10029 let e: BusinessError = error as BusinessError; 10030 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 10031 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 10032 } 10033 ``` 10034 10035### mapReadAndWriteAshmem<sup>(deprecated)</sup> 10036 10037mapReadAndWriteAshmem(): boolean 10038 10039Maps the shared file to the readable and writable virtual address space of the process. 10040 10041> **NOTE**<br/> 10042> 10043> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapReadWriteAshmem](#mapreadwriteashmem9) instead. 10044 10045**System capability**: SystemCapability.Communication.IPC.Core 10046 10047**Return value** 10048 10049 | Type | Description | 10050 | ------- | -------------------------------- | 10051 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 10052 10053**Example** 10054 10055 ```ts 10056 import { hilog } from '@kit.PerformanceAnalysisKit'; 10057 10058 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10059 let mapResult = ashmem.mapReadAndWriteAshmem(); 10060 hilog.info(0x0000, 'testTag', 'RpcTest: map ashmem result is ' + mapResult); 10061 ``` 10062 10063### mapReadonlyAshmem<sup>9+</sup> 10064 10065mapReadonlyAshmem(): void 10066 10067Maps the shared file to the read-only virtual address space of the process. 10068 10069**System capability**: SystemCapability.Communication.IPC.Core 10070 10071**Error codes** 10072 10073For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10074 10075 | ID| Error Message| 10076 | -------- | -------- | 10077 | 1900001 | Failed to call mmap. | 10078 10079**Example** 10080 10081 ```ts 10082 import { hilog } from '@kit.PerformanceAnalysisKit'; 10083 import { BusinessError } from '@kit.BasicServicesKit'; 10084 10085 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10086 try { 10087 ashmem.mapReadonlyAshmem(); 10088 } catch (error) { 10089 let e: BusinessError = error as BusinessError; 10090 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorCode ' + e.code); 10091 hilog.error(0x0000, 'testTag', 'Rpc map read and write ashmem fail, errorMessage ' + e.message); 10092 } 10093 ``` 10094 10095### mapReadOnlyAshmem<sup>(deprecated)</sup> 10096 10097mapReadOnlyAshmem(): boolean 10098 10099Maps the shared file to the read-only virtual address space of the process. 10100 10101> **NOTE**<br/> 10102> 10103> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [mapReadonlyAshmem](#mapreadonlyashmem9) instead. 10104 10105**System capability**: SystemCapability.Communication.IPC.Core 10106 10107**Return value** 10108 10109 | Type | Description | 10110 | ------- | -------------------------------- | 10111 | boolean | Returns **true** if the mapping is created; returns **false** otherwise.| 10112 10113**Example** 10114 10115 ```ts 10116 import { hilog } from '@kit.PerformanceAnalysisKit'; 10117 10118 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10119 let mapResult = ashmem.mapReadOnlyAshmem(); 10120 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem mapReadOnlyAshmem result is ' + mapResult); 10121 ``` 10122 10123### setProtectionType<sup>9+</sup> 10124 10125setProtectionType(protectionType: number): void 10126 10127Sets the protection level of the memory region to which the shared file is mapped. 10128 10129**System capability**: SystemCapability.Communication.IPC.Core 10130 10131**Parameters** 10132 10133 | Name | Type | Mandatory| Description | 10134 | -------------- | ------ | ---- | ------------------ | 10135 | protectionType | number | Yes | Protection type to set.| 10136 10137**Error codes** 10138 10139For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10140 10141 | ID| Error Message| 10142 | -------- | -------- | 10143 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10144 | 1900002 | Failed to call ioctl. | 10145 10146**Example** 10147 10148 ```ts 10149 import { hilog } from '@kit.PerformanceAnalysisKit'; 10150 import { BusinessError } from '@kit.BasicServicesKit'; 10151 10152 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10153 try { 10154 ashmem.setProtectionType(ashmem.PROT_READ); 10155 } catch (error) { 10156 let e: BusinessError = error as BusinessError; 10157 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorCode ' + e.code); 10158 hilog.error(0x0000, 'testTag', 'Rpc set protection type fail, errorMessage ' + e.message); 10159 } 10160 ``` 10161 10162### setProtection<sup>(deprecated)</sup> 10163 10164setProtection(protectionType: number): boolean 10165 10166Sets the protection level of the memory region to which the shared file is mapped. 10167 10168> **NOTE**<br/> 10169> 10170> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [setProtectionType](#setprotectiontype9) instead. 10171 10172**System capability**: SystemCapability.Communication.IPC.Core 10173 10174**Parameters** 10175 10176 | Name | Type | Mandatory| Description | 10177 | -------------- | ------ | ---- | ------------------ | 10178 | protectionType | number | Yes | Protection type to set.| 10179 10180**Return value** 10181 10182 | Type | Description | 10183 | ------- | -------------------------------- | 10184 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 10185 10186**Example** 10187 10188 ```ts 10189 import { hilog } from '@kit.PerformanceAnalysisKit'; 10190 10191 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10192 let result = ashmem.setProtection(ashmem.PROT_READ); 10193 hilog.info(0x0000, 'testTag', 'RpcTest: Ashmem setProtection result is ' + result); 10194 ``` 10195 10196### writeDataToAshmem<sup>11+</sup> 10197 10198writeDataToAshmem(buf: ArrayBuffer, size: number, offset: number): void 10199 10200Writes data to the shared file associated with this **Ashmem** object. 10201 10202> **NOTE**<br/> 10203> 10204> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10205 10206**System capability**: SystemCapability.Communication.IPC.Core 10207 10208**Parameters** 10209 10210 | Name| Type | Mandatory| Description | 10211 | ------ | -------- | ---- | -------------------------------------------------- | 10212 | buf | ArrayBuffer | Yes | Data to write. | 10213 | size | number | Yes | Size of the data to write. | 10214 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10215 10216**Error codes** 10217 10218For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10219 10220 | ID| Error Message| 10221 | -------- | -------- | 10222 | 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. | 10223 | 1900003 | Failed to write data to the shared memory. | 10224 10225**Example** 10226 10227 ```ts 10228 import { hilog } from '@kit.PerformanceAnalysisKit'; 10229 import { BusinessError } from '@kit.BasicServicesKit'; 10230 10231 let buffer = new ArrayBuffer(1024); 10232 let int32View = new Int32Array(buffer); 10233 for (let i = 0; i < int32View.length; i++) { 10234 int32View[i] = i * 2 + 1; 10235 } 10236 let size = buffer.byteLength; 10237 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10238 ashmem.mapReadWriteAshmem(); 10239 try { 10240 ashmem.writeDataToAshmem(buffer, size, 0); 10241 } catch (error) { 10242 let e: BusinessError = error as BusinessError; 10243 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10244 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10245 } 10246 ``` 10247 10248### writeAshmem<sup>(deprecated)</sup> 10249 10250writeAshmem(buf: number[], size: number, offset: number): void 10251 10252Writes data to the shared file associated with this **Ashmem** object. 10253 10254> **NOTE**<br/> 10255> 10256> >**NOTE**<br>This API is supported since API version 9 and deprecated since API version 11. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10257> 10258> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10259 10260**System capability**: SystemCapability.Communication.IPC.Core 10261 10262**Parameters** 10263 10264 | Name| Type | Mandatory| Description | 10265 | ------ | -------- | ---- | -------------------------------------------------- | 10266 | buf | number[] | Yes | Data to write. | 10267 | size | number | Yes | Size of the data to write. | 10268 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10269 10270**Error codes** 10271 10272For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10273 10274 | ID| Error Message| 10275 | -------- | -------- | 10276 | 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. | 10277 | 1900003 | Failed to write data to the shared memory. | 10278 10279**Example** 10280 10281 ```ts 10282 import { hilog } from '@kit.PerformanceAnalysisKit'; 10283 import { BusinessError } from '@kit.BasicServicesKit'; 10284 10285 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10286 ashmem.mapReadWriteAshmem(); 10287 let ByteArrayVar = [1, 2, 3, 4, 5]; 10288 try { 10289 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10290 } catch (error) { 10291 let e: BusinessError = error as BusinessError; 10292 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10293 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10294 } 10295 ``` 10296 10297### writeToAshmem<sup>(deprecated)</sup> 10298 10299writeToAshmem(buf: number[], size: number, offset: number): boolean 10300 10301Writes data to the shared file associated with this **Ashmem** object. 10302 10303> **NOTE**<br/> 10304> 10305> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [writeDataToAshmem](#writedatatoashmem11) instead. 10306> 10307> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10308 10309**System capability**: SystemCapability.Communication.IPC.Core 10310 10311**Parameters** 10312 10313 | Name| Type | Mandatory| Description | 10314 | ------ | -------- | ---- | -------------------------------------------------- | 10315 | buf | number[] | Yes | Data to write. | 10316 | size | number | Yes | Size of the data to write. | 10317 | offset | number | Yes | Start position of the data to write in the memory region associated with this **Ashmem** object.| 10318 10319**Return value** 10320 10321 | Type | Description | 10322 | ------- | ----------------------------------------------------------------------------- | 10323 | boolean | Returns **true** if the data is written successfully; returns **false** otherwise.| 10324 10325**Example** 10326 10327 ```ts 10328 import { hilog } from '@kit.PerformanceAnalysisKit'; 10329 10330 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10331 let mapResult = ashmem.mapReadAndWriteAshmem(); 10332 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10333 let ByteArrayVar = [1, 2, 3, 4, 5]; 10334 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10335 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10336 ``` 10337 10338### readDataFromAshmem<sup>11+</sup> 10339 10340readDataFromAshmem(size: number, offset: number): ArrayBuffer 10341 10342Reads data from the shared file associated with this **Ashmem** object. 10343 10344> **NOTE**<br/> 10345> 10346> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10347 10348**System capability**: SystemCapability.Communication.IPC.Core 10349 10350**Parameters** 10351 10352 | Name| Type | Mandatory| Description | 10353 | ------ | ------ | ---- | -------------------------------------------------- | 10354 | size | number | Yes | Size of the data to read. | 10355 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10356 10357**Return value** 10358 10359 | Type | Description | 10360 | -------- | ---------------- | 10361 | ArrayBuffer | Data read.| 10362 10363**Error codes** 10364 10365For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10366 10367 | ID| Error Message| 10368 | -------- | -------- | 10369 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10370 | 1900004 | Failed to read data from the shared memory. | 10371 10372**Example** 10373 10374 ```ts 10375 import { hilog } from '@kit.PerformanceAnalysisKit'; 10376 import { BusinessError } from '@kit.BasicServicesKit'; 10377 10378 let buffer = new ArrayBuffer(1024); 10379 let int32View = new Int32Array(buffer); 10380 for (let i = 0; i < int32View.length; i++) { 10381 int32View[i] = i * 2 + 1; 10382 } 10383 let size = buffer.byteLength; 10384 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10385 ashmem.mapReadWriteAshmem(); 10386 try { 10387 ashmem.writeDataToAshmem(buffer, size, 0); 10388 } catch (error) { 10389 let e: BusinessError = error as BusinessError; 10390 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorCode ' + e.code); 10391 hilog.error(0x0000, 'testTag', 'Rpc write to ashmem fail, errorMessage ' + e.message); 10392 } 10393 try { 10394 let readResult = ashmem.readDataFromAshmem(size, 0); 10395 let readInt32View = new Int32Array(readResult); 10396 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readInt32View); 10397 } catch (error) { 10398 let e: BusinessError = error as BusinessError; 10399 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10400 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10401 } 10402 ``` 10403 10404### readAshmem<sup>(deprecated)</sup> 10405 10406readAshmem(size: number, offset: number): number[] 10407 10408Reads data from the shared file associated with this **Ashmem** object. 10409 10410> **NOTE**<br/> 10411> 10412> >**NOTE**<br>This API is supported since API version 9 and deprecated since API version 11. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10413> 10414> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10415 10416 10417**System capability**: SystemCapability.Communication.IPC.Core 10418 10419**Parameters** 10420 10421 | Name| Type | Mandatory| Description | 10422 | ------ | ------ | ---- | -------------------------------------------------- | 10423 | size | number | Yes | Size of the data to read. | 10424 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10425 10426**Return value** 10427 10428 | Type | Description | 10429 | -------- | ---------------- | 10430 | number[] | Data read.| 10431 10432**Error codes** 10433 10434For details about the error codes, see [RPC Error Codes](errorcode-rpc.md). 10435 10436 | ID| Error Message| 10437 | -------- | -------- | 10438 | 401 | Parameter error. Possible causes: <br> 1.The number of parameters is incorrect; <br> 2.The parameter type does not match. | 10439 | 1900004 | Failed to read data from the shared memory. | 10440 10441**Example** 10442 10443 ```ts 10444 import { hilog } from '@kit.PerformanceAnalysisKit'; 10445 import { BusinessError } from '@kit.BasicServicesKit'; 10446 10447 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10448 ashmem.mapReadWriteAshmem(); 10449 let ByteArrayVar = [1, 2, 3, 4, 5]; 10450 ashmem.writeAshmem(ByteArrayVar, 5, 0); 10451 try { 10452 let readResult = ashmem.readAshmem(5, 0); 10453 hilog.info(0x0000, 'testTag', 'RpcTest: read from Ashmem result is ' + readResult); 10454 } catch (error) { 10455 let e: BusinessError = error as BusinessError; 10456 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorCode ' + e.code); 10457 hilog.error(0x0000, 'testTag', 'Rpc read from ashmem fail, errorMessage ' + e.message); 10458 } 10459 ``` 10460 10461### readFromAshmem<sup>(deprecated)</sup> 10462 10463readFromAshmem(size: number, offset: number): number[] 10464 10465Reads data from the shared file associated with this **Ashmem** object. 10466 10467> **NOTE**<br/> 10468> 10469> >**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. Use [readDataFromAshmem](#readdatafromashmem11) instead. 10470> 10471> Before writing an **Ashmem** object, you need to call [mapReadWriteAshmem](#mapreadwriteashmem9) for mapping. 10472 10473**System capability**: SystemCapability.Communication.IPC.Core 10474 10475**Parameters** 10476 10477 | Name| Type | Mandatory| Description | 10478 | ------ | ------ | ---- | -------------------------------------------------- | 10479 | size | number | Yes | Size of the data to read. | 10480 | offset | number | Yes | Start position of the data to read in the memory region associated with this **Ashmem** object.| 10481 10482**Return value** 10483 10484 | Type | Description | 10485 | -------- | ---------------- | 10486 | number[] | Data read.| 10487 10488**Example** 10489 10490 ``` ts 10491 import { hilog } from '@kit.PerformanceAnalysisKit'; 10492 10493 let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); 10494 let mapResult = ashmem.mapReadAndWriteAshmem(); 10495 hilog.info(0x0000, 'testTag', 'RpcTest map ashmem result is ' + mapResult); 10496 let ByteArrayVar = [1, 2, 3, 4, 5]; 10497 let writeResult = ashmem.writeToAshmem(ByteArrayVar, 5, 0); 10498 hilog.info(0x0000, 'testTag', 'RpcTest: write to Ashmem result is ' + writeResult); 10499 let readResult = ashmem.readFromAshmem(5, 0); 10500 hilog.info(0x0000, 'testTag', 'RpcTest: read to Ashmem result is ' + readResult); 10501 ``` 10502