1# @ohos.secureElement (安全单元的通道管理) 2 3本模块主要用于操作及管理安全单元(SecureElement,简称SE),电子设备上可能存在的安全单元有eSE(Embedded SE)和SIM卡。文档中出现的SE服务为SEService实例,参见[createService]((#omapicreateservice12)。 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 { omapi } from '@kit.ConnectivityKit'; 21``` 22 23## ServiceState 24 25定义不同的SE服务状态值。 26 27**系统能力:** SystemCapability.Communication.SecureElement 28 29| 名称 | 值 | 说明 | 30| ------------ | ---- | ------------------ | 31| DISCONNECTED | 0 | SE服务状态已断开。 | 32| CONNECTED | 1 | SE服务状态已连接。 | 33 34## omapi.newSEService<sup>(deprecated)</sup> 35 36newSEService(type: 'serviceState', callback: Callback\<ServiceState>): SEService 37 38建立一个可用于连接到系统中所有可用SE的新连接(服务)。连接过程较为耗时,所以此方法仅提供异步方式进行的。使用callback异步回调。 39 40仅当指定的回调或者当[isConnected](#seserviceisconnected)方法返回true时,该返回SEService对象是可用的。 41 42> **说明:** 43> 从 API version 10 开始支持,从 API version 12 开始废弃,建议使用[createService](#omapicreateservice12)替代。 44 45**系统能力:** SystemCapability.Communication.SecureElement 46 47**参数:** 48 49| **参数名** | **类型** | **必填** | **说明** | 50| ---------- | ---------------------------------------------------- | ------ | -------------------- | 51| type | string | 是 | 固定填'serviceState' 。 | 52| callback | Callback<[ServiceState](#servicestate)> | 是 | 返回SE服务状态的回调 。| 53 54**返回值:** 55 56| **类型** | **说明** | 57| -------- | --------- | 58| SEService | SE服务实例。 | 59 60**错误码:** 61 62错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 63 64| 错误码ID | 错误信息| 65| ------- | -------| 66| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 67| 801 | Capability not supported. | 68 69**示例:** 70 71```js 72import { omapi } from '@kit.ConnectivityKit'; 73import { hilog } from '@kit.PerformanceAnalysisKit'; 74 75let seService : omapi.SEService; 76 77function secureElementDemo() { 78 // 获取 service 79 try { 80 seService = omapi.newSEService("serviceState", (state) => { 81 hilog.info(0x0000, 'testTag', 'se service state = %{public}s', JSON.stringify(state)); 82 }); 83 } catch (error) { 84 hilog.error(0x0000, 'testTag', 'newSEService error %{public}s', JSON.stringify(error)); 85 } 86 if (seService == undefined || !seService.isConnected()) { 87 hilog.error(0x0000, 'testTag', 'secure element service disconnected.'); 88 return; 89 } 90} 91``` 92 93## omapi.createService<sup>12+</sup> 94 95createService(): Promise\<SEService>; 96 97建立一个可用于连接到系统中所有可用SE的新连接(服务)。连接过程较为耗时,所以此方法仅提供异步方式。使用Promise异步回调。 98 99仅当[isConnected](#seserviceisconnected)方法返回true时,该返回SEService对象是可用的。 100 101**系统能力:** SystemCapability.Communication.SecureElement 102 103**返回值:** 104 105| **类型** | **说明** | 106| :-------- | :--------- | 107| Promise\<[SEService](#seservice)> | 以Promise形式异步返回可用的SE服务实例。 | 108 109**错误码:** 110 111错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 112 113| 错误码ID | 错误信息 | 114| -------- | ----------------------------------------- | 115| 801 | Capability not supported. | 116 117**示例:** 118 119```js 120import { omapi } from '@kit.ConnectivityKit'; 121import { BusinessError } from '@kit.BasicServicesKit'; 122import { hilog } from '@kit.PerformanceAnalysisKit'; 123 124let seService : omapi.SEService; 125 126function secureElementDemo() { 127 omapi.createService().then((data) => { 128 seService = data; 129 if (seService == undefined || !seService.isConnected()) { 130 hilog.error(0x0000, 'testTag', 'seservice state disconnected'); 131 return; 132 } 133 hilog.info(0x0000, 'testTag', 'seservice state connected'); 134 }).catch((error : BusinessError)=> { 135 hilog.error(0x0000, 'testTag', 'createService error %{public}s', JSON.stringify(error)); 136 }); 137} 138``` 139 140## omapi.on<sup>18+</sup> 141 142on(type: 'stateChanged', callback: Callback\<ServiceState>): void; 143 144注册监听服务状态变化事件。 145 146调用[omapi.newSEService](#omapinewseservicedeprecated)或[omapi.createService](#omapicreateservice12)创建服务成功后再用on接口注册回调。 147 148**系统能力:** SystemCapability.Communication.SecureElement 149 150**参数:** 151 152| **参数名** | **类型** | **必填** | **说明** | 153| ---------- | ---------------------------------------------------- | ------ | -------------------- | 154| type | string | 是 | 订阅监听的事件类型,固定填'serviceState' 。 | 155| callback | Callback<[ServiceState](#servicestate)> | 是 | 返回SE服务状态的回调 。| 156 157**错误码:** 158 159错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 160 161| 错误码ID | 错误信息 | 162| -------- | ----------------------------------------- | 163| 801 | Capability not supported. | 164 165**示例:** 166 167示例请参见[off](#omapioff18)接口的示例。 168 169## omapi.off<sup>18+</sup> 170 171off(type: 'stateChanged', callback?: Callback\<ServiceState>): void; 172 173取消订阅服务状态更改事件。 174 175**系统能力:** SystemCapability.Communication.SecureElement 176 177**参数:** 178 179| **参数名** | **类型** | **必填** | **说明** | 180| ---------- | ---------------------------------------------------- | ------ | -------------------- | 181| type | string | 是 | 取消订阅监听的事件类型,固定填'serviceState' 。 | 182| callback | Callback<[ServiceState](#servicestate)> | 否 | 返回SE服务状态的回调 。| 183 184**错误码:** 185 186错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 187 188| 错误码ID | 错误信息 | 189| -------- | ----------------------------------------- | 190| 801 | Capability not supported. | 191 192**示例:** 193 194```js 195import { omapi } from '@kit.ConnectivityKit'; 196import { BusinessError } from '@kit.BasicServicesKit'; 197import { hilog } from '@kit.PerformanceAnalysisKit'; 198 199let seService: omapi.SEService; 200function seStateOnCb(data: omapi.ServiceState) { 201 console.log("omapi.on ServiceState: ", data); 202} 203 204function seStateOffCb(data: omapi.ServiceState) { 205 console.log("omapi.off ServiceState: ", data); 206} 207 208function secureElementDemo() { 209 try{ 210 omapi.createService().then((data) => { 211 seService = data; 212 if (seService == undefined || !seService.isConnected()) { 213 hilog.error(0x0000, 'testTag', 'seservice state disconnected'); 214 return; 215 } 216 hilog.info(0x0000, 'testTag', 'seservice state connected'); 217 }).catch((error : BusinessError)=> { 218 hilog.error(0x0000, 'testTag', 'createService error %{public}s', JSON.stringify(error)); 219 }); 220 omapi.on('stateChanged', seStateOnCb); 221 } catch (error) { 222 if (error as BusinessError) { 223 console.error(`omapi on error catch Code: ${(error as BusinessError).code}, ` + `message: ${(error as BusinessError).message}`); 224 } 225 } 226 try{ 227 omapi.off('stateChanged', seStateOffCb); 228 } catch (error) { 229 if (error as BusinessError) { 230 console.error(`omapi off error catch Code: ${(error as BusinessError).code}, ` + `message: ${(error as BusinessError).message}`); 231 } 232 } 233} 234``` 235 236## SEService 237 238SEService表示可用于连接到系统中所有可用SE的连接(服务),通过[createService](#omapicreateservice12)获取SEService实例。 239 240### SEService.getReaders 241 242getReaders(): Reader[] 243 244返回可用SE Reader的数组,包含该设备上支持的所有的安全单元。 245 246**系统能力:** SystemCapability.Communication.SecureElement 247 248**返回值:** 249 250| **类型** | **说明** | 251| :------- | :--------------------- | 252| [Reader](#reader)[] | 返回可用Reader对象数组。 | 253 254**错误码:** 255 256错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 257 258| 错误码ID | 错误信息 | 259| -------- | ----------------------------------------- | 260| 801 | Capability not supported. | 261 262**示例:** 263 264<!--code_no_check--> 265```js 266import { omapi } from '@kit.ConnectivityKit'; 267import { hilog } from '@kit.PerformanceAnalysisKit'; 268 269let seService : omapi.SEService; 270let seReaders : omapi.Reader[]; 271 272// 在使用seService之前,需要对seService进行初始化 273function secureElementDemo() { 274 // 获取readers 275 try { 276 seReaders = seService.getReaders(); 277 } catch (error) { 278 hilog.error(0x0000, 'testTag', 'getReaders error %{public}s', JSON.stringify(error)); 279 } 280 if (seReaders == undefined || seReaders.length == 0) { 281 hilog.error(0x0000, 'testTag', 'no valid reader found.'); 282 return; 283 } 284} 285``` 286 287### SEService.isConnected 288 289isConnected(): boolean 290 291检查SE服务是否已连接。 292 293**系统能力:** SystemCapability.Communication.SecureElement 294 295**返回值:** 296 297| **类型** | **说明** | 298| :------- | :--------------------------------------------- | 299| boolean | true: SE服务状态已连接,false: SE服务状态已断开。 | 300 301**错误码:** 302 303错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 304 305| 错误码ID | 错误信息 | 306| -------- | ----------------------------------------- | 307| 801 | Capability not supported. | 308 309**示例:** 310 311 312```JS 313import { omapi } from '@kit.ConnectivityKit'; 314import { BusinessError } from '@kit.BasicServicesKit'; 315import { hilog } from '@kit.PerformanceAnalysisKit'; 316 317let seService : omapi.SEService; 318 319function secureElementDemo() { 320 omapi.createService().then((data) => { 321 seService = data; 322 if (seService == undefined || !seService.isConnected()) { 323 hilog.error(0x0000, 'testTag', 'seservice state disconnected'); 324 return; 325 } 326 hilog.info(0x0000, 'testTag', 'seservice state connected'); 327 }).catch((error : BusinessError)=> { 328 hilog.error(0x0000, 'testTag', 'createService error %{public}s', JSON.stringify(error)); 329 }); 330} 331``` 332 333### SEService.shutdown 334 335shutdown(): void 336 337释放该Service分配的所有SE资源。此后[isConnected](#seserviceisconnected)将返回false。 338 339**系统能力:** SystemCapability.Communication.SecureElement 340 341**错误码:** 342 343错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 344 345| 错误码ID | 错误信息 | 346| -------- | ----------------------------------------- | 347| 801 | Capability not supported. | 348 349**示例:** 350 351<!--code_no_check--> 352```js 353import { omapi } from '@kit.ConnectivityKit'; 354import { BusinessError } from '@kit.BasicServicesKit'; 355import { hilog } from '@kit.PerformanceAnalysisKit'; 356 357let seService : omapi.SEService; 358 359// 在使用seService之前,需要对seService进行初始化 360 361try { 362 seService.shutdown(); 363} catch (error) { 364 hilog.error(0x0000, 'testTag', 'shutdown error %{public}s', JSON.stringify(error)); 365} 366``` 367 368### SEService.getVersion 369 370getVersion(): string 371 372返回此实现所基于的Open Mobile API规范的版本号。 373 374**系统能力:** SystemCapability.Communication.SecureElement 375 376**返回值:** 377 378| **类型** | **说明** | 379| -------- | -------------------------------------------------- | 380| string | OMA版本号(例如,“3.3”表示Open Mobile API规范版本3.3) | 381 382**错误码:** 383 384错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 385 386| 错误码ID | 错误信息 | 387| -------- | ----------------------------------------- | 388| 801 | Capability not supported. | 389 390**示例:** 391 392<!--code_no_check--> 393```JS 394import { omapi } from '@kit.ConnectivityKit'; 395import { BusinessError } from '@kit.BasicServicesKit'; 396import { hilog } from '@kit.PerformanceAnalysisKit'; 397 398let seService : omapi.SEService; 399 400// 在使用seService之前,需要对seService进行初始化 401 402try { 403 let version = seService.getVersion(); 404 hilog.error(0x0000, 'testTag', 'version %{public}s', JSON.stringify(version)); 405} catch (error) { 406 hilog.error(0x0000, 'testTag', 'getVersion error %{public}s', JSON.stringify(error)); 407} 408``` 409## Reader 410 411Reader的实例表示该设备支持的SE,如果支持eSE和SIM,则返回两个实例。通过[SEService.getReaders](#seservicegetreaders)获取Reader实例。 412 413### Reader.getName 414 415getName(): string 416 417返回此Reader的名称。如果此读卡器是SIM Reader,则其名称必须为“SIM”。如果读卡器是eSE,则其名称须为“eSE”。 418 419**系统能力:** SystemCapability.Communication.SecureElement 420 421**返回值:** 422 423| **类型** | **说明** | 424| -------- | ---------- | 425| string | [Reader](#reader)名称。 | 426 427**错误码:** 428 429以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 430 431| 错误码ID | 错误信息 | 432| -------- | ----------------------------------------- | 433| 801 | Capability not supported. | 434 435**示例:** 436 437<!--code_no_check--> 438```js 439import { hilog } from '@kit.PerformanceAnalysisKit'; 440import { omapi } from '@kit.ConnectivityKit'; 441 442let seReaders : omapi.Reader[]; 443 444// 在使用seReaders之前,需要对seReaders进行初始化 445 446try { 447 let reader = seReaders[0]; // 将其更改为所选的reader:ese 或 sim 448 let name = reader.getName(); 449 hilog.info(0x0000, 'testTag', 'name %{public}s', JSON.stringify(name)); 450} catch (error) { 451 hilog.error(0x0000, 'testTag', 'getName error %{public}s', JSON.stringify(error)); 452} 453``` 454 455### Reader.isSecureElementPresent 456 457isSecureElementPresent(): boolean 458 459检查当前Reader所对应的安全单元是否可用。 460 461**系统能力:** SystemCapability.Communication.SecureElement 462 463**返回值:** 464 465| **类型** | **说明** | 466| -------- | -------------------------------------------- | 467| boolean | true: 安全单元可用, false: 安全单元不可用。 | 468 469**错误码:** 470 471错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 472 473| 错误码ID | 错误信息 | 474| -------- | -------------------------------- | 475| 801 | Capability not supported. | 476| 3300101 | IllegalStateError, service state exception. | 477 478**示例:** 479 480<!--code_no_check--> 481```js 482import { hilog } from '@kit.PerformanceAnalysisKit'; 483import { omapi } from '@kit.ConnectivityKit'; 484 485let seReaders : omapi.Reader[]; 486 487// 在使用seReaders之前,需要对seReaders进行初始化 488 489try { 490 let reader = seReaders[0]; // 将其更改为所选的reader:ese 或 sim 491 let isPresent = reader.isSecureElementPresent(); 492 hilog.info(0x0000, 'testTag', 'isPresent %{public}s', JSON.stringify(isPresent)); 493} catch (error) { 494 hilog.error(0x0000, 'testTag', 'isSecureElementPresent error %{public}s', JSON.stringify(error)); 495} 496``` 497 498### Reader.openSession 499 500 openSession(): Session 501 502在SE Reader实例上创建连接会话,返回Session实例。在一个Reader上可能同时打开多个会话。 503 504**系统能力:** SystemCapability.Communication.SecureElement 505 506**返回值:** 507 508| **类型** | **说明** | 509| -------- | ------------------------------ | 510| [Session](#session) | 连接会话Session实例。| 511 512**错误码:** 513 514错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 515 516| 错误码ID | 错误信息 | 517| -------- | -------------------------------- | 518| 801 | Capability not supported. | 519| 3300101 | IllegalStateError, service state exception. | 520| 3300104 | IOError, there is a communication problem to the reader or the SE. | 521 522**示例:** 523 524<!--code_no_check--> 525```js 526import { hilog } from '@kit.PerformanceAnalysisKit'; 527import { omapi } from '@kit.ConnectivityKit'; 528 529let seReaders : omapi.Reader[]; 530let seSession : omapi.Session; 531 532// 在使用seReaders之前,需要对seReaders进行初始化 533function secureElementDemo() { 534 try { 535 let reader = seReaders[0]; // 将其更改为所选的reader:ese 或 sim 536 seSession = reader.openSession(); 537 } catch (error) { 538 hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error)); 539 } 540 if (seSession == undefined) { 541 hilog.error(0x0000, 'testTag', 'seSession invalid.'); 542 return; 543 } 544} 545``` 546 547### Reader.closeSessions 548 549 closeSessions(): void 550 551关闭在此Reader上打开的所有Session。所有这些Session打开的所有Channel都将关闭。 552 553**系统能力:** SystemCapability.Communication.SecureElement 554 555**错误码:** 556 557错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 558 559| 错误码ID | 错误信息 | 560| -------- | -------------------------------- | 561| 801 | Capability not supported. | 562| 3300101 | IllegalStateError, service state exception. | 563 564**示例:** 565 566<!--code_no_check--> 567```js 568import { hilog } from '@kit.PerformanceAnalysisKit'; 569import { omapi } from '@kit.ConnectivityKit'; 570 571let seReaders : omapi.Reader[]; 572let seSession : omapi.Session; 573let reader : omapi.Reader; 574 575// 在使用seReaders之前,需要对seReaders进行初始化 576function secureElementDemo() { 577 try { 578 reader = seReaders[0]; // 将其更改为所选的reader:ese 或 sim 579 seSession = reader.openSession(); 580 } catch (error) { 581 hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error)); 582 } 583 if (seSession == undefined) { 584 hilog.error(0x0000, 'testTag', 'seSession invalid.'); 585 return; 586 } 587 try { 588 reader.closeSessions(); 589 } catch (error) { 590 hilog.error(0x0000, 'testTag', 'closeSessions error %{public}s', JSON.stringify(error)); 591 } 592} 593``` 594 595## Session 596 597Session的实例表示在某个SE Reader实例上创建连接会话。通过[Reader.openSession](#readeropensession)获取Session实例。 598 599### Session.getReader 600 601getReader(): Reader 602 603获取提供此Session的Reader实例。 604 605**系统能力:** SystemCapability.Communication.SecureElement 606 607**返回值:** 608 609| **类型** | **说明** | 610| -------- | --------------------------- | 611| [Reader](#reader) | 返回此Session的Reader实例。 | 612 613**错误码:** 614 615错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 616 617| 错误码ID | 错误信息 | 618| -------- | ----------------------------------------- | 619| 801 | Capability not supported. | 620 621**示例:** 622 623<!--code_no_check--> 624```js 625import { hilog } from '@kit.PerformanceAnalysisKit'; 626import { omapi } from '@kit.ConnectivityKit'; 627 628let seReaders : omapi.Reader[]; 629let seSession : omapi.Session; 630let reader : omapi.Reader; 631 632// 在使用seReaders之前,需要对seReaders进行初始化 633function secureElementDemo() { 634 try { 635 reader = seReaders[0]; // 将其更改为所选的reader:ese 或 sim 636 seSession = reader.openSession(); 637 } catch (error) { 638 hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error)); 639 } 640 if (seSession == undefined) { 641 hilog.error(0x0000, 'testTag', 'seSession invalid.'); 642 return; 643 } 644 try { 645 let sessionReader = seSession.getReader(); 646 } catch (error) { 647 hilog.error(0x0000, 'testTag', 'getReader error %{public}s', JSON.stringify(error)); 648 } 649} 650``` 651 652### Session.getATR 653 654getATR(): number[] 655 656获取该SE的ATR。如果该SE的ATR不可用,则应返回空数组。 657 658**系统能力:** SystemCapability.Communication.SecureElement 659 660**返回值:** 661 662| **类型** | **说明** | 663| -------- | -------------------------------------------- | 664| number[] | 返回SE的ATR,SE的ATR不可用时,返回空的数组。 | 665 666**错误码:** 667 668错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 669 670| 错误码ID | 错误信息 | 671| -------- | -------------------------------- | 672| 801 | Capability not supported. | 673| 3300101 | IllegalStateError, service state exception. | 674 675**示例:** 676 677<!--code_no_check--> 678```js 679import { hilog } from '@kit.PerformanceAnalysisKit'; 680import { omapi } from '@kit.ConnectivityKit'; 681 682let seSession : omapi.Session; 683 684// 在使用seSession之前,需要对seSession进行初始化 685 686try { 687 let atr = seSession.getATR(); 688 hilog.info(0x0000, 'testTag', 'atr %{public}s', JSON.stringify(atr)); 689} catch (error) { 690 hilog.error(0x0000, 'testTag', 'getATR error %{public}s', JSON.stringify(error)); 691} 692``` 693 694### Session.close 695 696close(): void 697 698关闭与SE的当前会话连接。这将关闭此Session打开的所有Channel。 699 700**系统能力:** SystemCapability.Communication.SecureElement 701 702**错误码:** 703 704错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 705 706| 错误码ID | 错误信息 | 707| -------- | -------------------------------- | 708| 801 | Capability not supported. | 709| 3300101 | IllegalStateError, service state exception. | 710 711**示例:** 712 713<!--code_no_check--> 714```js 715import { hilog } from '@kit.PerformanceAnalysisKit'; 716import { omapi } from '@kit.ConnectivityKit'; 717 718let seSession : omapi.Session; 719 720// 在使用seSession之前,需要对seSession进行初始化 721 722try { 723 seSession.close(); 724} catch (error) { 725 hilog.error(0x0000, 'testTag', 'close error %{public}s', JSON.stringify(error)); 726} 727``` 728 729### Session. isClosed 730 731isClosed(): boolean 732 733检查Session是否关闭。 734 735**系统能力:** SystemCapability.Communication.SecureElement 736 737**返回值:** 738 739| **类型** | **说明** | 740| -------- | ------------------------------------ | 741| boolean | true:Session状态已关闭,false:Session是打开的。 | 742 743**错误码:** 744 745错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 746 747| 错误码ID | 错误信息 | 748| -------- | ----------------------------------------- | 749| 801 | Capability not supported. | 750 751**示例:** 752 753<!--code_no_check--> 754```Js 755import { hilog } from '@kit.PerformanceAnalysisKit'; 756import { omapi } from '@kit.ConnectivityKit'; 757 758let seSession : omapi.Session; 759 760// 在使用seSession之前,需要对seSession进行初始化 761 762try { 763 let isClosed = seSession.isClosed(); 764 hilog.info(0x0000, 'testTag', 'isClosed %{public}s', JSON.stringify(isClosed)); 765} catch (error) { 766 hilog.error(0x0000, 'testTag', 'isClosed error %{public}s', JSON.stringify(error)); 767} 768``` 769 770### Session.closeChannels 771 772closeChannels(): void 773 774关闭此Session上打开的所有Channel。 775 776**系统能力:** SystemCapability.Communication.SecureElement 777 778**错误码:** 779 780错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 781 782| 错误码ID | 错误信息 | 783| -------- | -------------------------------- | 784|801 | Capability not supported. | 785| 3300101 | IllegalStateError, service state exception. | 786 787**示例:** 788 789<!--code_no_check--> 790```js 791import { hilog } from '@kit.PerformanceAnalysisKit'; 792import { omapi } from '@kit.ConnectivityKit'; 793 794let seSession : omapi.Session; 795 796// 在使用seSession之前,需要对seSession进行初始化 797 798try { 799 seSession.closeChannels(); 800} catch (error) { 801 hilog.error(0x0000, 'testTag', 'closeChannels error %{public}s', JSON.stringify(error)); 802} 803``` 804 805### Session.openBasicChannel 806 807openBasicChannel(aid: number[]): Promise\<Channel> 808 809打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。使用Promise异步回调。 810 811**系统能力:** SystemCapability.Communication.SecureElement 812 813**参数:** 814 815| **参数名** | **类型** | **必填** | **说明** | 816| ---------- | -------- | ------ | ------------------------------------------------------------ | 817| aid | number[] | 是 |在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。| 818 819**返回值:** 820 821| **类型** | **说明** | 822| -------- | --------------------- | 823| Promise\<[Channel](#channel)> | 以Promise形式异步返回可用的基础Channel对象实例。 | 824 825**错误码:** 826 827错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 828 829| 错误码ID | 错误信息 | 830| -------- | -------------------------------- | 831|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 832|801 | Capability not supported. | 833| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 834| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 835| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 836| 3300104 | IOError, there is a communication problem to the reader or the SE. | 837 838**示例:** 839 840<!--code_no_check--> 841```js 842import { hilog } from '@kit.PerformanceAnalysisKit'; 843import { omapi } from '@kit.ConnectivityKit'; 844 845let seSession : omapi.Session; 846let seChannel : omapi.Channel; 847let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 848 849// 在使用seSession之前,需要对seSession进行初始化 850function secureElementDemo() { 851 try { 852 // 改为在此channel上选择的App的aid 853 seSession.openBasicChannel(aidArray).then((data) => { 854 seChannel = data; 855 }).catch((error : BusinessError)=> { 856 hilog.error(0x0000, 'testTag', 'openBasicChannel error %{public}s', JSON.stringify(error)); 857 }); 858 } catch (exception) { 859 hilog.error(0x0000, 'testTag', 'openBasicChannel exception %{public}s', JSON.stringify(exception)); 860 } 861 if (seChannel == undefined) { 862 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 863 return; 864 } 865} 866``` 867 868### Session.openBasicChannel 869 870 openBasicChannel(aid: number[], callback: AsyncCallback\<Channel>): void 871 872打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。使用callback异步回调。 873 874**系统能力:** SystemCapability.Communication.SecureElement 875 876**参数:** 877 878| **参数名** | **类型** | **必填** | **说明** | 879| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 880| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 881| callback | AsyncCallback\<[Channel](#channel)> | 是 | 以callback形式异步返回可用的基础Channel对象实例。 | 882 883**错误码:** 884 885错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 886 887| 错误码ID | 错误信息 | 888| -------- | -------------------------------- | 889|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 890|801 | Capability not supported. | 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<!--code_no_check--> 899```js 900import { hilog } from '@kit.PerformanceAnalysisKit'; 901import { omapi } from '@kit.ConnectivityKit'; 902 903let seSession : omapi.Session; 904let seChannel : omapi.Channel; 905let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 906 907// 在使用seSession之前,需要对seSession进行初始化 908function secureElementDemo() { 909 try { 910 // 改为在此channel上选择的App的aid 911 seSession.openBasicChannel(aidArray, (error, data) => { 912 if (error) { 913 hilog.error(0x0000, 'testTag', 'openBasicChannel error %{public}s', JSON.stringify(error)); 914 } else { 915 seChannel = data; 916 } 917 }); 918 } catch (exception) { 919 hilog.error(0x0000, 'testTag', 'openBasicChannel exception %{public}s', JSON.stringify(exception)); 920 } 921 if (seChannel == undefined) { 922 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 923 return; 924 } 925} 926``` 927 928### Session.openBasicChannel 929 930openBasicChannel(aid: number[], p2: number): Promise\<Channel> 931 932打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。使用Promise异步回调。 933 934**系统能力:** SystemCapability.Communication.SecureElement 935 936**参数:** 937 938| **参数名** | **类型** | **必填** | **说明** | 939| ---------- | -------- | ------ | ------------------------------------------------------------ | 940| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 941| p2 | number | 是 |在该Channel上执行的SELECT APDU的P2参数。 | 942 943**返回值:** 944 945| **类型** | **说明** | 946| -------- | --------------------- | 947| Promise\<[Channel](#channel)> | 以Promise形式异步返回可用的基础Channel对象实例。 | 948 949**错误码:** 950 951错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 952 953| 错误码ID | 错误信息 | 954| -------- | -------------------------------- | 955|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 956|801 | Capability not supported. | 957| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 958| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 959| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 960| 3300104 | IOError, there is a communication problem to the reader or the SE. | 961 962**示例:** 963 964<!--code_no_check--> 965```js 966import { hilog } from '@kit.PerformanceAnalysisKit'; 967import { omapi } from '@kit.ConnectivityKit'; 968 969let seSession : omapi.Session; 970let seChannel : omapi.Channel; 971let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 972let p2 : number = 0x00; 973 974// 在使用seSession之前,需要对seSession进行初始化 975function secureElementDemo() { 976 try { 977 // 改为在此channel上选择的App的aid 978 seSession.openBasicChannel(aidArray, p2).then((data) => { 979 seChannel = data; 980 }).catch((error : BusinessError)=> { 981 hilog.error(0x0000, 'testTag', 'openBasicChannel error %{public}s', JSON.stringify(error)); 982 }); 983 } catch (exception) { 984 hilog.error(0x0000, 'testTag', 'openBasicChannel exception %{public}s', JSON.stringify(exception)); 985 } 986 if (seChannel == undefined) { 987 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 988 return; 989 } 990} 991``` 992 993### Session.openBasicChannel 994 995openBasicChannel(aid: number[], p2:number, callback: AsyncCallback\<Channel>): void 996 997打开基础通道,参考[ISO 7816-4]协议,返回基础Channel实例对象。SE不能提供基础Channel或应用程序没有访问SE的权限时,返回null。使用callback异步回调。 998 999**系统能力:** SystemCapability.Communication.SecureElement 1000 1001**参数:** 1002 1003| **参数名** | **类型** | **必填** | **说明** | 1004| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1005| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1006| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1007| callback | AsyncCallback\<[Channel](#channel)> | 是 | 以callback形式异步返回可用的基础Channel对象实例。 | 1008 1009**错误码:** 1010 1011错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1012 1013| 错误码ID | 错误信息 | 1014| -------- | -------------------------------- | 1015|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1016|801 | Capability not supported. | 1017| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1018| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected. | 1019| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1020| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1021 1022**示例:** 1023 1024<!--code_no_check--> 1025```js 1026import { hilog } from '@kit.PerformanceAnalysisKit'; 1027import { omapi } from '@kit.ConnectivityKit'; 1028 1029let seSession : omapi.Session; 1030let seChannel : omapi.Channel; 1031let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 1032let p2 : number = 0x00; 1033 1034// 在使用seSession之前,需要对seSession进行初始化 1035function secureElementDemo() { 1036 try { 1037 // 改为在此channel上选择的App的aid 1038 seSession.openBasicChannel(aidArray, p2, (error, data) => { 1039 if (error) { 1040 hilog.error(0x0000, 'testTag', 'openBasicChannel error %{public}s', JSON.stringify(error)); 1041 } else { 1042 seChannel = data; 1043 } 1044 }); 1045 } catch (exception) { 1046 hilog.error(0x0000, 'testTag', 'openBasicChannel exception %{public}s', JSON.stringify(exception)); 1047 } 1048 if (seChannel == undefined) { 1049 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 1050 return; 1051 } 1052} 1053``` 1054 1055### Session.openLogicalChannel 1056 1057openLogicalChannel(aid: number[]): Promise\<Channel> 1058 1059打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。使用Promise异步回调。 1060 1061**系统能力:** SystemCapability.Communication.SecureElement 1062 1063**参数:** 1064 1065| **参数名** | **类型** | **必填** | **说明** | 1066| ---------- | -------- | ------ | --------------------------------------- | 1067| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1068 1069**返回值:** 1070 1071| **类型** | **说明** | 1072| -------- | ------------------------------------------------------------ | 1073| Promise\<[Channel](#channel)> | 以Promise形式异步返回可用的逻辑Channel对象实例。 | 1074 1075**错误码:** 1076 1077错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1078 1079| 错误码ID | 错误信息 | 1080| -------- | -------------------------------- | 1081|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1082|801 | Capability not supported. | 1083| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1084| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected or a logical channel is already open to a non-multi-selectable applet. | 1085| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1086| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1087 1088**示例:** 1089 1090<!--code_no_check--> 1091```js 1092import { hilog } from '@kit.PerformanceAnalysisKit'; 1093import { omapi } from '@kit.ConnectivityKit'; 1094 1095let seSession : omapi.Session; 1096let seChannel : omapi.Channel; 1097let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 1098 1099// 在使用seSession之前,需要对seSession进行初始化 1100function secureElementDemo() { 1101 try { 1102 // 改为在此channel上选择的App的aid 1103 seSession.openLogicalChannel(aidArray).then((data) => { 1104 seChannel = data; 1105 }).catch((error : BusinessError)=> { 1106 hilog.error(0x0000, 'testTag', 'openLogicalChannel error %{public}s', JSON.stringify(error)); 1107 }); 1108 } catch (exception) { 1109 hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception)); 1110 } 1111 if (seChannel == undefined) { 1112 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 1113 return; 1114 } 1115} 1116``` 1117 1118### Session.openLogicalChannel 1119 1120 openLogicalChannel(aid: number[], callback: AsyncCallback\<Channel>): void 1121 1122打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。使用callback异步回调。 1123 1124**系统能力:** SystemCapability.Communication.SecureElement 1125 1126**参数:** 1127 1128| **参数名** | **类型** | **必填** | **说明** | 1129| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1130| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1131| callback | AsyncCallback\<[Channel](#channel)> | 是 | 以callback形式异步返回可用的逻辑Channel对象实例。 | 1132 1133**错误码:** 1134 1135错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1136 1137| 错误码ID | 错误信息 | 1138| -------- | -------------------------------- | 1139|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1140|801 | Capability not supported. | 1141| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1142| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected or a logical channel is already open to a non-multi-selectable applet. | 1143| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1144| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1145 1146**示例:** 1147 1148<!--code_no_check--> 1149```js 1150import { hilog } from '@kit.PerformanceAnalysisKit'; 1151import { omapi } from '@kit.ConnectivityKit'; 1152 1153let seSession : omapi.Session; 1154let seChannel : omapi.Channel; 1155let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 1156 1157// 在使用seSession之前,需要对seSession进行初始化 1158function secureElementDemo() { 1159 try { 1160 // 改为在此channel上选择的App的aid 1161 seSession.openLogicalChannel(aidArray, (error, data) => { 1162 if (error) { 1163 hilog.error(0x0000, 'testTag', 'openLogicalChannel error %{public}s', JSON.stringify(error)); 1164 } else { 1165 seChannel = data; 1166 } 1167 }); 1168 } catch (exception) { 1169 hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception)); 1170 } 1171 if (seChannel == undefined) { 1172 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 1173 return; 1174 } 1175} 1176``` 1177 1178### Session.openLogicalChannel 1179 1180openLogicalChannel(aid: number[], p2: number): Promise\<Channel> 1181 1182打开逻辑通道,参考[ISO 7816-4]协议,返回逻辑Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。使用Promise异步回调。 1183 1184**系统能力:** SystemCapability.Communication.SecureElement 1185 1186**参数:** 1187 1188| **参数名** | **类型** | **必填** | **说明** | 1189| ---------- | -------- | ------ | ----------------------------------------- | 1190| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1191| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1192 1193**返回值:** 1194 1195| **类型** | **说明** | 1196| -------- | -------------- | 1197| Promise\<[Channel](#channel)> | 以Promise形式异步返回可用的逻辑Channel实例对象。 | 1198 1199**错误码:** 1200 1201错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1202 1203| 错误码ID | 错误信息 | 1204| -------- | -------------------------------- | 1205|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1206|801 | Capability not supported. | 1207| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1208| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected or a logical channel is already open to a non-multi-selectable applet. | 1209| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1210| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1211 1212**示例:** 1213 1214<!--code_no_check--> 1215```js 1216import { hilog } from '@kit.PerformanceAnalysisKit'; 1217import { omapi } from '@kit.ConnectivityKit'; 1218 1219let seSession : omapi.Session; 1220let seChannel : omapi.Channel; 1221let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 1222let p2 : number = 0x00; 1223 1224// 在使用seSession之前,需要对seSession进行初始化 1225function secureElementDemo() { 1226 try { 1227 // 改为在此channel上选择的App的aid 1228 seSession.openLogicalChannel(aidArray, p2).then((data) => { 1229 seChannel = data; 1230 }).catch((error : BusinessError)=> { 1231 hilog.error(0x0000, 'testTag', 'openLogicalChannel error %{public}s', JSON.stringify(error)); 1232 }); 1233 } catch (exception) { 1234 hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception)); 1235 } 1236 if (seChannel == undefined) { 1237 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 1238 return; 1239 } 1240} 1241``` 1242 1243### Session.openLogicalChannel 1244 1245openLogicalChannel(aid: number[], p2: number, callback: AsyncCallback\<Channel>):void 1246 1247打开逻辑通道,参考[ISO 7816-4]协议,返回Channel实例对象。SE不能提供逻辑Channel或应用程序没有访问SE的权限时,返回null。使用callback异步回调。 1248 1249**系统能力:** SystemCapability.Communication.SecureElement 1250 1251**参数:** 1252 1253| **参数名** | **类型** | **必填** | **说明** | 1254| ---------- | ---------------------- | ------ | ------------------------------------------------------------ | 1255| aid | number[] | 是 | 在此Channel上选择的Applet的AID或如果没有Applet被选择时空的数组。 | 1256| p2 | number | 是 | 此Channel上执行SELECT APDU命令的P2参数。 | 1257| callback | AsyncCallback\<[Channel](#channel)> | 是 | 以callback形式异步返回可用的逻辑Channel对象实例。 | 1258 1259**错误码:** 1260 1261错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1262 1263| 错误码ID | 错误信息 | 1264| -------- | -------------------------------- | 1265|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1266|801 | Capability not supported. | 1267| 3300101 | IllegalStateError, an attempt is made to use an SE session that has been closed. | 1268| 3300102 | NoSuchElementError, the AID on the SE is not available or cannot be selected or a logical channel is already open to a non-multi-selectable applet. | 1269| 3300103 | SecurityError, the calling application cannot be granted access to this AID or the default applet on this session. | 1270| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1271 1272**示例:** 1273 1274<!--code_no_check--> 1275```js 1276import { hilog } from '@kit.PerformanceAnalysisKit'; 1277import { omapi } from '@kit.ConnectivityKit'; 1278 1279let seSession : omapi.Session; 1280let seChannel : omapi.Channel; 1281let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10]; 1282let p2 : number = 0x00; 1283 1284// 在使用seSession之前,需要对seSession进行初始化 1285function secureElementDemo() { 1286 try { 1287 // 改为在此channel上选择的App的aid 1288 seSession.openLogicalChannel(aidArray, p2, (error, data) => { 1289 if (error) { 1290 hilog.error(0x0000, 'testTag', 'openLogicalChannel error %{public}s', JSON.stringify(error)); 1291 } else { 1292 seChannel = data; 1293 } 1294 }); 1295 } catch (exception) { 1296 hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception)); 1297 } 1298 if (seChannel == undefined) { 1299 hilog.error(0x0000, 'testTag', 'seChannel invalid.'); 1300 return; 1301 } 1302} 1303``` 1304## Channel 1305 1306Channel的实例表示在某个Session实例上创建通道,可能为基础通道或逻辑通道。通过[Session.openBasicChannel](#sessionopenbasicchannel)或[Session.openLogicalChannel](#sessionopenlogicalchannel)获取Channel实例。 1307 1308### Channel.getSession 1309 1310 getSession(): Session 1311 1312获取打开该Channel的Session对象。 1313 1314**系统能力:** SystemCapability.Communication.SecureElement 1315 1316**返回值:** 1317 1318| **类型** | **说明** | 1319| -------- | ----------------------------- | 1320| [Session](#session) | 该Channel绑定的Session 对象。 | 1321 1322**错误码:** 1323 1324错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1325 1326| 错误码ID | 错误信息 | 1327| -------- | ----------------------------------------- | 1328| 801 | Capability not supported. | 1329 1330**示例:** 1331 1332<!--code_no_check--> 1333```js 1334import { hilog } from '@kit.PerformanceAnalysisKit'; 1335import { omapi } from '@kit.ConnectivityKit'; 1336 1337let seSession : omapi.Session; 1338let seChannel : omapi.Channel; 1339 1340// 在使用seChannel之前,需要对seChannel进行初始化 1341 1342try { 1343 seSession = seChannel.getSession(); 1344} catch (exception) { 1345 hilog.error(0x0000, 'testTag', 'getSession exception %{public}s', JSON.stringify(exception)); 1346} 1347``` 1348 1349### Channel.close 1350 1351close(): void 1352 1353关闭Channel。 1354 1355**系统能力:** SystemCapability.Communication.SecureElement 1356 1357**错误码:** 1358 1359错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1360 1361| 错误码ID | 错误信息 | 1362| -------- | ----------------------------------------- | 1363| 801 | Capability not supported. | 1364 1365**示例:** 1366 1367<!--code_no_check--> 1368```js 1369import { hilog } from '@kit.PerformanceAnalysisKit'; 1370import { omapi } from '@kit.ConnectivityKit'; 1371 1372let seChannel : omapi.Channel; 1373 1374// 在使用seChannel之前,需要对seChannel进行初始化 1375try { 1376 seChannel.close(); 1377} catch (exception) { 1378 hilog.error(0x0000, 'testTag', 'close exception %{public}s', JSON.stringify(exception)); 1379} 1380``` 1381 1382### Channel.isBasicChannel 1383 1384isBasicChannel(): boolean 1385 1386检查该Channel是否为基础Channel。 1387 1388**系统能力:** SystemCapability.Communication.SecureElement 1389 1390**返回值:** 1391 1392| **类型** | **说明** | 1393| -------- | ------------------------------------------------------------ | 1394| boolean | true: 该Channel是基础Channel, false:该Channel逻辑Channel 。 | 1395 1396**错误码:** 1397 1398错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1399 1400| 错误码ID | 错误信息 | 1401| -------- | ----------------------------------------- | 1402| 801 | Capability not supported. | 1403 1404**示例:** 1405 1406<!--code_no_check--> 1407```js 1408import { hilog } from '@kit.PerformanceAnalysisKit'; 1409import { omapi } from '@kit.ConnectivityKit'; 1410 1411let seChannel : omapi.Channel; 1412 1413// 在使用seChannel之前,需要对seChannel进行初始化 1414try { 1415 let isBasic = seChannel.isBasicChannel(); 1416 hilog.info(0x0000, 'testTag', 'isBasic = %{public}s', JSON.stringify(isBasic)); 1417} catch (exception) { 1418 hilog.error(0x0000, 'testTag', 'isBasicChannel exception %{public}s', JSON.stringify(exception)); 1419} 1420``` 1421 1422### Channel.isClosed 1423 1424isClosed(): boolean 1425 1426检查该Channel是否已被关闭。 1427 1428**系统能力:** SystemCapability.Communication.SecureElement 1429 1430**返回值:** 1431 1432| **类型** | **说明** | 1433| -------- | --------------------------------------------- | 1434| boolean | true: Channel是关闭的,false: 不是关闭的。 | 1435 1436**错误码:** 1437 1438错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1439 1440| 错误码ID | 错误信息 | 1441| -------- | ----------------------------------------- | 1442| 801 | Capability not supported. | 1443 1444**示例:** 1445 1446<!--code_no_check--> 1447```js 1448import { hilog } from '@kit.PerformanceAnalysisKit'; 1449import { omapi } from '@kit.ConnectivityKit'; 1450 1451let seChannel : omapi.Channel; 1452 1453// 在使用seChannel之前,需要对seChannel进行初始化 1454try { 1455 let isClosed = seChannel.isClosed(); 1456 hilog.info(0x0000, 'testTag', 'isClosed = %{public}s', JSON.stringify(isClosed)); 1457} catch (exception) { 1458 hilog.error(0x0000, 'testTag', 'isClosed exception %{public}s', JSON.stringify(exception)); 1459} 1460``` 1461 1462### Channel.getSelectResponse 1463 1464getSelectResponse(): number[] 1465 1466获取SELECT Applet时的响应数据,包含状态字。 1467 1468**系统能力:** SystemCapability.Communication.SecureElement 1469 1470**返回值:** 1471 1472| **类型** | **说明** | 1473| -------- | ------------------------------------------------------------ | 1474| number[] | SELECT Applet时的响应数据,包含状态字。 | 1475 1476**错误码:** 1477 1478错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1479 1480| 错误码ID | 错误信息 | 1481| -------- | ----------------------------------------- | 1482| 801 | Capability not supported. | 1483 1484**示例:** 1485 1486<!--code_no_check--> 1487```js 1488import { hilog } from '@kit.PerformanceAnalysisKit'; 1489import { omapi } from '@kit.ConnectivityKit'; 1490 1491let seChannel : omapi.Channel; 1492 1493// 在使用seChannel之前,需要对seChannel进行初始化 1494try { 1495 let response = seChannel.getSelectResponse(); 1496 hilog.info(0x0000, 'testTag', 'response = %{public}s', JSON.stringify(response)); 1497} catch (exception) { 1498 hilog.error(0x0000, 'testTag', 'getSelectResponse exception %{public}s', JSON.stringify(exception)); 1499} 1500``` 1501 1502### Channel.transmit 1503 1504transmit(command: number[]): Promise\<number[]> 1505 1506向SE发送APDU数据,数据符合ISO/IEC 7816规范。使用Promise异步回调。 1507 1508**系统能力:** SystemCapability.Communication.SecureElement 1509 1510**参数:** 1511 1512| **参数名** | **类型** | **必填** | **说明** | 1513| ---------- | -------- | ------ | ------------------------------------- | 1514| command | number[] | 是 | 需要发送到SE的APDU数据。 | 1515 1516**返回值:** 1517 1518| **类型** | **说明** | 1519| -------- | -------------- | 1520| Promise\<number[]> | 以Promise形式异步返回接收到的响应APDU数据,number数组。 | 1521 1522**错误码:** 1523 1524错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1525 1526| 错误码ID | 错误信息 | 1527| -------- | -------------------------------- | 1528|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1529|801 | Capability not supported. | 1530| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1531| 3300103 | SecurityError, the command is filtered by the security policy. | 1532| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1533 1534**示例:** 1535 1536<!--code_no_check--> 1537```js 1538import { hilog } from '@kit.PerformanceAnalysisKit'; 1539import { omapi } from '@kit.ConnectivityKit'; 1540 1541let seChannel : omapi.Channel; 1542 1543// 在使用seChannel之前,需要对seChannel进行初始化 1544let cmdData = [0x01, 0x02, 0x03, 0x04]; // 请更改为正确的data 1545try { 1546 seChannel.transmit(cmdData).then((response) => { 1547 hilog.info(0x0000, 'testTag', 'transmit response = %{public}s.', JSON.stringify(response)); 1548 }).catch((error : BusinessError) => { 1549 hilog.error(0x0000, 'testTag', 'transmit error = %{public}s.', JSON.stringify(error)); 1550 }); 1551} catch (exception) { 1552 hilog.error(0x0000, 'testTag', 'transmit exception = %{public}s.', JSON.stringify(exception)); 1553} 1554``` 1555 1556### Channel.transmit 1557 1558transmit(command: number[], callback: AsyncCallback\<number[]>): void 1559 1560向SE发送APDU数据,数据符合ISO/IEC 7816规范。使用callback异步回调。 1561 1562**系统能力:** SystemCapability.Communication.SecureElement 1563 1564**参数:** 1565 1566| **参数名** | **类型** | **必填** | **说明** | 1567| ---------- | ----------------------- | ------ | ------------------------------------- | 1568| command | number[] | 是 | 需要发送到SE的APDU数据。 | 1569| callback | AsyncCallback\<number[]> | 是 | 返回接收到的响应APDU数据,number数组。 | 1570 1571**错误码:** 1572 1573错误码的详细介绍请参见[SE错误码](errorcode-se.md)。 1574 1575| 错误码ID | 错误信息 | 1576| -------- | -------------------------------- | 1577|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1578|801 | Capability not supported. | 1579| 3300101 | IllegalStateError, an attempt is made to use an SE session or channel that has been closed. | 1580| 3300103 | SecurityError, the command is filtered by the security policy. | 1581| 3300104 | IOError, there is a communication problem to the reader or the SE. | 1582 1583**示例:** 1584 1585<!--code_no_check--> 1586```js 1587import { hilog } from '@kit.PerformanceAnalysisKit'; 1588import { omapi } from '@kit.ConnectivityKit'; 1589 1590let seChannel : omapi.Channel; 1591 1592// 在使用seChannel之前,需要对seChannel进行初始化 1593let cmdData = [0x01, 0x02, 0x03, 0x04]; // 请更改为正确的data 1594try { 1595 seChannel.transmit(cmdData, (error, response) => { 1596 if (error) { 1597 hilog.error(0x0000, 'testTag', 'transmit error %{public}s', JSON.stringify(error)); 1598 } else { 1599 hilog.info(0x0000, 'testTag', 'transmit response = %{public}s.', JSON.stringify(response)); 1600 } 1601 }); 1602} catch (exception) { 1603 hilog.error(0x0000, 'testTag', 'transmit exception %{public}s', JSON.stringify(exception)); 1604} 1605``` 1606