Advertisement
Guest User

bip

a guest
Oct 20th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. package com.example.puntobip;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.util.Random;
  8.  
  9.  
  10.  
  11. import android.app.Activity;
  12.  
  13. import android.app.AlertDialog;
  14.  
  15. import android.app.PendingIntent;
  16.  
  17. import android.content.Intent;
  18.  
  19. import android.content.IntentFilter;
  20.  
  21. importandroid.content.IntentFilter.MalformedMimeTypeException;
  22.  
  23. import android.nfc.NfcAdapter;
  24.  
  25. import android.nfc.Tag;
  26.  
  27. import android.nfc.tech.MifareClassic;
  28.  
  29. import android.os.Bundle;
  30.  
  31. import android.util.Log;
  32.  
  33. import android.view.View;
  34.  
  35. import android.widget.TextView;
  36.  
  37. import android.widget.Toast;
  38.  
  39.  
  40.  
  41. public class MainActivity extends Activity {
  42.  
  43.         private final int DIALOG_SALDO = 1;
  44.  
  45.         private final int DIALOG_CARGAR = 2;
  46.  
  47.         private final int DIALOG_NUMERO = 3;
  48.  
  49.         private final byte[] KEY_B_SECTOR_0= new byte[]{(byte)0x1F,(byte)0xC2,(byte)0x35,(byte)0xAC,(byte)0x13,(byte)0x09};
  50.  
  51.         private final byte[] KEY_B_SECTOR_8= new byte[]{(byte)0x64,(byte)0xE3,(byte)0xC1,(byte)0x03,(byte)0x94,(byte)0xC2};
  52.  
  53.         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};
  54.  
  55.         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};
  56.  
  57.        
  58.  
  59.         private TapDialog tapDialog;
  60.  
  61.        
  62.  
  63.         private NfcAdapter mNfcAdapter;
  64.  
  65.     private PendingIntent mPendingIntent;
  66.  
  67.     private IntentFilter[] mFilters;
  68.  
  69.     private String[][] mTechList;
  70.  
  71.        
  72.  
  73.        
  74.  
  75.         @Override
  76.  
  77.         protected void onCreate(Bundle savedInstanceState) {
  78.  
  79.                super.onCreate(savedInstanceState);
  80.  
  81.                 setContentView(R.layout.activity_main);
  82.  
  83.                 showTapDialog(getResources().getString(R.string.consultar_saldo), DIALOG_SALDO);
  84.  
  85.                 inicializarNFC();
  86.  
  87.         }
  88.  
  89.        
  90.  
  91.         public void onCargarSaldo(View view){
  92.  
  93.                 showTapDialog(getResources().getString(R.string.cargar_saldo), DIALOG_CARGAR);
  94.  
  95.         }
  96.  
  97.        
  98.  
  99.         public void onCambiarNumero(Viewview) {
  100.  
  101.                 showTapDialog(getResources().getString(R.string.cambiar_numero), DIALOG_NUMERO);
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         private void showTapDialog(Stringmessage, int tag) {
  108.  
  109.                 if(tapDialog == null) {
  110.  
  111.                         tapDialog = newTapDialog(this);
  112.  
  113.                         tapDialog.setCanceledOnTouchOutside(false);
  114.  
  115.                 }
  116.  
  117.                 tapDialog.setTitle(message);
  118.  
  119.                 tapDialog.setTag(tag);
  120.  
  121.                 tapDialog.show();
  122.  
  123.         }
  124.  
  125.        
  126.  
  127.         private void inicializarNFC() {
  128.  
  129.                 mNfcAdapter =NfcAdapter.getDefaultAdapter(this);
  130.  
  131.         if (mNfcAdapter == null) {
  132.  
  133.             Toast.makeText(this, "Este dispositivo no soporta NFC.", Toast.LENGTH_LONG).show();
  134.  
  135.             finish();
  136.  
  137.             return;
  138.  
  139.         }
  140.  
  141.        
  142.  
  143.         if (!mNfcAdapter.isEnabled()) {
  144.  
  145.                 AlertDialog alertDialog =new AlertDialog.Builder(this).create();
  146.  
  147.                 alertDialog.setTitle("NFC deshabilitado");
  148.  
  149.                 alertDialog.setMessage("Su NFC está deshabilitado.\nIngrese a las opciones de configuración y activelo.");
  150.  
  151.                 alertDialog.show();
  152.  
  153.         }
  154.  
  155.        
  156.  
  157.         mPendingIntent =PendingIntent.getActivity(this, 0,
  158.  
  159.                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
  160.  
  161.        
  162.  
  163.         IntentFilter ndef = newIntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  164.  
  165.         try {
  166.  
  167.             ndef.addDataType("*/*");
  168.  
  169.         } catch (MalformedMimeTypeException e) {
  170.  
  171.             throw newRuntimeException("error", e);
  172.  
  173.         }
  174.  
  175.        
  176.  
  177.         mFilters = new IntentFilter[]{ndef};
  178.  
  179.         mTechList = new String[][] { newString[] { MifareClassic.class.getName() }};
  180.  
  181.         }
  182.  
  183.        
  184.  
  185.         @Override
  186.  
  187.     protected void onResume() {
  188.  
  189.         super.onResume();
  190.  
  191.        
  192.  
  193.         mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechList);
  194.  
  195.     }
  196.  
  197.    
  198.  
  199.     @Override
  200.  
  201.     protected void onPause() {
  202.  
  203.         super.onPause();
  204.  
  205.        
  206.  
  207.         mNfcAdapter.disableForegroundDispatch(this);
  208.  
  209.     }
  210.  
  211.    
  212.  
  213.     @Override
  214.  
  215.     protected void onNewIntent(Intent intent) {
  216.  
  217.         String action = intent.getAction();
  218.  
  219.        
  220.  
  221.                 if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)){
  222.  
  223.                         Tag tagFromIntent =intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  224.  
  225.                         MifareClassic mfc =MifareClassic.get(tagFromIntent);
  226.  
  227.                        
  228.  
  229.                 try {
  230.  
  231.                         mfc.connect();
  232.  
  233.                        
  234.  
  235.                        if(tapDialog.isShowing()) {
  236.  
  237.                                 switch(tapDialog.getTag()) {
  238.  
  239.                                         caseDIALOG_CARGAR:
  240.  
  241.                                                 if(mfc.authenticateSectorWithKeyB(0x08, KEY_B_SECTOR_8)) {
  242.  
  243.                                                 mfc.writeBlock(0x21, DATA_CARGA_B21);
  244.  
  245.                                                 mfc.writeBlock(0x22, DATA_CARGA_B22);
  246.  
  247.                                         }else {
  248.  
  249.                                                 Toast.makeText(this, "error autenticando sector 0x21", Toast.LENGTH_LONG).show();
  250.  
  251.                                         }
  252.  
  253.                                                 break;
  254.  
  255.                        
  256.  
  257.                                         caseDIALOG_NUMERO:
  258.  
  259.                                                 if(mfc.authenticateSectorWithKeyB(0x00, KEY_B_SECTOR_0)) {
  260.  
  261.                                                 byte[] data = mfc.readBlock(0x01);
  262.  
  263.                                                 Random rand = new Random();
  264.  
  265.                                                 data[4] = (byte) (rand.nextInt((0xff -0x00) + 1) + 0x00);
  266.  
  267.                                                 data[5] = (byte) (rand.nextInt((0xff -0x00) + 1) + 0x00);
  268.  
  269.                                                 data[6] = (byte) (rand.nextInt((0xff -0x00) + 1) + 0x00);
  270.  
  271.                                                 data[7] = (byte) (rand.nextInt((0xff -0x00) + 1) + 0x00);
  272.  
  273.                                                 mfc.writeBlock(0x01, data);
  274.  
  275.                                         }else {
  276.  
  277.                                                 Toast.makeText(this, "error autenticando sector 0x00", Toast.LENGTH_LONG).show();
  278.  
  279.                                         }
  280.  
  281.                                                 break;
  282.  
  283.                                         }
  284.  
  285.                         }
  286.  
  287.                        
  288.  
  289.                         //
  290.  
  291.                         // datos de la tarjeta
  292.  
  293.                         // ----------------------------------------------------------------
  294.  
  295.                         //
  296.  
  297.                         TextView tvNumeroUUID =(TextView)findViewById(R.id.tvNumeroUUID);
  298.  
  299.                         TextView tvNumeroBIP= (TextView)findViewById(R.id.tvNumeroBIP);
  300.  
  301.                         TextView tvSaldoBIP= (TextView)findViewById(R.id.tvSaldoBIP);
  302.  
  303.                        
  304.  
  305.                         // uuid
  306.  
  307.                         tvNumeroUUID.setText(leToNumericString(mfc.getTag().getId(),4));
  308.  
  309.                        
  310.  
  311.                         // numero bip
  312.  
  313.                        if(mfc.authenticateSectorWithKeyB(0x00, KEY_B_SECTOR_0)) {
  314.  
  315.                                 byte[] data= mfc.readBlock(0x01);
  316.  
  317.                                 tvNumeroBIP.setText(leToNumericString(newbyte[]{data[4],data[5],data[6],data[7]},4));
  318.  
  319.                         } else {
  320.  
  321.                                 Toast.makeText(this, "error autenticando sector 0x00", Toast.LENGTH_LONG).show();
  322.  
  323.                         }
  324.  
  325.                        
  326.  
  327.                         // saldo bip
  328.  
  329.                        if(mfc.authenticateSectorWithKeyB(0x08, KEY_B_SECTOR_8)) {
  330.  
  331.                                 byte[] data= mfc.readBlock(0x21);
  332.  
  333.                                 tvSaldoBIP.setText(formatMoneda(leToNumeric(data,2)));
  334.  
  335.                         } else {
  336.  
  337.                                 Toast.makeText(this, "error autenticando sector 0x21", Toast.LENGTH_LONG).show();
  338.  
  339.                         }
  340.  
  341.                        
  342.  
  343.                 } catch (Exception e) {
  344.  
  345.                                 Log.e("error", e.getLocalizedMessage());
  346.  
  347.                                 Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
  348.  
  349.                 } finally {
  350.  
  351.                         try {mfc.close();}catch (IOException e) {}
  352.  
  353.                 }
  354.  
  355.                 }
  356.  
  357.                
  358.  
  359.                 tapDialog.dismiss();
  360.  
  361.     }
  362.  
  363.    
  364.  
  365.     private long leToNumeric(byte[] buffer,int size) {
  366.  
  367.         long value = 0;
  368.  
  369.         for (int i=0; i<size; i++) { value+= ((long) buffer[i] & 0xffL) << (8 * i); }
  370.  
  371.         return value;
  372.  
  373.     }
  374.  
  375.    
  376.  
  377.     private String leToNumericString(byte[]buffer, int size) {
  378.  
  379.         returnString.valueOf(leToNumeric(buffer, size));
  380.  
  381.     }
  382.  
  383.    
  384.  
  385.     private String formatMoneda(long valor){
  386.  
  387.         return "$"+String.format("%,d", valor);
  388.  
  389.     }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement