Sunday, 15 September 2013

Android Activity Hangs or Screen freeze When Reading Tags From NFC

Android Activity Hangs or Screen freeze When Reading Tags From NFC

I am developing an Android App which is functionality of Reading NFC-Tag
ID from NFC Card. I am using following code for reading NFC Tag from Card.
@Override
protected void onNewIntent(Intent intent) {
ReadNFCTagFromIntent(intent);
}
private void ReadNFCTagFromIntent(Intent intent) {
byte[] inarray = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
int i, j, in;
String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F" };
String nfc_card_tag = "";
for (j = 0; j < inarray.length; ++j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
nfc_card_tag += hex[i];
i = in & 0x0f;
nfc_card_tag += hex[i];
}
ShowDialog(nfc_card_tag); // This function Will Display a Dialog With
Tag Value....
}
I have also configured Foreground Dispatch System in activity OnPause &
OnResume method of activity
@Override
protected void onResume() {
super.onResume();
try {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent,
new IntentFilter[] { filter }, this.techList);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
try {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
} catch (Exception e) {
}
}
Here is My Activity Declaration in Manifest File
<activity android:name="ActHome"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfctechlist" />
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
</intent-filter>
This the xml in resource file that i am using for "TECH_DISCOVERED"
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
This code is working fine, but the problem is when i try to attach
nfc-card to my device, my activity hangs/freeze for at least 10/15 seconds
and then Dialog is shown as per my code. If i try to attach 5 to 6 cards
continuously one by one then its showing me all dialog after 10-15 seconds
at the same time. I don't know why my Activity is Hanging while reading
Tag from NFC Card.
Thanks in Advance

No comments:

Post a Comment