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
}