public final class NfcController extends Object
Using interfaces provided by this class, you can enable and disable NFC, and query the NFC capabilities and status.
| 限定符和类型 | 类 | 说明 |
|---|---|---|
static interface |
NfcController.ReaderModeCallback |
A callback to be invoked when the system finds a tag while the foreground page is operating in reader mode.
|
class |
NfcController.ReaderModeCallbackStub |
Provides ReaderModeCallback for communicate with remote.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static String |
EXTRA_NFC_STATE |
Indicates the NFC status.
|
static String |
EXTRA_NFC_TRANSACTION |
Indicates the NFC transaction information.
|
static String |
EXTRA_TAG_INFO |
Indicates the key (in string format) used to obtain the tag information.
|
static String |
FIELD_OFF_DETECTED |
Indicates a field-off event detected by NFC.
|
static String |
FIELD_ON_DETECTED |
Indicates a field-on event detected by NFC.
|
static String |
STATE_CHANGED |
Indicates an NFC status change.
|
static int |
STATE_OFF |
Indicates that NFC is disabled.
|
static int |
STATE_ON |
Indicates that NFC is enabled.
|
static int |
STATE_TURNING_OFF |
Indicates that NFC is being disabled.
|
static int |
STATE_TURNING_ON |
Indicates that NFC is being enabled.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
closeNfc() |
Disables NFC.
|
static NfcController |
getInstance(Context context) |
Obtains a singleton instance of the
NfcController class. |
int |
getNfcState() |
Obtains the NFC status.
|
boolean |
isNfcAvailable() |
Checks whether a device supports NFC.
|
boolean |
isNfcOpen() |
Checks whether NFC is enabled.
|
boolean |
openNfc() |
Enables NFC.
|
void |
registerForegroundDispatch(Ability ability,
IntentAgent intent,
List<String> filters,
String[][] profiles) |
Register foreground dispatch to the given NFC OHOSApplication ability.
|
void |
setReaderMode(Ability ability,
NfcController.ReaderModeCallback callback,
int flags,
PacMapEx pacMapEx) |
Sets the NFC OHOSApplication ability to reader mode only when this ability is in the foreground.
|
void |
unregisterForegroundDispatch(Ability ability) |
Unregister foreground dispatch to the given NFC OHOSApplication ability.
|
void |
unsetReaderMode(Ability ability) |
Unsets the NFC OHOSApplication ability from reader mode only when this ability is in the foreground.
|
public static final String EXTRA_NFC_STATE
This is a string constant used to read the NFC status from a broadcast message.
public static final String EXTRA_NFC_TRANSACTION
This is a constant string used to obtain the NFC transaction information from a field-on broadcast message.
public static final String EXTRA_TAG_INFO
public static final String FIELD_OFF_DETECTED
This is a string constant used to send broadcast messages of field-off events.
public static final String FIELD_ON_DETECTED
This is a string constant used to send broadcast messages of field-on events.
public static final String STATE_CHANGED
This is a string constant used to send broadcast messages of NFC status changes.
public static final int STATE_OFF
public static final int STATE_ON
public static final int STATE_TURNING_OFF
public static final int STATE_TURNING_ON
public static NfcController getInstance(Context context) throws IllegalArgumentException
NfcController class.
If no singleton instance is available, a singleton instance will be created.
context - Indicates the application context.IllegalArgumentException - if the caller is missing required context.public boolean openNfc()
throws NfcPermissionException
true if NFC is enabled or has been enabled; returns false otherwise.NfcPermissionException - Throws this exception if you have no permission to invoke this method.public boolean closeNfc()
throws NfcPermissionException
true if NFC is disabled or has been disabled; returns false otherwise.NfcPermissionException - Throws this exception if you have no permission to invoke this method.public boolean isNfcOpen()
true if NFC is enabled; returns false otherwise.public int getNfcState()
The NFC status can be any of the following:
STATE_OFF: Indicates that NFC
is disabled. STATE_TURNING_ON: Indicates that NFC is being enabled.
STATE_ON: Indicates that NFC is enabled. STATE_TURNING_OFF: Indicates
that NFC is being disabled.public boolean isNfcAvailable()
true if the device supports NFC; returns false otherwise.public void setReaderMode(Ability ability, NfcController.ReaderModeCallback callback, int flags, PacMapEx pacMapEx)
ability - Indicates the NFC OHOSApplication ability to set.callback - Indicates the callback to be called when a tag is discovered.flags - Indicates poll profiles and other optional parameters.pacMapEx - Indicates additional extras for configuring reader mode.public void unsetReaderMode(Ability ability)
ability - Indicates the NFC OHOSApplication ability to unset.public void registerForegroundDispatch(Ability ability, IntentAgent intent, List<String> filters, String[][] profiles)
ability - Indicates the NFC OHOSApplication ability.intent - Indicates the IntentAgent to start for the dispatch.filters - Indicates the IntentFilters to override dispatching for, or null to always dispatch.profiles - Indicates the profiles used to perform matching for dispatching.public void unregisterForegroundDispatch(Ability ability)
ability - Indicates the NFC OHOSApplication ability.