1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
public abstract class NfcDetectorActivity extends BaseActivity {
private static final String TAG = NfcDetectorActivity.class.getName();
protected NfcAdapter nfcAdapter;
protected IntentFilter[] writeTagFilters;
protected PendingIntent nfcPendingIntent;
protected boolean foreground = false;
protected boolean intentProcessed = false;
protected boolean detecting = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
// Check for available NFC Adapter
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
initializeNfc();
}
}
/**
*
* Initialize Nfc fields
*
*/
protected void initializeNfc() {
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(
NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ndefDetected = new IntentFilter(
NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter techDetected = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
try {
techDetected.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
e.printStackTrace();
}
writeTagFilters = new IntentFilter[] { ndefDetected, tagDetected,
techDetected };
}
@Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
// enable foreground mode if nfc is on and we have started detecting
boolean enabled = nfcAdapter.isEnabled();
if (enabled && detecting) {
enableForeground();
}
}
if (!intentProcessed) {
intentProcessed = true;
processIntent();
}
}
@Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null) {
disableForeground();
}
}
@Override
public void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent");
// onResume gets called after this to handle the intent
intentProcessed = false;
setIntent(intent);
}
protected void enableForeground() {
if (!foreground) {
Log.d(TAG, "Enable nfc forground mode");
nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent,
writeTagFilters, new String[][] { new String[] {
MifareClassic.class.getName(),
NfcA.class.getName(), Ndef.class.getName() } });
foreground = true;
}
}
/**
*
* Start detecting NDEF messages
*
*/
protected void startDetecting() {
if (!detecting) {
enableForeground();
detecting = true;
}
}
/**
*
* Stop detecting NDEF messages
*
*/
protected void stopDetecting() {
if (detecting) {
disableForeground();
detecting = false;
}
}
protected void disableForeground() {
if (foreground) {
Log.d(TAG, "Disable nfc forground mode");
nfcAdapter.disableForegroundDispatch(this);
foreground = false;
}
}
/**
*
* Process the current intent, looking for NFC-related actions
*
*/
public void processIntent() {
Intent intent = getIntent();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.d(TAG, "Process NDEF discovered action");
nfcIntentDetected(intent, NfcAdapter.ACTION_NDEF_DISCOVERED);
} else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Log.d(TAG, "Process TAG discovered action");
nfcIntentDetected(intent, NfcAdapter.ACTION_TAG_DISCOVERED);
} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
Log.d(TAG, "Process TECH discovered action");
nfcIntentDetected(intent, NfcAdapter.ACTION_TECH_DISCOVERED);
} else {
Log.d(TAG, "Ignore action " + intent.getAction());
}
}
/**
*
* Launch an activity for nfc (or wireless) settings, so that the user might
* enable or disable nfc
*
*/
@SuppressLint("InlinedApi")
protected void startNfcSettingsActivity() {
if (android.os.Build.VERSION.SDK_INT >= 16) {
startActivity(new Intent(
android.provider.Settings.ACTION_NFC_SETTINGS));
} else {
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
/**
*
* Incoming NFC communication (in form of tag or beam) detected
*
*/
protected abstract void nfcIntentDetected(Intent intent, String action);
} |
Partager