android.nfc.tech.Ndef

Here are the examples of the java api class android.nfc.tech.Ndef taken from open source projects.

1. PassphraseWizardActivity#read()

Project: apg
File: PassphraseWizardActivity.java
private String read(Tag tag) throws IOException, FormatException {
    //read string from tag
    String password = null;
    Ndef ndef = Ndef.get(tag);
    ndef.connect();
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                password = readText(ndefRecord);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
    ndef.close();
    return password;
}

2. NdefTagTester#readMessage()

Project: CtsVerifier
File: NdefTagTester.java
private NdefMessage readMessage(Tag tag) throws IOException, FormatException {
    Ndef ndef = null;
    try {
        ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            return ndef.getNdefMessage();
        }
    } finally {
        if (ndef != null) {
            try {
                ndef.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing Ndef...", e);
            }
        }
    }
    return null;
}

3. NdefTagTester#writeMessage()

Project: CtsVerifier
File: NdefTagTester.java
private void writeMessage(Tag tag, NdefMessage message) throws IOException, FormatException {
    Ndef ndef = null;
    try {
        ndef = Ndef.get(tag);
        ndef.connect();
        ndef.writeNdefMessage(message);
    } finally {
        if (ndef != null) {
            try {
                ndef.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing NDEF...", e);
            }
        }
    }
}

4. PassphraseWizardActivity#write()

Project: apg
File: PassphraseWizardActivity.java
private void write(Tag tag) throws IOException, FormatException {
    //generate new random key and write them on the tag
    SecureRandom sr = new SecureRandom();
    sr.nextBytes(output);
    NdefRecord[] records = { createRecord(output.toString()) };
    NdefMessage message = new NdefMessage(records);
    Ndef ndef = Ndef.get(tag);
    ndef.connect();
    ndef.writeNdefMessage(message);
    ndef.close();
}