-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
65 lines (59 loc) · 1.81 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { Component } from 'react'
import {
View, Text, TouchableOpacity
} from 'react-native'
import NfcManager, { NfcEvents } from 'react-native-nfc-manager';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
nfcId: ""
};
}
componentDidMount() {
NfcManager.start();
NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
this.setState({ nfcId: tag })
NfcManager.setAlertMessageIOS('I got your tag!');
NfcManager.unregisterTagEvent().catch(() => 0);
});
}
componentWillUnmount() {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
NfcManager.unregisterTagEvent().catch(() => 0);
}
render() {
return (
<View style={{ padding: 20 }}>
<Text style={{ fontSize: 20, textAlign: 'center' }}>NFC Demo</Text>
<View style={{ justifyContent: 'center', alignItems: 'center', flexDirection: 'row' }}>
<TouchableOpacity
style={{ padding: 10, width: 100, margin: 20, borderWidth: 1, borderColor: 'black' }}
onPress={this._test}
>
<Text>Start</Text>
</TouchableOpacity>
<Text></Text>
<TouchableOpacity
style={{ padding: 10, width: 100, margin: 20, borderWidth: 1, borderColor: 'black' }}
onPress={this._cancel}
>
<Text>Cancel</Text>
</TouchableOpacity>
</View>
<Text style={{ fontSize: 20, textAlign: 'center' }}>NFC ID: {this.state.nfcId}</Text>
</View>
)
}
_cancel = () => {
NfcManager.unregisterTagEvent().catch(() => 0);
}
_test = async () => {
try {
await NfcManager.registerTagEvent();
} catch (ex) {
console.warn('ex', ex);
NfcManager.unregisterTagEvent().catch(() => 0);
}
}
}