forked from revtel/react-native-nfc-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidMifareClassic.js
322 lines (298 loc) · 9.98 KB
/
AndroidMifareClassic.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import React, { Component } from 'react';
import {
View,
Text,
Platform,
TouchableOpacity,
TextInput,
ScrollView,
} from 'react-native';
import NfcManager, { ByteParser, NfcTech } from 'react-native-nfc-manager';
const KeyTypes = ['A', 'B'];
class App extends Component {
constructor(props) {
super(props);
this.state = {
supported: false,
enabled: false,
isDetecting: false,
mode: 'read',
keyAorB: KeyTypes[1], // 'B'
keyToUse: 'FFFFFFFFFFFF',
sector: 0,
tag: null,
sectorCount: null,
blocksInSector: null,
parsedText: null,
firstBlockInSector: null,
textToWrite: 'Hello, world!',
};
}
componentDidMount() {
NfcManager.isSupported(NfcTech.MifareClassic).then(supported => {
this.setState({ supported });
if (supported) {
this._startNfc();
}
});
}
componentWillUnmount() {
if (this._stateChangedSubscription) {
this._stateChangedSubscription.remove();
}
}
render() {
let {
supported,
enabled,
isDetecting,
keyAorB,
keyToUse,
sector,
tag,
sectorCount,
blocksInSector,
parsedText,
firstBlockInSector,
textToWrite,
} = this.state;
return (
<ScrollView style={{ flex: 1 }}>
{Platform.OS === 'ios' && <View style={{ height: 60 }} />}
<View
style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}
>
<Text>{`Is MifareClassic supported ? ${supported}`}</Text>
<Text>{`Is NFC enabled (Android only)? ${enabled}`}</Text>
{!isDetecting && (
<TouchableOpacity
style={{ margin: 10 }}
onPress={() => this._startDetection()}
>
<Text
style={{ color: 'blue', textAlign: 'center', fontSize: 20 }}
>
{`CLICK TO START DETECTING ${this.state.mode === 'read' ? 'READ' : 'WRITE'}`}
</Text>
</TouchableOpacity>
)}
{isDetecting && (
<TouchableOpacity
style={{ margin: 10 }}
onPress={() => this._stopDetection()}
>
<Text style={{ color: 'red', textAlign: 'center', fontSize: 20 }}>
{`CLICK TO STOP DETECTING ${this.state.mode === 'read' ? 'READ' : 'WRITE'}`}
</Text>
</TouchableOpacity>
)}
{
<View
style={{ padding: 10, marginTop: 20, backgroundColor: '#e0e0e0' }}
>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity
style={[{ flex: 1, alignItems: 'center' }, this.state.mode === 'read' ? {backgroundColor: '#cc0000'} : {backgroundColor: '#d0d0d0'}]}
onPress={() => this.setState({
tag: null,
mode: 'read',
sectorCount: null,
blocksInSector: null,
parsedText: null,
firstBlockInSector: null,
})}
>
<Text>READ</Text>
</TouchableOpacity>
<TouchableOpacity
style={[{ flex: 1, alignItems: 'center' }, this.state.mode !== 'read' ? {backgroundColor: '#cc0000'} : {backgroundColor: '#d0d0d0'}]}
onPress={() => this.setState({
tag: null,
mode: 'write',
sectorCount: null,
blocksInSector: null,
parsedText: null,
firstBlockInSector: null,
})}
>
<Text>WRITE</Text>
</TouchableOpacity>
</View>
<View style={{ flexDirection: 'row', marginTop: 10 }}>
<Text style={{ marginRight: 33 }}>Key to use:</Text>
{KeyTypes.map(key => (
<TouchableOpacity
key={key}
style={{ marginRight: 10 }}
onPress={() => this.setState({ keyAorB: key })}
>
<Text style={{ color: keyAorB === key ? 'blue' : '#aaa' }}>
Use key {key}
</Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ marginRight: 35 }}>Key (hex):</Text>
<TextInput
style={{ width: 200 }}
value={keyToUse}
onChangeText={keyToUse => this.setState({ keyToUse })}
/>
</View>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ marginRight: 10 }}>Sector (0-15):</Text>
<TextInput
style={{ width: 200 }}
value={sector.toString(10)}
onChangeText={sector => this.setState({ sector: sector })}
/>
</View>
{this.state.mode !== 'read' && (
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ marginRight: 15 }}>Text to write:</Text>
<TextInput
style={{ width: 200 }}
value={textToWrite}
onChangeText={textToWrite => this.setState({ textToWrite })}
/>
</View>
)}
</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)} (${sectorCount} sectors)` : '---'
}`}</Text>
{parsedText && (
<Text
style={{ marginTop: 5 }}
>{`Parsed Text:\n${parsedText}`}</Text>
)}
{firstBlockInSector && (
<Text
style={{ marginTop: 5 }}
>{`First block in sector:\n${firstBlockInSector} [${blocksInSector} blocks]`}</Text>
)}
</View>
<TouchableOpacity
style={{ marginTop: 20, alignItems: 'center' }}
onPress={this._clearMessages}
>
<Text style={{ color: 'blue' }}>Clear above message</Text>
</TouchableOpacity>
</View>
</ScrollView>
);
}
_startDetection = () => {
const cleanUp = () => {
this.setState({ isDetecting: false });
NfcManager.closeTechnology();
NfcManager.unregisterTagEvent();
};
const read = () => {
return NfcManager.mifareClassicGetBlockCountInSector(parseInt(this.state.sector))
.then(blocksInSector => {
this.setState({ blocksInSector });
})
.then(() =>
NfcManager.mifareClassicReadSector(parseInt(this.state.sector)),
)
.then(tag => {
let parsedText = ByteParser.byteToHexString(tag);
this.setState({ parsedText });
})
.then(() =>
NfcManager.mifareClassicSectorToBlock(parseInt(this.state.sector)),
)
.then(block => NfcManager.mifareClassicReadBlock(block))
.then(data => {
const parsedText = ByteParser.byteToString(data);
this.setState({ firstBlockInSector: parsedText });
})
};
const write = () => {
return NfcManager.mifareClassicSectorToBlock(parseInt(this.state.sector))
.then(block => {
// Create 1 block
let data = [];
for (let i = 0; i < NfcManager.MIFARE_BLOCK_SIZE; i++) {
data.push(0);
}
// Fill the block with our text, but don't exceed the block size
for (let i = 0; i < this.state.textToWrite.length && i < NfcManager.MIFARE_BLOCK_SIZE; i++) {
data[i] = parseInt(this.state.textToWrite.charCodeAt(i));
}
return NfcManager.mifareClassicWriteBlock(block, data);
})
.then(read)
};
this.setState({ isDetecting: true });
NfcManager.registerTagEvent(tag => console.log(tag))
.then(() => NfcManager.requestTechnology(NfcTech.MifareClassic))
.then(() => NfcManager.getTag())
.then(tag => {
this.setState({ tag });
return NfcManager.mifareClassicGetSectorCount();
})
.then(sectorCount => {
this.setState({ sectorCount });
})
.then(() => {
let sector = parseInt(this.state.sector);
if (isNaN(sector)) {
this.setState({ sector: '0' });
sector = 0;
}
// Convert the key to a UInt8Array
const key = [];
for (let i = 0; i < this.state.keyToUse.length - 1; i += 2) {
key.push(parseInt(this.state.keyToUse.substring(i, i + 2), 16));
}
if (this.state.keyAorB === KeyTypes[0]) {
return NfcManager.mifareClassicAuthenticateA(sector, key);
} else {
return NfcManager.mifareClassicAuthenticateB(sector, key);
}
})
.then(() => { return this.state.mode === 'read' ? read() : write() })
.then(cleanUp)
.catch(err => {
console.warn(err);
cleanUp();
});
};
_stopDetection = () => {
NfcManager.cancelTechnologyRequest()
.then(() => this.setState({ isDetecting: false }))
.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,
sectorCount: null,
blocksInSector: null,
parsedText: null,
firstBlockInSector: null,
});
};
}
export default App;