I'm encoutering an issue using IsoDep and NfcA classes.
I need to scan a tag and transceive informations. To do so, I use the
following code :
IsoDep isoDep = IsoDep.get(tag);
try {
isoDep.connect();
isoDep.setTimeout(5000);
Log.d("ISODEP_TESTS", isoDep.isConnected() ? "connection OK"
: "connection NOK");
isoDep.close();
Log.d("ISODEP_TESTS",
"connection after close : "
+ (isoDep.isConnected() ? "connection still open"
: "connection closed"));
} catch (IOException e) {
Log.e("ISEODEP_ERROR",
"error on isodep connection : " + e.getMessage());
Log.e("ISEODEP_ERROR", "NfcAdapter state : " + mAdapter.isEnabled());
try {
isoDep.close();
Log.e("ISEODEP_ERROR",
"no error on isodep close when IOException happened");
} catch (IOException e1) {
Log.e("ISEODEP_ERROR",
"error on isodep close when IOException : "
+ e1.getMessage());
}
throw e;
}
The given tag is not null, and everything goes well in most cases.
Nonetheless when I take the nfc card away from the phone before the
transceive ended, I get an IOException.
It happens when I try to connect to the isodep technology. Moreover, once I
get this IOException, any other attempt to read the card leads to this same
IOException.
To get back the normal behavior, I have to finish the application, and then
I can scan a new tag.
Here are the logs I obtain :
10-18 11:24:20.926: D/MainActivity(28797): onResume called <==
First scan. Everything is OK
10-18 11:24:20.958: D/ISODEP_TESTS(28797): connection OK
10-18 11:24:20.981: D/ISODEP_TESTS(28797): connection after close :
connection closed
10-18 11:24:22.380: D/MainActivity(28797): onResume called <==
Second scan. Everything is OK
10-18 11:24:22.403: D/ISODEP_TESTS(28797): connection OK
10-18 11:24:22.426: D/ISODEP_TESTS(28797): connection after close :
connection closed
10-18 11:24:25.051: D/MainActivity(28797): onResume called <==
Thrid scan. I take away the card while the scan occurs.
10-18 11:24:25.059: E/ISEODEP_ERROR(28797): error on isodep connection :
null
10-18 11:24:25.059: E/ISEODEP_ERROR(28797): NfcAdapter state : true
10-18 11:24:25.059: E/ISEODEP_ERROR(28797): no error on isodep close when
IOException happened
10-18 11:24:29.137: D/MainActivity(28797): onResume called <==
Fourth scan. I do not take away the card. IOException occurs while
isodep.connect() is called
10-18 11:24:29.145: E/ISEODEP_ERROR(28797): error on isodep connection :
null
10-18 11:24:29.145: E/ISEODEP_ERROR(28797): NfcAdapter state : true
10-18 11:24:29.145: E/ISEODEP_ERROR(28797): no error on isodep close when
IOException happened
10-18 11:24:35.434: D/MainActivity(28797): onResume called <==
Fifth scan. Same as 4
10-18 11:24:35.434: E/ISEODEP_ERROR(28797): error on isodep connection :
null
10-18 11:24:35.434: E/ISEODEP_ERROR(28797): NfcAdapter state : true
10-18 11:24:35.442: E/ISEODEP_ERROR(28797): no error on isodep close when
IOException happened
Since the nfc cards I use are nfcA compatible too, I have tried this
technology too, and I get the same results.
Is there a way to get back the normal behavior without having to restart
the application? What am I doing wrong?
If it can helps, Here are my androidManifest.xml and the Activity main
methods :
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.NoActionBar" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity-alias
android:name="MainActivityLauncher"
android:label="@string/app_name"
android:targetActivity="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(this.getClass().getSimpleName(), "onCreate called");
mAdapter = NfcAdapter.getDefaultAdapter(this);
setContentView(R.layout.activity_main);
HomeFragment homeFragment = new HomeFragment();
getFragmentManager().beginTransaction()
.replace(R.id.frame_content, homeFragment).commit();
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
ndef.addDataType("*/*"); /*
* Handles all MIME based dispatches. You
* should specify only the ones that you
* need.
*/
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] { ndef, };
mTechLists = new String[][] { new String[] { IsoDep.class.getName() } };
}
@Override
protected void onResume() {
Log.d(this.getClass().getSimpleName(), "onResume called "
+ getIntent().getAction());
super.onResume();
mAdapter.enableForegroundDispatch(this, pendingIntent,
intentFiltersArray, mTechLists);
if (getIntent().getAction() == NfcAdapter.ACTION_TECH_DISCOVERED
|| getIntent().getAction() == NfcAdapter.ACTION_TAG_DISCOVERED) {
newNfcComputation();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
newNfcComputation();
}
@Override
protected void onPause() {
super.onPause();
getIntent().setAction(RECOVER_FROM_IOEXCEPTION); // Mega trick to
// prevent tag
// retrieval when
// manually resuming
// the app
mAdapter.disableForegroundDispatch(this);
}
private void newNfcComputation() {
Log.i("INTENT_ACTION", getIntent().getAction());
if (mAdapter != null) {
final Tag tag = getIntent()
.getParcelableExtra(NfcAdapter.EXTRA_TAG);
try {
if (mAuthentication != null) {
new Thread(new Runnable() {
@Override
public void run() {
try {
errorIsolationIso(tag);
} catch (IOException e) {
launchIOExceptionDialog(true);
}
}
}).start();
} else {
Log.e(this.getClass().getSimpleName(),
"Cannot perform NFC tag authentication because authentifier is null");
}
} catch (IllegalStateException e) {
Log.e(this.getClass().getSimpleName(), "IllegalStateException");
launchIOExceptionDialog(true);
} catch (NullPointerException e) {
Log.e(this.getClass().getSimpleName(), "NullPointerException");
launchIOExceptionDialog(true);
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en