Simple NFC Reader.
Add maven jitpack.io
and dependencies
in build.gradle (Project)
:
// build.gradle project
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/module
dependencies {
...
implementation 'com.github.gzeinnumer:NFCReader:version'
//required
implementation 'com.google.android.material:material:1.2.1'
implementation 'com.github.gzeinnumer:DialogAndroid:last-vesion'
//check on https://github.com/gzeinnumer/DialogAndroid
implementation 'com.github.gzeinnumer:EasyDialogFragment:last-vesion'
//check on https://github.com/gzeinnumer/EasyDialogFragment
implementation 'com.github.gzeinnumer:SimpleMaterialStyle:last-vesion'
//check on https://github.com/gzeinnumer/SimpleMaterialStyle
}
- Check NFC Hardware (Type 1).
- Start NFC Tagging (Type 1) Open Dialog And Start Tagging.
- Combine CheckNFCHardware and StartNFCTagging (Type 2) .
- End NFC Tagging Get Result.
Add This Line to res/color.xml
. Important
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest >
<uses-feature android:name="android.hardware.nfc" />
<uses-permission android:name="android.permission.NFC" />
<application>
</application>
</manifest>
use checkNFCHardware
will return true
if NFC available.
if (NFCTools.checkNFCHardware(MainActivity.this)){
Toast.makeText(this, "NFC Support", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "NFC Not Support", Toast.LENGTH_SHORT).show();
}
If device have the NFC, run this code new StartNFCTagging(MainActivity.this);
if (NFCTools.checkNFCHardware(MainActivity.this)){
new StartNFCTagging(MainActivity.this);
}
Note : Please make sure your divice have NFC feature before run new StartNFCTagging(MainActivity.this);
. Only For Type 1.
Combine checkNFCHardware and StartNFCTagging (Type 2)
It's save to run this code even when your device don't have NFC hardware
new StartNFCTagging(MainActivity.this, new NFCErrorCallBack() {
@Override
public void onNotSupport(String msg) {
//if Device not Support NFC
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
@Override
funcion onNewIntent
from AppCompatActivity
in your activity. and use EndNFCTagging
to get result.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
new EndNFCTagging(intent, getSupportFragmentManager()).observer(new NFCCallBack() {
@Override
public void callBack(String idHex, String idReversedHex, String idDec, String idReversedDec) {
Log.d(TAG, "onNewIntent: Hexadecimal Code " + idHex);
Log.d(TAG, "onNewIntent: Reversed Hexadecimal Code " + idReversedHex);
Log.d(TAG, "onNewIntent: Decimal Code " + idDec);
Log.d(TAG, "onNewIntent: Reversed Decimal Code " + idReversedDec);
}
});
}
Preview :
Ask NFC to active | Dialog Tagging | NFC feature not found in divice |
Result |
- Design
You can change design of the Dialog Tagging
.
- Make
default_tagging_dialog.xml
inres/layout
- Copy this code root default_tagging_dialog.xml
- Just Customize it
Customize Result |
- Message
You can change mesage of the Dialog Tagging
.
- Add this name to your
strings.xml
- Just Customize it
<resources>
<string name="nfc_reader_ask_active_title">Warning</string>
<string name="nfc_reader_ask_active_sub">NFC not active, enable NFC now?</string>
<string name="nfc_reader_ask_active_ok">Ok</string>
<string name="nfc_reader_ask_active_cancel">Cancel</string>
<string name="nfc_reader_msg_no_nfc">Your phone don\'t have NFC Hardware</string>
</resources>
- 1.0.0
- First Release
- 2.0.0
- Support SDK 16
- 2.0.1
- Bug Fixing
You can sent your constibution to branch
open-pull
.
Copyright 2021 M. Fadli Zein