1# @ohos.secureElement (安全单元的通道管理) 2 3本模块主要用于操作及管理安全单元(SecureElement,简称SE)。文档中出现的SE服务为SEService实例,参见[newSEService](#secureelementnewseservice)。 4 5对于文档中出现以下类型说明: 6 7| 类型 | 说明 | 8| ------- | ---------------------------------------------- | 9| Session | 此类的实例表示与设备上可用的某个SE的连接会话。 | 10| Reader | 此类的实例表示该设备支持的SE Reader。 | 11| Channel | 此类的实例表示向SE打开的ISO/IEC 7816-4通道。 | 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.log("newSEService occurs " + "exception: ${(e : BusinessError).message}"); 73} 74``` 75 76## SEService.getReaders 77 78getReaders(): Reader[] 79 80返回可用SE Reader的数组。返回的数组中不能有重复的对象。即使没有插入卡,也应列出所有可用的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.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 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.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 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.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 221返回此实现所基于的OMA规范的版本号。 222 223**系统能力:** SystemCapability.Communication.SecureElement 224 225**返回值:** 226 227| **类型** | **说明** | 228| -------- | -------------------------------------------------- | 229| string | OMA版本号(例如,“3.3”表示开放移动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.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 266返回此reader的名称。如果此读卡器是SIM reader,则其名称必须为“SIM[Slot]”。如果读卡器是嵌入式SE reader,则其名称须为“eSE[slot]”。 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.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 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.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 379连接到此reader中的SE。此方法在返回会话对象之前准备(初始化)SE进行通信。同一reader上可能同时打开多个会话。 380 381**系统能力:** SystemCapability.Communication.SecureElement 382 383**返回值:** 384 385| **类型** | **说明** | 386| -------- | ------------------------------ | 387| Session | 用于创建channel的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.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 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.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 nfcOmaReaderList[0].closeSessions() 482 console.log("closeSessions successfully"); 483 } else { 484 console.log("closeSessions failed"); 485 } 486} catch (e) { 487 console.log("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.log("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.log("get reader failed"); 538 } 539 } else { 540 console.log("getReader failed"); 541 } 542} catch (e) { 543 console.log("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.log("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.log("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.log("getATR failed"); 616 } 617} catch (e) { 618 console.log("getATR " + "exception: ${(e : BusinessError).message}"); 619} 620``` 621 622## Session.close 623 624close(): void 625 626关闭与SE的连接。这将关闭此应用程序与此SE打开的所有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.log("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.log("close failed"); 671 } 672 } 673} catch (e) { 674 console.log("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。 | 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.log("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 && omaSession.isClosed()) { 727 console.log("isClosed success"); 728 } else { 729 console.log("isClosed failed"); 730 } 731 } 732} catch (e) { 733 console.log("isClosed " + "exception: ${(e : BusinessError).message}"); 734} 735``` 736 737## Session.closeChannels 738 739closeChannels(): void 740 741关闭此session上打开的所有channel。 742 743**系统能力:** SystemCapability.Communication.SecureElement 744 745**错误码:** 746 747错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 748 749| 错误码ID | 错误信息 | 750| -------- | -------------------------------- | 751| 3300101 | IllegalStateError, service state exception. | 752 753**示例:** 754 755```js 756import omapi from '@ohos.secureElement'; 757import secureElement from '@ohos.secureElement'; 758import { BusinessError } from '@ohos.base'; 759 760let nfcSEService : omapi.SEService | null = null; 761let nfcOmaReaderList : omapi.Reader[] | null = null; 762let omaSession : omapi.Session | null = null; 763 764try { 765 nfcSEService = secureElement.newSEService("serviceState", (state) => { 766 if (state == secureElement.ServiceState.DISCONNECTED) { 767 console.log("Service state is Disconnected"); 768 } else { 769 console.log("Service state is Connected"); 770 } 771 }); 772} catch (e) { 773 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 774} 775 776try { 777 if(nfcSEService != null) { 778 nfcOmaReaderList = nfcSEService.getReaders(); 779 } 780 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 781 omaSession = nfcOmaReaderList[0].openSession(); 782 if (omaSession != null) { 783 omaSession.closeChannels(); 784 console.log("closeChannels success"); 785 } else { 786 console.log("closeChannels failed"); 787 } 788 } 789} catch (e) { 790 console.log("closeChannels " + "exception: ${(e : BusinessError).message}"); 791} 792``` 793 794## Session.openBasicChannel 795 796openBasicChannel(aid: number[]): Promise\<Channel> 797 798获取基本channel,参考[ISO 7816-4]协议,返回Channel实例对象,SE不能提供新逻辑Channel或因缺乏可用逻辑Channel对象而无法获取访问控制规则,返回null。 799 800**系统能力:** SystemCapability.Communication.SecureElement 801 802**参数:** 803 804| **参数名** | **类型** | **必填** | **说明** | 805| ---------- | -------- | ------ | ------------------------------------------------------------ | 806| aid | number[] | 是 |在此channel上选择的applet的AID数组或如果没有applet被选择时空的数组null。 | 807 808**返回值:** 809 810| **类型** | **说明** | 811| -------- | --------------------- | 812| Channel | 可用Channel对象实例。 | 813 814**错误码:** 815 816错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 817 818| 错误码ID | 错误信息 | 819| -------- | -------------------------------- | 820| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 821| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 822| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 823| 3300104 | IOError, there is a communication problem to the reader or the SE. | 824 825**示例:** 826 827```js 828import omapi from '@ohos.secureElement'; 829import secureElement from '@ohos.secureElement'; 830import { BusinessError } from '@ohos.base'; 831 832let nfcSEService : omapi.SEService | null = null; 833let nfcOmaReaderList : omapi.Reader[] | null = null; 834let omaSession : omapi.Session | null = null; 835let aidArray : number[] = [720, 1080]; 836let getPromise : Promise<omapi.Channel> | null = null; 837 838try { 839 nfcSEService = secureElement.newSEService("serviceState", (state) => { 840 if (state == secureElement.ServiceState.DISCONNECTED) { 841 console.log("Service state is Disconnected"); 842 } else { 843 console.log("Service state is Connected"); 844 } 845 }); 846} catch (e) { 847 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 848} 849 850try { 851 if(nfcSEService != null) { 852 nfcOmaReaderList = nfcSEService.getReaders(); 853 } 854 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 855 omaSession = nfcOmaReaderList[0].openSession(); 856 if (omaSession != null) { 857 getPromise = omaSession.openBasicChannel(aidArray); 858 } else { 859 console.log("openBasicChannel1 failed"); 860 } 861 } 862 if (getPromise != null) { 863 console.log("openBasicChannel1 get channel successfully"); 864 } 865} catch (e) { 866 console.log("openBasicChannel1 " + "exception: ${(e : BusinessError).message}"); 867} 868``` 869 870## Session.openBasicChannel 871 872 openBasicChannel(aid: number[], callback: AsyncCallback\<Channel>): void 873 874获取基本channel,参考[ISO 7816-4]协议,返回channel实例对象,SE不能提供新逻辑Channel或因缺乏可用逻辑Channel对象而无法获取访问控制规则,返回null。 875 876**系统能力:** SystemCapability.Communication.SecureElement 877 878**参数:** 879 880| **参数名** | **类型** | **必填** | **说明** | 881| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 882| aid | number[] | 是 | 在此channel上选择的applet的AID数组或null 如果没有applet被选择。 | 883| callback | AsyncCallback\<Channel> | 是 | callback返回可用Channel对象实例。 | 884 885**错误码:** 886 887错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 888 889| 错误码ID | 错误信息 | 890| -------- | -------------------------------- | 891| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 892| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 893| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 894| 3300104 | IOError, there is a communication problem to the reader or the SE. | 895 896**示例:** 897 898```js 899import omapi from '@ohos.secureElement'; 900import secureElement from '@ohos.secureElement'; 901import { BusinessError } from '@ohos.base'; 902 903let nfcSEService : omapi.SEService | null = null; 904let nfcOmaReaderList : omapi.Reader[] | null = null; 905let omaSession : omapi.Session | null = null; 906let aidArray : number[] = [720, 1080]; 907 908try { 909 nfcSEService = secureElement.newSEService("serviceState", (state) => { 910 if (state == secureElement.ServiceState.DISCONNECTED) { 911 console.log("Service state is Disconnected"); 912 } else { 913 console.log("Service state is Connected"); 914 } 915 }); 916} catch (e) { 917 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 918} 919 920try { 921 if(nfcSEService != null) { 922 nfcOmaReaderList = nfcSEService.getReaders(); 923 } 924 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 925 omaSession = nfcOmaReaderList[0].openSession(); 926 if (omaSession != null) { 927 omaSession.openBasicChannel(aidArray, (error, data) => { 928 if (error) { 929 console.log("openBasicChannel2 failed:" + JSON.stringify(error)); 930 return; 931 } 932 console.log("openBasicChannel2 get channel successfully"); 933 }); 934 } 935 } 936} catch (e) { 937 console.log("openBasicChannel2 " + "exception: ${(e : BusinessError).message}"); 938} 939``` 940 941## Session.openBasicChannel 942 943openBasicChannel(aid: number[], p2: number): Promise\<Channel> 944 945获取基本channel,参考[ISO 7816-4]协议,返回Channel实例对象,SE不能提供新逻辑Channel对象或因缺乏可用逻辑Channel对象而无法获取访问控制规则,返回null。 946 947**系统能力:** SystemCapability.Communication.SecureElement 948 949**参数:** 950 951| **参数名** | **类型** | **必填** | **说明** | 952| ---------- | -------- | ------ | ------------------------------------------------------------ | 953| aid | number[] | 是 | 在此channel上选择的applet的AID数组或null 如果没有applet被选择。 | 954| p2 | number | 是 |在该channel上执行的SELECT APDU的P2参数。 | 955 956**返回值:** 957 958| **类型** | **说明** | 959| -------- | --------------------- | 960| Channel | 可用Channel对象实例。 | 961 962**错误码:** 963 964错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 965 966| 错误码ID | 错误信息 | 967| -------- | -------------------------------- | 968| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 969| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 970| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 971| 3300104 | IOError, there is a communication problem to the reader or the SE. | 972 973**示例:** 974 975```js 976import omapi from '@ohos.secureElement'; 977import secureElement from '@ohos.secureElement'; 978import { BusinessError } from '@ohos.base'; 979 980let nfcSEService : omapi.SEService | null = null; 981let nfcOmaReaderList : omapi.Reader[] | null = null; 982let omaSession : omapi.Session | null = null; 983let getPromise : Promise<omapi.Channel> | null = null; 984let nfcOmaChannel : omapi.Channel | null = null; 985let aidArray : number[] = [720, 1080]; 986let p2 : number = 0x00; 987 988try { 989 nfcSEService = secureElement.newSEService("serviceState", (state) => { 990 if (state == secureElement.ServiceState.DISCONNECTED) { 991 console.log("Service state is Disconnected"); 992 } else { 993 console.log("Service state is Connected"); 994 } 995 }); 996} catch (e) { 997 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 998} 999 1000try { 1001 if(nfcSEService != null) { 1002 nfcOmaReaderList = nfcSEService.getReaders(); 1003 } 1004 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1005 omaSession = nfcOmaReaderList[0].openSession(); 1006 } 1007 if (omaSession != null) { 1008 getPromise = omaSession.openBasicChannel(aidArray, p2); 1009 getPromise.then((channel) => { 1010 nfcOmaChannel = channel; 1011 console.log("openBasicChannel3 get channel successfully"); 1012 }) 1013 } 1014} catch (e) { 1015 console.log("openBasicChannel3 " + "exception: ${(e : BusinessError).message}"); 1016} 1017``` 1018 1019## Session.openBasicChannel 1020 1021openBasicChannel(aid: number[], p2:number, callback: AsyncCallback\<Channel>): void 1022 1023获取基本channel,参考[ISO 7816-4]协议,返回channel实例对象,SE不能提供新逻辑Channel对象或因缺乏可用逻辑Channel对象而无法获取访问控制规则,返回null。 1024 1025**系统能力:** SystemCapability.Communication.SecureElement 1026 1027**参数:** 1028 1029| **参数名** | **类型** | **必填** | **说明** | 1030| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1031| aid | number[] | 是 | 在此channel上选择的applet的AID数组或null 如果没有applet被选择。 | 1032| p2 | number | 是 | 此channel上执行SELECT APDU命令的P2参数。 | 1033| callback | AsyncCallback\<Channel> | 是 | callback返回可用Channel对象实例。 | 1034 1035**错误码:** 1036 1037错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1038 1039| 错误码ID | 错误信息 | 1040| -------- | -------------------------------- | 1041| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1042| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1043| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1044| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1045 1046**示例:** 1047 1048```js 1049import omapi from '@ohos.secureElement'; 1050import secureElement from '@ohos.secureElement'; 1051import { BusinessError } from '@ohos.base'; 1052 1053let nfcSEService : omapi.SEService | null = null; 1054let nfcOmaReaderList : omapi.Reader[] | null = null; 1055let omaSession : omapi.Session | null = null; 1056let nfcOmaChannel : omapi.Channel | null = null; 1057let aidArray : number[] = [720, 1080]; 1058let p2 : number = 0x00; 1059 1060try { 1061 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1062 if (state == secureElement.ServiceState.DISCONNECTED) { 1063 console.log("Service state is Disconnected"); 1064 } else { 1065 console.log("Service state is Connected"); 1066 } 1067 }); 1068} catch (e) { 1069 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1070} 1071 1072try { 1073 if(nfcSEService != null) { 1074 nfcOmaReaderList = nfcSEService.getReaders(); 1075 } 1076 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1077 omaSession = nfcOmaReaderList[0].openSession(); 1078 } 1079 if (omaSession != null) { 1080 omaSession.openBasicChannel(aidArray, p2, (error , data) => { 1081 if (error) { 1082 console.log("openBasicChannel4 failed:" + JSON.stringify(error)); 1083 return; 1084 } 1085 nfcOmaChannel = data; 1086 console.log("openBasicChannel4 get channel successfully"); 1087 }); 1088 } 1089} catch (e) { 1090 console.log("openBasicChannel4 " + "exception: ${(e : BusinessError).message}"); 1091} 1092``` 1093 1094## Session.openLogicalChannel 1095 1096openLogicalChannel(aid: number[]): Promise\<Channel> 1097 1098打开指定SE的逻辑Channel对象。 1099 1100**系统能力:** SystemCapability.Communication.SecureElement 1101 1102**参数:** 1103 1104| **参数名** | **类型** | **必填** | **说明** | 1105| ---------- | -------- | ------ | --------------------------------------- | 1106| aid | number[] | 是 | 在该Channel对象上选择的applet AID数组。 | 1107 1108**返回值:** 1109 1110| **类型** | **说明** | 1111| -------- | ------------------------------------------------------------ | 1112| Channel | 返回可用Channel对象实例,SE不能提供新的Channel对象或因缺乏可用逻辑Channel对象无法获取访问控制规则返回null。 | 1113 1114**错误码:** 1115 1116错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1117 1118| 错误码ID | 错误信息 | 1119| -------- | -------------------------------- | 1120| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1121| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1122| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1123| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1124 1125**示例:** 1126 1127```js 1128import omapi from '@ohos.secureElement'; 1129import secureElement from '@ohos.secureElement'; 1130import { BusinessError } from '@ohos.base'; 1131 1132let nfcSEService : omapi.SEService | null = null; 1133let nfcOmaReaderList : omapi.Reader[] | null = null; 1134let omaSession : omapi.Session | null = null; 1135let aidArray : number[] = [720, 1080]; 1136let getPromise : Promise<omapi.Channel> | null = null; 1137 1138try { 1139 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1140 if (state == secureElement.ServiceState.DISCONNECTED) { 1141 console.log("Service state is Disconnected"); 1142 } else { 1143 console.log("Service state is Connected"); 1144 } 1145 }); 1146} catch (e) { 1147 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1148} 1149 1150try { 1151 if(nfcSEService != null) { 1152 nfcOmaReaderList = nfcSEService.getReaders(); 1153 } 1154 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1155 omaSession = nfcOmaReaderList[0].openSession(); 1156 if (omaSession != null) { 1157 getPromise = omaSession.openLogicalChannel(aidArray); 1158 } else { 1159 console.log("openLogicalChannel1 failed"); 1160 } 1161 } 1162 if (getPromise != null) { 1163 console.log("openLogicalChannel1 get channel successfully"); 1164 } 1165} catch (e) { 1166 console.log("openLogicalChannel1 " + "exception: ${(e : BusinessError).message}"); 1167} 1168``` 1169 1170## Session.openLogicalChannel 1171 1172 openLogicalChannel(aid:number[], callback: AsyncCallback\<Channel>): void 1173 1174打开指定SE的逻辑Channel对象。 1175 1176**系统能力:** SystemCapability.Communication.SecureElement 1177 1178**参数:** 1179 1180| **参数名** | **类型** | **必填** | **说明** | 1181| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1182| aid | number[] | 是 | 在该Channel对象上被选择的applet AID数组。 | 1183| callback | AsyncCallback\<Channel> | 是 | callback返回可用Channel对象实例,SE不能提供新的channel或因缺乏可用逻辑Channel对象无法获取访问控制规则返回null。 | 1184 1185**错误码:** 1186 1187错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1188 1189| 错误码ID | 错误信息 | 1190| -------- | -------------------------------- | 1191| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1192| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1193| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1194| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1195 1196**示例:** 1197 1198```js 1199import omapi from '@ohos.secureElement'; 1200import secureElement from '@ohos.secureElement'; 1201import { BusinessError } from '@ohos.base'; 1202 1203let nfcSEService : omapi.SEService | null = null; 1204let nfcOmaReaderList : omapi.Reader[] | null = null; 1205let omaSession : omapi.Session | null = null; 1206let aidArray : number[] = [720, 1080]; 1207 1208try { 1209 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1210 if (state == secureElement.ServiceState.DISCONNECTED) { 1211 console.log("Service state is Disconnected"); 1212 } else { 1213 console.log("Service state is Connected"); 1214 } 1215 }); 1216} catch (e) { 1217 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1218} 1219 1220try { 1221 if(nfcSEService != null) { 1222 nfcOmaReaderList = nfcSEService.getReaders(); 1223 } 1224 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1225 omaSession = nfcOmaReaderList[0].openSession(); 1226 } 1227 if (omaSession != null) { 1228 omaSession.openLogicalChannel(aidArray, (error, data) => { 1229 if (error) { 1230 console.log("openLogicalChannel2 failed:" + JSON.stringify(error)); 1231 return; 1232 } 1233 console.log("openLogicalChannel2 get channel successfully"); 1234 }); 1235 } 1236} catch (e) { 1237 console.log("openLogicalChannel2 " + "exception: ${(e : BusinessError).message}"); 1238} 1239``` 1240 1241## Session.openLogicalChannel 1242 1243openLogicalChannel(aid: number[], p2: number): Promise\<Channel> 1244 1245使用SE打开逻辑通道,选择由给定AID数组(AID非null且长度不为0)表示的applet. 1246 1247如果AID数组长度为0,则该方法将通过发送一个select命令来选择SE的Issuer Security Domain,该命令的AID长度为0(如[GPCS]中所定义)。 1248 1249如果AID为Null,则该方法应仅发送MANAGE CHANNEL Open(管理通道打开),而不应发送SELECT(选择)命令。在这种情况下,默认情况下将选择与逻辑通道关联的默认applet. 1250 1251P2通常为0x00。设备应允许P2的任何值,并且应允许以下值: 0x00, 0x04, 0x08, 0x0C (如 [ISO 7816-4](https://www.iso.org/standard/77180.html)中所定义). 1252 1253**系统能力:** SystemCapability.Communication.SecureElement 1254 1255**参数:** 1256 1257| **参数名** | **类型** | **必填** | **说明** | 1258| ---------- | -------- | ------ | ----------------------------------------- | 1259| aid | number[] | 是 | 在该Channel对象上被选择的applet AID数组。 | 1260| p2 | number | 是 | 此channel上执行SELECT APDU命令的P2参数。 | 1261 1262**错误码:** 1263 1264错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1265 1266| 错误码ID | 错误信息 | 1267| -------- | -------------------------------- | 1268| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1269| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1270| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1271| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1272 1273**示例:** 1274 1275```js 1276import omapi from '@ohos.secureElement'; 1277import secureElement from '@ohos.secureElement'; 1278import { BusinessError } from '@ohos.base'; 1279 1280let nfcSEService : omapi.SEService | null = null; 1281let nfcOmaReaderList : omapi.Reader[] | null = null; 1282let omaSession : omapi.Session | null = null; 1283let getPromise : Promise<omapi.Channel> | null = null; 1284let nfcOmaChannel : omapi.Channel | null = null; 1285let aidArray : number[] = [720, 1080]; 1286let p2 : number = 0x00; 1287 1288try { 1289 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1290 if (state == secureElement.ServiceState.DISCONNECTED) { 1291 console.log("Service state is Disconnected"); 1292 } else { 1293 console.log("Service state is Connected"); 1294 } 1295 }); 1296} catch (e) { 1297 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1298} 1299 1300try { 1301 if(nfcSEService != null) { 1302 nfcOmaReaderList = nfcSEService.getReaders(); 1303 } 1304 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1305 omaSession = nfcOmaReaderList[0].openSession(); 1306 } 1307 if (omaSession != null) { 1308 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1309 getPromise.then((channel) => { 1310 nfcOmaChannel = channel; 1311 console.log("openLogicalChannel3 get channel successfully"); 1312 }) 1313 } 1314} catch (e) { 1315 console.log("openLogicalChannel3 " + "exception: ${(e : BusinessError).message}"); 1316} 1317``` 1318 1319## Session.openLogicalChannel 1320 1321openLogicalChannel(aid: number[], p2: number, callback: AsyncCallback\<Channel>):void 1322 1323使用SE打开逻辑通道,选择由给定AID数组(AID非null且长度不为0)表示的applet. 1324 1325如果AID数组长度为0,则该方法将通过发送一个select命令来选择SE的Issuer Security Domain,该命令的AID长度为0(如[GPCS]中所定义)。 1326 1327如果AID为Null,则该方法应仅发送MANAGE CHANNEL Open(管理通道打开),而不应发送SELECT(选择)命令。在这种情况下,默认情况下将选择与逻辑通道关联的默认applet. 1328 1329P2通常为0x00。设备应允许P2的任何值,并且应允许以下值: 0x00, 0x04, 0x08, 0x0C (如 [ISO 7816-4](https://www.iso.org/standard/77180.html)中所定义). 1330 1331**系统能力:** SystemCapability.Communication.SecureElement 1332 1333**参数:** 1334 1335| **参数名** | **类型** | **必填** | **说明** | 1336| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1337| aid | number[] | 是 | 在该Channel对象上被选择的applet AID数组。 | 1338| p2 | number | 是 | 此channel上执行SELECT APDU命令的P2参数。 | 1339| callback | AsyncCallback\<Channel> | 是 | callback返回可用Channel对象实例,SE不能提供新的Channel对象或因缺乏可用逻辑Channel对象无法获取访问控制规则返回null。 | 1340 1341**错误码:** 1342 1343错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1344 1345| 错误码ID | 错误信息 | 1346| -------- | -------------------------------- | 1347| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1348| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1349| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1350| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1351 1352**示例:** 1353 1354```js 1355import omapi from '@ohos.secureElement'; 1356import secureElement from '@ohos.secureElement'; 1357import { BusinessError } from '@ohos.base'; 1358 1359let nfcSEService : omapi.SEService | null = null; 1360let nfcOmaReaderList : omapi.Reader[] | null = null; 1361let omaSession : omapi.Session | null = null; 1362let nfcOmaChannel : omapi.Channel | null = null; 1363let aidArray : number[] = [720, 1080]; 1364let p2 : number = 0x00; 1365 1366try { 1367 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1368 if (state == secureElement.ServiceState.DISCONNECTED) { 1369 console.log("Service state is Disconnected"); 1370 } else { 1371 console.log("Service state is Connected"); 1372 } 1373 }); 1374} catch (e) { 1375 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1376} 1377 1378try { 1379 if(nfcSEService != null) { 1380 nfcOmaReaderList = nfcSEService.getReaders(); 1381 } 1382 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1383 omaSession = nfcOmaReaderList[0].openSession(); 1384 } 1385 if (omaSession != null) { 1386 omaSession.openLogicalChannel(aidArray, p2, (error, data) => { 1387 if (error) { 1388 console.log("openLogicalChannel4 failed:" + JSON.stringify(error)); 1389 return; 1390 } 1391 nfcOmaChannel = data; 1392 console.log("openLogicalChannel4 get channel successfully"); 1393 }); 1394 } 1395} catch (e) { 1396 console.log("openLogicalChannel4 " + "exception: ${(e : BusinessError).message}"); 1397} 1398``` 1399 1400## Channel. getSession 1401 1402 getSession(): Session 1403 1404获取打开该channel的Session对象。 1405 1406**系统能力:** SystemCapability.Communication.SecureElement 1407 1408**返回值:** 1409 1410| **类型** | **说明** | 1411| -------- | ----------------------------- | 1412| Session | 该channel绑定的Session 对象。 | 1413 1414**示例:** 1415 1416```js 1417import omapi from '@ohos.secureElement'; 1418import secureElement from '@ohos.secureElement'; 1419import { BusinessError } from '@ohos.base'; 1420 1421let nfcSEService : omapi.SEService | null = null; 1422let nfcOmaReaderList : omapi.Reader[] | null = null; 1423let omaSession : omapi.Session | null = null; 1424let getPromise : Promise<omapi.Channel> | null = null; 1425let aidArray : number[] = [720, 1080]; 1426let p2 : number = 0x00; 1427let mySession : omapi.Session | null = null; 1428 1429try { 1430 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1431 if (state == secureElement.ServiceState.DISCONNECTED) { 1432 console.log("Service state is Disconnected"); 1433 } else { 1434 console.log("Service state is Connected"); 1435 } 1436 }); 1437} catch (e) { 1438 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1439} 1440 1441try { 1442 if(nfcSEService != null) { 1443 nfcOmaReaderList = nfcSEService.getReaders(); 1444 } 1445 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1446 omaSession = nfcOmaReaderList[0].openSession(); 1447 } 1448 if (omaSession != null) { 1449 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1450 getPromise.then((channel) => { 1451 mySession = channel.getSession(); 1452 console.log("openLogicalChannel get channel successfully"); 1453 }) 1454 } 1455 if (mySession != null) { 1456 console.log("get session successfully"); 1457 } else { 1458 console.log("get session failed"); 1459 } 1460} catch (e) { 1461 console.log("get session " + "exception: ${(e : BusinessError).message}"); 1462} 1463``` 1464 1465## Channel. close 1466 1467close(): void 1468 1469关闭与SE的此channel。 1470 1471**系统能力:** SystemCapability.Communication.SecureElement 1472 1473**示例:** 1474 1475```js 1476import omapi from '@ohos.secureElement'; 1477import secureElement from '@ohos.secureElement'; 1478import { BusinessError } from '@ohos.base'; 1479 1480let nfcSEService : omapi.SEService | null = null; 1481let nfcOmaReaderList : omapi.Reader[] | null = null; 1482let omaSession : omapi.Session | null = null; 1483let getPromise : Promise<omapi.Channel> | null = null; 1484let aidArray : number[] = [720, 1080]; 1485let p2 : number = 0x00; 1486 1487try { 1488 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1489 if (state == secureElement.ServiceState.DISCONNECTED) { 1490 console.log("Service state is Disconnected"); 1491 } else { 1492 console.log("Service state is Connected"); 1493 } 1494 }); 1495} catch (e) { 1496 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1497} 1498 1499try { 1500 if(nfcSEService != null) { 1501 nfcOmaReaderList = nfcSEService.getReaders(); 1502 } 1503 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1504 omaSession = nfcOmaReaderList[0].openSession(); 1505 } 1506 if (omaSession != null) { 1507 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1508 getPromise.then((channel) => { 1509 channel.close(); 1510 console.log("channel close successfully"); 1511 }) 1512 } 1513} catch (e) { 1514 console.log("channel close " + "exception: ${(e : BusinessError).message}"); 1515} 1516``` 1517 1518## Channel. isBasicChannel 1519 1520isBasicChannel(): boolean 1521 1522检查该channel是否为基本channel。 1523 1524**系统能力:** SystemCapability.Communication.SecureElement 1525 1526**返回值:** 1527 1528| **类型** | **说明** | 1529| -------- | ------------------------------------------------------------ | 1530| boolean | true: 该channel是基本channel false:该channel不是基本channel 。 | 1531 1532**示例:** 1533 1534```js 1535import omapi from '@ohos.secureElement'; 1536import secureElement from '@ohos.secureElement'; 1537import { BusinessError } from '@ohos.base'; 1538 1539let nfcSEService : omapi.SEService | null = null; 1540let nfcOmaReaderList : omapi.Reader[] | null = null; 1541let omaSession : omapi.Session | null = null; 1542let getPromise : Promise<omapi.Channel> | null = null; 1543let aidArray : number[] = [720, 1080]; 1544let p2 : number = 0x00; 1545let ret : boolean = false; 1546 1547try { 1548 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1549 if (state == secureElement.ServiceState.DISCONNECTED) { 1550 console.log("Service state is Disconnected"); 1551 } else { 1552 console.log("Service state is Connected"); 1553 } 1554 }); 1555} catch (e) { 1556 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1557} 1558 1559try { 1560 if(nfcSEService != null) { 1561 nfcOmaReaderList = nfcSEService.getReaders(); 1562 } 1563 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1564 omaSession = nfcOmaReaderList[0].openSession(); 1565 } 1566 if (omaSession != null) { 1567 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1568 getPromise.then((channel) => { 1569 ret = channel.isBasicChannel(); 1570 }) 1571 } 1572 if (ret) { 1573 console.log("isBasicChannel TRUE"); 1574 } else { 1575 console.log("isBasicChannel FALSE"); 1576 } 1577} catch (e) { 1578 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1579} 1580``` 1581 1582## Channel. isClosed 1583 1584isClosed(): boolean 1585 1586检查该channel是否为closed。 1587 1588**系统能力:** SystemCapability.Communication.SecureElement 1589 1590**返回值:** 1591 1592| **类型** | **说明** | 1593| -------- | --------------------------------------------- | 1594| boolean | true:channel是closed false: 不是closed状态。 | 1595 1596**示例:** 1597 1598```js 1599import omapi from '@ohos.secureElement'; 1600import secureElement from '@ohos.secureElement'; 1601import { BusinessError } from '@ohos.base'; 1602 1603let nfcSEService : omapi.SEService | null = null; 1604let nfcOmaReaderList : omapi.Reader[] | null = null; 1605let omaSession : omapi.Session | null = null; 1606let getPromise : Promise<omapi.Channel> | null = null; 1607let aidArray : number[] = [720, 1080]; 1608let p2 : number = 0x00; 1609let ret : boolean = false; 1610 1611try { 1612 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1613 if (state == secureElement.ServiceState.DISCONNECTED) { 1614 console.log("Service state is Disconnected"); 1615 } else { 1616 console.log("Service state is Connected"); 1617 } 1618 }); 1619} catch (e) { 1620 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1621} 1622 1623try { 1624 if(nfcSEService != null) { 1625 nfcOmaReaderList = nfcSEService.getReaders(); 1626 } 1627 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1628 omaSession = nfcOmaReaderList[0].openSession(); 1629 } 1630 if (omaSession != null) { 1631 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1632 getPromise.then((channel) => { 1633 ret = channel.isClosed(); 1634 }) 1635 } 1636 if (ret) { 1637 console.log("channel isClosed TRUE"); 1638 } else { 1639 console.log("channel isClosed False"); 1640 } 1641} catch (e) { 1642 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1643} 1644``` 1645 1646## Channel. getSelectResponse 1647 1648getSelectResponse():number[] 1649 1650返回从应用程序选择命令接收的数据,包括在applet选择时接收的状态字。 1651 1652**系统能力:** SystemCapability.Communication.SecureElement 1653 1654**返回值:** 1655 1656| **类型** | **说明** | 1657| -------- | ------------------------------------------------------------ | 1658| number[] | 返回从应用程序选择命令接收的数据,包括在applet选择时接收的状态字。 | 1659 1660**错误码:** 1661 1662错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1663 1664**示例:** 1665 1666```js 1667import omapi from '@ohos.secureElement'; 1668import secureElement from '@ohos.secureElement'; 1669import { BusinessError } from '@ohos.base'; 1670 1671let nfcSEService : omapi.SEService | null = null; 1672let nfcOmaReaderList : omapi.Reader[] | null = null; 1673let omaSession : omapi.Session | null = null; 1674let getPromise : Promise<omapi.Channel> | null = null; 1675let aidArray : number[] = [720, 1080]; 1676let p2 : number = 0x00; 1677let responseArray : number[] = [720, 1080]; 1678let str : string = ""; 1679 1680try { 1681 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1682 if (state == secureElement.ServiceState.DISCONNECTED) { 1683 console.log("Service state is Disconnected"); 1684 } else { 1685 console.log("Service state is Connected"); 1686 } 1687 }); 1688} catch (e) { 1689 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1690} 1691 1692try { 1693 if(nfcSEService != null) { 1694 nfcOmaReaderList = nfcSEService.getReaders(); 1695 } 1696 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1697 omaSession = nfcOmaReaderList[0].openSession(); 1698 } 1699 if (omaSession != null) { 1700 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1701 getPromise.then((channel) => { 1702 responseArray = channel.getSelectResponse(); 1703 }) 1704 } 1705 if (responseArray) { 1706 str = "getSelectResponse result:["; 1707 for (let i = 0; i < responseArray.length; ++i) { 1708 str += responseArray[i]; 1709 str += ' '; 1710 } 1711 str += ']'; 1712 console.log(str); 1713 } else { 1714 console.log("getSelectResponse result is null"); 1715 } 1716} catch (e) { 1717 console.log("isBasicChannel " + "exception: ${(e : BusinessError).message}"); 1718} 1719``` 1720 1721## Channel. transmit 1722 1723transmit(command: number[]): Promise<number[]> 1724 1725向SE发送APDU命令(根据ISO/IEC 7816)。 1726 1727**系统能力:** SystemCapability.Communication.SecureElement 1728 1729**参数:** 1730 1731| **参数名** | **类型** | **必填** | **说明** | 1732| ---------- | -------- | ------ | ------------------------------------- | 1733| command | number[] | 是 | 在该channel上被选择的applet AID数组。 | 1734 1735**返回值:** 1736 1737| **类型** | **说明** | 1738| -------- | -------------- | 1739| number[] | 响应结果数组。 | 1740 1741**错误码:** 1742 1743错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1744 1745| 错误码ID | 错误信息 | 1746| -------- | -------------------------------- | 1747| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1748| 3300103 | SecurityError, the command is filtered by the security policy. | 1749| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1750 1751**示例:** 1752 1753```js 1754import omapi from '@ohos.secureElement'; 1755import secureElement from '@ohos.secureElement'; 1756import { BusinessError } from '@ohos.base'; 1757 1758let nfcSEService : omapi.SEService | null = null; 1759let nfcOmaReaderList : omapi.Reader[] | null = null; 1760let omaSession : omapi.Session | null = null; 1761let getPromise : Promise<omapi.Channel> | null = null; 1762let aidArray : number[] = [720, 1080]; 1763let p2 : number = 0x00; 1764let responseArray : Promise<number[]> | null = null; 1765 1766try { 1767 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1768 if (state == secureElement.ServiceState.DISCONNECTED) { 1769 console.log("Service state is Disconnected"); 1770 } else { 1771 console.log("Service state is Connected"); 1772 } 1773 }); 1774} catch (e) { 1775 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1776} 1777 1778try { 1779 if(nfcSEService != null) { 1780 nfcOmaReaderList = nfcSEService.getReaders(); 1781 } 1782 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1783 omaSession = nfcOmaReaderList[0].openSession(); 1784 } 1785 if (omaSession != null) { 1786 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1787 getPromise.then((channel) => { 1788 let command: number[] = [100, 200]; 1789 // refer to Session.openBasicChannel for this.nfcOmaChannel 1790 responseArray = channel.transmit(command); 1791 }) 1792 } 1793 if (responseArray != null) { 1794 console.log("transmit1 success"); 1795 } else { 1796 console.log("transmit1 failed"); 1797 } 1798} catch (e) { 1799 console.log("transmit1 " + "exception: ${(e : BusinessError).message}"); 1800} 1801``` 1802 1803## Channel. transmit 1804 1805transmit(command: number[], callback: AsyncCallback<number[]>): void 1806 1807向SE发送APDU命令(根据ISO/IEC 7816)。 1808 1809**系统能力:** SystemCapability.Communication.SecureElement 1810 1811**参数:** 1812 1813| **参数名** | **类型** | **必填** | **说明** | 1814| ---------- | ----------------------- | ------ | ------------------------------------- | 1815| command | number[] | 是 | 在该Channel上被选择的applet AID数组。 | 1816| callback | AsyncCallback<number[]> | 是 | 返回接收到的响应的回调,number数组。 | 1817 1818**错误码:** 1819 1820错误码的详细介绍请参见[SE错误码](../errorcodes/errorcode-se.md)。 1821 1822| 错误码ID | 错误信息 | 1823| -------- | -------------------------------- | 1824| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1825| 3300103 | SecurityError, the command is filtered by the security policy. | 1826| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1827 1828**示例:** 1829 1830```js 1831import omapi from '@ohos.secureElement'; 1832import secureElement from '@ohos.secureElement'; 1833import { BusinessError } from '@ohos.base'; 1834 1835let nfcSEService : omapi.SEService | null = null; 1836let nfcOmaReaderList : omapi.Reader[] | null = null; 1837let omaSession : omapi.Session | null = null; 1838let getPromise : Promise<omapi.Channel> | null = null; 1839let aidArray : number[] = [720, 1080]; 1840let p2 : number = 0x00; 1841let str : string = ""; 1842 1843try { 1844 nfcSEService = secureElement.newSEService("serviceState", (state) => { 1845 if (state == secureElement.ServiceState.DISCONNECTED) { 1846 console.log("Service state is Disconnected"); 1847 } else { 1848 console.log("Service state is Connected"); 1849 } 1850 }); 1851} catch (e) { 1852 console.log("newSEService " + "exception: ${(e : BusinessError).message}"); 1853} 1854 1855try { 1856 if(nfcSEService != null) { 1857 nfcOmaReaderList = nfcSEService.getReaders(); 1858 } 1859 if (nfcOmaReaderList != null && nfcOmaReaderList.length > 0) { 1860 omaSession = nfcOmaReaderList[0].openSession(); 1861 } 1862 if (omaSession != null) { 1863 getPromise = omaSession.openLogicalChannel(aidArray, p2); 1864 getPromise.then((channel) => { 1865 let command: number[] = [100, 200]; 1866 // refer to Session.openBasicChannel for this.nfcOmaChannel 1867 channel.transmit(command, (error, data) => { 1868 if (error) { 1869 console.log("transmit2 exception:" + JSON.stringify(error)); 1870 return; 1871 } 1872 str = "transmit2 result:["; 1873 for (let i = 0; i < data.length; ++i) { 1874 str += data[i]; 1875 str += " "; 1876 } 1877 str += "]"; 1878 console.log(str) 1879 }); 1880 }) 1881 } 1882} catch (e) { 1883 console.log("transmit2 " + "exception: ${(e : BusinessError).message}"); 1884} 1885``` 1886