forked from revtel/react-native-nfc-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidTechTestNdef.js
160 lines (142 loc) · 5.76 KB
/
AndroidTechTestNdef.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, {Component} from 'react'
import {
View,
Text,
TouchableOpacity,
ScrollView,
TextInput,
} from 'react-native';
import NfcManager, {NdefParser, NfcTech} from 'react-native-nfc-manager';
function strToBytes(str) {
let result = [];
for (let i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i));
}
return result;
}
function buildTextPayload(valueToWrite) {
const textBytes = strToBytes(valueToWrite);
// in this example. we always use `en`
const headerBytes = [0xD1, 0x01, (textBytes.length + 3), 0x54, 0x02, 0x65, 0x6e];
return [...headerBytes, ...textBytes];
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
supported: false,
enabled: false,
isTestRunning: false,
text: 'hi, nfc!',
parsedText: null,
tag: null,
}
}
componentDidMount() {
NfcManager.isSupported()
.then(supported => {
this.setState({ supported });
if (supported) {
this._startNfc();
}
})
}
render() {
let { supported, enabled, tag, text, parsedText, isTestRunning} = this.state;
return (
<ScrollView style={{flex: 1}}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20 }}>
<Text>{`Is NFC supported ? ${supported}`}</Text>
<Text>{`Is NFC enabled (Android only)? ${enabled}`}</Text>
{
<View style={{padding: 20, marginTop: 20, backgroundColor: '#f0f0f0'}}>
<View style={{flexDirection: 'row', alignItems: 'center'}}>
<Text>Text to write:</Text>
<TextInput
value={text}
style={{marginLeft: 10, flex: 1}}
onChangeText={text => this.setState({text})}
/>
</View>
{!isTestRunning && (
<TouchableOpacity
style={{ margin: 10 }}
onPress={() => this._runTest(text)}
>
<Text style={{ color: 'blue', textAlign: 'center', fontSize: 20 }}>CLICK TO RUN TEST</Text>
</TouchableOpacity>
)}
{isTestRunning && (
<TouchableOpacity
style={{ margin: 10 }}
onPress={() => this._cancelTest()}
>
<Text style={{ color: 'red', textAlign: 'center', fontSize: 20 }}>CLICK TO CANCEL TEST</Text>
</TouchableOpacity>
)}
<Text style={{color: 'grey', textAlign: 'center'}}>
{`When the tag is available, this demo will:\n1. read original NdefMessage from the tag\n2. write a NdefMessage contains a RTD_TEXT into it `}
</Text>
</View>
}
<View style={{alignItems: 'center', justifyContent: 'center', padding: 20, marginTop: 20}}>
<Text >{`Original tag content:`}</Text>
<Text style={{marginTop: 5, color: 'grey'}}>{`${tag ? JSON.stringify(tag) : '---'}`}</Text>
{ parsedText && <Text style={{ marginTop: 5, }}>{`(Parsed Text: ${parsedText})`}</Text>}
</View>
<TouchableOpacity style={{ marginTop: 20, alignItems: 'center' }} onPress={this._clearMessages}>
<Text style={{color: 'blue'}}>Clear above message</Text>
</TouchableOpacity>
</View>
</ScrollView>
)
}
_runTest = textToWrite => {
const cleanUp = () => {
this.setState({isTestRunning: false});
NfcManager.closeTechnology()
NfcManager.unregisterTagEvent();
}
const parseText = (tag) => {
if (tag.ndefMessage) {
return NdefParser.parseText(tag.ndefMessage[0]);
}
return null;
}
this.setState({isTestRunning: true});
NfcManager.registerTagEvent(tag => console.log(tag))
.then(() => NfcManager.requestTechnology(NfcTech.Ndef))
.then(() => NfcManager.getTag())
.then(tag => {
console.log(JSON.stringify(tag));
})
.then(() => NfcManager.getNdefMessage())
.then(tag => {
let parsedText = parseText(tag);
this.setState({tag, parsedText})
})
.then(() => NfcManager.writeNdefMessage(buildTextPayload(textToWrite)))
.then(cleanUp)
.catch(err => {
console.warn(err);
cleanUp();
})
}
_cancelTest = () => {
NfcManager.cancelTechnologyRequest()
.catch(err => console.warn(err));
}
_startNfc = () => {
NfcManager.start()
.then(() => NfcManager.isEnabled())
.then(enabled => this.setState({enabled}))
.catch(err => {
console.warn(err);
this.setState({enabled: false})
})
}
_clearMessages = () => {
this.setState({tag: null, parsedText: null});
}
}
export default App;