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

The following examples show how to use android.nfc.tech.IsoDep#get() . 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: AndroidCard.java    From nordpol with MIT License 7 votes vote down vote up
public static AndroidCard get(Tag tag) throws IOException {
    IsoDep card = IsoDep.get(tag);

    if(card != null) {
        /* Workaround for the Samsung Galaxy S5 (since the
         * first connection always hangs on transceive).
         * TODO: This could be improved if we could identify
         * Samsung Galaxy S5 devices
         */
        card.connect();
        card.close();
        return new AndroidCard(card);
    } else {
        return null;
    }
}
 
Example 2
Source File: AndroidCard.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static AndroidCard get(Tag tag) throws IOException {
    IsoDep card = IsoDep.get(tag);

    /* Workaround for the Samsung Galaxy S5 (since the
     * first connection always hangs on transceive).
     * TODO: This could be improved if we could identify
     * Samsung Galaxy S5 devices
     */
    card.connect();
    card.close();

    if(card != null) {
        return new AndroidCard(card);
    } else {
        return null;
    }
}
 
Example 3
Source File: AndroidCard.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static AndroidCard get(Tag tag) throws IOException {
    IsoDep card = IsoDep.get(tag);

    /* Workaround for the Samsung Galaxy S5 (since the
     * first connection always hangs on transceive).
     * TODO: This could be improved if we could identify
     * Samsung Galaxy S5 devices
     */
    card.connect();
    card.close();

    if(card != null) {
        return new AndroidCard(card);
    } else {
        return null;
    }
}
 
Example 4
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 5
Source File: EmvReadActivity.java    From smartcard-reader with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onTagDiscovered(Tag tag) {
    doTapFeedback();
    clearImage();
    // maybe clear console or show separator, depends on settings
    if (mAutoClear) {
        clearMessages();
    } else {
        addMessageSeparator();
    }
    // get IsoDep handle and run xcvr thread
    IsoDep isoDep = IsoDep.get(tag);
    if (isoDep == null) {
        onError(getString(R.string.wrong_tag_err));
    } else {
        ReaderXcvr xcvr = new PaymentReaderXcvr(isoDep, "", this, TEST_MODE_EMV_READ);
        new Thread(xcvr).start();
    }
}
 
Example 6
Source File: BatchSelectActivity.java    From smartcard-reader with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onTagDiscovered(Tag tag) {
    doTapFeedback();
    clearImage();
    // maybe clear console or show separator, depends on settings
    if (mAutoClear) {
        clearMessages();
    } else {
        // two separators between taps/discoveries
        addMessageSeparator();
        addMessageSeparator();
    }
    // get IsoDep handle and run xcvr thread
    IsoDep isoDep = IsoDep.get(tag);
    if (isoDep == null) {
        onError(getString(R.string.wrong_tag_err));
    } else {
        List<SmartcardApp> memberApps = mGrpToMembersMap.get(mSelectedGrpPos);
        new Thread(new BatchReaderXcvr(isoDep, memberApps, this)).start();
    }
}
 
Example 7
Source File: NfcSession.java    From yubikit-android with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Iso7816Connection openIso7816Connection() throws IOException {
    IsoDep card = IsoDep.get(tag);
    if (card == null) {
        throw new IOException("the tag does not support ISO-DEP");
    }
    card.connect();
    return new NfcIso7816Connection(card);
}
 
Example 8
Source File: MainActivity.java    From flutter-nfc-app with MIT License 5 votes vote down vote up
@SuppressLint("MissingPermission")
@TargetApi(Build.VERSION_CODES.KITKAT)
private void setIsoDep(Intent intent) {
    try {
        Log.i("","setIsoDep entered");
        if (intent == null) {
            Log.i("","intent null");
            return;
        }
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        if (tag == null) {
            Log.i("","tag null");
            return;
        }

        isoDep = IsoDep.get(tag);

        if (isoDep != null && !isoDep.isConnected()) {
            isoDep.connect();
            Log.i("","isodep connect ok");
        }
        else
        {
            Log.i("","isodep null or connect not ok");
        }
    } catch (Exception e) {
        Log.e("","",e);
    }
}
 
Example 9
Source File: PassportConnection.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Opens a connection with the ID by doing BAC
 * Uses hardcoded parameters for now
 *
 * @param tag - NFC tag that started this activity (ID NFC tag)
 * @return PassportService - passportservice that has an open connection with the ID
 */
public PassportService openConnection(Tag tag, final DocumentData docData) throws CardServiceException {
    try {
        IsoDep nfc = IsoDep.get(tag);
        CardService cs = CardService.getInstance(nfc);
        this.ps = new PassportService(cs);
        this.ps.open();

        // Get the information needed for BAC from the data provided by OCR
        this.ps.sendSelectApplet(false);
        BACKeySpec bacKey = new BACKeySpec() {
            @Override
            public String getDocumentNumber() {
                return docData.getDocumentNumber();
            }

            @Override
            public String getDateOfBirth() { return docData.getDateOfBirth(); }

            @Override
            public String getDateOfExpiry() { return docData.getExpiryDate(); }
        };
        ps.doBAC(bacKey);
        return ps;
    } catch (CardServiceException ex) {
        try {
            ps.close();
        } catch (Exception ex2) {
            ex2.printStackTrace();
        }
        throw ex;
    }
}
 
Example 10
Source File: NfcBankomatCardReader.java    From bankomatinfos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Connects to IsoDep
 * 
 * @throws IOException
 */
public void connectIsoDep() throws IOException, NoSmartCardException {
	_localIsoDep = IsoDep.get(_nfcTag);
	if (_localIsoDep == null) {
		throw new NoSmartCardException("This NFC tag is no ISO 7816 card");
	}
	_localIsoDep.connect();
}
 
Example 11
Source File: AppSelectActivity.java    From smartcard-reader with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onTagDiscovered(Tag tag) {
    doTapFeedback();
    clearImage();
    // maybe clear console or show separator, depends on settings
    if (mAutoClear) {
        clearMessages();
    } else {
        addMessageSeparator();
    }
    // get IsoDep handle and run xcvr thread
    IsoDep isoDep = IsoDep.get(tag);
    if (isoDep == null) {
        onError(getString(R.string.wrong_tag_err));
    } else {
        ReaderXcvr xcvr;
        String aid = mApps.get(mSelectedAppPos).getAid();

        if (mManual) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Animation shake = AnimationUtils.loadAnimation(AppSelectActivity.this, R.anim.shake);
                    mSelectButton.startAnimation(shake);
                }
            });
            // manual select mode; for multiple selects per tap/connect
            // does not select ppse for payment apps unless specifically configured
            xcvr = new ManualReaderXcvr(isoDep, aid, this);
        } else if (mApps.get(mSelectedAppPos).getType() == SmartcardApp.TYPE_PAYMENT) {
            // payment, ie. always selects ppse first
            xcvr = new PaymentReaderXcvr(isoDep, aid, this, TEST_MODE_APP_SELECT);
        } else {
            // other/non-payment; auto select on each tap/connect
            xcvr = new OtherReaderXcvr(isoDep, aid, this);
        }

        new Thread(xcvr).start();
    }
}
 
Example 12
Source File: MainActivity.java    From host-card-emulation-sample with MIT License 5 votes vote down vote up
@Override
public void onTagDiscovered(Tag tag) {
	IsoDep isoDep = IsoDep.get(tag);
	IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
	Thread thread = new Thread(transceiver);
	thread.start();
}
 
Example 13
Source File: NfcManager.java    From nfcspy with GNU General Public License v3.0 4 votes vote down vote up
static IsoDep attachCard(Intent intent) {
	Tag tag = (Tag) intent.getParcelableExtra(EXTRA_TAG);
	return (tag != null) ? IsoDep.get(tag) : null;
}
 
Example 14
Source File: ReaderManager.java    From nfcard with GNU General Public License v3.0 3 votes vote down vote up
private Card readCard(Tag tag) {

		final Card card = new Card();

		try {

			publishProgress(SPEC.EVENT.READING);

			card.setProperty(SPEC.PROP.ID, Util.toHexString(tag.getId()));

			final IsoDep isodep = IsoDep.get(tag);
			if (isodep != null)
				StandardPboc.readCard(isodep, card);

			final NfcF nfcf = NfcF.get(tag);
			if (nfcf != null)
				FelicaReader.readCard(nfcf, card);

			publishProgress(SPEC.EVENT.IDLE);

		} catch (Exception e) {
			card.setProperty(SPEC.PROP.EXCEPTION, e);
			publishProgress(SPEC.EVENT.ERROR);
		}

		return card;
	}
 
Example 15
Source File: ReaderManager.java    From NFCard with GNU General Public License v3.0 3 votes vote down vote up
private Card readCard(Tag tag) {

		final Card card = new Card();

		try {

			publishProgress(SPEC.EVENT.READING);

			card.setProperty(SPEC.PROP.ID, Util.toHexString(tag.getId()));

			final IsoDep isodep = IsoDep.get(tag);
			if (isodep != null)
				StandardPboc.readCard(isodep, card);

			final NfcF nfcf = NfcF.get(tag);
			if (nfcf != null)
				FelicaReader.readCard(nfcf, card);

			publishProgress(SPEC.EVENT.IDLE);

		} catch (Exception e) {
			card.setProperty(SPEC.PROP.EXCEPTION, e);
			publishProgress(SPEC.EVENT.ERROR);
		}

		return card;
	}