forked from someone-noone/libnfc-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (39 loc) · 1.09 KB
/
index.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
'use strict'
const EventEmitter = require('events');
const binding = require('bindings')('nfc-binding');
const Promise = require('bluebird');
class NFCReader extends EventEmitter {
constructor() {
super();
this._nfc = new binding.NFCReaderRaw();
}
open() {
return this._nfc.open();
}
close() {
return this.close();
}
transceive(data, timeout) {
if (timeout)
return Promise.fromCallback(cb => this._nfc.transceive(data, timeout, cb));
else
return Promise.fromCallback(cb => this._nfc.transceive(data, cb));
}
release() {
return Promise.fromCallback(cb => this._nfc.release(cb));
}
poll() {
return Promise.fromCallback(cb => this._nfc.poll(cb))
.then(card => this.emit('card', card))
.catch(e => {
if (e.message != "NFC_ECHIP" && e.message != "Unknown error")
this.emit('error', e)
else
return this.poll();
});
}
}
module.exports = {
NFC: binding.NFC,
NFCReader
}