com.github.devnied.emvnfccard.utils.NFCUtils Maven / Gradle / Ivy
package com.github.devnied.emvnfccard.utils;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
/**
*
* Utils class used to manager NFC Adapter
*
* @author MILLAU julien
*
*/
public class NFCUtils {
/**
* Check if the NFCAdapter is enable
*
* @return true if the NFCAdapter is available enable
*/
public static boolean isNfcEnable(final Context pContext) {
boolean ret = true;
try {
NfcAdapter adpater = NfcAdapter.getDefaultAdapter(pContext);
ret = adpater != null && adpater.isEnabled();
} catch (UnsupportedOperationException e) {
ret = false;
}
return ret;
}
/**
* NFC adapter
*/
private NfcAdapter mNfcAdapter;
/**
* Intent sent
*/
private PendingIntent mPendingIntent;
/**
* Parent Activity
*/
private final Activity mActivity;
/**
* Constructor of this class
*
* @param pActivity
* activity context
*/
public NFCUtils(final Activity pActivity) {
mActivity = pActivity;
mNfcAdapter = NfcAdapter.getDefaultAdapter(pActivity);
mPendingIntent = PendingIntent.getActivity(mActivity, 0,
new Intent(mActivity, mActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
/**
* Disable dispacher Remove the most important priority for foreground application
*/
public void disableDispatch() {
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(mActivity);
}
}
/**
* Activate NFC dispacher to read NFC Card Set the most important priority to the foreground application
*/
public void enableDispatch() {
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(mActivity, mPendingIntent, null, null);
}
}
/**
* Getter mNfcAdapter
*
* @return the mNfcAdapter
*/
public NfcAdapter getmNfcAdapter() {
return mNfcAdapter;
}
}