• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SE Access Development
2
3## Introduction
4An electronic device may have one or more secure elements (SEs), such as the embedded SE (eSE) and SIM card. A SIM card that functions as an SE must have the NFC feature.
5
6## When to Use
7An application may need to write data to an SE to emulate an NFC card on the device. The NFC card data may be stored on an eSE or a SIM card. Generally, SEs are preset with rules for access control. An application must have related permissions and can access an SE only after a successful permission verification.
8
9## Available APIs
10For details about the JS APIs and sample code, see [SE Management](../../reference/apis-connectivity-kit/js-apis-secureElement.md).
11The following table describes the APIs for accessing SEs.
12
13| API                            | Description                                                                      |
14| ---------------------------------- | ------------------------------------------------------------------------------ |
15| createService(): Promise\<SEService>                    | Creates an **SEService** instance for connecting to all available SEs in the system.                                                               |
16| getReaders(): Reader[]                      | Obtains available SE readers, which include all the SEs on the device.                                                               |
17| openSession(): Session                 | Opens a session to connect to an SE in this reader. This API returns a session instance.                                                               |
18| openLogicalChannel(aid: number[]): Promise\<Channel>                  | Opens a logical channel. This API returns a logical channel instance.                                                               |
19| transmit(command: number[]): Promise\<number[]> | Transmits APDU data to this SE.                                                               |
20| close(): void | Closes this channel.                                                           |
21
22
23## How to Develop
24
25### Accessing an SE
261. Import modules.
272. Check whether the device supports SEs.
283. Access an SE and read or write data.
29
30```ts
31import { omapi } from '@kit.ConnectivityKit';
32import { BusinessError } from '@kit.BasicServicesKit';
33import { hilog } from '@kit.PerformanceAnalysisKit';
34import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
35
36let seService : omapi.SEService;
37let seReaders : omapi.Reader[];
38let seSession : omapi.Session;
39let seChannel : omapi.Channel;
40let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10];
41let p2 : number = 0x00;
42
43export default class EntryAbility extends UIAbility {
44  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
45    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
46
47    // Check whether the device supports SEs.
48    if (!canIUse("SystemCapability.Communication.SecureElement")) {
49      hilog.error(0x0000, 'testTag', 'secure element unavailable.');
50      return;
51    }
52    hilog.info(0x0000, 'testTag', 'secure element available.');
53    this.omaTest();
54  }
55
56  private async omaTest () {
57    // Obtain the service.
58    await omapi.createService().then((data) => {
59      if (data == undefined || !data.isConnected()) {
60        hilog.error(0x0000, 'testTag', 'secure element service disconnected.');
61        return;
62      }
63      seService = data;
64      hilog.info(0x0000, 'testTag', 'secure element service connected.');
65    }).catch((error: BusinessError) => {
66      hilog.error(0x0000, 'testTag', 'createService error %{public}s', JSON.stringify(error));
67      return;
68    });
69
70    // Obtain readers.
71    try {
72      seReaders = seService.getReaders();
73    } catch (error) {
74      hilog.error(0x0000, 'testTag', 'getReaders error %{public}s', JSON.stringify(error));
75    }
76    if (seReaders == undefined || seReaders.length == 0) {
77      hilog.error(0x0000, 'testTag', 'no valid reader found.');
78      seService.shutdown();
79      return;
80    }
81    let reader: (omapi.Reader | undefined);
82    for (let i = 0; i < seReaders.length; ++i) {
83      let r = seReaders[i];
84      if (r.getName().includes("SIM")) {
85        reader = r;
86        break;
87      }
88    }
89    if (reader == undefined) {
90      hilog.error(0x0000, 'testTag', 'no valid sim reader.');
91      return;
92    }
93    hilog.info(0x0000, 'testTag', 'reader is %{public}s', reader?.getName());
94
95    // Obtain the session.
96    try {
97      seSession = reader?.openSession() as omapi.Session;
98    } catch (error) {
99      hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error));
100    }
101    if (seSession == undefined) {
102      hilog.error(0x0000, 'testTag', 'seSession invalid.');
103      seService.shutdown();
104      return;
105    }
106
107    // Obtain the channel.
108    try {
109      // change the aid value for open logical channel
110      // Change the value to the AID of the application for which the logical channel is opened.
111      seChannel = await seSession.openLogicalChannel(aidArray, p2);
112    } catch (exception) {
113      hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception));
114    }
115
116    if (seChannel == undefined) {
117      hilog.error(0x0000, 'testTag', 'seChannel invalid.');
118      return;
119    }
120
121    // Send data.
122    let cmdData = [0x01, 0x02, 0x03, 0x04]; // Set command data correctly.
123    try {
124      let response: number[] = await seChannel.transmit(cmdData)
125      hilog.info(0x0000, 'testTag', 'seChannel.transmit() response = %{public}s.', JSON.stringify(response));
126    } catch (exception) {
127      hilog.error(0x0000, 'testTag', 'seChannel.transmit() exception = %{public}s.', JSON.stringify(exception));
128    }
129
130    // Close the channel. After performing the operation, make sure that the channel is truly closed.
131    try {
132      seChannel.close();
133    } catch (exception) {
134      hilog.error(0x0000, 'testTag', 'seChannel.close() exception = %{public}s.', JSON.stringify(exception));
135    }
136
137  }
138}
139
140```
141