I want to develop a android application witch can read the ID of a RFID card(mifare classic type A) usin NFC but my Application detect the card but it does not return any thing .. help please btw i hqve 2 class
class MainActivity:
package com.example.nfcreader;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
static String TAG = "NFCREADER";
NFCForegroundUtil nfcForegroundUtil = null;
private TextView info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView)findViewById(R.id.title);
nfcForegroundUtil = new NFCForegroundUtil(this);
}
public void onPause() {
super.onPause();
nfcForegroundUtil.disableForeground();
}
public void onResume() {
super.onResume();
nfcForegroundUtil.enableForeground();
if (!nfcForegroundUtil.getNfc().isEnabled())
{
Toast.makeText(getApplicationContext(),
"Please activate NFC and press Back to return to the application!",
Toast.LENGTH_LONG).show();
startActivity(
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
public void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
/*StringBuilder sb = new StringBuilder();
for(int i = 0; i < tag.getId().length; i++){
sb.append(new Integer(tag.getId()[i]) + " ");
}*/
info.setText("TagID: " + bytesToHex(tag.getId()));
//byte[] id = tag.getId();
}
/**
* Convenience method to convert a byte array to a hex string.
*
* @param data the byte[] to convert
* @return String the converted byte[]
*/
public static String bytesToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
buf.append(byteToHex(data[i]).toUpperCase());
buf.append(" ");
}
return (buf.toString());
}
/**
* method to convert a byte to a hex string.
*
* @param data the byte to convert
* @return String the converted byte
*/
public static String byteToHex(byte data) {
StringBuffer buf = new StringBuffer();
buf.append(toHexChar((data >>> 4) & 0x0F));
buf.append(toHexChar(data & 0x0F));
return buf.toString();
}
/**
* Convenience method to convert an int to a hex char.
*
* @param i the int to convert
* @return char the converted char
*/
public static char toHexChar(int i) {
if ((0 <= i) && (i <= 9)) {
return (char) ('0' + i);
} else {
return (char) ('a' + (i - 10));
}
}
}
class NFCForegroundUtil:
package com.example.nfcreader;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.tech.NfcA;
import android.util.Log;
public class NFCForegroundUtil {
private NfcAdapter nfc;
private Activity activity;
private IntentFilter intentFiltersArray[];
private PendingIntent intent;
private String techListsArray[][];
public NFCForegroundUtil(Activity activity) {
super();
this.activity = activity;
nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());
intent = PendingIntent.getActivity(activity, 0, new Intent(activity,
activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("Unable to speciy */* Mime Type", e);
}
intentFiltersArray = new IntentFilter[] { ndef };
techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
}
public void enableForeground()
{
Log.d("demo", "Foreground NFC dispatch enabled");
nfc.enableForegroundDispatch(
activity, intent, intentFiltersArray, techListsArray);
}
public void disableForeground()
{
Log.d("demo", "Foreground NFC dispatch disabled");
nfc.disableForegroundDispatch(activity);
}
public NfcAdapter getNfc() {
return nfc;
}
}
and my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfcreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<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.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>