added in API level 24

NfcFCardEmulation

public final class NfcFCardEmulation
extends Object

java.lang.Object
   ↳ android.nfc.cardemulation.NfcFCardEmulation


This class can be used to query the state of NFC-F card emulation services. For a general introduction into NFC card emulation, please read the NFC card emulation developer guide.

Use of this class requires the FEATURE_NFC_HOST_CARD_EMULATION_NFCF to be present on the device.

Summary

Public methods

boolean disableService(Activity activity)

Disables the service for the specified Activity.

boolean enableService(Activity activity, ComponentName service)

Allows a foreground application to specify which card emulation service should be enabled while a specific Activity is in the foreground.

static NfcFCardEmulation getInstance(NfcAdapter adapter)

Helper to get an instance of this class.

String getNfcid2ForService(ComponentName service)

Retrieves the current NFCID2 for the specified service.

String getSystemCodeForService(ComponentName service)

Retrieves the current System Code for the specified service.

boolean registerSystemCodeForService(ComponentName service, String systemCode)

Registers a System Code for the specified service.

boolean setNfcid2ForService(ComponentName service, String nfcid2)

Set a NFCID2 for the specified service.

boolean unregisterSystemCodeForService(ComponentName service)

Removes a registered System Code for the specified service.

Inherited methods

Public methods

disableService

added in API level 24
boolean disableService (Activity activity)

Disables the service for the specified Activity.

Note that the specified Activity must still be in resumed state at the time of this call. A good place to call this method is in your onPause() implementation.

Parameters
activity Activity: The activity which the service was registered for

Returns
boolean true when successful

Throws
RuntimeException

enableService

added in API level 24
boolean enableService (Activity activity, 
                ComponentName service)

Allows a foreground application to specify which card emulation service should be enabled while a specific Activity is in the foreground.

The specified HCE-F service is only enabled when the corresponding application is in the foreground and this method has been called. When the application is moved to the background, disableService(Activity) is called, or NFCID2 or System Code is replaced, the HCE-F service is disabled.

The specified Activity must currently be in resumed state. A good paradigm is to call this method in your onResume(), and to call disableService(Activity) in your onPause().

Note that this preference is not persisted by the OS, and hence must be called every time the Activity is resumed.

Parameters
activity Activity: The activity which prefers this service to be invoked

service ComponentName: The service to be preferred while this activity is in the foreground

Returns
boolean whether the registration was successful

Throws
RuntimeException

getInstance

added in API level 24
NfcFCardEmulation getInstance (NfcAdapter adapter)

Helper to get an instance of this class.

Parameters
adapter NfcAdapter: A reference to an NfcAdapter object.

Returns
NfcFCardEmulation

getNfcid2ForService

added in API level 24
String getNfcid2ForService (ComponentName service)

Retrieves the current NFCID2 for the specified service.

Before calling setNfcid2ForService(ComponentName, String), the NFCID2 contained in the Manifest file is returned. If "random" is specified in the Manifest file, a random number assigned by the system at installation time is returned. After setting an NFCID2 with setNfcid2ForService(ComponentName, String), this NFCID2 is returned.

Parameters
service ComponentName: The component name of the service

Returns
String the current NFCID2

Throws
RuntimeException

getSystemCodeForService

added in API level 24
String getSystemCodeForService (ComponentName service)

Retrieves the current System Code for the specified service.

Before calling registerSystemCodeForService(ComponentName, String), the System Code contained in the Manifest file is returned. After calling registerSystemCodeForService(ComponentName, String), the System Code registered there is returned. After calling unregisterSystemCodeForService(ComponentName), "null" is returned.

Parameters
service ComponentName: The component name of the service

Returns
String the current System Code

Throws
RuntimeException

registerSystemCodeForService

added in API level 24
boolean registerSystemCodeForService (ComponentName service, 
                String systemCode)

Registers a System Code for the specified service.

The System Code must be in range from "4000" to "4FFF" (excluding "4*FF").

If a System Code was previously registered for this service (either statically through the manifest, or dynamically by using this API), it will be replaced with this one.

Even if the same System Code is already registered for another service, this method succeeds in registering the System Code.

Note that you can only register a System Code for a service that is running under the same UID as the caller of this API. Typically this means you need to call this from the same package as the service itself, though UIDs can also be shared between packages using shared UIDs.

Parameters
service ComponentName: The component name of the service

systemCode String: The System Code to be registered

Returns
boolean whether the registration was successful.

Throws
RuntimeException

setNfcid2ForService

added in API level 24
boolean setNfcid2ForService (ComponentName service, 
                String nfcid2)

Set a NFCID2 for the specified service.

The NFCID2 must be in range from "02FE000000000000" to "02FEFFFFFFFFFFFF".

If a NFCID2 was previously set for this service (either statically through the manifest, or dynamically by using this API), it will be replaced.

Note that you can only set the NFCID2 for a service that is running under the same UID as the caller of this API. Typically this means you need to call this from the same package as the service itself, though UIDs can also be shared between packages using shared UIDs.

Parameters
service ComponentName: The component name of the service

nfcid2 String: The NFCID2 to be registered

Returns
boolean whether the setting was successful.

Throws
RuntimeException

unregisterSystemCodeForService

added in API level 24
boolean unregisterSystemCodeForService (ComponentName service)

Removes a registered System Code for the specified service.

Parameters
service ComponentName: The component name of the service

Returns
boolean whether the System Code was successfully removed.

Throws
RuntimeException