NfcAdapter
class NfcAdapter
kotlin.Any | |
↳ | android.nfc.NfcAdapter |
Represents the local NFC adapter.
Use the helper getDefaultAdapter(android.content.Context)
to get the default NFC adapter for this Android device.
Summary
Nested classes | |
---|---|
abstract | |
abstract |
A callback to be invoked when another NFC device capable of NDEF push (Android Beam) is within range. |
abstract |
A callback to be invoked when the system successfully delivers your |
abstract |
A callback that is invoked when a tag is removed from the field. |
abstract |
A callback to be invoked when the system finds a tag while the foreground activity is operating in reader mode. |
Constants | |
---|---|
static String |
Broadcast Action: The state of the local NFC adapter has been changed. |
static String |
Intent to start an activity when a tag with NDEF payload is discovered. |
static String |
Broadcast Action: Intent to notify if the preferred payment service changed. |
static String |
Intent to start an activity when a tag is discovered. |
static String |
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag. |
static String |
Broadcast Action: Intent to notify an application that a transaction event has occurred on the Secure Element. |
static String |
Used as an int extra field in |
static String |
Mandatory byte[] extra field in |
static String |
Optional byte[] extra field in |
static String |
Optional extra containing a byte array containing the ID of the discovered tag for the |
static String |
Extra containing an array of |
static String |
Mandatory String extra field in |
static String |
Int Extra for use with |
static String |
Mandatory String extra field in |
static String |
Mandatory extra containing the |
static Int |
Flags for use with |
static Int |
Flags for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flags for use with |
static Int |
Flags for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
Flag for use with |
static Int |
User selected another payment application as the preferred payment. |
static Int |
Nfc is enabled and the preferred payment aids are registered. |
static Int |
Current preferred payment has issued an update (registered/unregistered new aids or has been updated itself). |
static Int | |
static Int | |
static Int | |
static Int |
Public methods | |
---|---|
Boolean |
disable() Disable NFC hardware. |
Unit |
disableForegroundDispatch(activity: Activity!) Disable foreground dispatch to the given activity. |
Unit |
disableReaderMode(activity: Activity!) Restore the NFC adapter to normal mode of operation: supporting peer-to-peer (Android Beam), card emulation, and polling for all supported tag technologies. |
Boolean |
enable() Enable NFC hardware. |
Unit |
enableForegroundDispatch(activity: Activity!, intent: PendingIntent!, filters: Array<IntentFilter!>!, techLists: Array<Array<String!>!>!) Enable foreground dispatch to the given Activity. |
Unit |
enableReaderMode(activity: Activity!, callback: NfcAdapter.ReaderCallback!, flags: Int, extras: Bundle!) Limit the NFC controller to reader mode while this Activity is in the foreground. |
static NfcAdapter! |
getDefaultAdapter(context: Context!) Helper to get the default NFC Adapter. |
NfcAntennaInfo? |
Returns information regarding Nfc antennas on the device such as their relative positioning on the device. |
Boolean |
ignore(tag: Tag!, debounceMs: Int, tagRemovedListener: NfcAdapter.OnTagRemovedListener!, handler: Handler!) Signals that you are no longer interested in communicating with an NFC tag for as long as it remains in range. |
Boolean |
Return true if this NFC Adapter has any features enabled. |
Boolean |
Returns whether Observe Mode is currently enabled or not. |
Boolean |
Returns whether the device supports observe mode or not. |
Boolean |
Checks NFC Reader option feature is enabled. |
Boolean |
Checks if the device supports NFC Reader option functionality. |
Boolean |
Checks Secure NFC feature is enabled. |
Boolean |
Checks if the device supports Secure NFC functionality. |
Unit |
resetDiscoveryTechnology(activity: Activity) Restore the poll/listen technologies of NFC controller to its default state, which were changed by |
Unit |
setDiscoveryTechnology(activity: Activity, pollTechnology: Int, listenTechnology: Int) Set the NFC controller to enable specific poll/listen technologies, as specified in parameters, while this Activity is in the foreground. |
Boolean |
setObserveModeEnabled(enabled: Boolean) Controls whether the NFC adapter will allow transactions to proceed or be in observe mode and simply observe and notify the APDU service of polling loop frames. |
Constants
ACTION_ADAPTER_STATE_CHANGED
static val ACTION_ADAPTER_STATE_CHANGED: String
Broadcast Action: The state of the local NFC adapter has been changed.
For example, NFC has been turned on or off.
Always contains the extra field EXTRA_ADAPTER_STATE
Value: "android.nfc.action.ADAPTER_STATE_CHANGED"
ACTION_NDEF_DISCOVERED
static val ACTION_NDEF_DISCOVERED: String
Intent to start an activity when a tag with NDEF payload is discovered.
The system inspects the first NdefRecord
in the first NdefMessage
and looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the intent will contain the URI in its data field. If a MIME record is found the intent will contain the MIME type in its type field. This allows activities to register IntentFilter
s targeting specific content on tags. Activities should register the most specific intent filters possible to avoid the activity chooser dialog, which can disrupt the interaction with the tag as the user interacts with the screen.
If the tag has an NDEF payload this intent is started before ACTION_TECH_DISCOVERED
. If any activities respond to this intent neither ACTION_TECH_DISCOVERED
or ACTION_TAG_DISCOVERED
will be started.
The MIME type or data URI of this intent are normalized before dispatch - so that MIME, URI scheme and URI host are always lower-case.
Value: "android.nfc.action.NDEF_DISCOVERED"
ACTION_PREFERRED_PAYMENT_CHANGED
static val ACTION_PREFERRED_PAYMENT_CHANGED: String
Broadcast Action: Intent to notify if the preferred payment service changed.
This intent will only be sent to the application has requested permission for android.Manifest.permission#NFC_PREFERRED_PAYMENT_INFO
and if the application has the necessary access to Secure Element which witnessed the particular event.
Requires android.Manifest.permission#NFC_PREFERRED_PAYMENT_INFO
Value: "android.nfc.action.PREFERRED_PAYMENT_CHANGED"
ACTION_TAG_DISCOVERED
static val ACTION_TAG_DISCOVERED: String
Intent to start an activity when a tag is discovered.
This intent will not be started when a tag is discovered if any activities respond to ACTION_NDEF_DISCOVERED
or ACTION_TECH_DISCOVERED
for the current tag.
Value: "android.nfc.action.TAG_DISCOVERED"
ACTION_TECH_DISCOVERED
static val ACTION_TECH_DISCOVERED: String
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.
To receive this intent an activity must include an intent filter for this action and specify the desired tech types in a manifest meta-data
entry. Here is an example manfiest entry:
<activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"> <!-- Add a technology filter --> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/filter_nfc" /> </activity>
The meta-data XML file should contain one or more tech-list
entries each consisting or one or more tech
entries. The tech
entries refer to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
A tag matches if any of the tech-list
sets is a subset of Tag.getTechList()
. Each of the tech-list
s is considered independently and the activity is considered a match is any single tech-list
matches the tag that was discovered. This provides AND and OR semantics for filtering desired techs. Here is an example that will match any tag using NfcF
or any tag using NfcA
, MifareClassic
, and Ndef
:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- capture anything using NfcF --> <tech-list> <tech>android.nfc.tech.NfcF</tech> </tech-list> <!-- OR --> <!-- capture all MIFARE Classics with NDEF payloads --> <tech-list> <tech>android.nfc.tech.NfcA</tech> <tech>android.nfc.tech.MifareClassic</tech> <tech>android.nfc.tech.Ndef</tech> </tech-list> </resources>
This intent is started after ACTION_NDEF_DISCOVERED
and before ACTION_TAG_DISCOVERED
. If any activities respond to ACTION_NDEF_DISCOVERED
this intent will not be started. If any activities respond to this intent ACTION_TAG_DISCOVERED
will not be started.
Value: "android.nfc.action.TECH_DISCOVERED"
ACTION_TRANSACTION_DETECTED
static val ACTION_TRANSACTION_DETECTED: String
Broadcast Action: Intent to notify an application that a transaction event has occurred on the Secure Element.
This intent will only be sent if the application has requested permission for android.Manifest.permission#NFC_TRANSACTION_EVENT
and if the application has the necessary access to Secure Element which witnessed the particular event.
Requires android.Manifest.permission#NFC_TRANSACTION_EVENT
Value: "android.nfc.action.TRANSACTION_DETECTED"
EXTRA_ADAPTER_STATE
static val EXTRA_ADAPTER_STATE: String
Used as an int extra field in ACTION_ADAPTER_STATE_CHANGED
intents to request the current power state. Possible values are: STATE_OFF
, STATE_TURNING_ON
, STATE_ON
, STATE_TURNING_OFF
,
Value: "android.nfc.extra.ADAPTER_STATE"
EXTRA_AID
static val EXTRA_AID: String
Mandatory byte[] extra field in ACTION_TRANSACTION_DETECTED
Value: "android.nfc.extra.AID"
EXTRA_DATA
static val EXTRA_DATA: String
Optional byte[] extra field in ACTION_TRANSACTION_DETECTED
Value: "android.nfc.extra.DATA"
EXTRA_ID
static val EXTRA_ID: String
Optional extra containing a byte array containing the ID of the discovered tag for the ACTION_NDEF_DISCOVERED
, ACTION_TECH_DISCOVERED
, and ACTION_TAG_DISCOVERED
intents.
Value: "android.nfc.extra.ID"
EXTRA_NDEF_MESSAGES
static val EXTRA_NDEF_MESSAGES: String
Extra containing an array of NdefMessage
present on the discovered tag.
This extra is mandatory for ACTION_NDEF_DISCOVERED
intents, and optional for ACTION_TECH_DISCOVERED
, and ACTION_TAG_DISCOVERED
intents.
When this extra is present there will always be at least one NdefMessage
element. Most NDEF tags have only one NDEF message, but we use an array for future compatibility.
Value: "android.nfc.extra.NDEF_MESSAGES"
EXTRA_PREFERRED_PAYMENT_CHANGED_REASON
static val EXTRA_PREFERRED_PAYMENT_CHANGED_REASON: String
Mandatory String extra field in ACTION_PREFERRED_PAYMENT_CHANGED
Indicates the condition when trigger this event. Possible values are: PREFERRED_PAYMENT_LOADED
, PREFERRED_PAYMENT_CHANGED
, PREFERRED_PAYMENT_UPDATED
,
Value: "android.nfc.extra.PREFERRED_PAYMENT_CHANGED_REASON"
EXTRA_READER_PRESENCE_CHECK_DELAY
static val EXTRA_READER_PRESENCE_CHECK_DELAY: String
Int Extra for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this integer extra allows the calling application to specify the delay that the platform will use for performing presence checks on any discovered tag.
Value: "presence"
EXTRA_SECURE_ELEMENT_NAME
static val EXTRA_SECURE_ELEMENT_NAME: String
Mandatory String extra field in ACTION_TRANSACTION_DETECTED
Indicates the Secure Element on which the transaction occurred. eSE1...eSEn for Embedded Secure Elements, SIM1...SIMn for UICC/EUICC, etc.
Value: "android.nfc.extra.SECURE_ELEMENT_NAME"
EXTRA_TAG
static val EXTRA_TAG: String
Mandatory extra containing the Tag
that was discovered for the ACTION_NDEF_DISCOVERED
, ACTION_TECH_DISCOVERED
, and ACTION_TAG_DISCOVERED
intents.
Value: "android.nfc.extra.TAG"
FLAG_LISTEN_DISABLE
static val FLAG_LISTEN_DISABLE: Int
Flags for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag disables listening.
Value: 0
FLAG_LISTEN_KEEP
static val FLAG_LISTEN_KEEP: Int
Flags for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag makes listening to keep the current technology configuration.
Value: -2147483648
FLAG_LISTEN_NFC_PASSIVE_A
static val FLAG_LISTEN_NFC_PASSIVE_A: Int
Flag for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag enables listening for Nfc-A technology.
Value: 1
FLAG_LISTEN_NFC_PASSIVE_B
static val FLAG_LISTEN_NFC_PASSIVE_B: Int
Flag for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag enables listening for Nfc-B technology.
Value: 2
FLAG_LISTEN_NFC_PASSIVE_F
static val FLAG_LISTEN_NFC_PASSIVE_F: Int
Flag for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag enables listening for Nfc-F technology.
Value: 4
FLAG_READER_DISABLE
static val FLAG_READER_DISABLE: Int
Flags for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag disables polling.
Value: 0
FLAG_READER_KEEP
static val FLAG_READER_KEEP: Int
Flags for use with setDiscoveryTechnology(android.app.Activity,int,int)
.
Setting this flag makes polling to keep the current technology configuration.
Value: -2147483648
FLAG_READER_NFC_A
static val FLAG_READER_NFC_A: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag enables polling for Nfc-A technology.
Value: 1
FLAG_READER_NFC_B
static val FLAG_READER_NFC_B: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag enables polling for Nfc-B technology.
Value: 2
FLAG_READER_NFC_BARCODE
static val FLAG_READER_NFC_BARCODE: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag enables polling for NfcBarcode technology.
Value: 16
FLAG_READER_NFC_F
static val FLAG_READER_NFC_F: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag enables polling for Nfc-F technology.
Value: 4
FLAG_READER_NFC_V
static val FLAG_READER_NFC_V: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag enables polling for Nfc-V (ISO15693) technology.
Value: 8
FLAG_READER_NO_PLATFORM_SOUNDS
static val FLAG_READER_NO_PLATFORM_SOUNDS: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag allows the caller to prevent the platform from playing sounds when it discovers a tag.
Value: 256
FLAG_READER_SKIP_NDEF_CHECK
static val FLAG_READER_SKIP_NDEF_CHECK: Int
Flag for use with enableReaderMode(android.app.Activity,android.nfc.NfcAdapter.ReaderCallback,int,android.os.Bundle)
.
Setting this flag allows the caller to prevent the platform from performing an NDEF check on the tags it finds.
Value: 128
PREFERRED_PAYMENT_CHANGED
static val PREFERRED_PAYMENT_CHANGED: Int
User selected another payment application as the preferred payment.
Value: 2
PREFERRED_PAYMENT_LOADED
static val PREFERRED_PAYMENT_LOADED: Int
Nfc is enabled and the preferred payment aids are registered.
Value: 1
PREFERRED_PAYMENT_UPDATED
static val PREFERRED_PAYMENT_UPDATED: Int
Current preferred payment has issued an update (registered/unregistered new aids or has been updated itself).
Value: 3
Public methods
disable
fun disable(): Boolean
Disable NFC hardware.
No NFC features will work after this call, and the hardware will not perform or respond to any NFC communication.
This call is asynchronous. Listen for ACTION_ADAPTER_STATE_CHANGED
broadcasts to find out when the operation is complete.
This API is only allowed to be called by system apps or apps which are Device Owner or Profile Owner.
If this returns true, then either NFC is already off, or a ACTION_ADAPTER_STATE_CHANGED
broadcast will be sent to indicate a state transition. If this returns false, then there is some problem that prevents an attempt to turn NFC off.
Requires android.Manifest.permission#WRITE_SECURE_SETTINGS
disableForegroundDispatch
fun disableForegroundDispatch(activity: Activity!): Unit
Disable foreground dispatch to the given activity.
After calling enableForegroundDispatch
, an activity must call this method before its Activity.onPause
callback completes.
This method must be called from the main thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
activity |
Activity!: the Activity to disable dispatch to |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the Activity has already been paused |
java.lang.UnsupportedOperationException |
if FEATURE_NFC is unavailable. |
disableReaderMode
fun disableReaderMode(activity: Activity!): Unit
Restore the NFC adapter to normal mode of operation: supporting peer-to-peer (Android Beam), card emulation, and polling for all supported tag technologies.
Parameters | |
---|---|
activity |
Activity!: the Activity that currently has reader mode enabled |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC is unavailable. |
enable
fun enable(): Boolean
Enable NFC hardware.
This call is asynchronous. Listen for ACTION_ADAPTER_STATE_CHANGED
broadcasts to find out when the operation is complete.
This API is only allowed to be called by system apps or apps which are Device Owner or Profile Owner.
If this returns true, then either NFC is already on, or a ACTION_ADAPTER_STATE_CHANGED
broadcast will be sent to indicate a state transition. If this returns false, then there is some problem that prevents an attempt to turn NFC on (for example we are in airplane mode and NFC is not toggleable in airplane mode on this platform).
Requires android.Manifest.permission#WRITE_SECURE_SETTINGS
enableForegroundDispatch
fun enableForegroundDispatch(
activity: Activity!,
intent: PendingIntent!,
filters: Array<IntentFilter!>!,
techLists: Array<Array<String!>!>!
): Unit
Enable foreground dispatch to the given Activity.
This will give priority to the foreground activity when dispatching a discovered Tag
to an application.
If any IntentFilters are provided to this method they are used to match dispatch Intents for both the NfcAdapter.ACTION_NDEF_DISCOVERED
and NfcAdapter.ACTION_TAG_DISCOVERED
. Since NfcAdapter.ACTION_TECH_DISCOVERED
relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled by passing in the tech lists separately. Each first level entry in the tech list represents an array of technologies that must all be present to match. If any of the first level sets match then the dispatch is routed through the given PendingIntent. In other words, the second level is ANDed together and the first level entries are ORed together.
If you pass null
for both the filters
and techLists
parameters that acts a wild card and will cause the foreground activity to receive all tags via the NfcAdapter.ACTION_TAG_DISCOVERED
intent.
This method must be called from the main thread, and only when the activity is in the foreground (resumed). Also, activities must call disableForegroundDispatch
before the completion of their Activity.onPause
callback to disable foreground dispatch after it has been enabled.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
activity |
Activity!: the Activity to dispatch to |
intent |
PendingIntent!: the PendingIntent to start for the dispatch |
filters |
Array<IntentFilter!>!: the IntentFilters to override dispatching for, or null to always dispatch |
techLists |
Array<Array<String!>!>!: the tech lists used to perform matching for dispatching of the NfcAdapter.ACTION_TECH_DISCOVERED intent |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the Activity is not currently in the foreground |
java.lang.UnsupportedOperationException |
if FEATURE_NFC is unavailable. |
enableReaderMode
fun enableReaderMode(
activity: Activity!,
callback: NfcAdapter.ReaderCallback!,
flags: Int,
extras: Bundle!
): Unit
Limit the NFC controller to reader mode while this Activity is in the foreground.
In this mode the NFC controller will only act as an NFC tag reader/writer, thus disabling any peer-to-peer (Android Beam) and card-emulation modes of the NFC adapter on this device.
Use FLAG_READER_SKIP_NDEF_CHECK
to prevent the platform from performing any NDEF checks in reader mode. Note that this will prevent the Ndef
tag technology from being enumerated on the tag, and that NDEF-based tag dispatch will not be functional.
For interacting with tags that are emulated on another Android device using Android's host-based card-emulation, the recommended flags are FLAG_READER_NFC_A
and FLAG_READER_SKIP_NDEF_CHECK
.
Parameters | |
---|---|
activity |
Activity!: the Activity that requests the adapter to be in reader mode |
callback |
NfcAdapter.ReaderCallback!: the callback to be called when a tag is discovered |
flags |
Int: Flags indicating poll technologies and other optional parameters |
extras |
Bundle!: Additional extras for configuring reader mode. |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC is unavailable. |
getDefaultAdapter
static fun getDefaultAdapter(context: Context!): NfcAdapter!
Helper to get the default NFC Adapter.
Most Android devices will only have one NFC Adapter (NFC Controller).
This helper is the equivalent of:
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); NfcAdapter adapter = manager.getDefaultAdapter();
Parameters | |
---|---|
context |
Context!: the calling application's context |
Return | |
---|---|
NfcAdapter! |
the default NFC adapter, or null if no NFC adapter exists |
getNfcAntennaInfo
fun getNfcAntennaInfo(): NfcAntennaInfo?
Returns information regarding Nfc antennas on the device such as their relative positioning on the device.
Return | |
---|---|
NfcAntennaInfo? |
Information on the nfc antenna(s) on the device. This value may be null . |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC, FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE are unavailable |
ignore
fun ignore(
tag: Tag!,
debounceMs: Int,
tagRemovedListener: NfcAdapter.OnTagRemovedListener!,
handler: Handler!
): Boolean
Signals that you are no longer interested in communicating with an NFC tag for as long as it remains in range. All future attempted communication to this tag will fail with IOException
. The NFC controller will be put in a low-power polling mode, allowing the device to save power in cases where it's "attached" to a tag all the time (e.g. a tag in car dock). Additionally the debounceMs parameter allows you to specify for how long the tag needs to have gone out of range, before it will be dispatched again. Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms). This means that if the tag repeatedly goes in and out of range (for example, in case of a flaky connection), and the controller happens to poll every time the tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag having been "in range" during the interval. Note 2: if a tag with another UID is detected after this API is called, its effect will be cancelled; if this tag shows up before the amount of time specified in debounceMs, it will be dispatched again. Note 3: some tags have a random UID, in which case this API won't work reliably.
Parameters | |
---|---|
tag |
Tag!: the Tag to ignore. |
debounceMs |
Int: minimum amount of time the tag needs to be out of range before being dispatched again. |
tagRemovedListener |
NfcAdapter.OnTagRemovedListener!: listener to be called when the tag is removed from the field. Note that this will only be called if the tag has been out of range for at least debounceMs, or if another tag came into range before debounceMs. May be null in case you don't want a callback. |
handler |
Handler!: the Handler that will be used for delivering the callback. if the handler is null, then the thread used for delivering the callback is unspecified. |
Return | |
---|---|
Boolean |
false if the tag couldn't be found (or has already gone out of range), true otherwise |
isEnabled
fun isEnabled(): Boolean
Return true if this NFC Adapter has any features enabled.
If this method returns false, the NFC hardware is guaranteed not to generate or respond to any NFC communication over its NFC radio.
Applications can use this to check if NFC is enabled. Applications can request Settings UI allowing the user to toggle NFC using:
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))
Return | |
---|---|
Boolean |
true if this NFC Adapter has any features enabled |
isObserveModeEnabled
fun isObserveModeEnabled(): Boolean
Returns whether Observe Mode is currently enabled or not.
Return | |
---|---|
Boolean |
true if observe mode is enabled, false otherwise. |
isObserveModeSupported
fun isObserveModeSupported(): Boolean
Returns whether the device supports observe mode or not. When observe mode is enabled, the NFC hardware will listen to NFC readers, but not respond to them. While enabled, observed polling frames will be sent to the APDU service (see setObserveModeEnabled(boolean)
. When observe mode is disabled (or if it's not supported), the NFC hardware will automatically respond to the reader and proceed with the transaction.
Return | |
---|---|
Boolean |
true if the mode is supported, false otherwise. |
isReaderOptionEnabled
fun isReaderOptionEnabled(): Boolean
Checks NFC Reader option feature is enabled.
Return | |
---|---|
Boolean |
True if NFC Reader option is enabled, false otherwise |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if device doesn't support NFC Reader option functionality. isReaderOptionSupported |
isReaderOptionSupported
fun isReaderOptionSupported(): Boolean
Checks if the device supports NFC Reader option functionality.
Return | |
---|---|
Boolean |
True if device supports NFC Reader option, false otherwise |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC is unavailable. |
isSecureNfcEnabled
fun isSecureNfcEnabled(): Boolean
Checks Secure NFC feature is enabled.
Return | |
---|---|
Boolean |
True if Secure NFC is enabled, false otherwise |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if device doesn't support Secure NFC functionality. isSecureNfcSupported |
isSecureNfcSupported
fun isSecureNfcSupported(): Boolean
Checks if the device supports Secure NFC functionality.
Return | |
---|---|
Boolean |
True if device supports Secure NFC, false otherwise |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC, FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE are unavailable |
resetDiscoveryTechnology
fun resetDiscoveryTechnology(activity: Activity): Unit
Restore the poll/listen technologies of NFC controller to its default state, which were changed by setDiscoveryTechnology(android.app.Activity,int,int)
Parameters | |
---|---|
activity |
Activity: The Activity that requested to change technologies. This value cannot be null . |
setDiscoveryTechnology
fun setDiscoveryTechnology(
activity: Activity,
pollTechnology: Int,
listenTechnology: Int
): Unit
Set the NFC controller to enable specific poll/listen technologies, as specified in parameters, while this Activity is in the foreground. Use FLAG_READER_KEEP
to keep current polling technology. Use FLAG_LISTEN_KEEP
to keep current listenig technology. (if the _KEEP flag is specified the other technology flags shouldn't be set and are quietly ignored otherwise). Use FLAG_READER_DISABLE
to disable polling. Use FLAG_LISTEN_DISABLE
to disable listening. Also refer to resetDiscoveryTechnology(android.app.Activity)
to restore these changes.
Poll Listen Passive A 0x01 (NFC_A) 0x01 (NFC_PASSIVE_A) Passive B 0x02 (NFC_B) 0x02 (NFC_PASSIVE_B) Passive F 0x04 (NFC_F) 0x04 (NFC_PASSIVE_F) ISO 15693 0x08 (NFC_V) - Kovio 0x10 (NFC_BARCODE) -
Example usage in an Activity that requires to disable poll, keep current listen technologies:
protected void onResume() { mNfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); mNfcAdapter.setDiscoveryTechnology(this, NfcAdapter.FLAG_READER_DISABLE, NfcAdapter.FLAG_LISTEN_KEEP); }
Parameters | |
---|---|
activity |
Activity: The Activity that requests NFC controller to enable specific technologies. This value cannot be null . |
pollTechnology |
Int: Flags indicating poll technologies. Value is either 0 or a combination of android.nfc.NfcAdapter.FLAG_SET_DEFAULT_TECH, android.nfc.NfcAdapter#FLAG_READER_KEEP , android.nfc.NfcAdapter#FLAG_READER_DISABLE , android.nfc.NfcAdapter#FLAG_READER_NFC_A , android.nfc.NfcAdapter#FLAG_READER_NFC_B , android.nfc.NfcAdapter#FLAG_READER_NFC_F , android.nfc.NfcAdapter#FLAG_READER_NFC_V , and android.nfc.NfcAdapter#FLAG_READER_NFC_BARCODE |
listenTechnology |
Int: Flags indicating listen technologies. Value is either 0 or a combination of android.nfc.NfcAdapter.FLAG_SET_DEFAULT_TECH, android.nfc.NfcAdapter#FLAG_LISTEN_KEEP , android.nfc.NfcAdapter#FLAG_LISTEN_DISABLE , android.nfc.NfcAdapter#FLAG_LISTEN_NFC_PASSIVE_A , android.nfc.NfcAdapter#FLAG_LISTEN_NFC_PASSIVE_B , and android.nfc.NfcAdapter#FLAG_LISTEN_NFC_PASSIVE_F |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if FEATURE_NFC, FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF are unavailable. |
setObserveModeEnabled
fun setObserveModeEnabled(enabled: Boolean): Boolean
Controls whether the NFC adapter will allow transactions to proceed or be in observe mode and simply observe and notify the APDU service of polling loop frames. See isObserveModeSupported()
for a description of observe mode. Only the package of the currently preferred service (the service set as preferred by the current foreground application via android.nfc.cardemulation.CardEmulation#setPreferredService(Activity, * android.content.ComponentName)
or the current Default Wallet Role Holder android.app.role.RoleManager#ROLE_WALLET
), otherwise a call to this method will fail and return false.
Parameters | |
---|---|
enabled |
Boolean: false disables observe mode to allow the transaction to proceed while true enables observe mode and does not allow transactions to proceed. |
Return | |
---|---|
Boolean |
boolean indicating success or failure. |