How to solve "android.nfc.TagLostException: tag was lost".? (Select PICC application)

I am facing a problem with Mifare DESFire card.

I am using the method transceive()

to send apdu to select PICC application. Application flow:

1) Technology discovered

2) Create a MifareDESFire object with

intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

      

3) Called connect();

4) Send the app app command to select the PICC using transceive ();

5) When sending a "android.nfc.TagLostException: Tag was lost."

APDU. I point the line aligned above the error to '****'

Here's my event onNewIntent

:

    public void onNewIntent(Intent intent) 
        {

            String action = intent.getAction();

            if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
                    || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
                    || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

                Toast.makeText(this, "discovered", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(this, "Falied", Toast.LENGTH_SHORT).show();
            }



     byte cmdSelect_PICCapp=(byte) 0x5A ; //select my app
        byte[] PICCAPPID = new byte[]{(byte)0x00 ,(byte)0x00 , (byte)0x00};
   byte[] tagResponse_cmdSelect_PICC_app = new byte[]{(byte)0x12 ,(byte)0x12};          



Tag desfire = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);


            isodep = IsoDep.get(desfire);

            try 
            {
                isodep.connect();
            } catch (IOException e) 
            {
                e.printStackTrace();
            }




**** tagResponse_cmdSelect_PICC_app = isodep.transceive(Utils.wrapMessage(cmdSelect_PICCapp, PICCAPPID));
                Log.d("picc app selected", "111 "+Utils.bytesToHex(tagResponse_cmdSelect_PICC_app));




            try 
            {
                isodep.close();

            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
   }

      

And the Utils class is here:

public class Utils {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static byte[] wrapMessage (byte command, byte[] parameters) throws Exception {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        stream.write((byte) 0x90);
        stream.write(command);
        stream.write((byte) 0x00);
        stream.write((byte) 0x00);
        if (parameters != null) {
            stream.write((byte) parameters.length);
            stream.write(parameters);
        }
        stream.write((byte) 0x00);

        byte[] b = stream.toByteArray();
        return b;
    }
    //**************************************
    public static String bytesToHex(byte[] bytes) 
    {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) 
        {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}//end class Utils

      

Appreciate that anyone can help me.

Thank.

+3


source to share





All Articles