protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Definizione UI element btreadchip = (Button) findViewById(R.id.btreadchip); btreset = (Button) findViewById(R.id.btreset); txsend = (EditText) findViewById(R.id.txsend); txresponse = (TextView) findViewById(R.id.txresponse); pbScan = (ProgressBar) findViewById(R.id.pbScan); //Set on click listner View.OnClickListener btManager = new View.OnClickListener() { public void onClick(View view) { switch(view.getId()) { case R.id.btreadchip: String apdu = txsend.getText().toString(); if(apdu.length() > (int) 8){ command = new byte[apdu.length()/2]; hexStrTOarray(apdu, command); pbScan.setVisibility(view.VISIBLE); lifecycle = 1; //means app is ready to read from the card } break; case R.id.btreset: lifecycle = 0; txsend.setText(""); txresponse.setText(""); pbScan.setVisibility(pbScan.INVISIBLE); break; } } }; btreset.setOnClickListener(btManager); btreadchip.setOnClickListener(btManager); mAdapter = NfcAdapter.getDefaultAdapter(this); // Create a generic PendingIntent that will be deliver to this activity. // The NFC stack // will fill in the intent with the details of the discovered tag before // delivering to // this activity. mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); try { ndef.addDataType("*/*"); } catch (MalformedMimeTypeException e) { throw new RuntimeException("fail", e); } mFilters = new IntentFilter[] { ndef, }; mTechLists = new String[][] { new String[] { NfcB.class.getName() }, new String[] { NfcA.class.getName() }}; Intent intent = getIntent(); String response = resolveIntent(intent); txresponse.setText(response); } //Rilevo la carta dal NFC String resolveIntent(Intent intent) { //byte[] command; byte[] feedback; String dati = ""; int res, MaxIndex, lastCmd; final int bqty = 192; //numero massiomo di byte letti per singola apdu (lettura DG2) //Verifico che siamo pronti per leggere la carta if(lifecycle != 1) return(""); String action = intent.getAction(); if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { // status_Data.setText("Discovered tag with intent: " + intent); //Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //MifareClassic mfc = MifareClassic.get(tagFromIntent); Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] tectList = tagFromIntent.getTechList(); byte[] tagid = tagFromIntent.getId(); int lenght = tagid.length; byte[] lenghtArray = {(byte) lenght}; String Stagid = MainActivity.getHex(tagid); //NfcB nfcB = NfcB.get(tagFromIntent); IsoDep iso = IsoDep.get(tagFromIntent); try { iso.connect(); //##### Invio apdu: Select EF 00 (DG1) dati = ""; //command = new byte[]{(byte) 0x00, (byte) 0xA4, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x3F, (byte) 0x00}; feedback = iso.transceive(command); dati = getHex(feedback).replace(" ", "");; // if(dati.length() < 4 || !dati.substring(dati.length() - 4, dati.length()).equals("9000")){ // showAlert("Status Word Error - Personal Data Selection"); // return(""); // } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lifecycle = 0; return(dati); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onResume() { super.onResume(); mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); resolveIntent(getIntent()); } @Override public void onNewIntent(Intent intent) { if (lifecycle == 1){ String response = resolveIntent(intent); //activate for card reading txresponse.setText(response); pbScan.setVisibility(pbScan.INVISIBLE); //resolveIntent(intent); //activate for card reading } // mText.setText("Discovered tag " + ++mCount + " with intent: " + // intent); } @Override public void onPause() { super.onPause(); mAdapter.disableForegroundDispatch(this); } }