-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfcInterface.js
58 lines (44 loc) · 1.61 KB
/
nfcInterface.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
/*
const { NFC } = require('nfc-pcsc');
var nfcInterfaceCallback;
const nfc = new NFC(); // optionally you can pass logger
nfc.on('reader', reader => {
console.log(`${reader.reader.name} device attached`);
// enable when you want to auto-process ISO 14443-4 tags (standard=TAG_ISO_14443_4)
// when an ISO 14443-4 is detected, SELECT FILE command with the AID is issued
// the response is available as card.data in the card event
// see examples/basic.js line 17 for more info
// reader.aid = 'F222222222';
reader.on('card', card => {
// card is object containing following data
// [always] String type: TAG_ISO_14443_3 (standard nfc tags like MIFARE) or TAG_ISO_14443_4 (Android HCE and others)
// [always] String standard: same as type
// [only TAG_ISO_14443_3] String uid: tag uid
// [only TAG_ISO_14443_4] Buffer data: raw data from select APDU response
console.log(`${reader.reader.name} card detected`, card);
// if(nfcInterfaceCallback)nfcInterfaceCallback(card);
});
reader.on('card.off', card => {
console.log(`${reader.reader.name} card removed`, card);
card.uid = reverseBytes(card.uid);
if(nfcInterfaceCallback)nfcInterfaceCallback(card);
});
reader.on('error', err => {
console.log(`${reader.reader.name} an error occurred`, err);
});
reader.on('end', () => {
console.log(`${reader.reader.name} device removed`);
});
});
nfc.on('error', err => {
console.log('an error occurred', err);
});
function reverseBytes(id){
var reverseId = "";
for(var i = id.length; i >= 0 ; i-=2){
reverseId+=id.substring(i-2,i);
}
console.log(reverseId)
return reverseId;
}
*/