Java Code Examples for android.nfc.tech.IsoDep#transceive()

The following examples show how to use android.nfc.tech.IsoDep#transceive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoyaltyCardReader.java    From android-CardReader with Apache License 2.0 6 votes vote down vote up
/**
 * Callback when a new tag is discovered by the system.
 *
 * <p>Communication with the card should take place here.
 *
 * @param tag Discovered tag
 */
@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);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error communicating with card: " + e.toString());
        }
    }
}
 
Example 2
Source File: NfcManager.java    From nfcspy with GNU General Public License v3.0 5 votes vote down vote up
static byte[] transceiveApdu(IsoDep tag, byte[] cmd) {
	if (tag != null) {
		try {
			if (!tag.isConnected()) {
				tag.connect();
				tag.setTimeout(10000);
			}

			return tag.transceive(cmd);
		} catch (Exception e) {
		}
	}
	return null;
}