Advertisement
Guest User

Untitled

a guest
Oct 16th, 2013
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package eu.meinemensa.light.activities;
  2.  
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import android.annotation.TargetApi;
  6. import android.app.Activity;
  7. import android.nfc.NfcAdapter;
  8. import android.nfc.Tag;
  9. import android.nfc.tech.IsoDep;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13.  
  14. public class NFCActivity extends Activity
  15. {
  16.     private IsoDep desfire;
  17.     private final byte[] NATIVE_SELECT_APP_COMMAND = new byte[]
  18.     {
  19.         (byte) 0x90, (byte) 0x5A, (byte) 0x00, (byte) 0x00, 3// SELECT
  20.         (byte) 0x5F, (byte) 0x84, (byte) 0x15, (byte) 0x00      // APPLICATION ID
  21.     };
  22.     private final byte[] NATIVE_SELECT_FILE_COMMAND = new byte[]
  23.     {
  24.         (byte) 0x90, (byte) 0xBD, (byte) 0x00, (byte) 0x00, 7// READ
  25.         (byte) 0x01,                                            // FILE ID
  26.         (byte) 0x00, (byte) 0x00, (byte) 0x00,                  // OFFSET
  27.         (byte) 0x00, (byte) 0x00, (byte) 0x00,                  // LENGTH
  28.         (byte) 0x00
  29.     };
  30.     private final String TAG = "MyApp";
  31.  
  32.     /**
  33.      *
  34.      */
  35.     @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
  36.     @Override
  37.     public void onCreate (Bundle savedInstanceState)
  38.     {
  39.         super.onCreate(savedInstanceState);
  40.  
  41.         String action = getIntent().getAction();
  42.         if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
  43.         {
  44.             Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
  45.             desfire = IsoDep.get(tag);
  46.  
  47.             try
  48.             {
  49.                 desfire.connect();
  50.  
  51.                 selectApplication();
  52.                 readFile();
  53.  
  54.                 desfire.close();
  55.             }
  56.             catch (IOException e)
  57.             {
  58.                 e.printStackTrace();
  59.             }
  60.         }
  61.     }
  62.  
  63.     @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
  64.     private void readFile ()
  65.     {
  66.         byte[] response;
  67.         try
  68.         {
  69.             response = desfire.transceive(NATIVE_SELECT_FILE_COMMAND);
  70.             DisplayInfo("Read DATA", response);
  71.         }
  72.         catch (IOException e)
  73.         {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.  
  78.     @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
  79.     private void selectApplication ()
  80.     {
  81.         byte[] response;
  82.         try
  83.         {
  84.             response = desfire.transceive(NATIVE_SELECT_APP_COMMAND);
  85.             DisplayInfo("Select APPLICATION", response);
  86.         }
  87.         catch (IOException e)
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.  
  93.     private void DisplayInfo (String string, byte[] transceive)
  94.     {
  95.         Log.i(TAG, string + ": " + bin2hex(transceive));
  96.     }
  97.  
  98.     private String bin2hex (byte[] data)
  99.     {
  100.         return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement