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| newSEService(type: 'serviceState', callback: Callback\<ServiceState>): 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
36
37let seService : omapi.SEService;
38let seReaders : omapi.Reader[];
39let seSession : omapi.Session;
40let seChannel : omapi.Channel;
41let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10];
42let p2 : number = 0x00;
43
44export default class EntryAbility extends UIAbility {
45  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
46    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
47
48    // Check whether the device supports SEs.
49    if (!canIUse("SystemCapability.Communication.SecureElement")) {
50      hilog.error(0x0000, 'testTag', 'secure element unavailable.');
51      return;
52    }
53
54    // Obtain the service.
55    try {
56      seService = omapi.newSEService("serviceState", (state) => {
57        hilog.info(0x0000, 'testTag', 'se service state = %{public}s', JSON.stringify(state));
58      });
59    } catch (error) {
60      hilog.error(0x0000, 'testTag', 'newSEService error %{public}s', JSON.stringify(error));
61    }
62    if (seService == undefined || !seService.isConnected()) {
63      hilog.error(0x0000, 'testTag', 'secure element service disconnected.');
64      return;
65    }
66
67    // get readers
68    try {
69      seReaders = seService.getReaders();
70    } catch (error) {
71      hilog.error(0x0000, 'testTag', 'getReaders error %{public}s', JSON.stringify(error));
72    }
73    if (seReaders == undefined || seReaders.length == 0) {
74      hilog.error(0x0000, 'testTag', 'no valid reader found.');
75      return;
76    }
77
78    // get session
79    try {
80      let reader = seReaders[0]; // change it to the selected reader, ese or sim.
81      seSession = reader.openSession();
82    } catch (error) {
83      hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error));
84    }
85    if (seSession == undefined) {
86      hilog.error(0x0000, 'testTag', 'seSession invalid.');
87      return;
88    }
89
90    // get channel
91    try {
92      // change the aid value for open logical channel.
93      seSession.openLogicalChannel(aidArray, p2, (error, data) => {
94        if (error) {
95          hilog.error(0x0000, 'testTag', 'openLogicalChannel error %{public}s', JSON.stringify(error));
96        } else {
97          seChannel = data;
98        }
99      });
100    } catch (exception) {
101      hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception));
102    }
103    if (seChannel == undefined) {
104      hilog.error(0x0000, 'testTag', 'seChannel invalid.');
105      return;
106    }
107
108    // transmit data
109    let cmdData = [0x01, 0x02, 0x03, 0x04]; // please change the raw data to be correct.
110    try {
111      seChannel.transmit(cmdData).then((response) => {
112        hilog.info(0x0000, 'testTag', 'seChannel.transmit() response = %{public}s.', JSON.stringify(response));
113      }).catch((error : BusinessError) => {
114        hilog.error(0x0000, 'testTag', 'seChannel.transmit() error = %{public}s.', JSON.stringify(error));
115      });
116    } catch (exception) {
117      hilog.error(0x0000, 'testTag', 'seChannel.transmit() exception = %{public}s.', JSON.stringify(exception));
118    }
119
120    // close channel. must make sure the channel is closed at last.
121    try {
122      seChannel.close();
123    } catch (exception) {
124      hilog.error(0x0000, 'testTag', 'seChannel.close() exception = %{public}s.', JSON.stringify(exception));
125    }
126  }
127}
128```
129