Это моя код для запуска менеджера nfc в компоненте приложения:
Код: Выделить всё
useEffect(() => {
const setupNfc = async () => {
try {
const isSupported = await NfcManager.isSupported();
if (!isSupported) {
console.warn("NFC is not supported on this device.");
return;
}
const isEnabled = await NfcManager.isEnabled();
if (!isEnabled) {
console.warn("NFC is not enabled on this device.");
return;
}
await NfcManager.start();
if (Platform.OS === "android") {
await NfcManager.registerTagEvent();
}
} catch (error) {
console.error("Error starting NFC:", error);
}
};
setupNfc();
}, []);
Код: Выделить всё
const scanNFC = async (): Promise => {
try {
await NfcManager.requestTechnology(NfcTech.Ndef);
const tag = await NfcManager.getTag();
if (!tag || !tag.id) {
console.log("NFC: No tag found or tag ID undefined");
return undefined;
}
const tagId = tag.id;
console.log(`NFC: Tag ID found: ${tagId}`);
return tagId;
} catch (ex) {
if (ex instanceof NfcError.UserCancel) {
console.log("NFC: User canceled the NFC scan.");
} else {
console.error("NFC: NFC read error:", ex);
}
return undefined;
} finally {
await NfcManager.cancelTechnologyRequest();
}
};
Код: Выделить всё
LOG NFC: Tag ID found: 0422DA34BA2A81
LOG HomePage: Tag: 0422DA34BA2A81
LOG HomePage: Scanned tag id is: 0422DA34BA2A81
LOG HomePage: Scanned tag belongs to an admin, skipping updating stamps...
LOG NFC: Tag ID found: 0422DA34BA2A81
LOG HomePage: Tag: 0422DA34BA2A81
LOG HomePage: Scanned tag id is: 0422DA34BA2A81
LOG HomePage: Scanned tag belongs to an admin, skipping updating stamps...
LOG NFC: Tag ID found: 0422DA34BA2A81
LOG HomePage: Tag: 0422DA34BA2A81
LOG HomePage: Scanned tag id is: 0422DA34BA2A81
LOG HomePage: Scanned tag belongs to an admin, skipping updating stamps...
LOG NFC: User canceled the NFC scan.
LOG HomePage: Tag: undefined
LOG HomePage: NFC scan did not return a valid tag
Подробнее здесь: https://stackoverflow.com/questions/791 ... os-devices
Мобильная версия