0

This is a read/scan NFC code.

I'm trying it out and I need it to work so that I would write my own code for another read activity.

The app keeps on crashing and the error is always, "There's a bug, please fix it for the app to start."

The source video/code was this link https://www.youtube.com/watch?v=QzphwRdJ7r0

Main Activity

package com.example.nfcreadscan;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private IntentFilter[] readfilters;
    private PendingIntent pendingintent;

    @SuppressLint("UnspecifiedImmutableFlag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);



        try {
            Intent intent = new Intent (this, getClass());
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            pendingintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            IntentFilter jdf = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
            jdf.addDataScheme("http");
            jdf.addDataAuthority("javadude.com", null);
            IntentFilter jdt = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED,"text/plain");
            readfilters = new IntentFilter[]{jdf, jdt};

        } catch (IntentFilter.MalformedMimeTypeException e) {
            e.printStackTrace();
        }
        processNFC(getIntent());
    }

    private void enableRead(){
        NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this,pendingintent, readfilters,null);
    }

    private void disableRead(){
        NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
    }
    @Override
    protected void onResume(){
        super.onResume();
        enableRead();
    }

    @Override
    protected void onPause(){
        super.onPause();
        disableRead();
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        processNFC(intent);
    }
    private void processNFC(Intent intent){
        Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        textView.setText("");
        if(messages != null){
            for(Parcelable message : messages){
                NdefMessage ndefMessage = (NdefMessage) message;
                for(NdefRecord record : ndefMessage.getRecords()) {
                    if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
                        textView.append("well known: ");
                        if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
                            textView.append("text: ");
                            textView.append(new String(record.getPayload()));
                            textView.append("\n");

                        } else if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
                            textView.append("uri");
                            textView.append(new String(record.getPayload()));
                            textView.append("\n");

                        }
                    }
                }
            }
        }


    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfcreadscan">
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Nfcreadscan">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 1
    When you launch the app via the debug button in Android Studio it should generate an error in the debug and or logat windows of Android Studio. Knowing what the error in these windows is saying will give more info on the problem. I've seen other people comment that on the I think Android 12 there seems to be problems using `Intents` and the fact that you have had to `@SuppressLint("UnspecifiedImmutableFlag")` suggests that this might be a problems so knowing what version of Android would also help. – Andrew Apr 23 '22 at 09:46
  • I have no errors in the debug windows. And I'm using Android 12 – Amar Dib Apr 23 '22 at 10:37
  • I would also check logcat as it might be the system not able to launch the process and that would appear in logcat only. The other thing is some people had better success with the newer NFC API `enableReaderMode` on Android 12 because of new `Intent` restrictions. – Andrew Apr 23 '22 at 12:55
  • I'll check logcat again. New Intent restrictions... Do you think it's still possible to scan an NFC and let it send SMS messages to specific contacts automatically? – Amar Dib Apr 23 '22 at 13:37
  • In reply to "Do you think it's still possible to scan an NFC and let it send SMS messages to specific contacts automatically" I'm not familiar with programmatically sending sms's but if you can write a program to send the SMS then the NFC can trigger that (I don't think you can craft an Ndef message to do that without writing your own program) – Andrew Apr 23 '22 at 13:52
  • I already wrote a program to send SMS automatically upon shaking the phone, but I can't figure out how to use it upon contact with an NFC tag – Amar Dib Apr 23 '22 at 17:25
  • Here is an example of using enableReaderMode that does not use Intents to work https://stackoverflow.com/a/64921434/2373819 – Andrew Apr 23 '22 at 19:33
  • I ran the code and it worked, I'll try using my code for with it – Amar Dib Apr 24 '22 at 10:54

0 Answers0