Example usage for android.nfc.tech IsoDep transceive

List of usage examples for android.nfc.tech IsoDep transceive

Introduction

In this page you can find the example usage for android.nfc.tech IsoDep transceive.

Prototype

public byte[] transceive(byte[] data) throws IOException 

Source Link

Document

Send raw ISO-DEP data to the tag and receive the response.

Usage

From source file:de.lazyheroproductions.campuscardreader.CardReaderIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (BuildConfig.DEBUG) {
        Log.i(this.getClass().getName(), "CardReaderService started");
    }//from  w  ww  .j  ava 2  s  . c  o  m

    // get an instance of the nfc tag to communicate
    IsoDep isodep = IsoDep.get((Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    try {
        // connect to the nfc tag
        isodep.connect();
        // select application which contains the credit and last transaction
        resultOk = isodep.transceive(selectAid);
        if (resultOk[0] == 0) {
            // get the credit
            creditBytes = isodep.transceive(creditPayload);
            // get the last transaction
            lastTransactionBytes = isodep.transceive(transactionPayload);
        } else {
            if (BuildConfig.DEBUG) {
                Log.w(this.getClass().getName(), "Wrong result: " + arrayToString(resultOk));
            }
        }
        isodep.close();
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            Log.e(this.getClass().getName(), e.getMessage());
        }
        unknownErrorBool = true;
    } catch (NullPointerException e) {
        if (BuildConfig.DEBUG) {
            Log.e(this.getClass().getName(), e.getMessage());
        }
        unknownCampusCardErrorBool = true;
    }

    // send the gathered data back to the activity
    sendBroadcast();
}

From source file:com.example.marcieltorres.nfcproject_serverapp.cardreader.LoyaltyCardReader.java

/**
 * Callback when a new tag is discovered by the system.
 *
 * <p>Communication with the card should take place here.
 *
 * @param tag Discovered tag/*from   w w w.  j  a v a2s.c o m*/
 */
@Override
public void onTagDiscovered(Tag tag) {
    //Log.i(TAG, "New tag discovered");
    // Android's Host-based Card Emulation (HCE) feature implements the ISO-DEP (ISO 14443-4)
    // protocol.
    //
    // In order to communicate with a device using HCE, the discovered tag should be processed
    // using the IsoDep class.
    IsoDep isoDep = IsoDep.get(tag);
    if (isoDep != null) {
        try {
            // Connect to the remote NFC device
            isoDep.connect();
            // Build SELECT AID command for our loyalty card service.
            // This command tells the remote device which service we wish to communicate with.
            //Log.i(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);
            byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);
            // Send command to remote device
            //Log.i(TAG, "Sending: " + ByteArrayToHexString(command));
            byte[] result = isoDep.transceive(command);
            // If AID is successfully selected, 0x9000 is returned as the status word (last 2
            // bytes of the result) by convention. Everything before the status word is
            // optional payload, which is used here to hold the account number.
            int resultLength = result.length;
            byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] };
            byte[] payload = Arrays.copyOf(result, resultLength - 2);
            if (Arrays.equals(SELECT_OK_SW, statusWord)) {
                // The remote NFC device will immediately respond with its stored account number
                String accountNumber = new String(payload, "UTF-8");
                //Log.i(TAG, "Received: " + accountNumber);
                // Inform CardReaderFragment of received account number
                mAccountCallback.get().onAccountReceived(accountNumber);
                mAccountCallback.get().onAccountReceived(accountNumber);

            }
        } catch (IOException e) {
            //Log.e(TAG, "Error communicating with card: " + e.toString());
        }
    }
}

From source file:org.esupportail.nfctagdroid.authentication.DesfireAuthProvider.java

public String desfireAuth(Tag tag) throws ExecutionException, InterruptedException {
    String response = "ERROR";

    IsoDep isoDep = null;
    String[] techList = tag.getTechList();
    for (String tech : techList) {
        if (tech.equals(IsoDep.class.getName())) {
            isoDep = IsoDep.get(tag);/* w  w  w  . j a  v a2s. com*/
            log.info("Detected Desfire tag with id : " + HexaUtils.swapPairs(tag.getId()));
        }
    }
    if (isoDep == null) {
        throw new NfcTagDroidException("Did not detect a Desfire tag ");
    }

    try {
        String[] command = new String[2];
        String result = "";
        command[1] = "1";
        isoDep.connect();

        while (!command[1].equals("OK") && !command[1].equals("ERROR")) {

            DesfireHttpRequestAsync desfireHttpRequestAsync = new DesfireHttpRequestAsync();
            response = desfireHttpRequestAsync.execute(new String[] { command[1] + "/?result=" + result })
                    .get(time, TimeUnit.MILLISECONDS);
            try {
                JSONArray jsonArr = new JSONArray(response);
                command[0] = jsonArr.getString(0);
                command[1] = jsonArr.getString(1);

                if (!command[1].equals("OK") && !command[1].equals("ERROR")) {
                    byte[] byteResult = isoDep.transceive(HexaUtils.hexStringToByteArray(command[0]));
                    result = HexaUtils.byteArrayToHexString(byteResult);
                }
                log.debug("command step: " + command[1]);
                log.debug("command to send: " + command[0]);
                log.debug("result : " + result);
            } catch (Exception e) {
                throw new NfcTagDroidException(e);
            }
        }
        response = command[0];

    } catch (TimeoutException e) {
        log.warn("Time out");
        throw new NfcTagDroidException("Time out Desfire", e);
    } catch (TagLostException e) {
        throw new NfcTagDroidPleaseRetryTagException("TagLostException - authentication aborted", e);
    } catch (NfcTagDroidException e) {
        throw new NfcTagDroidInvalidTagException("nfctagdroidInvalidTagException - tag not valid", e);
    } catch (IOException e) {
        throw new NfcTagDroidInvalidTagException("IOException - authentication aborted", e);
    } catch (ExecutionException e) {
        throw new NfcTagDroidPleaseRetryTagException("ExecutionException - authentication aborted", e);
    } catch (InterruptedException e) {
        throw new NfcTagDroidPleaseRetryTagException("InterruptedException - authentication aborted", e);
    } finally {
        try {
            isoDep.close();
        } catch (IOException e) {
            throw new NfcTagDroidException(e);
        }
    }

    return response;
}

From source file:com.example.multi_ndef.Frag_Write.java

public boolean varifyTag() {
    boolean test = false;

    byte[] NdefSelectAppliFrame = new byte[] { (byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x07,
            (byte) 0xD2, (byte) 0x76, (byte) 0x00, (byte) 0x00, (byte) 0x85, (byte) 0x01, (byte) 0x01 };

    byte[] CCSelect = new byte[] { (byte) 0x00, (byte) 0xA4, (byte) 0x00, (byte) 0x0C, (byte) 0x02, (byte) 0xE1,
            (byte) 0x03 };

    byte[] CCReadLength = new byte[] { (byte) 0x00, (byte) 0xB0, (byte) 0x00, (byte) 0x00, (byte) 0x0F };

    byte[] ndefSelectcmd = new byte[] { (byte) 0x00, (byte) 0xA4, (byte) 0x00, (byte) 0x0C, (byte) 0x02,
            (byte) 0x00, (byte) 0x01 // Ndef File ID to select
    };//from   www  .j  av  a 2s .c  o  m

    //          int a = 491;

    byte[] ndefreadlengthcmd = new byte[] { (byte) 0x00, (byte) 0xB0, (byte) 0x00, (byte) 0x00, (byte) 0x6E };

    byte[] readBinary = new byte[] { (byte) 0x00, (byte) 0xB0, (byte) 0x00, (byte) 0x00, (byte) 0x0F };

    byte[] SYSSelect = new byte[] { (byte) 0x00, (byte) 0xA4, (byte) 0x00, (byte) 0x0C, (byte) 0x02,
            (byte) 0xE1, (byte) 0x01 };

    byte[] response1 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response2 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response3 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response4 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response5 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response6 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response7 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
    byte[] response8 = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };

    try {

        IsoDep isoDepCurrentTag = (IsoDep) IsoDep.get(ma.getCurrentTag());

        NfcA nfcaTag = (NfcA) NfcA.get(ma.getCurrentTag());
        nfcaTag.close();
        boolean a;
        a = isoDepCurrentTag.isConnected();
        byte[] at = new byte[10];
        //at=isoDepCurrentTag.getAtqa();
        isoDepCurrentTag.connect();
        int time;
        time = isoDepCurrentTag.getTimeout();

        response1 = isoDepCurrentTag.transceive(NdefSelectAppliFrame);
        response2 = isoDepCurrentTag.transceive(CCSelect);
        response3 = isoDepCurrentTag.transceive(CCReadLength);
        response4 = isoDepCurrentTag.transceive(SYSSelect);
        response5 = isoDepCurrentTag.transceive(readBinary);
        int ad = 10;
        isoDepCurrentTag.close();

    }

    catch (Exception e) {

        String exp = e.toString();
        Toast toast = Toast.makeText(getApplicationContext(),
                "Tag not found in range. Press back button and re click Write button", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
        toast.show();

    }

    if (response5[8] == (byte) 0x02) {

        test = true;
    }

    else {

        test = false;

    }

    return test;
}