Android reads NFC while maintaining Bluetooth connection

In my app, I have an active Bluetooth connection and I am reading NFC tags at the same time. My problem is when I read the NFC tag the bluetooth connection is lost.

I am using parallel asynctasks in both Bluetooth and NFC to read asynctasks, with:

.executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR)

Any ideas what I am missing?

EDIT: It seems that when scanning an NFC tag, the enableForegroundDispatch () function destroys background activity. Any way to change the way enableForegroundDispatch () works and not kill the rest of the activity?

EDIT2: This is my manifest declaration

<activity
    android:name="mainActivities.ScreensClasses.NFCReader"
    android:label="@string/title_activity_nfcreader" >
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

      

In summary, I call: setupForegroundDispatch (this is, mNfcAdapter); In Pause, I call: stopForegroundDispatch (this, mNfcAdapter);

And this is my front point:

                  public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }
    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

      

And I am reading NDEF tags. In my dispattch foreground, I've tried different intent filters, but it seems like it makes some difference.

So with this code, when I scan the NDEF tag containing the string, all my app actions are destroyed (onDestroy is called)

+3
android android-asynctask bluetooth nfc


source to share


No one has answered this question yet

Check out similar questions:

2510
How to persist android activity state by persisting instance state?
1296
Reloading activity on android rotation
1270
How to pass data between activities in an Android app?
872
How to create a transparent activity on Android?
1
Android Kotlin - How to read NFC tag inside Activity
1
Reading NFC Tag from Android API
0
Android: action control and NFC tag detector
0
NFC app keeps hanging after intent goes to new activity
0
Android, NFC, documentation: confusion
-2
Reading NFC Tags



All Articles
Loading...
X
Show
Funny
Dev
Pics