Skip to content

Instantly share code, notes, and snippets.

@3gcodes
Created August 1, 2025 17:47
Show Gist options
  • Select an option

  • Save 3gcodes/845a0a47257f649e15999515ceab4461 to your computer and use it in GitHub Desktop.

Select an option

Save 3gcodes/845a0a47257f649e15999515ceab4461 to your computer and use it in GitHub Desktop.
import {useCallback, useEffect, useState} from "react";
import NfcManager, {Ndef, NdefRecord, NfcTech, TagEvent} from "react-native-nfc-manager";
import {Alert} from "react-native";
export type NfcStateProps = {
isSupported: boolean;
isEnabled: boolean;
error: string | null;
loading: boolean
}
export function useNfc() {
const [tag, setTag] = useState<TagEvent | null>(null);
const [tagContents, setTagContents] = useState<string | null>(null);
const [isScanning, setIsScanning] = useState(false);
const [isWriting, setIsWriting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [nfcState, setNfcState] = useState<NfcStateProps>({
isSupported: false,
isEnabled: false,
error: null,
loading: false,
})
useEffect(() => {
NfcManager.start().catch(console.warn)
return () => {
NfcManager.cancelTechnologyRequest().catch(() => {
})
}
}, [])
const startScan = useCallback(async () => {
try {
setIsScanning(true);
await NfcManager.requestTechnology(NfcTech.Ndef);
const tag = await NfcManager.getTag();
setTag(tag);
const ndefRecord: NdefRecord | undefined = tag?.ndefMessage?.[0];
const payload: Uint8Array<ArrayBufferLike> = ndefRecord!.payload as unknown as Uint8Array<ArrayBufferLike>
if (ndefRecord) {
const text = Ndef.text.decodePayload(payload);
setTagContents(text)
}
} catch (err) {
console.warn("NFC scan failed", err);
} finally {
setIsScanning(false);
NfcManager.cancelTechnologyRequest().catch(() => {});
}
}, [])
const stopScan = useCallback(async () => {
setIsScanning(false);
NfcManager.cancelTechnologyRequest().catch(() => {
});
}, [])
const writeTag = useCallback(async (text: string) => {
try {
setIsWriting(true);
setError(null);
await NfcManager.requestTechnology(NfcTech.Ndef);
const bytes = Ndef.encodeMessage([
Ndef.textRecord(text),
]);
if (!bytes) throw new Error("Failed to encode NDEF message");
if (bytes) {
await NfcManager.ndefHandler.writeNdefMessage(bytes)
Alert.alert("Success", "Written!!")
}
} catch (err: any) {
setError(err?.message || "Write failed");
console.warn("Write error:", err);
} finally {
setIsWriting(false);
NfcManager.cancelTechnologyRequest().catch(() => {});
}
}, []);
const checkNfc = useCallback(async () => {
if (!NfcManager) {
setNfcState({
isSupported: false,
isEnabled: false,
error: "NFC module not available",
loading: false,
})
return
}
try {
const isSupported = await NfcManager.isSupported();
let isEnabled = false;
if (isSupported) {
isEnabled = await NfcManager.isEnabled();
if (isEnabled) {
try {
await NfcManager.start();
} catch (error) {
console.warn("Error starting...", error);
}
}
}
setNfcState({
isSupported,
isEnabled,
error: null,
loading: false
})
} catch (error) {
console.warn("Error checking NFC", error)
setNfcState({
isSupported: false,
isEnabled: false,
error: "unknown error",
loading: false
})
}
}, [])
return {
tag,
tagContents,
isScanning,
isWriting,
startScan,
stopScan,
nfcState,
checkNfc,
writeTag,
error,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment