6 Star 27 Fork 8

leohan1992 / AndroidNFC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
README.md 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
leohan1992 提交于 2017-06-13 22:51 . Create 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

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

搜索帮助