always connect() when creating NFCReader

removes SCReader.present() as it was never used

fixes #21
This commit is contained in:
Frank Morgner
2014-09-29 07:29:28 +02:00
parent 7dd1dfee91
commit 26e5411158
3 changed files with 18 additions and 28 deletions

View File

@@ -25,11 +25,6 @@ public class DummyReader implements SCReader {
public void eject() { public void eject() {
} }
@Override
public boolean present() {
return true;
}
@Override @Override
public void powerOn() { public void powerOn() {
} }

View File

@@ -33,46 +33,38 @@ public class NFCReader implements SCReader {
private IsoDep card; private IsoDep card;
public NFCReader(IsoDep sc) { public NFCReader(IsoDep sc) throws IOException {
this.card = sc; this.card = sc;
sc.connect();
card.setTimeout(TIMEOUT);
} }
@Override @Override
public void eject() throws IOException { public void eject() throws IOException {
card.close(); card.close();
} }
@Override
public boolean present() {
return card.isConnected();
}
private static int TIMEOUT = 10000; private static int TIMEOUT = 10000;
@Override @Override
public void powerOn() throws IOException { public void powerOn() {
if (!card.isConnected()) { /* should already be connected... */
card.connect();
card.setTimeout(TIMEOUT);
}
} }
private static byte[] SELECT_MF = {(byte) 0x00, (byte) 0xa4, (byte) 0x00, (byte) 0x0C}; private static byte[] SELECT_MF = {(byte) 0x00, (byte) 0xa4, (byte) 0x00, (byte) 0x0C};
private void selectMFifConnected() throws IOException { private void selectMF() throws IOException {
if (card != null && card.isConnected()) { byte[] response = card.transceive(SELECT_MF);
byte[] response = card.transceive(SELECT_MF); if (response.length == 2 && response[0] == 0x90 && response[1] == 0x00) {
if (response.length == 2 && response[0] == 0x90 && response[1] == 0x00) { Log.d(this.getClass().getName(), "Resetting the card by selecting the MF results in " + Hex.getHexString(response));
Log.d(this.getClass().getName(), "Resetting the card by selecting the MF results in " + Hex.getHexString(response));
}
} }
} }
@Override @Override
public void powerOff() throws IOException { public void powerOff() throws IOException {
selectMFifConnected(); selectMF();
} }
@Override @Override
public void reset() throws IOException { public void reset() throws IOException {
selectMFifConnected(); selectMF();
} }
/* calculation based on https://code.google.com/p/ifdnfc/source/browse/src/atr.c */ /* calculation based on https://code.google.com/p/ifdnfc/source/browse/src/atr.c */
@@ -110,9 +102,13 @@ public class NFCReader implements SCReader {
NFCReader nfcReader = null; NFCReader nfcReader = null;
if (intent.getExtras() != null) { if (intent.getExtras() != null) {
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) { if (tag != null) {
nfcReader = new NFCReader(IsoDep.get(tag)); try {
nfcReader = new NFCReader(IsoDep.get(tag));
} catch (IOException e) {
e.printStackTrace();
}
intent.removeExtra(NfcAdapter.EXTRA_TAG); intent.removeExtra(NfcAdapter.EXTRA_TAG);
} }
} }

View File

@@ -25,7 +25,6 @@ public interface SCReader {
public void eject() throws IOException; public void eject() throws IOException;
public boolean present();
public void powerOn() throws IOException; public void powerOn() throws IOException;
public void powerOff() throws IOException; public void powerOff() throws IOException;
public void reset() throws IOException; public void reset() throws IOException;
@@ -44,4 +43,4 @@ public interface SCReader {
* @return response data or null in case of an error * @return response data or null in case of an error
*/ */
public byte[] transmit(byte[] apdu) throws IOException; public byte[] transmit(byte[] apdu) throws IOException;
} }