Pages

Saturday, April 2, 2016

Android NFC read MifareClassic RFID tag with android nfc action TECH DISCOVERED

This example show using intent-filter of "android.nfc.action.TECH_DISCOVERED", specify target tech of android.nfc.tech.MifareClassic, and read basic info my MifareClassic RFID tag.


Edit AndroidManifest.xml, to specify <uses-permission> of "android.permission.NFC", and <uses-feature> of "android.hardware.nfc".
Add <intent-filter>> of "android.nfc.action.TECH_DISCOVERED", and <meta-data> for "@xml/nfc_tech_filter".
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.blogspot.android_er.androidnfctechdiscovered" >

<uses-permission android_name="android.permission.NFC"/>
<uses-feature android_name="android.hardware.nfc"
android_required="true"/>

<application
android_allowBackup="true"
android_icon="@mipmap/ic_launcher"
android_label="@string/app_name"
android_supportsRtl="true"
android_theme="@style/AppTheme" >
<activity android_name=".MainActivity" >
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android_name="android.nfc.action.TECH_DISCOVERED"/>
<category android_name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android_name="android.nfc.action.TECH_DISCOVERED"
android_resource="@xml/nfc_tech_filter" />
</activity>
</application>

</manifest>


Create xml/nfc_tech_filter.xml. In order to make it simple, we specify one tech only, android.nfc.tech.MifareClassic.
<resources >
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>

MainActivity.java
package com.blogspot.android_er.androidnfctechdiscovered;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private NfcAdapter nfcAdapter;
TextView textViewInfo, textViewTagInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInfo = (TextView)findViewById(R.id.info);
textViewTagInfo = (TextView)findViewById(R.id.taginfo);

nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter == null){
Toast.makeText(this,
"NFC NOT supported on this devices!",
Toast.LENGTH_LONG).show();
finish();
}else if(!nfcAdapter.isEnabled()){
Toast.makeText(this,
"NFC NOT Enabled!",
Toast.LENGTH_LONG).show();
finish();
}
}

@Override
protected void onResume() {
super.onResume();

Intent intent = getIntent();
String action = intent.getAction();

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Toast.makeText(this,
"onResume() - ACTION_TECH_DISCOVERED",
Toast.LENGTH_SHORT).show();

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag == null){
textViewInfo.setText("tag == null");
}else{
String tagInfo = tag.toString() + " ";

tagInfo += " Tag Id: ";
byte[] tagId = tag.getId();
tagInfo += "length = " + tagId.length +" ";
for(int i=0; i<tagId.length; i++){
tagInfo += Integer.toHexString(tagId[i] & 0xFF) + " ";
}
tagInfo += " ";

String[] techList = tag.getTechList();
tagInfo += " Tech List ";
tagInfo += "length = " + techList.length +" ";
for(int i=0; i<techList.length; i++){
tagInfo += techList[i] + " ";
}

textViewInfo.setText(tagInfo);

//Only android.nfc.tech.MifareClassic specified in nfc_tech_filter.xml,
//so must be MifareClassic
readMifareClassic(tag);
}
}else{
Toast.makeText(this,
"onResume() : " + action,
Toast.LENGTH_SHORT).show();
}
}

public void readMifareClassic(Tag tag){
MifareClassic mifareClassicTag = MifareClassic.get(tag);

String typeInfoString = "--- MifareClassic tag --- ";
int type = mifareClassicTag.getType();
switch(type){
case MifareClassic.TYPE_PLUS:
typeInfoString += "MifareClassic.TYPE_PLUS ";
break;
case MifareClassic.TYPE_PRO:
typeInfoString += "MifareClassic.TYPE_PRO ";
break;
case MifareClassic.TYPE_CLASSIC:
typeInfoString += "MifareClassic.TYPE_CLASSIC ";
break;
case MifareClassic.TYPE_UNKNOWN:
typeInfoString += "MifareClassic.TYPE_UNKNOWN ";
break;
default:
typeInfoString += "unknown...! ";
}

int size = mifareClassicTag.getSize();
switch(size){
case MifareClassic.SIZE_1K:
typeInfoString += "MifareClassic.SIZE_1K ";
break;
case MifareClassic.SIZE_2K:
typeInfoString += "MifareClassic.SIZE_2K ";
break;
case MifareClassic.SIZE_4K:
typeInfoString += "MifareClassic.SIZE_4K ";
break;
case MifareClassic.SIZE_MINI:
typeInfoString += "MifareClassic.SIZE_MINI ";
break;
default:
typeInfoString += "unknown size...! ";
}

int blockCount = mifareClassicTag.getBlockCount();
typeInfoString += "BlockCount = " + blockCount + " ";
int sectorCount = mifareClassicTag.getSectorCount();
typeInfoString += "SectorCount = " + sectorCount + " ";

textViewTagInfo.setText(typeInfoString);
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_margin="10dp"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold"/>

<TextView
android_id="@+id/info"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="italic"/>

<TextView
android_id="@+id/taginfo"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="bold"/>

</LinearLayout>


Reference:
- http://developer.android.com/guide/topics/connectivity/nfc/nfc.html
- http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html
- http://developer.android.com/reference/android/nfc/tech/MifareClassic.html

Next:
- Android NFC: readBlock() for MifareClassic, to dump data in RFID tag

Related Posts by Categories

0 comments:

Post a Comment