Rita
Rita

Reputation: 1

Trouble reading NFC's - Android

I'm trying to do a simple application that reads Tags NFC with URL and writes the URL in a TextView. I tried to run it but didn't work. Someone can help me with that? The code is bellow and the AndroidManifest too! I hope you can help me.


/*****Main Class******* /

public class MainActivity extends Activity {
    private TextView mCardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ImageView that we'll use to display cards
        mCardView = (TextView) findViewById(R.id.result);

        Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        NdefRecord cardRecord = msg.getRecords()[0];
        String val = new String(cardRecord.getPayload());
        displayCard(val);
    }

    private void displayCard(String val) {
        mCardView.setText(val);
    }
}


/************** /


/****Android Manifest***** /

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.readnfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="true">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <!-- Handle a collectable card NDEF record -->
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                <category android:name="android.intent.category.DEFAULT" />

                <data
                    android:host="developer.android.com"
                    android:pathPrefix="/index.html"
                    android:scheme="http" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                <data android:mimeType="text/plain" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>


/************** /

Tank you for your time.

Sincerely, Rita

Upvotes: 0

Views: 1059

Answers (3)

Zohaib Nauman
Zohaib Nauman

Reputation: 3

            textView.setText(new String(ndefMessage.getRecords()[1].getPayload()));

Upvotes: 0

Janahan
Janahan

Reputation: 431

after you get the Parcelable array, iterate like this and try to read.It might solve your problem.

        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            messages = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                messages[i] = (NdefMessage) rawMsgs[i];
            }
        }
        if(messages[0] != null) {
            String result="";
            byte[] payload = messages[0].getRecords()[0].getPayload();
            for (int b = 1; b<payload.length; b++) {
                result += (char) payload[b];
            }
            Toast.makeText(getApplicationContext(), "Tag Contains " + result, Toast.LENGTH_SHORT).show();
        }

Upvotes: 1

NFC guy
NFC guy

Reputation: 10228

The cardRecord.getPayload() results in a byte array, containing binary data. This data cannot be simply be converted to a URL, as part of the URL is compressed to save storage space. The first byte of the payload encodes common prefixes of URLs (such as "http://www." and "https://"), so you need to decode that separately. See the NFC URI RTD Technical Specification for the complete list. You can easily implement it yourself or try using the NDEF decoding classes from https://github.com/grundid/nfctools to do it for you.

Upvotes: 0

Related Questions