Score
0
Watch 5 Star 18 Fork 3

leohan1992 / AndroidNFCAndroid

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
a base activity for use nfc spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

AndroidNFC

a base activity for use nfc

dependency

Maven

<dependency>
  <groupId>xyz.leohan</groupId>
  <artifactId>AndroidNFC</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

Gradle

compile 'xyz.leohan:AndroidNFC:1.0.0'

How to Use

  1. create a New Activity extends xyz.leohan.androidnfclib.NfcActivity,implement onNfcTouch() method.Then do other things in an Activit as usual
public class MyActivity extends NfcActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onNfcTouch() {
        //this method will called when a NFC tag touched the phone and can be analysed
        //we can get NFC tag id here;
    }
}
  1. write these in your AndroidManifest.xml.
    android:launchMode="singleTask" and the intent-filter is necessary
       <activity
            android:name=".yourActivityName"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>
  1. main methods in NFCActivity:
void readNfcContent();//can read message from NFC tag
boolean writeNfc(String msg);//write something to NFC tag
boolean deleteNfc(); //clear NFC tag

Sample

public class MainActivity extends NfcActivity {
    private TextView tvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    protected void onNfcTouch() {
        Log.i("nfc","ontouch");
        tvContent.setText("NFC TagId:" + getTagId());
    }

    public void readNfc(View view) {
        String s = null;
        try {
            //read nfc content from tag;
            s = this.readNfcContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvContent.setText(s);
    }

    public void writeNfcContent(View view) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String content = dateFormat.format(System.currentTimeMillis());
        try {
            //write something to tag;
            this.writeNfc(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearNfc(View view) {
        try {
            //clear nfcContent
            this.deleteNfc();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. I already checked the Runtime Permission in library. Don't worry about it.

Contact Me

you can sended me an e-mail :leo@leohan.xyz

Comments ( 0 )

Sign in for post a comment

Android
1
https://gitee.com/leohan1992/AndroidNFC.git
git@gitee.com:leohan1992/AndroidNFC.git
leohan1992
AndroidNFC
AndroidNFC
master

Help Search