Last active
December 11, 2015 03:38
-
-
Save kamituel/4539409 to your computer and use it in GitHub Desktop.
Android: basic usage of NFC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); | |
| if ( nfcAdapter == null ) { | |
| // Alert user that his device is incompatible | |
| // or deal with it in different way | |
| Log.w("nfc-test", "No NFC adapter present"); | |
| } else { | |
| if ( nfcAdapter.isEnabled() ) { | |
| // Everything ok: NFC present and enabled. | |
| Log.d("nfc-test", "NFC enabled"); | |
| } else { | |
| // NFC present, but disabled. Deal with it. | |
| Log.d("nfc-test", "NFC disabled"); | |
| // On example, user can be presented with settings | |
| // screen, where he can enable NFC. | |
| alertNfcDisabled(); | |
| } | |
| } | |
| private void alertNfcDisabled () { | |
| AlertDialog.Builder b = new AlertDialog.Builder(this); | |
| b.setTitle("NFC disabled") | |
| .setMessage("You have NFC disabled. Please enable it in the system settings.") | |
| .setPositiveButton("Open settings", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| // Open Settings app on the Wireless Settings tab, so it is easy | |
| // for the user to enable NFC. | |
| ParentClass.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); | |
| } | |
| }); | |
| return b.create(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment