2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.nfc.tech.TagTechnology;
20 import android.os.RemoteException;
21 import android.util.Log;
23 import java.io.IOException;
25 //import android.util.Log;
28 * This class provides the primary API for managing all aspects Secure Element.
29 * Get an instance of this class by calling
30 * Context.getSystemService(Context.NFC_SERVICE).
33 public final class NfcSecureElement {
35 private static final String TAG = "NfcSecureElement";
37 private INfcSecureElement mService;
43 public NfcSecureElement(INfcSecureElement mSecureElementService) {
44 mService = mSecureElementService;
47 public int openSecureElementConnection(String seType) throws IOException {
48 if (seType.equals("SmartMX")) {
50 int handle = mService.openSecureElementConnection();
51 // Handle potential errors
55 throw new IOException("SmartMX connection not allowed");
57 } catch (RemoteException e) {
58 Log.e(TAG, "RemoteException in openSecureElementConnection(): ", e);
62 } else if (seType.equals("UICC")) {
65 throw new IOException("Wrong Secure Element type");
70 public byte [] exchangeAPDU(int handle,byte [] data) throws IOException {
73 // Perform exchange APDU
75 byte[] response = mService.exchangeAPDU(handle, data);
76 // Handle potential errors
77 if (response == null) {
78 throw new IOException("Exchange APDU failed");
81 } catch (RemoteException e) {
82 Log.e(TAG, "RemoteException in exchangeAPDU(): ", e);
87 public void closeSecureElementConnection(int handle) throws IOException {
90 int status = mService.closeSecureElementConnection(handle);
91 // Handle potential errors
92 if (ErrorCodes.isError(status)) {
93 throw new IOException("Error during the conection close");
95 } catch (RemoteException e) {
96 Log.e(TAG, "RemoteException in closeSecureElement(): ", e);
102 * Returns target type. constants.
104 * @return Secure Element technology type. The possible values are defined in
105 * {@link TagTechnology}
108 public int[] getSecureElementTechList(int handle) throws IOException {
110 return mService.getSecureElementTechList(handle);
111 } catch (RemoteException e) {
112 Log.e(TAG, "RemoteException in getType(): ", e);
118 * Returns Secure Element UID.
120 * @return Secure Element UID.
122 public byte[] getSecureElementUid(int handle) throws IOException {
126 uid = mService.getSecureElementUid(handle);
127 // Handle potential errors
129 throw new IOException("Get Secure Element UID failed");
132 } catch (RemoteException e) {
133 Log.e(TAG, "RemoteException in getType(): ", e);