Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/android/se/omapi/Session#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/se/omapi/Session#inhmethods)

# Session

*** ** * ** ***

` public final class Session `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.se.omapi.Session |

<br />

*** ** * ** ***

Instances of this class represent a connection session to one of the Secure Elements available on the device. These objects can be used to get a communication channel with an Applet in the Secure Element. This channel can be the basic channel or a logical channel.
**See also:**

- [SIMalliance Open Mobile API v3.0](http://simalliance.org)

## Summary

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/se/omapi/Session#close()() ` Close the connection with the Secure Element. |
| ` void` | ` https://developer.android.com/reference/android/se/omapi/Session#closeChannels()() ` Close any channel opened on this session. |
| ` byte[]` | ` https://developer.android.com/reference/android/se/omapi/Session#getATR()() ` Get the Answer to Reset of this Secure Element. |
| ` https://developer.android.com/reference/android/se/omapi/Reader` | ` https://developer.android.com/reference/android/se/omapi/Session#getReader()() ` Get the reader that provides this session. |
| ` boolean` | ` https://developer.android.com/reference/android/se/omapi/Session#isClosed()() ` Tells if this session is closed. |
| ` https://developer.android.com/reference/android/se/omapi/Channel` | ` https://developer.android.com/reference/android/se/omapi/Session#openBasicChannel(byte[])(byte[] aid) ` This method is provided to ease the development of mobile application and for compliancy with existing applications. |
| ` https://developer.android.com/reference/android/se/omapi/Channel` | ` https://developer.android.com/reference/android/se/omapi/Session#openBasicChannel(byte[],%20byte)(byte[] aid, byte p2) ` Get an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0). |
| ` https://developer.android.com/reference/android/se/omapi/Channel` | ` https://developer.android.com/reference/android/se/omapi/Session#openLogicalChannel(byte[],%20byte)(byte[] aid, byte p2) ` Open a logical channel with the Secure Element, selecting the Applet represented by the given AID. |
| ` https://developer.android.com/reference/android/se/omapi/Channel` | ` https://developer.android.com/reference/android/se/omapi/Session#openLogicalChannel(byte[])(byte[] aid) ` This method is provided to ease the development of mobile application and for compliancy with existing applications. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### close

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

Close the connection with the Secure Element. This will close any channels opened by this application with this Secure Element.

<br />

### closeChannels

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void closeChannels ()
```

Close any channel opened on this session.

<br />

### getATR

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public byte[] getATR ()
```

Get the Answer to Reset of this Secure Element.   

The returned byte array can be null if the ATR for this Secure Element is not available.

<br />

| Returns ||
|---|---|
| `byte[]` | the ATR as a byte array or null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if there was an error connecting to SE or if the service was not connected. |

### getReader

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Reader getReader ()
```

Get the reader that provides this session.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/se/omapi/Reader` | The Reader object. This value cannot be `null`. <br /> |

### isClosed

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isClosed ()
```

Tells if this session is closed.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the session is closed, false otherwise. <br /> |

### openBasicChannel

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Channel openBasicChannel (byte[] aid)
```

This method is provided to ease the development of mobile application and for compliancy with existing applications. This method is equivalent to openBasicChannel(aid, P2=0x00)

<br />

| Parameters ||
|---|---|
| `aid` | `byte`: the AID of the Applet to be selected on this channel, as a byte array, or null if no Applet is to be selected. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/se/omapi/Channel` | an instance of Channel if available or null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is a communication problem to the reader or the Secure Element. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the aid's length is not within 5 to 16 (inclusive). |
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the Secure Element session is used after being closed. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the calling application cannot be granted access to this AID or the default Applet on this session. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the given P2 parameter is not supported by the device |
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if the AID on the Secure Element is not available or cannot be selected. |

### openBasicChannel

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Channel openBasicChannel (byte[] aid, 
                byte p2)
```

Get an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0). The obtained object is an instance of the Channel class. If the AID is null, it means no Applet is to be selected on this channel and the default Applet is used. If the AID is defined then the corresponding Applet is selected. Once this channel has been opened by a device application, it is considered as "locked" by this device application, and other calls to this method will return null, until the channel is closed. Some Secure Elements (like the UICC) might always keep the basic channel locked (i.e. return null to applications), to prevent access to the basic channel, while some other might return a channel object implementing some kind of filtering on the commands, restricting the set of accepted command to a smaller set. It is recommended for the UICC to reject the opening of the basic channel to a specific applet, by always answering null to such a request. For other Secure Elements, the recommendation is to accept opening the basic channel on the default applet until another applet is selected on the basic channel. As there is no other way than a reset to select again the default applet, the implementation of the transport API should guarantee that the openBasicChannel(null) command will return null until a reset occurs. With previous release (V2.05) it was not possible to set P2 value, this value was always set to '00'.Except for specific needs it is recommended to keep P2 to '00'. It is recommended that the device allows all values for P2, however only the following values are mandatory: '00', '04', '08', '0C'(as defined in \[2\]) The implementation of the underlying SELECT command within this method shall be based on ISO 7816-4 with following options:

- CLA = '00'
- INS = 'A4'
- P1 = '04' (Select by DF name/application identifier)

The select response data can be retrieved with byte\[\] getSelectResponse(). The API shall handle received status word as follow. If the status word indicates that the Secure Element was able to open a channel (e.g. status word '90 00' or status words referencing a warning in ISO-7816-4: '62 XX' or '63 XX') the API shall keep the channel opened and the next getSelectResponse() shall return the received status word. Other received status codes indicating that the Secure Element was able not to open a channel shall be considered as an error and the corresponding channel shall not be opened. The function without P2 as parameter is provided for backwards compatibility and will fall back to a select command with P2='00'.

<br />

<br />

| Parameters ||
|---|---|
| `aid` | `byte`: the AID of the Applet to be selected on this channel, as a byte array, or null if no Applet is to be selected. <br /> |
| `p2` | `byte`: the P2 parameter of the SELECT APDU executed on this channel. This value may be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/se/omapi/Channel` | an instance of Channel if available or null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is a communication problem to the reader or the Secure Element. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the aid's length is not within 5 to 16 (inclusive). |
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the Secure Element session is used after being closed. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the calling application cannot be granted access to this AID or the default Applet on this session. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the given P2 parameter is not supported by the device |
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if the AID on the Secure Element is not available or cannot be selected. |

### openLogicalChannel

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Channel openLogicalChannel (byte[] aid, 
                byte p2)
```

Open a logical channel with the Secure Element, selecting the Applet represented by the given AID. If the AID is null, which means no Applet is to be selected on this channel, the default Applet is used. It's up to the Secure Element to choose which logical channel will be used. With previous release (V2.05) it was not possible to set P2 value, this value was always set to '00'.Except for specific needs it is recommended to keep P2 to '00'. It is recommended that the device allows all values for P2, however only the following values are mandatory: '00', '04', '08', '0C'(as defined in \[2\]) The implementation of the underlying SELECT command within this method shall be based on ISO 7816-4 with following options:

- CLA = '01' to '03', '40 to 4F'
- INS = 'A4'
- P1 = '04' (Select by DF name/application identifier)

The select response data can be retrieved with byte\[\] getSelectResponse(). The API shall handle received status word as follow. If the status word indicates that the Secure Element was able to open a channel (e.g. status word '90 00' or status words referencing a warning in ISO-7816-4: '62 XX' or '63 XX') the API shall keep the channel opened and the next getSelectResponse() shall return the received status word. Other received status codes indicating that the Secure Element was able not to open a channel shall be considered as an error and the corresponding channel shall not be opened. In case of UICC it is recommended for the API to reject the opening of the logical channel without a specific AID, by always answering null to such a request. The function without P2 as parameter is provided for backwards compatibility and will fall back to a select command with P2=00.

<br />

<br />

| Parameters ||
|---|---|
| `aid` | `byte`: the AID of the Applet to be selected on this channel, as a byte array. This value may be `null`. <br /> |
| `p2` | `byte`: the P2 parameter of the SELECT APDU executed on this channel. This value may be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/se/omapi/Channel` | an instance of Channel. Null if the Secure Element is unable to provide a new logical channel. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is a communication problem to the reader or the Secure Element. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the aid's length is not within 5 to 16 (inclusive). |
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the Secure Element is used after being closed. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the calling application cannot be granted access to this AID or the default Applet on this session. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the given P2 parameter is not supported by the device. |
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if the AID on the Secure Element is not available or cannot be selected or a logical channel is already open to a non-multiselectable Applet. |

### openLogicalChannel

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Channel openLogicalChannel (byte[] aid)
```

This method is provided to ease the development of mobile application and for compliancy with existing applications. This method is equivalent to openLogicalChannel(aid, P2=0x00)

<br />

| Parameters ||
|---|---|
| `aid` | `byte`: the AID of the Applet to be selected on this channel, as a byte array. This value may be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/se/omapi/Channel` | an instance of Channel. Null if the Secure Element is unable to provide a new logical channel. This value may be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is a communication problem to the reader or the Secure Element. |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the aid's length is not within 5 to 16 (inclusive). |
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the Secure Element is used after being closed. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the calling application cannot be granted access to this AID or the default Applet on this session. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the given P2 parameter is not supported by the device. |
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if the AID on the Secure Element is not available or cannot be selected or a logical channel is already open to a non-multiselectable Applet. |