FileDocCategorySizeDatePackage
NfcAdapterExtras.javaAPI DocAndroid 5.1 API8778Thu Mar 12 22:22:40 GMT 2015com.android.nfc_extras

NfcAdapterExtras

public final class NfcAdapterExtras extends Object
Provides additional methods on an {@link NfcAdapter} for Card Emulation and management of {@link NfcExecutionEnvironment}'s. There is a 1-1 relationship between an {@link NfcAdapterExtras} object and a {@link NfcAdapter} object.

Fields Summary
private static final String
TAG
public static final String
ACTION_RF_FIELD_ON_DETECTED
Broadcast Action: an RF field ON has been detected.

This is an unreliable signal, and will be removed.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission to receive.

public static final String
ACTION_RF_FIELD_OFF_DETECTED
Broadcast Action: an RF field OFF has been detected.

This is an unreliable signal, and will be removed.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission to receive.

private static android.nfc.INfcAdapterExtras
sService
private static final CardEmulationRoute
ROUTE_OFF
private static final HashMap
sNfcExtras
private final NfcExecutionEnvironment
mEmbeddedEe
private final CardEmulationRoute
mRouteOnWhenScreenOn
private final android.nfc.NfcAdapter
mAdapter
final String
mPackageName
Constructors Summary
private NfcAdapterExtras(android.nfc.NfcAdapter adapter)

        mAdapter = adapter;
        mPackageName = adapter.getContext().getPackageName();
        mEmbeddedEe = new NfcExecutionEnvironment(this);
        mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON,
                mEmbeddedEe);
    
Methods Summary
voidattemptDeadServiceRecovery(java.lang.Exception e)
NFC service dead - attempt best effort recovery

        Log.e(TAG, "NFC Adapter Extras dead - attempting to recover");
        mAdapter.attemptDeadServiceRecovery(e);
        initService(mAdapter);
    
public voidauthenticate(byte[] token)
Authenticate the client application. Some implementations of NFC Adapter Extras may require applications to authenticate with a token, before using other methods.

param
token a implementation specific token
throws
java.lang.SecurityException if authentication failed

        try {
            sService.authenticate(mPackageName, token);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }
    
public static com.android.nfc_extras.NfcAdapterExtrasget(android.nfc.NfcAdapter adapter)
Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.

param
adapter a {@link NfcAdapter}, must not be null
return
the {@link NfcAdapterExtras} object for the given {@link NfcAdapter}

        Context context = adapter.getContext();
        if (context == null) {
            throw new UnsupportedOperationException(
                    "You must pass a context to your NfcAdapter to use the NFC extras APIs");
        }

        synchronized (NfcAdapterExtras.class) {
            if (sService == null) {
                initService(adapter);
            }
            NfcAdapterExtras extras = sNfcExtras.get(adapter);
            if (extras == null) {
                extras = new NfcAdapterExtras(adapter);
                sNfcExtras.put(adapter,  extras);
            }
            return extras;
        }
    
public com.android.nfc_extras.NfcAdapterExtras$CardEmulationRoutegetCardEmulationRoute()
Get the routing state of this NFC EE.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.

        try {
            int route = sService.getCardEmulationRoute(mPackageName);
            return route == CardEmulationRoute.ROUTE_OFF ?
                    ROUTE_OFF :
                    mRouteOnWhenScreenOn;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return ROUTE_OFF;
        }
    
public java.lang.StringgetDriverName()
Returns the name of this adapter's driver.

Different NFC adapters may use different drivers. This value is informational and should not be parsed.

return
the driver name, or empty string if unknown

        try {
            return sService.getDriverName(mPackageName);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return "";
        }
    
public NfcExecutionEnvironmentgetEmbeddedExecutionEnvironment()
Get the {@link NfcExecutionEnvironment} that is embedded with the {@link NfcAdapter}.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.

return
a {@link NfcExecutionEnvironment}, or null if there is no embedded NFC-EE

        return mEmbeddedEe;
    
android.nfc.INfcAdapterExtrasgetService()

        return sService;
    
private static voidinitService(android.nfc.NfcAdapter adapter)
get service handles


        
         
        final INfcAdapterExtras service = adapter.getNfcAdapterExtrasInterface();
        if (service != null) {
            // Leave stale rather than receive a null value.
            sService = service;
        }
    
public voidsetCardEmulationRoute(com.android.nfc_extras.NfcAdapterExtras$CardEmulationRoute route)
Set the routing state of this NFC EE.

This routing state is not persisted across reboot.

Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.

param
route a {@link CardEmulationRoute}

        try {
            sService.setCardEmulationRoute(mPackageName, route.route);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }