Example usage for android.nfc.tech Ndef getType

List of usage examples for android.nfc.tech Ndef getType

Introduction

In this page you can find the example usage for android.nfc.tech Ndef getType.

Prototype

public String getType() 

Source Link

Document

Get the NDEF tag type.

Usage

From source file:Main.java

static JSONObject ndefToJSON(Ndef ndef) {
    JSONObject json = new JSONObject();

    if (ndef != null) {
        try {//from  ww w  .j a va  2 s. c  om

            Tag tag = ndef.getTag();
            // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored
            if (tag != null) {
                json.put("id", byteArrayToJSON(tag.getId()));
                json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
            }

            json.put("type", translateType(ndef.getType()));
            json.put("maxSize", ndef.getMaxSize());
            json.put("isWritable", ndef.isWritable());
            json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
            // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
            // mTag.getTagService(); of the Ndef object sometimes returns null
            // see http://issues.mroland.at/index.php?do=details&task_id=47
            try {
                json.put("canMakeReadOnly", ndef.canMakeReadOnly());
            } catch (NullPointerException e) {
                json.put("canMakeReadOnly", JSONObject.NULL);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
        }
    }
    return json;
}

From source file:org.protocoderrunner.apprunner.AppRunnerActivity.java

@Override
public void onNewIntent(Intent intent) {
    MLog.d(TAG, "New intent " + intent);

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

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        String nfcID = StrUtils.bytetostring(tag.getId());

        // if there is mContext message waiting to be written
        if (PNFC.nfcMsg != null) {
            MLog.d(TAG, "->" + PNFC.nfcMsg);
            PNFC.writeTag(this, tag, PNFC.nfcMsg);
            onNFCWrittenListener.onNewTag();
            onNFCWrittenListener = null;
            PNFC.nfcMsg = null;/*from ww w  . jav  a2  s  .c  om*/

            // 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());
                    MLog.d(TAG, "qq " + i + " " + recTypes[i] + " " + recPayloads[i]);

                }
                nfcMessage = recPayloads[0];

            }

            onNFCListener.onNewTag(nfcID, nfcMessage);
        }

    }

}