Java Code Examples for android.nfc.tech.Ndef#getType()

The following examples show how to use android.nfc.tech.Ndef#getType() . 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: ReadUriActivity.java    From android-nfc with MIT License 5 votes vote down vote up
@Override
public void onNewIntent(Intent intent) {
    //获取Tag对象
    Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //获取Ndef的实例
    Ndef ndef = Ndef.get(detectedTag);
    mTagText = ndef.getType() + "\n max size:" + ndef.getMaxSize() + " bytes\n\n";
    readNfcTag(intent);
    mNfcText.setText(mTagText);
}
 
Example 2
Source File: ReadTextActivity.java    From android-nfc with MIT License 5 votes vote down vote up
@Override
public void onNewIntent(Intent intent) {
    //1.获取Tag对象
    Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //2.获取Ndef的实例
    Ndef ndef = Ndef.get(detectedTag);
    mTagText = ndef.getType() + "\nmaxsize:" + ndef.getMaxSize() + "bytes\n\n";
    readNfcTag(intent);
    mNfcText.setText(mTagText);
}
 
Example 3
Source File: AppRunnerActivity.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Listen to NFC incomming data
 */
@Override
public void onNewIntent(Intent intent) {
    MLog.d(TAG, "New intent " + intent);

    if (intent.getAction() != null) {
        MLog.d(TAG, "Discovered tag with intent: " + intent);

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String nfcID = StrUtils.bytetostring(tag.getId());

        // if there is a message waiting to be written
        if (PNfc.nfcMsg != null) {
            MLog.d(TAG, "->" + PNfc.nfcMsg);
            PNfc.writeTag(this, tag, PNfc.nfcMsg);
            if (onNFCWrittenListener != null) onNFCWrittenListener.onNewTag();
            onNFCWrittenListener = null;
            PNfc.nfcMsg = null;

            // read the nfc tag info
        } else {
            // get NDEF tag details
            Ndef ndefTag = Ndef.get(tag);
            if (ndefTag == null) {
                return;
            }

            int size = ndefTag.getMaxSize(); // tag size
            boolean writable = ndefTag.isWritable(); // is tag writable?
            String type = ndefTag.getType(); // tag type

            String nfcMessage = "";

            // get NDEF message details
            NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
            if (ndefMesg != null) {
                NdefRecord[] ndefRecords = ndefMesg.getRecords();
                int len = ndefRecords.length;
                String[] recTypes = new String[len]; // will contain the
                // NDEF record types
                String[] recPayloads = new String[len]; // will contain the
                // NDEF record types
                for (int i = 0; i < len; i++) {
                    recTypes[i] = new String(ndefRecords[i].getType());
                    recPayloads[i] = new String(ndefRecords[i].getPayload());
                }
                nfcMessage = recPayloads[0];
            }
            if (onNFCListener != null) onNFCListener.onNewTag(nfcID, nfcMessage);
        }
    }
}