Possible Unhandled Promise Rejection (id: 0):

0
I have upgraded a native mobile app with NFC dependency from 8.6.9 (all working well) to 8.18.4. I built a new  dev .apk with native builder UI from the modeler. I have ran npm install --save react-native-nfc-manager in the root and also npm install react-native-nfc-manager in javascriptsource\nativenfc\actions folder. Now my js action to read an nfc tag is not working anymore. Warning in console when using dev tools: Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not a function (near '..._reactNativeNfcManager.default.onStateChanged...')   Any ideas on how to solve this?
asked
1 answers
3

Hi,

It turned out that the NFCRead javascript action was only compatible with an older version of the React-Native-Nfc-Manager. Since version 1.2.2 is over two years old, we decided to upgrade the javascript action to support the latest version of the module.

The javascript action below has been tested with React-Native-Nfc-Manager version 3.3.0

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import { Big } from "big.js";
import { Platform, BackHandler } from "react-native";
import NfcManager, { Ndef, NfcEvents } from "react-native-nfc-manager";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @returns {Promise.<string>}
 */
export async function ReadNFCTag() {
	// BEGIN USER CODE
	if (Platform.OS === "android") {
		const enabled = await NfcManager.isEnabled();
		if (!enabled) {
			throw(new Error("NFC staat niet aan"));
		}
	}

	return new Promise(async(resolve, reject) => {
		let success = false;

		const cleanUp = () => {
			NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
			NfcManager.setEventListener(NfcEvents.StateChanged,null);
		};
		
		await NfcManager.start();
		
		if (Platform.OS === "android") {
			BackHandler.addEventListener("hardwareBackPress", async () => {
				await NfcManager.unregisterTagEvent();
				cleanUp();
				return reject(new Error("NFC is afgebroken door de gebruiker"));
			});
			 NfcManager.setEventListener(NfcEvents.StateChanged, async (event) => {
				if (event.state === "off" || event.state === "turning_off") {
					await NfcManager.unregisterTagEvent();
					cleanUp();
					return reject(new Error("NFC is uitgeschakeld door de gebruiker"));
				}
			});
		}

		NfcManager.setEventListener(NfcEvents.DiscoverTag, async (tag) => {
			success = true;
			await NfcManager.unregisterTagEvent();
			cleanUp();
			const text = Ndef.text.decodePayload(tag.id);
			resolve(text);
		});

		NfcManager.registerTagEvent();
	});
	// END USER CODE
}

 

answered