use enableReaderMode if possible
This commit is contained in:
@@ -26,6 +26,8 @@ import android.content.ClipboardManager;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
|
import android.nfc.Tag;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@@ -49,7 +51,7 @@ import com.vsmartcard.remotesmartcardreader.app.screaders.DummyReader;
|
|||||||
import com.vsmartcard.remotesmartcardreader.app.screaders.NFCReader;
|
import com.vsmartcard.remotesmartcardreader.app.screaders.NFCReader;
|
||||||
import com.vsmartcard.remotesmartcardreader.app.screaders.SCReader;
|
import com.vsmartcard.remotesmartcardreader.app.screaders.SCReader;
|
||||||
|
|
||||||
public class MainActivity extends Activity {
|
public class MainActivity extends Activity implements NfcAdapter.ReaderCallback {
|
||||||
|
|
||||||
class VPCDHandler extends Handler {
|
class VPCDHandler extends Handler {
|
||||||
VPCDHandler(Looper looper) {
|
VPCDHandler(Looper looper) {
|
||||||
@@ -181,6 +183,36 @@ public class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enableReaderMode() {
|
||||||
|
//mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 2000);
|
||||||
|
NfcAdapter.getDefaultAdapter(this).enableReaderMode(this, this,
|
||||||
|
NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
|
||||||
|
bundle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTagDiscovered(Tag tag) {
|
||||||
|
NFCReader nfcReader = NFCReader.get(tag);
|
||||||
|
if (nfcReader != null) {
|
||||||
|
/* avoid updating UI components since this may end up on a non ui thread */
|
||||||
|
vpcdDisconnect();
|
||||||
|
vpcdConnect(nfcReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableReaderMode() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
|
||||||
|
if (nfc != null) {
|
||||||
|
nfc.disableReaderMode(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String saved_status_key = "textViewVPCDStatus";
|
private static String saved_status_key = "textViewVPCDStatus";
|
||||||
@Override
|
@Override
|
||||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||||
@@ -199,6 +231,7 @@ public class MainActivity extends Activity {
|
|||||||
|
|
||||||
testing = false;
|
testing = false;
|
||||||
vpcdDisconnect();
|
vpcdDisconnect();
|
||||||
|
disableReaderMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buttonOnClickDisConnect(View view) {
|
public void buttonOnClickDisConnect(View view) {
|
||||||
@@ -286,7 +319,7 @@ public class MainActivity extends Activity {
|
|||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
// Check to see that the Activity started due to a discovered tag
|
// Check to see that the Activity started due to a discovered tag
|
||||||
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
|
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
|
||||||
NFCReader nfcReader = NFCReader.processIntent(intent);
|
NFCReader nfcReader = NFCReader.get(intent);
|
||||||
if (nfcReader != null) {
|
if (nfcReader != null) {
|
||||||
textViewVPCDStatus.append(getResources().getString(R.string.status_tag_discovered)+"\n");
|
textViewVPCDStatus.append(getResources().getString(R.string.status_tag_discovered)+"\n");
|
||||||
forceConnect(nfcReader);
|
forceConnect(nfcReader);
|
||||||
@@ -316,6 +349,7 @@ public class MainActivity extends Activity {
|
|||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
enableReaderMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ public class NFCReader implements SCReader {
|
|||||||
sc.connect();
|
sc.connect();
|
||||||
card.setTimeout(TIMEOUT);
|
card.setTimeout(TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eject() throws IOException {
|
public void eject() throws IOException {
|
||||||
card.close();
|
card.close();
|
||||||
@@ -98,21 +99,27 @@ public class NFCReader implements SCReader {
|
|||||||
return card.transceive(apdu);
|
return card.transceive(apdu);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NFCReader processIntent(Intent intent) {
|
public static NFCReader get(Intent intent) {
|
||||||
NFCReader nfcReader = null;
|
NFCReader nfcReader = null;
|
||||||
|
|
||||||
if (intent.getExtras() != null) {
|
if (intent.getExtras() != null) {
|
||||||
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
||||||
if (tag != null) {
|
if (tag != null) {
|
||||||
try {
|
nfcReader = NFCReader.get(tag);
|
||||||
nfcReader = new NFCReader(IsoDep.get(tag));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
intent.removeExtra(NfcAdapter.EXTRA_TAG);
|
intent.removeExtra(NfcAdapter.EXTRA_TAG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nfcReader;
|
return nfcReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NFCReader get(Tag tag) {
|
||||||
|
NFCReader nfcReader = null;
|
||||||
|
try {
|
||||||
|
nfcReader = new NFCReader(IsoDep.get(tag));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return nfcReader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user