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