1# @ohos.secureElement (安全单元的通道管理) 2 3本模块主要用于操作及管理安全单元(SecureElement,简称SE),电子设备上可能存在的安全单元有eSE(Embedded SE)和SIM卡。文档中出现的SE服务为SEService实例,参见[newSEService](#secureelementnewseservice)。 4 5对于文档中出现以下类型说明: 6 7| 类型 | 说明 | 8| ------- | ---------------------------------------------- | 9| Reader | 此类的实例表示该设备支持的SE,如果支持eSE和SIM,这返回两个实例。 | 10| Session | 此类的实例表示在某个SE Reader实例上创建连接会话。 | 11| Channel | 此类的实例表示在某个Session实例上创建通道,可能为基础通道或逻辑通道。 | 12 13> **说明:** 14> 15> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 16 17## **导入模块** 18 19```js 20import secureElement from '@ohos.secureElement'; 21``` 22 23## secureElement.ServiceState 24 25定义不同的SE服务状态值。 26 27**系统能力:** SystemCapability.Communication.SecureElement 28 29| 名称 | 值 | 说明 | 30| ------------ | ---- | ------------------ | 31| DISCONNECTED | 0 | SE服务状态已断开。 | 32| CONNECTED | 1 | SE服务状态已连接。 | 33 34## secureElement.newSEService 35 36newSEService(type: 'serviceState', callback: Callback<[ServiceState](#secureelementservicestate)>): SEService 37 38建立一个可用于连接到系统中所有可用SE的新连接(服务)。连接过程较为耗时,所以此方法仅提供异步方式进行的。 39 40仅当指定的回调或者当[isConnected](#seserviceisconnected)方法返回true时,该返回SEService对象是可用的。 41 42**系统能力:** SystemCapability.Communication.SecureElement 43 44**参数:** 45 46| **参数名** | **类型** | **必填** | **说明** | 47| ---------- | ---------------------------------------------------- | ------ | -------------------- | 48| type | string | 是 | 固定填'serviceState' 。 | 49| callback | Callback<[ServiceState](#secureelementservicestate)> | 是 | 返回SE服务状态的回调 。| 50 51**返回值:** 52 53| **类型** | **说明** | 54| :-------- | :--------- | 55| SEService | SE服务实例。 | 56 57**示例:** 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.error("newSEService occurs " + "exception: ${(e : BusinessError).message}"); 73} 74``` 75 76## SEService.getReaders 77 78getReaders(): Reader[] 79 80返回可用SE Reader的数组,包含该设备上支持的所有的安全单元。 81 82**系统能力:** SystemCapability.Communication.SecureElement 83 84**返回值:** 85 86| **类型** | **说明** | 87| :------- | :--------------------- | 88| Reader[] | 返回可用Reader对象数组。 | 89 90**示例:** 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.error("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.error("get reader failed"); 120 } 121} catch (e) { 122 console.error("getReaders " + "exception: ${(e : BusinessError).message}"); 123} 124``` 125 126## SEService.isConnected 127 128isConnected(): boolean 129 130检查SE服务是否已连接。 131 132**系统能力:** SystemCapability.Communication.SecureElement 133 134**返回值:** 135 136| **类型** | **说明** | 137| :------- | :--------------------------------------------- | 138| boolean | true: SE服务状态已连接,false: SE服务状态已断开。 | 139 140**示例:** 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.error("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.error("isConnected " + "exception: ${(e : BusinessError).message}"); 174} 175``` 176 177## SEService.shutdown 178 179shutdown(): void 180 181释放该Service分配的所有SE资源。此后[isConnected](#seserviceisconnected)将返回false。 182 183**系统能力:** SystemCapability.Communication.SecureElement 184 185**示例:** 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.error("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.error("shutdown exception:" + "exception: ${(e : BusinessError).message}"); 214} 215``` 216 217## SEService.getVersion 218 219getVersion(): string 220 221返回此实现所基于的Open Mobile API规范的版本号。 222 223**系统能力:** SystemCapability.Communication.SecureElement 224 225**返回值:** 226 227| **类型** | **说明** | 228| -------- | -------------------------------------------------- | 229| string | OMA版本号(例如,“3.3”表示Open Mobile API规范版本3.3) | 230 231**示例:** 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.error("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.error("getVersion " + "exception: ${(e : BusinessError).message}"); 259} 260``` 261 262## Reader.getName 263 264getName(): string 265 266返回此Reader的名称。如果此读卡器是SIM Reader,则其名称必须为“SIM[Slot]”。如果读卡器是eSE,则其名称须为“eSE”。 267 268**系统能力:** SystemCapability.Communication.SecureElement 269 270**返回值:** 271 272| **类型** | **说明** | 273| -------- | ---------- | 274| string | Reader名称。 | 275 276**示例:** 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.error("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.error("getName failed"); 306 } 307} catch (e) { 308 console.error("getName " + "exception: ${(e : BusinessError).message}"); 309} 310``` 311 312## Reader.isSecureElementPresent 313 314isSecureElementPresent(): boolean 315 316检查当前Reader所对应的安全单元是否可用。 317 318**系统能力:** SystemCapability.Communication.SecureElement 319 320**返回值:** 321 322| **类型** | **说明** | 323| -------- | -------------------------------------------- | 324| boolean | true: 安全单元可用, false: 安全单元不可用。 | 325 326**错误码:** 327 328错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 329 330| 错误码ID | 错误信息 | 331| -------- | -------------------------------- | 332| 3300101 | IllegalStateError, service state exception. | 333 334**示例:** 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.error("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.error("isSecureElementPresent failed"); 369 } 370} catch (e) { 371 console.error("isSecureElementPresent " + "exception: ${(e : BusinessError).message}"); 372} 373``` 374 375## Reader.openSession 376 377 openSession(): Session 378 379在SE Reader实例上创建连接会话,返回Session实例。在一个Reader上可能同时打开多个会话。 380 381**系统能力:** SystemCapability.Communication.SecureElement 382 383**返回值:** 384 385| **类型** | **说明** | 386| -------- | ------------------------------ | 387| Session | 连接会话Session实例。| 388 389**错误码:** 390 391错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 392 393| 错误码ID | 错误信息 | 394| -------- | -------------------------------- | 395| 3300101 | IllegalStateError, service state exception. | 396| 3300104 | IOError, there is a communication problem to the reader or the SE. | 397 398**示例:** 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.error("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.error("OpenSession failed"); 432 } 433} catch (e) { 434 console.error("OpenSession " + "exception: ${(e : BusinessError).message}"); 435} 436``` 437 438## Reader.closeSessions 439 440 closeSessions(): void 441 442关闭在此Reader上打开的所有Session。所有这些Session打开的所有Channel都将关闭。 443 444**系统能力:** SystemCapability.Communication.SecureElement 445 446**错误码:** 447 448错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 449 450| 错误码ID | 错误信息 | 451| -------- | -------------------------------- | 452| 3300101 | IllegalStateError, service state exception. | 453 454**示例:** 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.error("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 nfcOmaReaderList[0].closeSessions(); 482 console.log("closeSessions successfully"); 483 } else { 484 console.error("closeSessions failed"); 485 } 486} catch (e) { 487 console.error("closeSessions " + "exception: ${(e : BusinessError).message}"); 488} 489``` 490 491## Session.getReader 492 493getReader(): Reader 494 495获取提供此Session的Reader实例。 496 497**系统能力:** SystemCapability.Communication.SecureElement 498 499**返回值:** 500 501| **类型** | **说明** | 502| -------- | --------------------------- | 503| Reader | 返回此Session的Reader实例。 | 504 505**示例:** 506 507```js 508import omapi from '@ohos.secureElement'; 509import secureElement from '@ohos.secureElement'; 510import { BusinessError } from '@ohos.base'; 511 512let nfcSEService : omapi.SEService | null = null; 513let nfcOmaReaderList : omapi.Reader[] | null = null; 514let omaSession : omapi.Session | null = null; 515 516try { 517 nfcSEService = secureElement.newSEService("serviceState", (state) => { 518 if (state == secureElement.ServiceState.DISCONNECTED) { 519 console.log("Service state is Disconnected"); 520 } else { 521 console.log("Service state is Connected"); 522 } 523 }); 524} catch (e) { 525 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 526} 527 528try { 529 if (nfcSEService != null) { 530 nfcOmaReaderList = nfcSEService.getReaders(); 531 } 532 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 533 omaSession = nfcOmaReaderList[0].openSession(); 534 if (omaSession.getReader() != null) { 535 console.log("get reader successfully"); 536 } else { 537 console.error("get reader failed"); 538 } 539 } else { 540 console.error("getReader failed"); 541 } 542} catch (e) { 543 console.error("getReader " + "exception: ${(e : BusinessError).message}"); 544} 545``` 546 547## Session.getATR 548 549getATR(): number[] 550 551获取该SE的ATR。如果该SE的ATR不可用,则应返回空数组。 552 553**系统能力:** SystemCapability.Communication.SecureElement 554 555**返回值:** 556 557| **类型** | **说明** | 558| -------- | -------------------------------------------- | 559| number[] | 返回SE的ATR,SE的ATR不可用时,返回空的数组。 | 560 561**错误码:** 562 563错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 564 565| 错误码ID | 错误信息 | 566| -------- | -------------------------------- | 567| 3300101 | IllegalStateError, service state exception. | 568 569**示例:** 570 571```js 572import omapi from '@ohos.secureElement'; 573import secureElement from '@ohos.secureElement'; 574import { BusinessError } from '@ohos.base'; 575 576let nfcSEService : omapi.SEService | null = null; 577let nfcOmaReaderList : omapi.Reader[] | null = null; 578let omaSession : omapi.Session | null = null; 579let omaATR : number[] | null = null; 580let str : string = ""; 581 582try { 583 nfcSEService = secureElement.newSEService("serviceState", (state) => { 584 if (state == secureElement.ServiceState.DISCONNECTED) { 585 console.log("Service state is Disconnected"); 586 } else { 587 console.log("Service state is Connected"); 588 } 589 }); 590} catch (e) { 591 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 592} 593 594try { 595 if (nfcSEService != null) { 596 nfcOmaReaderList = nfcSEService.getReaders(); 597 } 598 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 599 omaSession = nfcOmaReaderList[0].openSession(); 600 if (omaSession != null) { 601 omaATR = omaSession.getATR(); 602 } else { 603 console.error("getATR failed"); 604 } 605 } 606 if (omaATR != null && omaATR.length > 0) { 607 str = 'getATR result:['; 608 for (let i = 0; i < omaATR.length; ++i) { 609 str += omaATR[i]; 610 str += ' '; 611 } 612 str += ']'; 613 console.log(str); 614 } else { 615 console.error("getATR failed"); 616 } 617} catch (e) { 618 console.error("getATR " + "exception: ${(e : BusinessError).message}"); 619} 620``` 621 622## Session.close 623 624close(): void 625 626关闭与SE的当前会话连接。这将关闭此Session打开的所有Channel。 627 628**系统能力:** SystemCapability.Communication.SecureElement 629 630**错误码:** 631 632错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 633 634| 错误码ID | 错误信息 | 635| -------- | -------------------------------- | 636| 3300101 | IllegalStateError, service state exception. | 637 638**示例:** 639 640```js 641import omapi from '@ohos.secureElement'; 642import secureElement from '@ohos.secureElement'; 643import { BusinessError } from '@ohos.base'; 644 645let nfcSEService : omapi.SEService | null = null; 646let nfcOmaReaderList : omapi.Reader[] | null = null; 647let omaSession : omapi.Session | null = null; 648 649try { 650 nfcSEService = secureElement.newSEService("serviceState", (state) => { 651 if (state == secureElement.ServiceState.DISCONNECTED) { 652 console.log("Service state is Disconnected"); 653 } else { 654 console.log("Service state is Connected"); 655 } 656 }); 657} catch (e) { 658 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 659} 660 661try { 662 if (nfcSEService != null) { 663 nfcOmaReaderList = nfcSEService.getReaders(); 664 } 665 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 666 omaSession = nfcOmaReaderList[0].openSession(); 667 if (omaSession != null) { 668 omaSession.close(); 669 } else { 670 console.error("close failed"); 671 } 672 } 673} catch (e) { 674 console.error("close " + "exception: ${(e : BusinessError).message}"); 675} 676 677``` 678 679## Session. isClosed 680 681isClosed(): boolean 682 683检查Session是否关闭。 684 685**系统能力:** SystemCapability.Communication.SecureElement 686 687**返回值:** 688 689| **类型** | **说明** | 690| -------- | ------------------------------------ | 691| boolean | true:Session状态已关闭,false:Session是打开的。 | 692 693**错误码:** 694 695错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 696 697**示例:** 698 699```Js 700import omapi from '@ohos.secureElement'; 701import secureElement from '@ohos.secureElement'; 702import { BusinessError } from '@ohos.base'; 703 704let nfcSEService : omapi.SEService | null = null; 705let nfcOmaReaderList : omapi.Reader[] | null = null; 706let omaSession : omapi.Session | null = null; 707 708try { 709 nfcSEService = secureElement.newSEService("serviceState", (state) => { 710 if (state == secureElement.ServiceState.DISCONNECTED) { 711 console.log("Service state is Disconnected"); 712 } else { 713 console.log("Service state is Connected"); 714 } 715 }); 716} catch (e) { 717 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 718} 719 720try { 721 if (nfcSEService != null) { 722 nfcOmaReaderList = nfcSEService.getReaders(); 723 } 724 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 725 omaSession = nfcOmaReaderList[0].openSession(); 726 if (omaSession != null) { 727 console.log("openSession success"); 728 if (omaSession.isClosed()) { 729 console.log("session is closed"); 730 } 731 } else { 732 console.error("openSession failed"); 733 } 734 } 735} catch (e) { 736 console.error("isClosed " + "exception: ${(e : BusinessError).message}"); 737} 738``` 739 740## Session.closeChannels 741 742closeChannels(): void 743 744关闭此Session上打开的所有Channel。 745 746**系统能力:** SystemCapability.Communication.SecureElement 747 748**错误码:** 749 750错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 751 752| 错误码ID | 错误信息 | 753| -------- | -------------------------------- | 754| 3300101 | IllegalStateError, service state exception. | 755 756**示例:** 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.error("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.error("closeChannels failed"); 790 } 791 } 792} catch (e) { 793 console.error("closeChannels " + "exception: ${(e : BusinessError).message}"); 794} 795``` 796 797## Session.openBasicChannel 798 799openBasicChannel(aid: number[]): Promise\<Channel> 800 801打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。 802 803**系统能力:** SystemCapability.Communication.SecureElement 804 805**参数:** 806 807| **参数名** | **类型** | **必填** | **说明** | 808| ---------- | -------- | ------ | ------------------------------------------------------------ | 809| aid | number[] | 是 |在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。| 810 811**返回值:** 812 813| **类型** | **说明** | 814| -------- | --------------------- | 815| Channel | 以Promise形式异步返回可用的基础Channel对象实例。 | 816 817**错误码:** 818 819错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 820 821| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openBasicChannel1 failed"); 863 } 864 } 865 if (getPromise != null) { 866 console.log("openBasicChannel1 get channel successfully"); 867 } 868} catch (e) { 869 console.error("openBasicChannel1 " + "exception: ${(e : BusinessError).message}"); 870} 871``` 872 873## Session.openBasicChannel 874 875 openBasicChannel(aid: number[], callback: AsyncCallback\<Channel>): void 876 877打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。 878 879**系统能力:** SystemCapability.Communication.SecureElement 880 881**参数:** 882 883| **参数名** | **类型** | **必填** | **说明** | 884| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 885| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 886| callback | AsyncCallback\<Channel> | 是 | 以callback形式异步返回可用的基础Channel对象实例。 | 887 888**错误码:** 889 890错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 891 892| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openBasicChannel2 failed:" + JSON.stringify(error)); 933 return; 934 } 935 console.log("openBasicChannel2 get channel successfully"); 936 }); 937 } 938 } 939} catch (e) { 940 console.error("openBasicChannel2 " + "exception: ${(e : BusinessError).message}"); 941} 942``` 943 944## Session.openBasicChannel 945 946openBasicChannel(aid: number[], p2: number): Promise\<Channel> 947 948打开基础通道,参考[ISO 7816-4]协议,返回基础hannel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。 949 950**系统能力:** SystemCapability.Communication.SecureElement 951 952**参数:** 953 954| **参数名** | **类型** | **必填** | **说明** | 955| ---------- | -------- | ------ | ------------------------------------------------------------ | 956| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 957| p2 | number | 是 |在该Channel上执行的SELECT APDU的P2参数。 | 958 959**返回值:** 960 961| **类型** | **说明** | 962| -------- | --------------------- | 963| Channel | 以Promise形式异步返回可用的基础Channel对象实例。 | 964 965**错误码:** 966 967错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 968 969| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openBasicChannel3 " + "exception: ${(e : BusinessError).message}"); 1019} 1020``` 1021 1022## Session.openBasicChannel 1023 1024openBasicChannel(aid: number[], p2:number, callback: AsyncCallback\<Channel>): void 1025 1026打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。 1027 1028**系统能力:** SystemCapability.Communication.SecureElement 1029 1030**参数:** 1031 1032| **参数名** | **类型** | **必填** | **说明** | 1033| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1034| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1035| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1036| callback | AsyncCallback\<Channel> | 是 | 以callback形式异步返回可用的基础Channel对象实例。 | 1037 1038**错误码:** 1039 1040错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1041 1042| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("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.error("openBasicChannel4 " + "exception: ${(e : BusinessError).message}"); 1094} 1095``` 1096 1097## Session.openLogicalChannel 1098 1099openLogicalChannel(aid: number[]): Promise\<Channel> 1100 1101打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。 1102 1103**系统能力:** SystemCapability.Communication.SecureElement 1104 1105**参数:** 1106 1107| **参数名** | **类型** | **必填** | **说明** | 1108| ---------- | -------- | ------ | --------------------------------------- | 1109| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1110 1111**返回值:** 1112 1113| **类型** | **说明** | 1114| -------- | ------------------------------------------------------------ | 1115| Channel | 以Promise形式异步返回可用的逻辑Channel对象实例。 | 1116 1117**错误码:** 1118 1119错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1120 1121| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openLogicalChannel1 failed"); 1163 } 1164 } 1165 if (getPromise != null) { 1166 console.log("openLogicalChannel1 get channel successfully"); 1167 } 1168} catch (e) { 1169 console.error("openLogicalChannel1 " + "exception: ${(e : BusinessError).message}"); 1170} 1171``` 1172 1173## Session.openLogicalChannel 1174 1175 openLogicalChannel(aid: number[], callback: AsyncCallback\<Channel>): void 1176 1177打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。 1178 1179**系统能力:** SystemCapability.Communication.SecureElement 1180 1181**参数:** 1182 1183| **参数名** | **类型** | **必填** | **说明** | 1184| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1185| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1186| callback | AsyncCallback\<Channel> | 是 | 以callback形式异步返回可用的逻辑Channel对象实例。 | 1187 1188**错误码:** 1189 1190错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1191 1192| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openLogicalChannel2 failed:" + JSON.stringify(error)); 1234 return; 1235 } 1236 console.log("openLogicalChannel2 get channel successfully"); 1237 }); 1238 } 1239} catch (e) { 1240 console.error("openLogicalChannel2 " + "exception: ${(e : BusinessError).message}"); 1241} 1242``` 1243 1244## Session.openLogicalChannel 1245 1246openLogicalChannel(aid: number[], p2: number): Promise\<Channel> 1247 1248打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。 1249 1250**系统能力:** SystemCapability.Communication.SecureElement 1251 1252**参数:** 1253 1254| **参数名** | **类型** | **必填** | **说明** | 1255| ---------- | -------- | ------ | ----------------------------------------- | 1256| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1257| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1258 1259**返回值:** 1260 1261| **类型** | **说明** | 1262| -------- | -------------- | 1263| Channel | 以Promise形式异步返回可用的逻辑Channel实例对象。 | 1264 1265**错误码:** 1266 1267错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1268 1269| 错误码ID | 错误信息 | 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**示例:** 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.error("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.error("openLogicalChannel3 " + "exception: ${(e : BusinessError).message}"); 1319} 1320``` 1321 1322## Session.openLogicalChannel 1323 1324openLogicalChannel(aid: number[], p2: number, callback: AsyncCallback\<Channel>):void 1325 1326打开逻辑通道,参考[ISO 7816-4]协议,返回Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。 1327 1328**系统能力:** SystemCapability.Communication.SecureElement 1329 1330**参数:** 1331 1332| **参数名** | **类型** | **必填** | **说明** | 1333| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1334| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1335| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1336| callback | AsyncCallback\<Channel> | 是 | 以callback形式异步返回可用的逻辑Channel对象实例。 | 1337 1338**错误码:** 1339 1340错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1341 1342| 错误码ID | 错误信息 | 1343| -------- | -------------------------------- | 1344| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1345| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1346| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1347| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1348 1349**示例:** 1350 1351```js 1352import omapi from '@ohos.secureElement'; 1353import secureElement from '@ohos.secureElement'; 1354import { BusinessError } from '@ohos.base'; 1355 1356let nfcSEService : omapi.SEService | null = null; 1357let nfcOmaReaderList : omapi.Reader[] | null = null; 1358let omaSession : omapi.Session | null = null; 1359let nfcOmaChannel : omapi.Channel | null = null; 1360let aidArray : number[] = [720, 1080]; 1361let p2 : number = 0x00; 1362 1363try { 1364 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1365 if (state == secureElement.ServiceState.DISCONNECTED) { 1366 console.log("Service state is Disconnected"); 1367 } else { 1368 console.log("Service state is Connected"); 1369 } 1370 }); 1371} catch (e) { 1372 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1373} 1374 1375try { 1376 if (nfcSEService != null) { 1377 nfcOmaReaderList = nfcSEService.getReaders(); 1378 } 1379 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1380 omaSession = nfcOmaReaderList[0].openSession(); 1381 } 1382 if (omaSession != null) { 1383 omaSession.openLogicalChannel(aidArray, p2, (error, data) => { 1384 if (error) { 1385 console.error("openLogicalChannel4 failed:" + JSON.stringify(error)); 1386 return; 1387 } 1388 nfcOmaChannel = data; 1389 console.log("openLogicalChannel4 get channel successfully"); 1390 }); 1391 } 1392} catch (e) { 1393 console.error("openLogicalChannel4 " + "exception: ${(e : BusinessError).message}"); 1394} 1395``` 1396 1397## Channel. getSession 1398 1399 getSession(): Session 1400 1401获取打开该Channel的Session对象。 1402 1403**系统能力:** SystemCapability.Communication.SecureElement 1404 1405**返回值:** 1406 1407| **类型** | **说明** | 1408| -------- | ----------------------------- | 1409| Session | 该Channel绑定的Session 对象。 | 1410 1411**示例:** 1412 1413```js 1414import omapi from '@ohos.secureElement'; 1415import secureElement from '@ohos.secureElement'; 1416import { BusinessError } from '@ohos.base'; 1417 1418let nfcSEService : omapi.SEService | null = null; 1419let nfcOmaReaderList : omapi.Reader[] | null = null; 1420let omaSession : omapi.Session | null = null; 1421let getPromise : Promise<omapi.Channel> | null = null; 1422let aidArray : number[] = [720, 1080]; 1423let p2 : number = 0x00; 1424let mySession : omapi.Session | null = null; 1425 1426try { 1427 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1428 if (state == secureElement.ServiceState.DISCONNECTED) { 1429 console.log("Service state is Disconnected"); 1430 } else { 1431 console.log("Service state is Connected"); 1432 } 1433 }); 1434} catch (e) { 1435 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1436} 1437 1438try { 1439 if (nfcSEService != null) { 1440 nfcOmaReaderList = nfcSEService.getReaders(); 1441 } 1442 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1443 omaSession = nfcOmaReaderList[0].openSession(); 1444 } 1445 if (omaSession != null) { 1446 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1447 getPromise.then((channel) => { 1448 mySession = channel.getSession(); 1449 console.log("openLogicalChannel get channel successfully"); 1450 }) 1451 } 1452 if (mySession != null) { 1453 console.log("get session successfully"); 1454 } else { 1455 console.error("get session failed"); 1456 } 1457} catch (e) { 1458 console.error("get session " + "exception: ${(e : BusinessError).message}"); 1459} 1460``` 1461 1462## Channel. close 1463 1464close(): void 1465 1466关闭Channel。 1467 1468**系统能力:** SystemCapability.Communication.SecureElement 1469 1470**示例:** 1471 1472```js 1473import omapi from '@ohos.secureElement'; 1474import secureElement from '@ohos.secureElement'; 1475import { BusinessError } from '@ohos.base'; 1476 1477let nfcSEService : omapi.SEService | null = null; 1478let nfcOmaReaderList : omapi.Reader[] | null = null; 1479let omaSession : omapi.Session | null = null; 1480let getPromise : Promise<omapi.Channel> | null = null; 1481let aidArray : number[] = [720, 1080]; 1482let p2 : number = 0x00; 1483 1484try { 1485 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1486 if (state == secureElement.ServiceState.DISCONNECTED) { 1487 console.log("Service state is Disconnected"); 1488 } else { 1489 console.log("Service state is Connected"); 1490 } 1491 }); 1492} catch (e) { 1493 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1494} 1495 1496try { 1497 if (nfcSEService != null) { 1498 nfcOmaReaderList = nfcSEService.getReaders(); 1499 } 1500 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1501 omaSession = nfcOmaReaderList[0].openSession(); 1502 } 1503 if (omaSession != null) { 1504 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1505 getPromise.then((channel) => { 1506 channel.close(); 1507 console.log("channel close successfully"); 1508 }) 1509 } 1510} catch (e) { 1511 console.error("channel close " + "exception: ${(e : BusinessError).message}"); 1512} 1513``` 1514 1515## Channel. isBasicChannel 1516 1517isBasicChannel(): boolean 1518 1519检查该Channel是否为基础Channel。 1520 1521**系统能力:** SystemCapability.Communication.SecureElement 1522 1523**返回值:** 1524 1525| **类型** | **说明** | 1526| -------- | ------------------------------------------------------------ | 1527| boolean | true: 该Channel是基础Channel, false:该Channel逻辑Channel 。 | 1528 1529**示例:** 1530 1531```js 1532import omapi from '@ohos.secureElement'; 1533import secureElement from '@ohos.secureElement'; 1534import { BusinessError } from '@ohos.base'; 1535 1536let nfcSEService : omapi.SEService | null = null; 1537let nfcOmaReaderList : omapi.Reader[] | null = null; 1538let omaSession : omapi.Session | null = null; 1539let getPromise : Promise<omapi.Channel> | null = null; 1540let aidArray : number[] = [720, 1080]; 1541let p2 : number = 0x00; 1542let ret : boolean = false; 1543 1544try { 1545 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1546 if (state == secureElement.ServiceState.DISCONNECTED) { 1547 console.log("Service state is Disconnected"); 1548 } else { 1549 console.log("Service state is Connected"); 1550 } 1551 }); 1552} catch (e) { 1553 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1554} 1555 1556try { 1557 if (nfcSEService != null) { 1558 nfcOmaReaderList = nfcSEService.getReaders(); 1559 } 1560 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1561 omaSession = nfcOmaReaderList[0].openSession(); 1562 } 1563 if (omaSession != null) { 1564 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1565 getPromise.then((channel) => { 1566 ret = channel.isBasicChannel(); 1567 }) 1568 } 1569 if (ret) { 1570 console.log("isBasicChannel TRUE"); 1571 } else { 1572 console.log("isBasicChannel FALSE"); 1573 } 1574} catch (e) { 1575 console.error("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1576} 1577``` 1578 1579## Channel. isClosed 1580 1581isClosed(): boolean 1582 1583检查该Channel是否已被关闭。 1584 1585**系统能力:** SystemCapability.Communication.SecureElement 1586 1587**返回值:** 1588 1589| **类型** | **说明** | 1590| -------- | --------------------------------------------- | 1591| boolean | true: Channel是关闭的,false: 不是关闭的。 | 1592 1593**示例:** 1594 1595```js 1596import omapi from '@ohos.secureElement'; 1597import secureElement from '@ohos.secureElement'; 1598import { BusinessError } from '@ohos.base'; 1599 1600let nfcSEService : omapi.SEService | null = null; 1601let nfcOmaReaderList : omapi.Reader[] | null = null; 1602let omaSession : omapi.Session | null = null; 1603let getPromise : Promise<omapi.Channel> | null = null; 1604let aidArray : number[] = [720, 1080]; 1605let p2 : number = 0x00; 1606let ret : boolean = false; 1607 1608try { 1609 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1610 if (state == secureElement.ServiceState.DISCONNECTED) { 1611 console.log("Service state is Disconnected"); 1612 } else { 1613 console.log("Service state is Connected"); 1614 } 1615 }); 1616} catch (e) { 1617 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1618} 1619 1620try { 1621 if (nfcSEService != null) { 1622 nfcOmaReaderList = nfcSEService.getReaders(); 1623 } 1624 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1625 omaSession = nfcOmaReaderList[0].openSession(); 1626 } 1627 if (omaSession != null) { 1628 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1629 getPromise.then((channel) => { 1630 ret = channel.isClosed(); 1631 }) 1632 } 1633 if (ret) { 1634 console.log("channel isClosed TRUE"); 1635 } else { 1636 console.log("channel isClosed False"); 1637 } 1638} catch (e) { 1639 console.error("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1640} 1641``` 1642 1643## Channel. getSelectResponse 1644 1645getSelectResponse():number[] 1646 1647获取SELECT Applet时的响应数据,包含状态字。 1648 1649**系统能力:** SystemCapability.Communication.SecureElement 1650 1651**返回值:** 1652 1653| **类型** | **说明** | 1654| -------- | ------------------------------------------------------------ | 1655| number[] | SELECT Applet时的响应数据,包含状态字。 | 1656 1657**错误码:** 1658 1659错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1660 1661**示例:** 1662 1663```js 1664import omapi from '@ohos.secureElement'; 1665import secureElement from '@ohos.secureElement'; 1666import { BusinessError } from '@ohos.base'; 1667 1668let nfcSEService : omapi.SEService | null = null; 1669let nfcOmaReaderList : omapi.Reader[] | null = null; 1670let omaSession : omapi.Session | null = null; 1671let getPromise : Promise<omapi.Channel> | null = null; 1672let aidArray : number[] = [720, 1080]; 1673let p2 : number = 0x00; 1674let responseArray : number[] = [720, 1080]; 1675let str : string = ""; 1676 1677try { 1678 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1679 if (state == secureElement.ServiceState.DISCONNECTED) { 1680 console.log("Service state is Disconnected"); 1681 } else { 1682 console.log("Service state is Connected"); 1683 } 1684 }); 1685} catch (e) { 1686 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1687} 1688 1689try { 1690 if (nfcSEService != null) { 1691 nfcOmaReaderList = nfcSEService.getReaders(); 1692 } 1693 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1694 omaSession = nfcOmaReaderList[0].openSession(); 1695 } 1696 if (omaSession != null) { 1697 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1698 getPromise.then((channel) => { 1699 responseArray = channel.getSelectResponse(); 1700 }) 1701 } 1702 if (responseArray) { 1703 str = "getSelectResponse result:["; 1704 for (let i = 0; i < responseArray.length; ++i) { 1705 str += responseArray[i]; 1706 str += ' '; 1707 } 1708 str += ']'; 1709 console.log(str); 1710 } else { 1711 console.error("getSelectResponse result is null"); 1712 } 1713} catch (e) { 1714 console.error("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1715} 1716``` 1717 1718## Channel. transmit 1719 1720transmit(command: number[]): Promise\<number[]> 1721 1722向SE发送APDU数据,数据符合ISO/IEC 7816规范。 1723 1724**系统能力:** SystemCapability.Communication.SecureElement 1725 1726**参数:** 1727 1728| **参数名** | **类型** | **必填** | **说明** | 1729| ---------- | -------- | ------ | ------------------------------------- | 1730| command | number[] | 是 | 需要发送到SE的APDU数据。 | 1731 1732**返回值:** 1733 1734| **类型** | **说明** | 1735| -------- | -------------- | 1736| number[] | 以Promise形式异步返回接收到的响应APDU数据,number数组。 | 1737 1738**错误码:** 1739 1740错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1741 1742| 错误码ID | 错误信息 | 1743| -------- | -------------------------------- | 1744| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1745| 3300103 | SecurityError, the command is filtered by the security policy. | 1746| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1747 1748**示例:** 1749 1750```js 1751import omapi from '@ohos.secureElement'; 1752import secureElement from '@ohos.secureElement'; 1753import { BusinessError } from '@ohos.base'; 1754 1755let nfcSEService : omapi.SEService | null = null; 1756let nfcOmaReaderList : omapi.Reader[] | null = null; 1757let omaSession : omapi.Session | null = null; 1758let getPromise : Promise<omapi.Channel> | null = null; 1759let aidArray : number[] = [720, 1080]; 1760let p2 : number = 0x00; 1761let responseArray : Promise<number[]> | null = null; 1762 1763try { 1764 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1765 if (state == secureElement.ServiceState.DISCONNECTED) { 1766 console.log("Service state is Disconnected"); 1767 } else { 1768 console.log("Service state is Connected"); 1769 } 1770 }); 1771} catch (e) { 1772 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1773} 1774 1775try { 1776 if (nfcSEService != null) { 1777 nfcOmaReaderList = nfcSEService.getReaders(); 1778 } 1779 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1780 omaSession = nfcOmaReaderList[0].openSession(); 1781 } 1782 if (omaSession != null) { 1783 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1784 getPromise.then((channel) => { 1785 let command: number[] = [100, 200]; 1786 // refer to Session.openBasicChannel for this.nfcOmaChannel 1787 responseArray = channel.transmit(command); 1788 }) 1789 } 1790 if (responseArray != null) { 1791 console.log("transmit1 success"); 1792 } else { 1793 console.error("transmit1 failed"); 1794 } 1795} catch (e) { 1796 console.error("transmit1 " + "exception: ${(e : BusinessError).message}"); 1797} 1798``` 1799 1800## Channel. transmit 1801 1802transmit(command: number[], callback: AsyncCallback\<number[]>): void 1803 1804向SE发送APDU数据,数据符合ISO/IEC 7816规范。 1805 1806**系统能力:** SystemCapability.Communication.SecureElement 1807 1808**参数:** 1809 1810| **参数名** | **类型** | **必填** | **说明** | 1811| ---------- | ----------------------- | ------ | ------------------------------------- | 1812| command | number[] | 是 | 需要发送到SE的APDU数据。 | 1813| callback | AsyncCallback\<number[]> | 是 | 返回接收到的响应APDU数据,number数组。 | 1814 1815**错误码:** 1816 1817错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1818 1819| 错误码ID | 错误信息 | 1820| -------- | -------------------------------- | 1821| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1822| 3300103 | SecurityError, the command is filtered by the security policy. | 1823| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1824 1825**示例:** 1826 1827```js 1828import omapi from '@ohos.secureElement'; 1829import secureElement from '@ohos.secureElement'; 1830import { BusinessError } from '@ohos.base'; 1831 1832let nfcSEService : omapi.SEService | null = null; 1833let nfcOmaReaderList : omapi.Reader[] | null = null; 1834let omaSession : omapi.Session | null = null; 1835let getPromise : Promise<omapi.Channel> | null = null; 1836let aidArray : number[] = [720, 1080]; 1837let p2 : number = 0x00; 1838let str : string = ""; 1839 1840try { 1841 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1842 if (state == secureElement.ServiceState.DISCONNECTED) { 1843 console.log("Service state is Disconnected"); 1844 } else { 1845 console.log("Service state is Connected"); 1846 } 1847 }); 1848} catch (e) { 1849 console.error("newSEService " + "exception: ${(e : BusinessError).message}"); 1850} 1851 1852try { 1853 if (nfcSEService != null) { 1854 nfcOmaReaderList = nfcSEService.getReaders(); 1855 } 1856 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1857 omaSession = nfcOmaReaderList[0].openSession(); 1858 } 1859 if (omaSession != null) { 1860 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1861 getPromise.then((channel) => { 1862 let command: number[] = [100, 200]; 1863 // refer to Session.openBasicChannel for this.nfcOmaChannel 1864 channel.transmit(command, (error, data) => { 1865 if (error) { 1866 console.error("transmit2 exception:" + JSON.stringify(error)); 1867 return; 1868 } 1869 str = "transmit2 result:["; 1870 for (let i = 0; i < data.length; ++i) { 1871 str += data[i]; 1872 str += " "; 1873 } 1874 str += "]"; 1875 console.log(str) 1876 }); 1877 }) 1878 } 1879} catch (e) { 1880 console.error("transmit2 " + "exception: ${(e : BusinessError).message}"); 1881} 1882``` 1883