1# @ohos.secureElement (SE Management) 2 3The **secureElement** module provides APIs for operating and managing the SecureElement (SE). The SE service mentioned in this document is an **SEService** instance. For details, see [newSEService](#secureelementnewseservice). 4 5The instances of the following classes are involved in this document. 6 7| Class | Description | 8| ------- | ---------------------------------------------- | 9| Session | A **Session** instance represents a session for connecting to an available SE on the device.| 10| Reader | A **Reader** instance represents an SE reader supported by the device. | 11| Channel | A **Channel** instance represents an ISO/IEC 7816-4 channel opened to the SE. | 12 13> **NOTE** 14> 15> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 16 17## **Modules to Import** 18 19```js 20import secureElement from '@ohos.secureElement'; 21``` 22 23## secureElement.ServiceState 24 25Defines the SE service status values. 26 27**System capability**: SystemCapability.Communication.SecureElement 28 29| Name | Value | Description | 30| ------------ | ---- | ------------------ | 31| DISCONNECTED | 0 | The SE service is disconnected.| 32| CONNECTED | 1 | The SE service is connected.| 33 34## secureElement.newSEService 35 36newSEService(type: 'serviceState', callback: Callback<[ServiceState](#secureelementservicestate)>): SEService 37 38Creates an **SEService** instance for connecting to all available SEs in the system. The connection is time-consuming. Therefore, this API supports only the asynchronous mode. 39 40The returned **SEService** object is available only when **true** is returned by the specified callback or [isConnected](#seserviceisconnected). 41 42**System capability**: SystemCapability.Communication.SecureElement 43 44**Parameters** 45 46| **Name**| **Type** | **Mandatory**| **Description** | 47| ---------- | ---------------------------------------------------- | ------ | -------------------- | 48| type | string | Yes | 'serviceState' | 49| callback | Callback<[ServiceState](#secureelementservicestate)> | Yes | Callback invoked to return the SE service status.| 50 51**Return value** 52 53| **Type** | **Description** | 54| :-------- | :--------- | 55| SEService | Returns the **SEService** instance created.| 56 57**Example** 58 59```js 60import secureElement from '@ohos.secureElement'; 61import { BusinessError } from '@ohos.base'; 62 63try { 64 let nfcSEService = secureElement.newSEService("serviceState", (state) => { 65 if (state == secureElement.ServiceState.DISCONNECTED) { 66 console.log("Service state is Disconnected"); 67 } else { 68 console.log("Service state is Connected"); 69 } 70 }); 71} catch (e) { 72 console.log("newSEService occurs " + "exception: ${(e : BusinessError).message}"); 73} 74``` 75 76## SEService.getReaders 77 78getReaders(): Reader[] 79 80Obtains the available SE readers. The returned array cannot contain duplicate objects. Even if no card is inserted, all available readers should be listed. 81 82**System capability**: SystemCapability.Communication.SecureElement 83 84**Return value** 85 86| **Type**| **Description** | 87| :------- | :--------------------- | 88| Reader[] | Returns an array of available **Reader** objects.| 89 90**Example** 91 92```js 93import omapi from '@ohos.secureElement'; 94import secureElement from '@ohos.secureElement'; 95import { BusinessError } from '@ohos.base'; 96 97let nfcSEService : omapi.SEService | null = null; 98let nfcOmaReaderList : omapi.Reader[] | null = null; 99 100try { 101 nfcSEService = secureElement.newSEService("serviceState", (state) => { 102 if (state == secureElement.ServiceState.DISCONNECTED) { 103 console.log("Service state is Disconnected"); 104 } else { 105 console.log("Service state is Connected"); 106 } 107 }); 108} catch (e) { 109 console.log("newSEService " + "excpetion: ${(e : BusinessError).message}"); 110} 111 112try { 113 if(nfcSEService != null) { 114 nfcOmaReaderList = nfcSEService.getReaders(); 115 } 116 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 117 console.log("get reader successfully"); 118 } else { 119 console.log("get reader failed"); 120 } 121} catch (e) { 122 console.log("getReaders " + "exception: ${(e : BusinessError).message}"); 123} 124``` 125 126## SEService.isConnected 127 128isConnected(): boolean 129 130Checks whether this SE service is connected. 131 132**System capability**: SystemCapability.Communication.SecureElement 133 134**Return value** 135 136| **Type**| **Description** | 137| :------- | :--------------------------------------------- | 138| boolean | Returns **true** if the SE service is connected; returns **false** otherwise.| 139 140**Example** 141 142```JS 143import omapi from '@ohos.secureElement'; 144import secureElement from '@ohos.secureElement'; 145import { BusinessError } from '@ohos.base'; 146 147let nfcSEService : omapi.SEService | null = null; 148 149try { 150 nfcSEService = secureElement.newSEService("serviceState", (state) => { 151 if (state == secureElement.ServiceState.DISCONNECTED) { 152 console.log("Service state is Disconnected"); 153 } else { 154 console.log("Service state is Connected"); 155 } 156 }); 157} catch (e) { 158 console.log("newSEService" + "exception: ${(e : BusinessError).message}"); 159} 160 161try { 162 let ret: boolean = false; 163 // Refer to newSEService for this.nfcSEService. 164 if (nfcSEService != null) { 165 ret = nfcSEService.isConnected(); 166 } 167 if (ret) { 168 console.log("get state: connected"); 169 } else { 170 console.log("get state: not connected"); 171 } 172} catch (e) { 173 console.log("isConnected " + "exception: ${(e : BusinessError).message}"); 174} 175``` 176 177## SEService.shutdown 178 179shutdown(): void 180 181Releases all SE resources allocated to this service. After that, [isConnected](#seserviceisconnected) returns **false**. 182 183**System capability**: SystemCapability.Communication.SecureElement 184 185**Example** 186 187```js 188import omapi from '@ohos.secureElement'; 189import secureElement from '@ohos.secureElement'; 190import { BusinessError } from '@ohos.base'; 191 192let nfcSEService : omapi.SEService | null = null; 193 194try { 195 nfcSEService = secureElement.newSEService("serviceState", (state) => { 196 if (state == secureElement.ServiceState.DISCONNECTED) { 197 console.log("Service state is Disconnected"); 198 } else { 199 console.log("Service state is Connected"); 200 } 201 }); 202} catch (e) { 203 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 204} 205 206try { 207 // Refer to newSEService for this.nfcSEService. 208 if (nfcSEService != null) { 209 nfcSEService.shutdown(); 210 } 211 console.log("shutdown successfully"); 212} catch (e) { 213 console.log("shutdown exception:" + "exception: ${(e : BusinessError).message}"); 214} 215``` 216 217## SEService.getVersion 218 219getVersion(): string 220 221Obtains the version of the Open Mobile API Specification used for the implementation. 222 223**System capability**: SystemCapability.Communication.SecureElement 224 225**Return value** 226 227| **Type**| **Description** | 228| -------- | -------------------------------------------------- | 229| string | Returns the OMA version. For example, **3.3** indicates Open Mobile API Specification v3.3.| 230 231**Example** 232 233```JS 234import omapi from '@ohos.secureElement'; 235import secureElement from '@ohos.secureElement'; 236import { BusinessError } from '@ohos.base'; 237 238let nfcSEService : omapi.SEService | null = null; 239 240try { 241 nfcSEService = secureElement.newSEService("serviceState", (state) => { 242 if (state == secureElement.ServiceState.DISCONNECTED) { 243 console.log("Service state is Disconnected"); 244 } else { 245 console.log("Service state is Connected"); 246 } 247 }); 248} catch (e) { 249 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 250} 251 252try { 253 // Refer to newSEService for this.nfcSEService. 254 if (nfcSEService != null) { 255 console.log("version: " + nfcSEService.getVersion()); 256 } 257} catch (e) { 258 console.log("getVersion " + "exception: ${(e : BusinessError).message}"); 259} 260``` 261 262## Reader.getName 263 264getName(): string 265 266Obtains the reader name. If the card reader is a SIM reader, its name must be in **SIM[Slot]** format. If the card reader is an embedded SE reader, its name must be in **eSE[slot]** format. 267 268**System capability**: SystemCapability.Communication.SecureElement 269 270**Return value** 271 272| **Type**| **Description** | 273| -------- | ---------- | 274| string | Returns the reader name obtained.| 275 276**Example** 277 278```js 279import omapi from '@ohos.secureElement'; 280import secureElement from '@ohos.secureElement'; 281import { BusinessError } from '@ohos.base'; 282 283let nfcSEService : omapi.SEService | null = null; 284let nfcOmaReaderList : omapi.Reader[] | null = null; 285 286try { 287 nfcSEService = secureElement.newSEService("serviceState", (state) => { 288 if (state == secureElement.ServiceState.DISCONNECTED) { 289 console.log("Service state is Disconnected"); 290 } else { 291 console.log("Service state is Connected"); 292 } 293 }); 294} catch (e) { 295 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 296} 297 298try { 299 if(nfcSEService != null) { 300 nfcOmaReaderList = nfcSEService.getReaders(); 301 } 302 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 303 console.log(nfcOmaReaderList[0].getName()); 304 } else { 305 console.log("getName failed"); 306 } 307} catch (e) { 308 console.log("getName " + "exception: ${(e : BusinessError).message}"); 309} 310``` 311 312## Reader.isSecureElementPresent 313 314isSecureElementPresent(): boolean 315 316Checks whether the SE corresponding to this reader is available. 317 318**System capability**: SystemCapability.Communication.SecureElement 319 320**Return value** 321 322| **Type**| **Description** | 323| -------- | -------------------------------------------- | 324| boolean | Returns **true** if the SE is available; returns **false** otherwise.| 325 326**Error codes** 327 328For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 329 330| ID| Error Message | 331| -------- | -------------------------------- | 332| 3300101 | IllegalStateError, service state exception. | 333 334**Example** 335 336```js 337 338import omapi from '@ohos.secureElement'; 339import secureElement from '@ohos.secureElement'; 340import { BusinessError } from '@ohos.base'; 341 342let nfcSEService : omapi.SEService | null = null; 343let nfcOmaReaderList : omapi.Reader[] | null = null; 344 345try { 346 nfcSEService = secureElement.newSEService("serviceState", (state) => { 347 if (state == secureElement.ServiceState.DISCONNECTED) { 348 console.log("Service state is Disconnected"); 349 } else { 350 console.log("Service state is Connected"); 351 } 352 }); 353} catch (e) { 354 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 355} 356 357try { 358 if(nfcSEService != null) { 359 nfcOmaReaderList = nfcSEService.getReaders(); 360 } 361 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 362 if (nfcOmaReaderList[0].isSecureElementPresent()) { 363 console.log("isSecureElementPresent success"); 364 } else { 365 console.log("isSecureElementPresent failed"); 366 } 367 } else { 368 console.log("isSecureElementPresent failed"); 369 } 370} catch (e) { 371 console.log("isSecureElementPresent " + "exception: ${(e : BusinessError).message}"); 372} 373``` 374 375## Reader.openSession 376 377 openSession(): Session 378 379Connects to the SE of this reader. This API initializes the SE for communication before returning the session object. Multiple sessions may be opened on a reader at the same time. 380 381**System capability**: SystemCapability.Communication.SecureElement 382 383**Return value** 384 385| **Type**| **Description** | 386| -------- | ------------------------------ | 387| Session | Returns the **Session** object used to create a channel.| 388 389**Error codes** 390 391For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 392 393| ID| Error Message | 394| -------- | -------------------------------- | 395| 3300101 | IllegalStateError, service state exception. | 396| 3300104 | IOError, there is a communication problem to the reader or the SE. | 397 398**Example** 399 400```js 401import omapi from '@ohos.secureElement'; 402import secureElement from '@ohos.secureElement'; 403import { BusinessError } from '@ohos.base'; 404 405let nfcSEService : omapi.SEService | null = null; 406let nfcOmaReaderList : omapi.Reader[] | null = null; 407 408try { 409 nfcSEService = secureElement.newSEService("serviceState", (state) => { 410 if (state == secureElement.ServiceState.DISCONNECTED) { 411 console.log("Service state is Disconnected"); 412 } else { 413 console.log("Service state is Connected"); 414 } 415 }); 416} catch (e) { 417 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 418} 419 420try { 421 if(nfcSEService != null) { 422 nfcOmaReaderList = nfcSEService.getReaders(); 423 } 424 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 425 if (nfcOmaReaderList[0].openSession()) { 426 console.log("get session successfully"); 427 } else { 428 console.log("get session failed"); 429 } 430 } else { 431 console.log("OpenSession failed"); 432 } 433} catch (e) { 434 console.log("OpenSession " + "exception: ${(e : BusinessError).message}"); 435} 436``` 437 438## Reader.closeSessions 439 440 closeSessions(): void 441 442Closes all sessions opened on this reader. This API closes all channels opened by these sessions. 443 444**System capability**: SystemCapability.Communication.SecureElement 445 446**Error codes** 447 448For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 449 450| ID| Error Message | 451| -------- | -------------------------------- | 452| 3300101 | IllegalStateError, service state exception. | 453 454**Example** 455 456```js 457import omapi from '@ohos.secureElement'; 458import secureElement from '@ohos.secureElement'; 459import { BusinessError } from '@ohos.base'; 460 461let nfcSEService : omapi.SEService | null = null; 462let nfcOmaReaderList : omapi.Reader[] | null = null; 463 464try { 465 nfcSEService = secureElement.newSEService("serviceState", (state) => { 466 if (state == secureElement.ServiceState.DISCONNECTED) { 467 console.log("Service state is Disconnected"); 468 } else { 469 console.log("Service state is Connected"); 470 } 471 }); 472} catch (e) { 473 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 474} 475 476try { 477 if(nfcSEService != null) { 478 nfcOmaReaderList = nfcSEService.getReaders(); 479 } 480 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 481 if (nfcOmaReaderList[0].closeSessions()) { 482 console.log("closeSessions successfully"); 483 } else { 484 console.log("closeSessions failed"); 485 } 486 } else { 487 console.log("closeSessions failed"); 488 } 489} catch (e) { 490 console.log("closeSessions " + "exception: ${(e : BusinessError).message}"); 491} 492``` 493 494## Session.getReader 495 496getReader(): Reader 497 498Obtains the reader that provides this session. 499 500**System capability**: SystemCapability.Communication.SecureElement 501 502**Return value** 503 504| **Type**| **Description** | 505| -------- | --------------------------- | 506| Reader | Returns the **Reader** object obtained.| 507 508**Example** 509 510```js 511import omapi from '@ohos.secureElement'; 512import secureElement from '@ohos.secureElement'; 513import { BusinessError } from '@ohos.base'; 514 515let nfcSEService : omapi.SEService | null = null; 516let nfcOmaReaderList : omapi.Reader[] | null = null; 517let omaSession : omapi.Session | null = null; 518 519try { 520 nfcSEService = secureElement.newSEService("serviceState", (state) => { 521 if (state == secureElement.ServiceState.DISCONNECTED) { 522 console.log("Service state is Disconnected"); 523 } else { 524 console.log("Service state is Connected"); 525 } 526 }); 527} catch (e) { 528 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 529} 530 531try { 532 if(nfcSEService != null) { 533 nfcOmaReaderList = nfcSEService.getReaders(); 534 } 535 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 536 omaSession = nfcOmaReaderList[0].openSession(); 537 if (omaSession.getReader() != null) { 538 console.log("get reader successfully"); 539 } else { 540 console.log("get reader failed"); 541 } 542 } else { 543 console.log("getReader failed"); 544 } 545} catch (e) { 546 console.log("getReader " + "exception: ${(e : BusinessError).message}"); 547} 548``` 549 550## Session.getATR 551 552getATR(): number[] 553 554Obtains the ATR of this SE. If the ATR of this SE is not available, an empty array will be returned. 555 556**System capability**: SystemCapability.Communication.SecureElement 557 558**Return value** 559 560| **Type**| **Description** | 561| -------- | -------------------------------------------- | 562| number[] | Returns the ATR obtained if the SE has an available ATR; returns an empty array otherwise.| 563 564**Error codes** 565 566For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 567 568| ID| Error Message | 569| -------- | -------------------------------- | 570| 3300101 | IllegalStateError, service state exception. | 571 572**Example** 573 574```js 575import omapi from '@ohos.secureElement'; 576import secureElement from '@ohos.secureElement'; 577import { BusinessError } from '@ohos.base'; 578 579let nfcSEService : omapi.SEService | null = null; 580let nfcOmaReaderList : omapi.Reader[] | null = null; 581let omaSession : omapi.Session | null = null; 582let omaATR : number[] | null = null; 583let str : string = ""; 584 585try { 586 nfcSEService = secureElement.newSEService("serviceState", (state) => { 587 if (state == secureElement.ServiceState.DISCONNECTED) { 588 console.log("Service state is Disconnected"); 589 } else { 590 console.log("Service state is Connected"); 591 } 592 }); 593} catch (e) { 594 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 595} 596 597try { 598 if(nfcSEService != null) { 599 nfcOmaReaderList = nfcSEService.getReaders(); 600 } 601 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 602 omaSession = nfcOmaReaderList[0].openSession(); 603 if (omaSession != null) { 604 omaATR = omaSession.getATR(); 605 } else { 606 console.log("getATR failed"); 607 } 608 } 609 if (omaATR != null && omaATR.length > 0) { 610 str = 'getATR result:['; 611 for (let i = 0; i < omaATR.length; ++i) { 612 str += omaATR[i]; 613 tr += ' '; 614 } 615 str += ']'; 616 console.log(str); 617 } else { 618 console.log("getATR failed"); 619 } 620} catch (e) { 621 console.log("getATR " + "exception: ${(e : BusinessError).message}"); 622} 623``` 624 625## Session.close 626 627close(): void 628 629Closes the connection with this SE. This API closes all channels opened between this application and the SE. 630 631**System capability**: SystemCapability.Communication.SecureElement 632 633**Error codes** 634 635For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 636 637| ID| Error Message | 638| -------- | -------------------------------- | 639| 3300101 | IllegalStateError, service state exception. | 640 641**Example** 642 643```js 644import omapi from '@ohos.secureElement'; 645import secureElement from '@ohos.secureElement'; 646import { BusinessError } from '@ohos.base'; 647 648let nfcSEService : omapi.SEService | null = null; 649let nfcOmaReaderList : omapi.Reader[] | null = null; 650let omaSession : omapi.Session | null = null; 651 652try { 653 nfcSEService = secureElement.newSEService("serviceState", (state) => { 654 if (state == secureElement.ServiceState.DISCONNECTED) { 655 console.log("Service state is Disconnected"); 656 } else { 657 console.log("Service state is Connected"); 658 } 659 }); 660} catch (e) { 661 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 662} 663 664try { 665 if(nfcSEService != null) { 666 nfcOmaReaderList = nfcSEService.getReaders(); 667 } 668 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 669 omaSession = nfcOmaReaderList[0].openSession(); 670 if (omaSession != null) { 671 omaSession.close(); 672 } else { 673 console.log("close failed"); 674 } 675 } 676} catch (e) { 677 console.log("close " + "exception: ${(e : BusinessError).message}"); 678} 679 680``` 681 682## Session. isClosed 683 684isClosed(): boolean 685 686Checks whether the session is closed. 687 688**System capability**: SystemCapability.Communication.SecureElement 689 690**Return value** 691 692| **Type**| **Description** | 693| -------- | ------------------------------------ | 694| boolean | Returns **true** if the session is closed; returns **false** otherwise.| 695 696**Error codes** 697 698For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 699 700**Example** 701 702```Js 703import omapi from '@ohos.secureElement'; 704import secureElement from '@ohos.secureElement'; 705import { BusinessError } from '@ohos.base'; 706 707let nfcSEService : omapi.SEService | null = null; 708let nfcOmaReaderList : omapi.Reader[] | null = null; 709let omaSession : omapi.Session | null = null; 710 711try { 712 nfcSEService = secureElement.newSEService("serviceState", (state) => { 713 if (state == secureElement.ServiceState.DISCONNECTED) { 714 console.log("Service state is Disconnected"); 715 } else { 716 console.log("Service state is Connected"); 717 } 718 }); 719} catch (e) { 720 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 721} 722 723try { 724 if(nfcSEService != null) { 725 nfcOmaReaderList = nfcSEService.getReaders(); 726 } 727 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 728 omaSession = nfcOmaReaderList[0].openSession(); 729 if (omaSession != null && omaSession.isClosed()) { 730 console.log("isClosed success"); 731 } else { 732 console.log("isClosed failed"); 733 } 734 } 735} catch (e) { 736 console.log("isClosed " + "exception: ${(e : BusinessError).message}"); 737} 738``` 739 740## Session.closeChannels 741 742closeChannels(): void 743 744Closes all channels opened in this session. 745 746**System capability**: SystemCapability.Communication.SecureElement 747 748**Error codes** 749 750For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 751 752| ID| Error Message | 753| -------- | -------------------------------- | 754| 3300101 | IllegalStateError, service state exception. | 755 756**Example** 757 758```js 759import omapi from '@ohos.secureElement'; 760import secureElement from '@ohos.secureElement'; 761import { BusinessError } from '@ohos.base'; 762 763let nfcSEService : omapi.SEService | null = null; 764let nfcOmaReaderList : omapi.Reader[] | null = null; 765let omaSession : omapi.Session | null = null; 766 767try { 768 nfcSEService = secureElement.newSEService("serviceState", (state) => { 769 if (state == secureElement.ServiceState.DISCONNECTED) { 770 console.log("Service state is Disconnected"); 771 } else { 772 console.log("Service state is Connected"); 773 } 774 }); 775} catch (e) { 776 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 777} 778 779try { 780 if(nfcSEService != null) { 781 nfcOmaReaderList = nfcSEService.getReaders(); 782 } 783 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 784 omaSession = nfcOmaReaderList[0].openSession(); 785 if (omaSession != null) { 786 omaSession.closeChannels(); 787 console.log("closeChannels success"); 788 } else { 789 console.log("closeChannels failed"); 790 } 791 } 792} catch (e) { 793 console.log("closeChannels " + "exception: ${(e : BusinessError).message}"); 794} 795``` 796 797## Session.openBasicChannel 798 799openBasicChannel(aid: number[]): Promise\<Channel> 800 801Opens a basic channel. This API uses a promise to return the result. 802 803**System capability**: SystemCapability.Communication.SecureElement 804 805**Parameters** 806 807| **Name**| **Type**| **Mandatory**| **Description** | 808| ---------- | -------- | ------ | ------------------------------------------------------------ | 809| aid | number[] | Yes |AIDs of the applets selected on this channel or null if no applet is selected.| 810 811**Return value** 812 813| **Type**| **Description** | 814| -------- | --------------------- | 815| Channel | Returns the **Channel** instance opened. If the SE cannot provide a new basic channel or cannot obtain the access control rule due to lack of available basic channels, null will be returned.| 816 817**Error codes** 818 819For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 820 821| ID| Error Message | 822| -------- | -------------------------------- | 823| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 824| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 825| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 826| 3300104 | IOError, there is a communication problem to the reader or the SE. | 827 828**Example** 829 830```js 831import omapi from '@ohos.secureElement'; 832import secureElement from '@ohos.secureElement'; 833import { BusinessError } from '@ohos.base'; 834 835let nfcSEService : omapi.SEService | null = null; 836let nfcOmaReaderList : omapi.Reader[] | null = null; 837let omaSession : omapi.Session | null = null; 838let aidArray : number[] = [720, 1080]; 839let getPromise : Promise<omapi.Channel> | null = null; 840 841try { 842 nfcSEService = secureElement.newSEService("serviceState", (state) => { 843 if (state == secureElement.ServiceState.DISCONNECTED) { 844 console.log("Service state is Disconnected"); 845 } else { 846 console.log("Service state is Connected"); 847 } 848 }); 849} catch (e) { 850 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 851} 852 853try { 854 if(nfcSEService != null) { 855 nfcOmaReaderList = nfcSEService.getReaders(); 856 } 857 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 858 omaSession = nfcOmaReaderList[0].openSession(); 859 if (omaSession != null) { 860 getPromise = omaSession.openBasicChannel(aidArray); 861 } else { 862 console.log("openBasicChannel1 failed"); 863 } 864 } 865 if (getPromise != null) { 866 console.log("openBasicChannel1 get channel successfully"); 867 } 868} catch (e) { 869 console.log("openBasicChannel1 " + "exception: ${(e : BusinessError).message}"); 870} 871``` 872 873## Session.openBasicChannel 874 875 openBasicChannel(aid: number[], callback: AsyncCallback\<Channel>): void 876 877Opens a basic channel. This API uses an asynchronous callback to return the result. 878 879**System capability**: SystemCapability.Communication.SecureElement 880 881**Parameters** 882 883| **Name**| **Type** | **Mandatory**| **Description** | 884| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 885| aid | number[] | Yes | AIDs of the applets selected on this channel or null if no applet is selected.| 886| callback | AsyncCallback\<Channel> | Yes | Callback invoked to return the **Channel** instance opened. If the SE cannot provide a new basic channel or cannot obtain the access control rule due to lack of available basic channels, null will be returned. | 887 888**Error codes** 889 890For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 891 892| ID| Error Message | 893| -------- | -------------------------------- | 894| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 895| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 896| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 897| 3300104 | IOError, there is a communication problem to the reader or the SE. | 898 899**Example** 900 901```js 902import omapi from '@ohos.secureElement'; 903import secureElement from '@ohos.secureElement'; 904import { BusinessError } from '@ohos.base'; 905 906let nfcSEService : omapi.SEService | null = null; 907let nfcOmaReaderList : omapi.Reader[] | null = null; 908let omaSession : omapi.Session | null = null; 909let aidArray : number[] = [720, 1080]; 910 911try { 912 nfcSEService = secureElement.newSEService("serviceState", (state) => { 913 if (state == secureElement.ServiceState.DISCONNECTED) { 914 console.log("Service state is Disconnected"); 915 } else { 916 console.log("Service state is Connected"); 917 } 918 }); 919} catch (e) { 920 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 921} 922 923try { 924 if(nfcSEService != null) { 925 nfcOmaReaderList = nfcSEService.getReaders(); 926 } 927 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 928 omaSession = nfcOmaReaderList[0].openSession(); 929 if (omaSession != null) { 930 omaSession.openBasicChannel(aidArray, (error, data) => { 931 if (error) { 932 console.log("openBasicChannel2 failed:" + JSON.stringify(error)); 933 return; 934 } 935 console.log("openBasicChannel2 get channel successfully"); 936 }); 937 } 938 } 939} catch (e) { 940 console.log("openBasicChannel2 " + "exception: ${(e : BusinessError).message}"); 941} 942``` 943 944## Session.openBasicChannel 945 946openBasicChannel(aid: number[], p2: number): Promise\<Channel> 947 948Opens a basic channel. This API uses a promise to return the result. 949 950**System capability**: SystemCapability.Communication.SecureElement 951 952**Parameters** 953 954| **Name**| **Type**| **Mandatory**| **Description** | 955| ---------- | -------- | ------ | ------------------------------------------------------------ | 956| aid | number[] | Yes | AIDs of the applets selected on this channel or null if no applet is selected.| 957| p2 | number | Yes |P2 parameter of the **SELECT APDU** command executed on the channel. | 958 959**Return value** 960 961| **Type**| **Description** | 962| -------- | --------------------- | 963| Channel | Returns the **Channel** instance opened. If the SE cannot provide a new basic channel or cannot obtain the access control rule due to lack of available basic channels, null will be returned.| 964 965**Error codes** 966 967For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 968 969| ID| Error Message | 970| -------- | -------------------------------- | 971| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 972| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 973| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 974| 3300104 | IOError, there is a communication problem to the reader or the SE. | 975 976**Example** 977 978```js 979import omapi from '@ohos.secureElement'; 980import secureElement from '@ohos.secureElement'; 981import { BusinessError } from '@ohos.base'; 982 983let nfcSEService : omapi.SEService | null = null; 984let nfcOmaReaderList : omapi.Reader[] | null = null; 985let omaSession : omapi.Session | null = null; 986let getPromise : Promise<omapi.Channel> | null = null; 987let nfcOmaChannel : omapi.Channel | null = null; 988let aidArray : number[] = [720, 1080]; 989let p2 : number = 0x00; 990 991try { 992 nfcSEService = secureElement.newSEService("serviceState", (state) => { 993 if (state == secureElement.ServiceState.DISCONNECTED) { 994 console.log("Service state is Disconnected"); 995 } else { 996 console.log("Service state is Connected"); 997 } 998 }); 999} catch (e) { 1000 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1001} 1002 1003try { 1004 if(nfcSEService != null) { 1005 nfcOmaReaderList = nfcSEService.getReaders(); 1006 } 1007 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1008 omaSession = nfcOmaReaderList[0].openSession(); 1009 } 1010 if (omaSession != null) { 1011 getPromise = omaSession.openBasicChannel(aidArray, p2); 1012 getPromise.then((channel) => { 1013 nfcOmaChannel = channel; 1014 console.log("openBasicChannel3 get channel successfully"); 1015 }) 1016 } 1017} catch (e) { 1018 console.log("openBasicChannel3 " + "exception: ${(e : BusinessError).message}"); 1019} 1020``` 1021 1022## Session.openBasicChannel 1023 1024openBasicChannel(aid: number[], p2:number, callback: AsyncCallback\<Channel>): void 1025 1026Opens a basic channel. This API uses an asynchronous callback to return the result. 1027 1028**System capability**: SystemCapability.Communication.SecureElement 1029 1030**Parameters** 1031 1032| **Name**| **Type** | **Mandatory**| **Description** | 1033| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1034| aid | number[] | Yes | AIDs of the applets selected on this channel or null if no applet is selected.| 1035| p2 | number | Yes | P2 parameter of the **SELECT APDU** command executed on the channel. | 1036| callback | AsyncCallback\<Channel> | Yes | Callback invoked to return the **Channel** instance opened. If the SE cannot provide a new basic channel or cannot obtain the access control rule due to lack of available basic channels, null will be returned. | 1037 1038**Error codes** 1039 1040For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1041 1042| ID| Error Message | 1043| -------- | -------------------------------- | 1044| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1045| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1046| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1047| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1048 1049**Example** 1050 1051```js 1052import omapi from '@ohos.secureElement'; 1053import secureElement from '@ohos.secureElement'; 1054import { BusinessError } from '@ohos.base'; 1055 1056let nfcSEService : omapi.SEService | null = null; 1057let nfcOmaReaderList : omapi.Reader[] | null = null; 1058let omaSession : omapi.Session | null = null; 1059let nfcOmaChannel : omapi.Channel | null = null; 1060let aidArray : number[] = [720, 1080]; 1061let p2 : number = 0x00; 1062 1063try { 1064 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1065 if (state == secureElement.ServiceState.DISCONNECTED) { 1066 console.log("Service state is Disconnected"); 1067 } else { 1068 console.log("Service state is Connected"); 1069 } 1070 }); 1071} catch (e) { 1072 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1073} 1074 1075try { 1076 if(nfcSEService != null) { 1077 nfcOmaReaderList = nfcSEService.getReaders(); 1078 } 1079 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1080 omaSession = nfcOmaReaderList[0].openSession(); 1081 } 1082 if (omaSession != null) { 1083 omaSession.openBasicChannel(aidArray, p2, (error , data) => { 1084 if (error) { 1085 console.log("openBasicChannel4 failed:" + JSON.stringify(error)); 1086 return; 1087 } 1088 nfcOmaChannel = data; 1089 console.log("openBasicChannel4 get channel successfully"); 1090 }); 1091 } 1092} catch (e) { 1093 console.log("openBasicChannel4 " + "exception: ${(e : BusinessError).message}"); 1094} 1095``` 1096 1097## Session.openLogicalChannel 1098 1099openLogicalChannel(aid: number[]): Promise\<Channel> 1100 1101Opens a logical channel. This API uses a promise to return the result. 1102 1103**System capability**: SystemCapability.Communication.SecureElement 1104 1105**Parameters** 1106 1107| **Name**| **Type**| **Mandatory**| **Description** | 1108| ---------- | -------- | ------ | --------------------------------------- | 1109| aid | number[] | Yes | AIDs of the applets selected on the **Channel** instance.| 1110 1111**Return value** 1112 1113| **Type**| **Description** | 1114| -------- | ------------------------------------------------------------ | 1115| Channel | Returns the **Channel** instance opened. If the SE cannot provide a new **Channel** instance or cannot obtain access control rules due to lack of available logical **Channel** instances, null will be returned.| 1116 1117**Error codes** 1118 1119For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1120 1121| ID| Error Message | 1122| -------- | -------------------------------- | 1123| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1124| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1125| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1126| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1127 1128**Example** 1129 1130```js 1131import omapi from '@ohos.secureElement'; 1132import secureElement from '@ohos.secureElement'; 1133import { BusinessError } from '@ohos.base'; 1134 1135let nfcSEService : omapi.SEService | null = null; 1136let nfcOmaReaderList : omapi.Reader[] | null = null; 1137let omaSession : omapi.Session | null = null; 1138let aidArray : number[] = [720, 1080]; 1139let getPromise : Promise<omapi.Channel> | null = null; 1140 1141try { 1142 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1143 if (state == secureElement.ServiceState.DISCONNECTED) { 1144 console.log("Service state is Disconnected"); 1145 } else { 1146 console.log("Service state is Connected"); 1147 } 1148 }); 1149} catch (e) { 1150 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1151} 1152 1153try { 1154 if(nfcSEService != null) { 1155 nfcOmaReaderList = nfcSEService.getReaders(); 1156 } 1157 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1158 omaSession = nfcOmaReaderList[0].openSession(); 1159 if (omaSession != null) { 1160 getPromise = omaSession.openLogicalChannel(aidArray); 1161 } else { 1162 console.log("openLogicalChannel1 failed"); 1163 } 1164 } 1165 if (getPromise != null) { 1166 console.log("openLogicalChannel1 get channel successfully"); 1167 } 1168} catch (e) { 1169 console.log("openLogicalChannel1 " + "exception: ${(e : BusinessError).message}"); 1170} 1171``` 1172 1173## Session.openLogicalChannel 1174 1175 openLogicalChannel(aid:number[], callback: AsyncCallback\<Channel>): void 1176 1177Opens a logical channel. This API uses an asynchronous callback to return the result. 1178 1179**System capability**: SystemCapability.Communication.SecureElement 1180 1181**Parameters** 1182 1183| **Name**| **Type** | **Mandatory**| **Description** | 1184| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1185| aid | number[] | Yes | AIDs of the applets selected on the **Channel** instance. | 1186| callback | AsyncCallback\<Channel> | Yes | Callback invoked to return the **Channel** instance opened. If the SE cannot provide a new **Channel** instance or cannot obtain access control rules due to lack of available logical **Channel** instances, null will be returned.| 1187 1188**Error codes** 1189 1190For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1191 1192| ID| Error Message | 1193| -------- | -------------------------------- | 1194| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1195| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1196| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1197| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1198 1199**Example** 1200 1201```js 1202import omapi from '@ohos.secureElement'; 1203import secureElement from '@ohos.secureElement'; 1204import { BusinessError } from '@ohos.base'; 1205 1206let nfcSEService : omapi.SEService | null = null; 1207let nfcOmaReaderList : omapi.Reader[] | null = null; 1208let omaSession : omapi.Session | null = null; 1209let aidArray : number[] = [720, 1080]; 1210 1211try { 1212 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1213 if (state == secureElement.ServiceState.DISCONNECTED) { 1214 console.log("Service state is Disconnected"); 1215 } else { 1216 console.log("Service state is Connected"); 1217 } 1218 }); 1219} catch (e) { 1220 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1221} 1222 1223try { 1224 if(nfcSEService != null) { 1225 nfcOmaReaderList = nfcSEService.getReaders(); 1226 } 1227 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1228 omaSession = nfcOmaReaderList[0].openSession(); 1229 } 1230 if (omaSession != null) { 1231 omaSession.openLogicalChannel(aidArray, (error, data) => { 1232 if (error) { 1233 console.log("openLogicalChannel2 failed:" + JSON.stringify(error)); 1234 return; 1235 } 1236 console.log("openLogicalChannel2 get channel successfully"); 1237 }); 1238 } 1239} catch (e) { 1240 console.log("openLogicalChannel2 " + "exception: ${(e : BusinessError).message}"); 1241} 1242``` 1243 1244## Session.openLogicalChannel 1245 1246openLogicalChannel(aid: number[], p2: number): Promise\<Channel> 1247 1248Opens a logical channel with the applet represented by the given AID (the AID is not null and the length is not 0). 1249 1250If the AID length is 0, this API sends a **select** command with the AID length of 0 (as per [GPCS]) to select the Issuer Security Domain of the SE. 1251 1252If the AID is null, this API sends the **MANAGE CHANNEL Open** only. In this case, the default applet associated with the logical channel is selected. 1253 1254**P2** is usually **0x00**. The device shall allow any value of **P2** and the following values: **0x00**, **0x04**, **0x08**, **0x0C** as defined in [ISO 7816-4](https://www.iso.org/standard/77180.html). 1255 1256**System capability**: SystemCapability.Communication.SecureElement 1257 1258**Parameters** 1259 1260| **Name**| **Type**| **Mandatory**| **Description** | 1261| ---------- | -------- | ------ | ----------------------------------------- | 1262| aid | number[] | Yes | AIDs of the applets selected on the **Channel** instance.| 1263| p2 | number | Yes | P2 parameter of the **SELECT APDU** command executed on the channel. | 1264 1265**Error codes** 1266 1267For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1268 1269| ID| Error Message | 1270| -------- | -------------------------------- | 1271| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1272| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1273| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1274| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1275 1276**Example** 1277 1278```js 1279import omapi from '@ohos.secureElement'; 1280import secureElement from '@ohos.secureElement'; 1281import { BusinessError } from '@ohos.base'; 1282 1283let nfcSEService : omapi.SEService | null = null; 1284let nfcOmaReaderList : omapi.Reader[] | null = null; 1285let omaSession : omapi.Session | null = null; 1286let getPromise : Promise<omapi.Channel> | null = null; 1287let nfcOmaChannel : omapi.Channel | null = null; 1288let aidArray : number[] = [720, 1080]; 1289let p2 : number = 0x00; 1290 1291try { 1292 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1293 if (state == secureElement.ServiceState.DISCONNECTED) { 1294 console.log("Service state is Disconnected"); 1295 } else { 1296 console.log("Service state is Connected"); 1297 } 1298 }); 1299} catch (e) { 1300 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1301} 1302 1303try { 1304 if(nfcSEService != null) { 1305 nfcOmaReaderList = nfcSEService.getReaders(); 1306 } 1307 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1308 omaSession = nfcOmaReaderList[0].openSession(); 1309 } 1310 if (omaSession != null) { 1311 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1312 getPromise.then((channel) => { 1313 nfcOmaChannel = channel; 1314 console.log("openLogicalChannel3 get channel successfully"); 1315 }) 1316 } 1317} catch (e) { 1318 console.log("openLogicalChannel3 " + "exception: ${(e : BusinessError).message}"); 1319} 1320``` 1321 1322## Session.openLogicalChannel 1323 1324openLogicalChannel(aid: number[], p2: number, callback: AsyncCallback\<Channel>):void 1325 1326Opens a logical channel with the applet represented by the given AID (the AID is not null and the length is not 0). 1327 1328If the AID length is 0, this API sends a **select** command with the AID length of 0 (as per [GPCS]) to select the Issuer Security Domain of the SE. 1329 1330If the AID is null, this API sends the **MANAGE CHANNEL Open** only. In this case, the default applet associated with the logical channel is selected. 1331 1332**P2** is usually **0x00**. The device shall allow any value of **P2** and the following values: **0x00**, **0x04**, **0x08**, **0x0C** as defined in [ISO 7816-4](https://www.iso.org/standard/77180.html). 1333 1334**System capability**: SystemCapability.Communication.SecureElement 1335 1336**Parameters** 1337 1338| **Name**| **Type** | **Mandatory**| **Description** | 1339| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1340| aid | number[] | Yes | AIDs of the applets selected on the **Channel** instance. | 1341| p2 | number | Yes | P2 parameter of the **SELECT APDU** command executed on the channel. | 1342| callback | AsyncCallback\<Channel> | Yes | Callback invoked to return the **Channel** instance opened. If the SE cannot provide a new **Channel** instance or cannot obtain access control rules due to lack of available logical **Channel** instances, null will be returned.| 1343 1344**Error codes** 1345 1346For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1347 1348| ID| Error Message | 1349| -------- | -------------------------------- | 1350| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1351| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1352| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1353| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1354 1355**Example** 1356 1357```js 1358import omapi from '@ohos.secureElement'; 1359import secureElement from '@ohos.secureElement'; 1360import { BusinessError } from '@ohos.base'; 1361 1362let nfcSEService : omapi.SEService | null = null; 1363let nfcOmaReaderList : omapi.Reader[] | null = null; 1364let omaSession : omapi.Session | null = null; 1365let nfcOmaChannel : omapi.Channel | null = null; 1366let aidArray : number[] = [720, 1080]; 1367let p2 : number = 0x00; 1368 1369try { 1370 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1371 if (state == secureElement.ServiceState.DISCONNECTED) { 1372 console.log("Service state is Disconnected"); 1373 } else { 1374 console.log("Service state is Connected"); 1375 } 1376 }); 1377} catch (e) { 1378 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1379} 1380 1381try { 1382 if(nfcSEService != null) { 1383 nfcOmaReaderList = nfcSEService.getReaders(); 1384 } 1385 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1386 omaSession = nfcOmaReaderList[0].openSession(); 1387 } 1388 if (omaSession != null) { 1389 omaSession.openLogicalChannel(aidArray, p2, (error, data) => { 1390 if (error) { 1391 console.log("openLogicalChannel4 failed:" + JSON.stringify(error)); 1392 return; 1393 } 1394 nfcOmaChannel = data; 1395 console.log("openLogicalChannel4 get channel successfully"); 1396 }); 1397 } 1398} catch (e) { 1399 console.log("openLogicalChannel4 " + "exception: ${(e : BusinessError).message}"); 1400} 1401``` 1402 1403## Channel. getSession 1404 1405 getSession(): Session 1406 1407Obtains the session that opens this channel. 1408 1409**System capability**: SystemCapability.Communication.SecureElement 1410 1411**Return value** 1412 1413| **Type**| **Description** | 1414| -------- | ----------------------------- | 1415| Session | Returns the session obtained.| 1416 1417**Example** 1418 1419```js 1420import omapi from '@ohos.secureElement'; 1421import secureElement from '@ohos.secureElement'; 1422import { BusinessError } from '@ohos.base'; 1423 1424let nfcSEService : omapi.SEService | null = null; 1425let nfcOmaReaderList : omapi.Reader[] | null = null; 1426let omaSession : omapi.Session | null = null; 1427let getPromise : Promise<omapi.Channel> | null = null; 1428let aidArray : number[] = [720, 1080]; 1429let p2 : number = 0x00; 1430let mySession : omapi.Session | null = null; 1431 1432try { 1433 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1434 if (state == secureElement.ServiceState.DISCONNECTED) { 1435 console.log("Service state is Disconnected"); 1436 } else { 1437 console.log("Service state is Connected"); 1438 } 1439 }); 1440} catch (e) { 1441 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1442} 1443 1444try { 1445 if(nfcSEService != null) { 1446 nfcOmaReaderList = nfcSEService.getReaders(); 1447 } 1448 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1449 omaSession = nfcOmaReaderList[0].openSession(); 1450 } 1451 if (omaSession != null) { 1452 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1453 getPromise.then((channel) => { 1454 mySession = channel.getSession(); 1455 console.log("openLogicalChannel get channel successfully"); 1456 }) 1457 } 1458 if (mySession != null) { 1459 console.log("get session successfully"); 1460 } else { 1461 console.log("get session failed"); 1462 } 1463} catch (e) { 1464 console.log("get session " + "exception: ${(e : BusinessError).message}"); 1465} 1466``` 1467 1468## Channel. close 1469 1470close(): void 1471 1472Closes the channel of the SE. 1473 1474**System capability**: SystemCapability.Communication.SecureElement 1475 1476**Example** 1477 1478```js 1479import omapi from '@ohos.secureElement'; 1480import secureElement from '@ohos.secureElement'; 1481import { BusinessError } from '@ohos.base'; 1482 1483let nfcSEService : omapi.SEService | null = null; 1484let nfcOmaReaderList : omapi.Reader[] | null = null; 1485let omaSession : omapi.Session | null = null; 1486let getPromise : Promise<omapi.Channel> | null = null; 1487let aidArray : number[] = [720, 1080]; 1488let p2 : number = 0x00; 1489 1490try { 1491 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1492 if (state == secureElement.ServiceState.DISCONNECTED) { 1493 console.log("Service state is Disconnected"); 1494 } else { 1495 console.log("Service state is Connected"); 1496 } 1497 }); 1498} catch (e) { 1499 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1500} 1501 1502try { 1503 if(nfcSEService != null) { 1504 nfcOmaReaderList = nfcSEService.getReaders(); 1505 } 1506 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1507 omaSession = nfcOmaReaderList[0].openSession(); 1508 } 1509 if (omaSession != null) { 1510 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1511 getPromise.then((channel) => { 1512 channel.close(); 1513 console.log("channel close successfully"); 1514 }) 1515 } 1516} catch (e) { 1517 console.log("channel close " + "exception: ${(e : BusinessError).message}"); 1518} 1519``` 1520 1521## Channel. isBasicChannel 1522 1523isBasicChannel(): boolean 1524 1525Checks whether this channel is a basic channel. 1526 1527**System capability**: SystemCapability.Communication.SecureElement 1528 1529**Return value** 1530 1531| **Type**| **Description** | 1532| -------- | ------------------------------------------------------------ | 1533| boolean | Returns **true** if the channel is a basic channel; returns **false** otherwise.| 1534 1535**Example** 1536 1537```js 1538import omapi from '@ohos.secureElement'; 1539import secureElement from '@ohos.secureElement'; 1540import { BusinessError } from '@ohos.base'; 1541 1542let nfcSEService : omapi.SEService | null = null; 1543let nfcOmaReaderList : omapi.Reader[] | null = null; 1544let omaSession : omapi.Session | null = null; 1545let getPromise : Promise<omapi.Channel> | null = null; 1546let aidArray : number[] = [720, 1080]; 1547let p2 : number = 0x00; 1548let ret : boolean = false; 1549 1550try { 1551 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1552 if (state == secureElement.ServiceState.DISCONNECTED) { 1553 console.log("Service state is Disconnected"); 1554 } else { 1555 console.log("Service state is Connected"); 1556 } 1557 }); 1558} catch (e) { 1559 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1560} 1561 1562try { 1563 if(nfcSEService != null) { 1564 nfcOmaReaderList = nfcSEService.getReaders(); 1565 } 1566 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1567 omaSession = nfcOmaReaderList[0].openSession(); 1568 } 1569 if (omaSession != null) { 1570 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1571 getPromise.then((channel) => { 1572 ret = channel.isBasicChannel(); 1573 }) 1574 } 1575 if (ret) { 1576 console.log("isBasicChannel TRUE"); 1577 } else { 1578 console.log("isBasicChannel FALSE"); 1579 } 1580} catch (e) { 1581 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1582} 1583``` 1584 1585## Channel. isClosed 1586 1587isClosed(): boolean 1588 1589Checks whether this channel is closed. 1590 1591**System capability**: SystemCapability.Communication.SecureElement 1592 1593**Return value** 1594 1595| **Type**| **Description** | 1596| -------- | --------------------------------------------- | 1597| boolean | Returns **true** if this channel is closed; returns **false** otherwise.| 1598 1599**Example** 1600 1601```js 1602import omapi from '@ohos.secureElement'; 1603import secureElement from '@ohos.secureElement'; 1604import { BusinessError } from '@ohos.base'; 1605 1606let nfcSEService : omapi.SEService | null = null; 1607let nfcOmaReaderList : omapi.Reader[] | null = null; 1608let omaSession : omapi.Session | null = null; 1609let getPromise : Promise<omapi.Channel> | null = null; 1610let aidArray : number[] = [720, 1080]; 1611let p2 : number = 0x00; 1612let ret : boolean = false; 1613 1614try { 1615 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1616 if (state == secureElement.ServiceState.DISCONNECTED) { 1617 console.log("Service state is Disconnected"); 1618 } else { 1619 console.log("Service state is Connected"); 1620 } 1621 }); 1622} catch (e) { 1623 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1624} 1625 1626try { 1627 if(nfcSEService != null) { 1628 nfcOmaReaderList = nfcSEService.getReaders(); 1629 } 1630 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1631 omaSession = nfcOmaReaderList[0].openSession(); 1632 } 1633 if (omaSession != null) { 1634 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1635 getPromise.then((channel) => { 1636 ret = channel.isClosed(); 1637 }) 1638 } 1639 if (ret) { 1640 console.log("channel isClosed TRUE"); 1641 } else { 1642 console.log("channel isClosed False"); 1643 } 1644} catch (e) { 1645 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1646} 1647``` 1648 1649## Channel. getSelectResponse 1650 1651getSelectResponse():number[] 1652 1653Obtains the data as received from the application **select** command, including the status word received when the applet is selected. 1654 1655**System capability**: SystemCapability.Communication.SecureElement 1656 1657**Return value** 1658 1659| **Type**| **Description** | 1660| -------- | ------------------------------------------------------------ | 1661| number[] | Returns the data obtained.| 1662 1663**Error codes** 1664 1665For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1666 1667**Example** 1668 1669```js 1670import omapi from '@ohos.secureElement'; 1671import secureElement from '@ohos.secureElement'; 1672import { BusinessError } from '@ohos.base'; 1673 1674let nfcSEService : omapi.SEService | null = null; 1675let nfcOmaReaderList : omapi.Reader[] | null = null; 1676let omaSession : omapi.Session | null = null; 1677let getPromise : Promise<omapi.Channel> | null = null; 1678let aidArray : number[] = [720, 1080]; 1679let p2 : number = 0x00; 1680let responseArray : number[] = [720, 1080]; 1681let str : string = ""; 1682 1683try { 1684 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1685 if (state == secureElement.ServiceState.DISCONNECTED) { 1686 console.log("Service state is Disconnected"); 1687 } else { 1688 console.log("Service state is Connected"); 1689 } 1690 }); 1691} catch (e) { 1692 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1693} 1694 1695try { 1696 if(nfcSEService != null) { 1697 nfcOmaReaderList = nfcSEService.getReaders(); 1698 } 1699 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1700 omaSession = nfcOmaReaderList[0].openSession(); 1701 } 1702 if (omaSession != null) { 1703 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1704 getPromise.then((channel) => { 1705 responseArray = channel.getSelectResponse(); 1706 }) 1707 } 1708 if (responseArray) { 1709 str = "getSelectResponse result:["; 1710 for (let i = 0; i < responseArray.length; ++i) { 1711 str += responseArray[i]; 1712 str += ' '; 1713 } 1714 str += ']'; 1715 console.log(str); 1716 } else { 1717 console.log("getSelectResponse result is null"); 1718 } 1719} catch (e) { 1720 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1721} 1722``` 1723 1724## Channel. transmit 1725 1726transmit(command: number[]): Promise<number[]> 1727 1728Transmits the **APDU** command to the SE (according to ISO/IEC 7816). This API uses a promise to return the result. 1729 1730**System capability**: SystemCapability.Communication.SecureElement 1731 1732**Parameters** 1733 1734| **Name**| **Type**| **Mandatory**| **Description** | 1735| ---------- | -------- | ------ | ------------------------------------- | 1736| command | number[] | Yes | AIDs of the applets selected on the channel.| 1737 1738**Return value** 1739 1740| **Type**| **Description** | 1741| -------- | -------------- | 1742| number[] | Returns the response obtained.| 1743 1744**Error codes** 1745 1746For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1747 1748| ID| Error Message | 1749| -------- | -------------------------------- | 1750| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1751| 3300103 | SecurityError, the command is filtered by the security policy. | 1752| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1753 1754**Example** 1755 1756```js 1757import omapi from '@ohos.secureElement'; 1758import secureElement from '@ohos.secureElement'; 1759import { BusinessError } from '@ohos.base'; 1760 1761let nfcSEService : omapi.SEService | null = null; 1762let nfcOmaReaderList : omapi.Reader[] | null = null; 1763let omaSession : omapi.Session | null = null; 1764let getPromise : Promise<omapi.Channel> | null = null; 1765let aidArray : number[] = [720, 1080]; 1766let p2 : number = 0x00; 1767let responseArray : Promise<number[]> | null = null; 1768 1769try { 1770 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1771 if (state == secureElement.ServiceState.DISCONNECTED) { 1772 console.log("Service state is Disconnected"); 1773 } else { 1774 console.log("Service state is Connected"); 1775 } 1776 }); 1777} catch (e) { 1778 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1779} 1780 1781try { 1782 if(nfcSEService != null) { 1783 nfcOmaReaderList = nfcSEService.getReaders(); 1784 } 1785 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1786 omaSession = nfcOmaReaderList[0].openSession(); 1787 } 1788 if (omaSession != null) { 1789 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1790 getPromise.then((channel) => { 1791 let command: number[] = [100, 200]; 1792 // Refer to Session.openBasicChannel for this.nfcOmaChannel. 1793 responseArray = channel.transmit(command); 1794 }) 1795 } 1796 if (responseArray != null) { 1797 console.log("transmit1 success"); 1798 } else { 1799 console.log("transmit1 failed"); 1800 } 1801} catch (e) { 1802 console.log("transmit1 " + "exception: ${(e : BusinessError).message}"); 1803} 1804``` 1805 1806## Channel. transmit 1807 1808transmit(command: number[], callback: AsyncCallback<number[]>): void 1809 1810Transmits the **APDU** command to the SE (according to ISO/IEC 7816). This API uses an asynchronous callback to return the result. 1811 1812**System capability**: SystemCapability.Communication.SecureElement 1813 1814**Parameters** 1815 1816| **Name**| **Type** | **Mandatory**| **Description** | 1817| ---------- | ----------------------- | ------ | ------------------------------------- | 1818| command | number[] | Yes | AIDs of the applets selected on the channel.| 1819| callback | AsyncCallback<number[]> | Yes | Callback invoked to return the result. | 1820 1821**Error codes** 1822 1823For details about error codes, see [SE Error Codes](../errorcodes/errorcode-se.md). 1824 1825| ID| Error Message | 1826| -------- | -------------------------------- | 1827| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1828| 3300103 | SecurityError, the command is filtered by the security policy. | 1829| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1830 1831**Example** 1832 1833```js 1834import omapi from '@ohos.secureElement'; 1835import secureElement from '@ohos.secureElement'; 1836import { BusinessError } from '@ohos.base'; 1837 1838let nfcSEService : omapi.SEService | null = null; 1839let nfcOmaReaderList : omapi.Reader[] | null = null; 1840let omaSession : omapi.Session | null = null; 1841let getPromise : Promise<omapi.Channel> | null = null; 1842let aidArray : number[] = [720, 1080]; 1843let p2 : number = 0x00; 1844let str : string = ""; 1845 1846try { 1847 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1848 if (state == secureElement.ServiceState.DISCONNECTED) { 1849 console.log("Service state is Disconnected"); 1850 } else { 1851 console.log("Service state is Connected"); 1852 } 1853 }); 1854} catch (e) { 1855 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1856} 1857 1858try { 1859 if(nfcSEService != null) { 1860 nfcOmaReaderList = nfcSEService.getReaders(); 1861 } 1862 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1863 omaSession = nfcOmaReaderList[0].openSession(); 1864 } 1865 if (omaSession != null) { 1866 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1867 getPromise.then((channel) => { 1868 let command: number[] = [100, 200]; 1869 // Refer to Session.openBasicChannel for this.nfcOmaChannel. 1870 channel.transmit(command, (error, data) => { 1871 if (error) { 1872 console.log("transmit2 exception:" + JSON.stringify(error)); 1873 return; 1874 } 1875 str = "transmit2 result:["; 1876 for (let i = 0; i < data.length; ++i) { 1877 str += data[i]; 1878 str += " "; 1879 } 1880 str += "]"; 1881 console.log(str) 1882 }); 1883 }) 1884 } 1885} catch (e) { 1886 console.log("transmit2 " + "exception: ${(e : BusinessError).message}"); 1887} 1888``` 1889