Advertisement
Guest User

MainActivity.java

a guest
Oct 18th, 2014
46,862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. package com.example.puntobip;
  2.  
  3. import java.io.IOException;
  4. import java.util.Random;
  5.  
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.PendingIntent;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.content.IntentFilter.MalformedMimeTypeException;
  12. import android.nfc.NfcAdapter;
  13. import android.nfc.Tag;
  14. import android.nfc.tech.MifareClassic;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends Activity {
  22.     private final int DIALOG_SALDO = 1;
  23.     private final int DIALOG_CARGAR = 2;
  24.     private final int DIALOG_NUMERO = 3;
  25.     private final byte[] KEY_B_SECTOR_0 = new byte[]{(byte)0x1F,(byte)0xC2,(byte)0x35,(byte)0xAC,(byte)0x13,(byte)0x09};
  26.     private final byte[] KEY_B_SECTOR_8 = new byte[]{(byte)0x64,(byte)0xE3,(byte)0xC1,(byte)0x03,(byte)0x94,(byte)0xC2};
  27.     private final byte[] DATA_CARGA_B21 = new byte[]{(byte)0x10,(byte)0x27,(byte)0x00,(byte)0x00,(byte)0xef,(byte)0xd8,(byte)0xff,(byte)0xff,(byte)0x10,(byte)0x27,(byte)0x00,(byte)0x00,(byte)0x21,(byte)0xde,(byte)0x21,(byte)0xde};
  28.     private final byte[] DATA_CARGA_B22 = new byte[]{(byte)0x10,(byte)0x27,(byte)0x00,(byte)0x00,(byte)0xef,(byte)0xd8,(byte)0xff,(byte)0xff,(byte)0x10,(byte)0x27,(byte)0x00,(byte)0x00,(byte)0x22,(byte)0xdd,(byte)0x22,(byte)0xdd};
  29.    
  30.     private TapDialog tapDialog;
  31.    
  32.     private NfcAdapter mNfcAdapter;
  33.     private PendingIntent mPendingIntent;
  34.     private IntentFilter[] mFilters;
  35.     private String[][] mTechList;
  36.    
  37.    
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_main);
  42.         showTapDialog(getResources().getString(R.string.consultar_saldo), DIALOG_SALDO);
  43.         inicializarNFC();
  44.     }
  45.    
  46.     public void onCargarSaldo(View view) {
  47.         showTapDialog(getResources().getString(R.string.cargar_saldo), DIALOG_CARGAR);
  48.     }
  49.    
  50.     public void onCambiarNumero(View view) {
  51.         showTapDialog(getResources().getString(R.string.cambiar_numero), DIALOG_NUMERO);
  52.     }
  53.    
  54.     private void showTapDialog(String message, int tag) {
  55.         if(tapDialog == null) {
  56.             tapDialog = new TapDialog(this);
  57.             tapDialog.setCanceledOnTouchOutside(false);
  58.         }
  59.         tapDialog.setTitle(message);
  60.         tapDialog.setTag(tag);
  61.         tapDialog.show();
  62.     }
  63.    
  64.     private void inicializarNFC() {
  65.         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  66.         if (mNfcAdapter == null) {
  67.             Toast.makeText(this, "Este dispositivo no soporta NFC.", Toast.LENGTH_LONG).show();
  68.             finish();
  69.             return;
  70.         }
  71.        
  72.         if (!mNfcAdapter.isEnabled()) {
  73.             AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  74.             alertDialog.setTitle("NFC deshabilitado");
  75.             alertDialog.setMessage("Su NFC está deshabilitado.\nIngrese a las opciones de configuración y activelo.");
  76.             alertDialog.show();
  77.         }
  78.        
  79.         mPendingIntent = PendingIntent.getActivity(this, 0,
  80.                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  81.        
  82.         IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  83.         try {
  84.             ndef.addDataType("*/*");
  85.         } catch (MalformedMimeTypeException e) {
  86.             throw new RuntimeException("error", e);
  87.         }
  88.        
  89.         mFilters = new IntentFilter[] {ndef};
  90.         mTechList = new String[][] { new String[] { MifareClassic.class.getName() } };
  91.     }
  92.    
  93.     @Override
  94.     protected void onResume() {
  95.         super.onResume();
  96.        
  97.         mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechList);
  98.     }
  99.    
  100.     @Override
  101.     protected void onPause() {
  102.         super.onPause();
  103.        
  104.         mNfcAdapter.disableForegroundDispatch(this);
  105.     }
  106.    
  107.     @Override
  108.     protected void onNewIntent(Intent intent) {
  109.         String action = intent.getAction();
  110.        
  111.         if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
  112.             Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  113.             MifareClassic mfc = MifareClassic.get(tagFromIntent);
  114.            
  115.             try {
  116.                 mfc.connect();
  117.                
  118.                 if(tapDialog.isShowing()) {
  119.                     switch (tapDialog.getTag()) {
  120.                     case DIALOG_CARGAR:
  121.                         if(mfc.authenticateSectorWithKeyB(0x08, KEY_B_SECTOR_8)) {
  122.                             mfc.writeBlock(0x21, DATA_CARGA_B21);
  123.                             mfc.writeBlock(0x22, DATA_CARGA_B22);
  124.                         } else {
  125.                             Toast.makeText(this, "error autenticando sector 0x21", Toast.LENGTH_LONG).show();
  126.                         }
  127.                         break;
  128.            
  129.                     case DIALOG_NUMERO:
  130.                         if(mfc.authenticateSectorWithKeyB(0x00, KEY_B_SECTOR_0)) {
  131.                             byte[] data = mfc.readBlock(0x01);
  132.                             Random rand = new Random();
  133.                             data[4] = (byte) (rand.nextInt((0xff - 0x00) + 1) + 0x00);
  134.                             data[5] = (byte) (rand.nextInt((0xff - 0x00) + 1) + 0x00);
  135.                             data[6] = (byte) (rand.nextInt((0xff - 0x00) + 1) + 0x00);
  136.                             data[7] = (byte) (rand.nextInt((0xff - 0x00) + 1) + 0x00);
  137.                             mfc.writeBlock(0x01, data);
  138.                         } else {
  139.                             Toast.makeText(this, "error autenticando sector 0x00", Toast.LENGTH_LONG).show();
  140.                         }
  141.                         break;
  142.                     }
  143.                 }
  144.                
  145.                 //
  146.                 // datos de la tarjeta
  147.                 // ----------------------------------------------------------------
  148.                 //
  149.                 TextView tvNumeroUUID = (TextView)findViewById(R.id.tvNumeroUUID);
  150.                 TextView tvNumeroBIP = (TextView)findViewById(R.id.tvNumeroBIP);
  151.                 TextView tvSaldoBIP = (TextView)findViewById(R.id.tvSaldoBIP);
  152.                
  153.                 // uuid
  154.                 tvNumeroUUID.setText(leToNumericString(mfc.getTag().getId(), 4));
  155.                
  156.                 // numero bip
  157.                 if(mfc.authenticateSectorWithKeyB(0x00, KEY_B_SECTOR_0)) {
  158.                     byte[] data = mfc.readBlock(0x01);
  159.                     tvNumeroBIP.setText(leToNumericString(new byte[]{data[4],data[5],data[6],data[7]}, 4));
  160.                 } else {
  161.                     Toast.makeText(this, "error autenticando sector 0x00", Toast.LENGTH_LONG).show();
  162.                 }
  163.                
  164.                 // saldo bip
  165.                 if(mfc.authenticateSectorWithKeyB(0x08, KEY_B_SECTOR_8)) {
  166.                     byte[] data = mfc.readBlock(0x21);
  167.                     tvSaldoBIP.setText(formatMoneda(leToNumeric(data, 2)));
  168.                 } else {
  169.                     Toast.makeText(this, "error autenticando sector 0x21", Toast.LENGTH_LONG).show();
  170.                 }
  171.                
  172.             } catch (Exception e) {
  173.                 Log.e("error", e.getLocalizedMessage());
  174.                 Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
  175.             } finally {
  176.                 try {mfc.close();} catch (IOException e) {}
  177.             }
  178.         }
  179.        
  180.         tapDialog.dismiss();
  181.     }
  182.    
  183.     private long leToNumeric(byte[] buffer, int size) {
  184.         long value = 0;
  185.         for (int i=0; i<size; i++) { value += ((long) buffer[i] & 0xffL) << (8 * i); }
  186.         return value;
  187.     }
  188.    
  189.     private String leToNumericString(byte[] buffer, int size) {
  190.         return String.valueOf(leToNumeric(buffer, size));
  191.     }
  192.    
  193.     private String formatMoneda(long valor) {
  194.         return "$"+String.format("%,d", valor);
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement