• 16-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing error when Android 12 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent in Chariot Solutions Phonegap NFC

Error when Android 12 – Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent in Chariot Solutions Phonegap NFC

Lightrun Team
Lightrun Team
16-Jan-2023

Explanation of the problem

The plugin, designed for reading NFC tags, functions correctly across all devices except for those running Android 12. The error encountered is as follows:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

Upon investigation, it appears that the issue lies within the createPendingIntent() method. The relevant code is as follows:

private void createPendingIntent() {
        if (pendingIntent == null) {
            Activity activity = getActivity();
            Intent intent = new Intent(activity, activity.getClass());
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0); // Problem in the last param here
        }
    }

It is unclear at this time if the error is due to an incorrect implementation on the developer’s part or if it is an issue that requires a fix. Further analysis is necessary to determine the root cause of the problem.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for error when Android 12 – Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent in Chariot Solutions – Phonegap NFC

To resolve this issue, developers need to ensure that one of the FLAG_IMMUTABLE or FLAG_MUTABLE flags is specified when creating a PendingIntent within the Chariot Solutions Phonegap NFC plugin. The following code block demonstrates how to specify the FLAG_IMMUTABLE flag when creating a PendingIntent:

Intent intent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

Alternatively, the FLAG_MUTABLE flag can be specified like this:

Intent intent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_MUTABLE);

It’s important to note that FLAG_IMMUTABLE will create a PendingIntent that cannot be modified or updated, whereas FLAG_MUTABLE will create a PendingIntent that can be modified or updated. Developers should choose the appropriate flag based on their specific use case.

In summary, this issue can be resolved by specifying one of the FLAG_IMMUTABLE or FLAG_MUTABLE flags when creating a PendingIntent within the Chariot Solutions Phonegap NFC plugin. This change ensures that the PendingIntent is created correctly and that the application will function correctly on Android 12 devices with version 31 and above.

Other popular problems with Phonegap NFC

Problem: Limited support for certain NFC chip types

Phonegap NFC is a plugin that allows developers to access the NFC (Near Field Communication) functionality of a device, but it has limited support for certain NFC chip types. This can cause issues for developers trying to use the plugin for devices with less common NFC chip types.

Solution:

One solution is to use a different plugin that has broader support for different NFC chip types, such as the Cordova NFC Plugin. Additionally, developers can use a combination of both Phonegap NFC and Cordova NFC Plugin to access the full range of NFC functionality on a device. Additionally, developers can use the PhoneGap Build service which allows developers to package their app and provide it to different platforms. This allows you to use the same codebase to target different devices which might have different NFC chip types.

Problem: Difficulty in implementing advanced NFC functionality

Phonegap NFC provides basic NFC functionality, but it can be difficult to implement more advanced features such as peer-to-peer communication or card emulation.

Solution:

One solution is to use a different plugin that provides more advanced NFC functionality, such as the Cordova NFC Plugin. Additionally, Phonegap NFC provides an ndef object which can be used to access the NDEF (NFC Data Exchange Format) records on a device. This allows developers to implement advanced NFC functionality such as peer-to-peer communication and card emulation. Additionally, developers can use the PhoneGap Build service which allows developers to package their app and provide it to different platforms. This allows you to use the same codebase to target different devices which might have different NFC functionality.

Problem: Limited support for certain platforms

Phonegap NFC is only available for certain platforms, such as iOS and Android, which can cause issues for developers trying to use the plugin on other platforms.

Solution:

One solution is to use a different plugin that has broader support for different platforms, such as the Cordova NFC Plugin. Additionally, developers can use the PhoneGap Build service which allows developers to package their app and provide it to different platforms. This allows you to use the same codebase to target different platforms which might have different NFC functionality. Additionally, developers can also write platform-specific code using PhoneGap’s APIs to access the NFC functionality of a device.

A brief introduction to Phonegap NFC

PhoneGap NFC is a plugin for the PhoneGap framework that allows developers to access the NFC (Near Field Communication) functionality of a mobile device. NFC is a technology that enables short-range wireless communication between devices and can be used for a variety of purposes, such as contactless payments, access control, and data exchange. The PhoneGap NFC plugin provides a JavaScript API that allows developers to access the NFC functionality of a device, such as reading and writing NFC tags and sharing data with other NFC-enabled devices.

The PhoneGap NFC plugin provides a set of functions for reading and writing NFC tags, as well as for starting and stopping NFC readers. Once the plugin is installed and configured, developers can use the JavaScript API to perform operations such as scanning for NFC tags, reading the data stored on a tag, and writing new data to a tag. The plugin also provides an event-based API for handling NFC-related events, such as when a tag is scanned or when a device is brought into proximity with another NFC-enabled device. This allows developers to create interactive and responsive applications that can take advantage of the NFC capabilities of a device.

Most popular use cases for Phonegap NFC include

  1. Phonegap NFC can be used for reading and writing NFC tags. This allows developers to create applications that can read and write data to NFC tags, such as storing information about a product or linking to a website. The following code block demonstrates how to read data from an NFC tag using the Phonegap NFC plugin:
nfc.addNdefListener(
    function (nfcEvent) {
        var tag = nfcEvent.tag,
            ndefMessage = tag.ndefMessage;

        // read the data from the tag
        var data = nfc.bytesToString(ndefMessage[0].payload).substring(3);
    },
    function () {
        console.log("Listening for NDEF tags.");
    },
    function (error) {
        console.log("Error adding NDEF listener " + JSON.stringify(error));
    }
);
  1. Phonegap NFC can be used for creating peer-to-peer NFC connections. This allows developers to create applications that can establish NFC connections between two devices, allowing for data transfer and other interactions.
  2. Phonegap NFC can be used for creating mobile payments applications by using the NFC functionality to communicate with point-of-sale (POS) terminals. This allows developers to create mobile applications that can process payments using NFC technology, enabling users to make payments by tapping their mobile device on a POS terminal.

 

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.