package eu.meinemensa.light.activities;
import java.io.IOException;
import java.math.BigInteger;
import android.annotation.TargetApi;
import android.app.Activity;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
public class NFCActivity extends Activity
{
private IsoDep desfire;
private final byte[] NATIVE_SELECT_APP_COMMAND = new byte[]
{
(byte) 0x90, (byte) 0x5A, (byte) 0x00, (byte) 0x00, 3, // SELECT
(byte) 0x5F, (byte) 0x84, (byte) 0x15, (byte) 0x00 // APPLICATION ID
};
private final byte[] NATIVE_SELECT_FILE_COMMAND = new byte[]
{
(byte) 0x90, (byte) 0xBD, (byte) 0x00, (byte) 0x00, 7, // READ
(byte) 0x01, // FILE ID
(byte) 0x00, (byte) 0x00, (byte) 0x00, // OFFSET
(byte) 0x00, (byte) 0x00, (byte) 0x00, // LENGTH
(byte) 0x00
};
private final String TAG = "MyApp";
/**
*
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String action = getIntent().getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
{
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
desfire = IsoDep.get(tag);
try
{
desfire.connect();
selectApplication();
readFile();
desfire.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
private void readFile ()
{
byte[] response;
try
{
response = desfire.transceive(NATIVE_SELECT_FILE_COMMAND);
DisplayInfo("Read DATA", response);
}
catch (IOException e)
{
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
private void selectApplication ()
{
byte[] response;
try
{
response = desfire.transceive(NATIVE_SELECT_APP_COMMAND);
DisplayInfo("Select APPLICATION", response);
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void DisplayInfo (String string, byte[] transceive)
{
Log.i(TAG, string + ": " + bin2hex(transceive));
}
private String bin2hex (byte[] data)
{
return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
}
}