Skip to content

Commit 2181a0e

Browse files
committed
update react-native-hce to 0.3.0 and adapt to new API
1 parent 0aa8a45 commit 2181a0e

4 files changed

Lines changed: 754 additions & 20 deletions

File tree

components/NFCButton.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { Platform, TouchableOpacity } from 'react-native';
33
import { inject, observer } from 'mobx-react';
44
import { checkNfcEnabled } from '../utils/NFCUtils';
55

6-
import HCESession, { NFCContentType, NFCTagType4 } from 'react-native-hce';
6+
import {
7+
HCESession,
8+
NFCTagType4,
9+
NFCTagType4NDEFContentType
10+
} from 'react-native-hce';
711

812
import NfcIcon from '../assets/images/SVG/NFC-alt.svg';
913

@@ -69,12 +73,20 @@ export default class NFCButton extends React.Component<
6973
};
7074

7175
startSimulation = async () => {
72-
const tag = new NFCTagType4(NFCContentType.Text, this.props.value);
73-
this.simulation = await new HCESession(tag).start();
76+
const tag = new NFCTagType4({
77+
type: NFCTagType4NDEFContentType.Text,
78+
content: this.props.value,
79+
writable: false
80+
});
81+
this.simulation = await HCESession.getInstance();
82+
await this.simulation.setApplication(tag);
83+
await this.simulation.setEnabled(true);
7484
};
7585

7686
stopSimulation = async () => {
77-
await this.simulation.terminate();
87+
if (this.simulation) {
88+
await this.simulation.setEnabled(false);
89+
}
7890
};
7991

8092
render() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"react-native-gesture-handler": "2.30.0",
125125
"react-native-get-random-values": "1.9.0",
126126
"react-native-haptic-feedback": "2.3.3",
127-
"react-native-hce": "0.1.2",
127+
"react-native-hce": "0.3.0",
128128
"react-native-image-picker": "8.2.1",
129129
"react-native-keychain": "10.0.0",
130130
"react-native-nfc-manager": "3.17.2",

utils/NFCUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ export async function scanNfcTag(
6868
if (Platform.OS === 'android') modalStore.toggleAndroidNfcModal(true);
6969

7070
NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag: TagEvent) => {
71+
if (!tag.ndefMessage?.[0]?.payload) {
72+
if (Platform.OS === 'android')
73+
modalStore.toggleAndroidNfcModal(false);
74+
resolve(undefined);
75+
NfcManager.unregisterTagEvent().catch(() => 0);
76+
return;
77+
}
78+
7179
tagFound = tag;
80+
7281
const bytes = new Uint8Array(tag.ndefMessage[0].payload);
7382

7483
let str: string;

0 commit comments

Comments
 (0)