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


source to share





All Articles