Search code examples
androidbroadcastreceivernfc

can broadcast receiver handle ACTION_NDEF_DISCOVERED?


I am trying to write an app that would perform following operations:

once the user unlock the phone he has 10 seconds to scan an NFC tag in order to start camera app. and the NFC tag will have a mime type of application/com.testformat.nfcdemo

To detect screen unlock, I used a broadcast receiver to detected User present. And this works fine.

And I started a CountdownTimer inside the broadcast receiver for 10 seconds.

And the NFC detection part did not work, I could use some help here! thank you all!

My activity:

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class DisplayActivity extends Activity {

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

    }
}

Manifest xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.nfc"
          android:versionCode="1"
          android:versionName="1.0" >

    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <uses-permission android:name="android.permission.NFC" />

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity
                android:name=".DisplayActivity"
                android:label="@string/activity_title" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".ScreenStateReceiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/com.testformat.nfcdemo" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

Receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.CountDownTimer;
import android.widget.Toast;

public class ScreenStateReceiver extends BroadcastReceiver {
    private boolean countdown_end = false;
    NfcAdapter mNfcAdapter;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {

            CharSequence text = context.getResources().getString(R.string.unlock_message);
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            startCountdown(10000);

            if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {

                if (!countdown_end) {
                    text = context.getResources().getString(R.string.nfc_detected);
                    Toast nfc_toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }else if ( intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)){
                if (!countdown_end) {
                    text = context.getResources().getString(R.string.nfc_detected);
                    Toast nfc_toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }

        }
    }

    private void startCountdown(long milliseconds){
        new CountDownTimer(milliseconds, 1000) {
            @Override
            public void onTick(long l) {
            }

            public void onFinish() {
                countdown_end=true;
            }
        }.start();
    }
}

Solution

  • can broadcast receiver handle ACTION_NDEF_DISCOVERED?

    No. That is an activity action, used with startActivity(). It is not broadcast and therefore cannot be picked up by a BroadcastReceiver.