Skip to content

gzeinnumer/NFCReader

Repository files navigation

NFCReader


Simple NFC Reader.


Content List


Download

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
}

Feature List


Usage

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>

Permission

<?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>

Check NFC Hardware (Type 1)

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();
}

Start NFC Tagging (Type 1)

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();
    }
});

End NFC Tagging

@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);
        }
    });
}

Sample Code Type 1 & Type 2

Preview :

Ask NFC to active Dialog Tagging NFC feature not found in divice
Result

Customize

  • Design

You can change design of the Dialog Tagging.

  1. Make default_tagging_dialog.xml in res/layout
  2. Copy this code root default_tagging_dialog.xml
  3. Just Customize it
Customize Result

  • Message

You can change mesage of the Dialog Tagging.

  1. Add this name to your strings.xml
  2. 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>

Example Code/App

Sample Code And App


Version

  • 1.0.0
    • First Release
  • 2.0.0
    • Support SDK 16
  • 2.0.1
    • Bug Fixing

Contribution

You can sent your constibution to branch open-pull.


Copyright 2021 M. Fadli Zein

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages