Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Reading NFC tag in service class

I am working on an application where i need to read the data stored in NFC tag, by data i mean is simple integer values as such 0,1,2,3 and so. The functions to read the data from NFC works fine when it's in Activity class but i need to run the application in the background so even when application is not running in foreground i can read the data from the NFC. So i wrote a service class and moved the function from the Activity to the service class. But it didn't work.

This is the "MainActivity.java"

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startService(new Intent(this, MyService.class));
    }

startService(new Intent(this, MyService.class));
This line starts the service class.

The Service class is "MyService.java"

public class MyService extends Service {

    private NfcAdapter mNfcAdapter;
    private PendingIntent mNfcPendingIntent;
    private IntentFilter[] mReadTagFilters;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
         mNfcPendingIntent = PendingIntent.getActivity(this, 0, new
         Intent(this,
         getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
         | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0); 
         IntentFilter ndefDetected = new IntentFilter(
         NfcAdapter.ACTION_NDEF_DISCOVERED);
         ndefDetected.addDataScheme("http");
         IntentFilter techDetected = new IntentFilter(
         NfcAdapter.ACTION_TECH_DISCOVERED);    
         mReadTagFilters = new IntentFilter[] { ndefDetected, techDetected };    
            Log.v("service", "return from onCreate function");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);


        Log.v("service", "onStart function");
        if (mNfcAdapter != null) {
            if (!mNfcAdapter.isEnabled()) {
                new AlertDialog.Builder(this)
                        .setTitle("NFC Dialog")
                        .setMessage("Please switch on NFC")
                        .setPositiveButton("Settings",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface arg0,
                                            int arg1) {
                                        Intent setnfc = new Intent(
                                                Settings.ACTION_WIRELESS_SETTINGS);
                                        startActivity(setnfc);
                                    }
                                })
                        .setOnCancelListener(
                                new DialogInterface.OnCancelListener() {
                                    public void onCancel(DialogInterface dialog) {
                                        dialog.dismiss(); 
                                    }
                                }).create().show();
            }
            mNfcAdapter.enableForegroundDispatch (MainActivity.mContext, mNfcPendingIntent,
                    mReadTagFilters, null);
        } else {
            Toast.makeText(this,
                    "Sorry, NFC adapter not available on your device.",
                    Toast.LENGTH_SHORT).show();
        }
        Log.v("service", "return fonStart function");
    }

}

In this the onCreate() function is running fine. The problem is this line :

 mNfcAdapter.enableForegroundDispatch (mContext, mNfcPendingIntent,
                    mReadTagFilters, null);

The first argument of the function accepts the context of an Activity and this is where i am stuck. Is there any way to solve this or any alternative. Thankyou for your help.

like image 466
Rana Ranvijay Singh Avatar asked Oct 28 '13 13:10

Rana Ranvijay Singh


People also ask

Can you emulate an NFC tag Android?

Many Android-powered devices that offer NFC functionality already support NFC card emulation. In most cases, the card is emulated by a separate chip in the device, called a secure element.

How do I read data on my NFC card?

To read the NFC tag, the app needs to register for handling ACTION_NDEF_DISCOVERED intent. Registering this intent will let your app handle any NFC tag that is tapped to the Android device. If you wish to handle only those tags that belong to your application then you can add a filter.


1 Answers

Android's NFC functionality (specifically receiving Intents for tags, Beam, etc) is available to foreground activities only.

like image 186
Michael Roland Avatar answered Oct 20 '22 20:10

Michael Roland