Skip to content

Commit

Permalink
updated project to support enableReaderMode
Browse files Browse the repository at this point in the history
  • Loading branch information
grundid committed Nov 25, 2013
1 parent b6e9e5c commit 4aaffe9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="18"
android:minSdkVersion="19"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.NFC" />
Expand Down
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<string name="app_name">HceDemo</string>
<string name="action_settings">Settings</string>
<string name="activity_into">ForegroundDispatch for IsoDep active</string>
<string name="activity_into">Reader mode active</string>
<string name="aiddescription">aiddescription</string>
<string name="servicedesc">servicedesc</string>

Expand Down
7 changes: 1 addition & 6 deletions src/de/grundid/hcedemo/IsoDepAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
public class IsoDepAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private List<String> messages;
private List<String> messages = new ArrayList<String>(100);
private int messageCounter;

public IsoDepAdapter(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}

public void resetMessages() {
messages = new ArrayList<String>(100);
notifyDataSetInvalidated();
}

public void addMessage(String message) {
messageCounter++;
messages.add("Message [" + messageCounter + "]: " + message);
Expand Down
18 changes: 17 additions & 1 deletion src/de/grundid/hcedemo/IsoDepTranceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import java.io.IOException;

import android.nfc.tech.IsoDep;
import android.util.Log;

public class IsoDepTranceiver implements Runnable {

private static final byte[] CLA_INS_P1_P2 = { 0x00, (byte)0xA4, 0x04, 0x00 };
private static final byte[] AID_ANDROID = { (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

public interface OnMessageReceived {

void onMessage(byte[] message);
Expand All @@ -21,14 +25,26 @@ public IsoDepTranceiver(IsoDep isoDep, OnMessageReceived onMessageReceived) {
this.onMessageReceived = onMessageReceived;
}

private byte[] createSelectAidApdu(byte[] aid) {
byte[] result = new byte[6 + aid.length];
System.arraycopy(CLA_INS_P1_P2, 0, result, 0, CLA_INS_P1_P2.length);
result[4] = (byte)aid.length;
System.arraycopy(aid, 0, result, 5, aid.length);
result[result.length - 1] = 0;
return result;
}

@Override
public void run() {
int messageCounter = 0;
try {
isoDep.connect();
byte[] response = isoDep.transceive(createSelectAidApdu(AID_ANDROID));
Log.i("HCE", "Received: " + new String(response) + " Length " + response.length);
while (isoDep.isConnected() && !Thread.interrupted()) {
String message = "Message from IsoDep " + messageCounter++;
byte[] response = isoDep.transceive(message.getBytes());
response = isoDep.transceive(message.getBytes());
Log.i("HCE", "Received: " + new String(response) + " Length " + response.length);
onMessageReceived.onMessage(response);
}
isoDep.close();
Expand Down
42 changes: 11 additions & 31 deletions src/de/grundid/hcedemo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,46 @@
package de.grundid.hcedemo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import de.grundid.hcedemo.IsoDepTranceiver.OnMessageReceived;

public class MainActivity extends Activity implements OnMessageReceived {
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {

private PendingIntent pendingIntent;
private NfcAdapter nfcAdapter;
private IntentFilter[] intentFiltersArrays;
private String[][] techListsArrays;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
private TextView status;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
status = (TextView)findViewById(R.id.intro);
isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
listView.setAdapter(isoDepAdapter);
prepareForegroundDispatch();
}

private void prepareForegroundDispatch() {
pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
intentFiltersArrays = new IntentFilter[] { ndef };
techListsArrays = new String[][] { new String[] { IsoDep.class.getName() } };
}

@Override
public void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
status.setText("IsoDep ForegroundDispatch disabled");
public void onResume() {
super.onResume();
nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
null);
}

@Override
public void onResume() {
super.onResume();
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArrays, techListsArrays);
status.setText("IsoDep ForegroundDispatch enabled");
public void onPause() {
super.onPause();
nfcAdapter.disableReaderMode(this);
}

@Override
public void onNewIntent(Intent intent) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
IsoDep isoDep = IsoDep.get(tagFromIntent);
isoDepAdapter.resetMessages();
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
IsoDepTranceiver tranceiver = new IsoDepTranceiver(isoDep, this);
Thread thread = new Thread(tranceiver);
thread.start();
Expand Down

0 comments on commit 4aaffe9

Please sign in to comment.