Android Question NFC2.EnableForegroundDispatch no longer works on api 31 (FLAG_MUTABLE issue)

Pxs

Member
Licensed User
EDIT: Using the internal NFC library to handle dispatch seems to solve this problem.


This code fails on API 31, worked fine before:

B4X:
Sub Activity_Resume
    Try
        NFC2.EnableForegroundDispatch
    Catch
        Log("ERROR RESUMING FOREGROUND DISPATCH") '<----------ERROR ON API 31
    End Try
End sub

the exception is the dreaded mutable flag

B4X:
java.lang.IllegalArgumentException: com.pixsys.mypixsys: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

How can i set that flag if i dont have the intent handled in the nfc2 library? tried adding this before the try catch, but of course has no effect.
B4X:
    ATRIntent = Activity.GetStartingIntent
    If ATRIntent.IsInitialized = False  Then Return
    ATRIntent.Flags=0x2000000 ' FLAG_IMMUTABLE


Is this a bug or i'm supposed to somehow set the flags before calling the dispatch?
The result is the on screen app cannot handle the intent, and another instance is opened to process it and read the tag.
using API 31 e b4a 12.00 64bit
 
Last edited:

Pxs

Member
Licensed User
Using the internal NFC library instead of the custom NFC2 seems to solve the problem.
 
Upvote 0
Solution
Top