-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
161 lines (130 loc) · 4.17 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
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
import { NativeEventEmitter, NativeModules } from 'react-native';
const { ReactNativeNfcIos: nativeModule } = NativeModules;
const _nfcNDEFReaderSessions = {};
const listeners = {};
export const EMPTY_RECORD = 'EMPTY_RECORD';
export const WELL_KNOWN_RECORD = 'WELL_KNOWN_RECORD';
export const MIME_MEDIA_RECORD = 'MIME_MEDIA_RECORD';
export const ABSOLUTE_URI_RECORD = 'ABSOLUTE_URI_RECORD';
export const EXTERNAL_RECORD = 'EXTERNAL_RECORD';
export const UNKNOWN_RECORD = 'UNKNOWN_RECORD';
export const UNCHANGED_RECORD = 'UNCHANGED_RECORD';
const recordTypes = {
0: EMPTY_RECORD,
1: WELL_KNOWN_RECORD,
2: MIME_MEDIA_RECORD,
3: ABSOLUTE_URI_RECORD,
4: EXTERNAL_RECORD,
5: UNKNOWN_RECORD,
6: UNCHANGED_RECORD,
};
let nextInstanceId = 0;
function genInstanceId() {
const id = nextInstanceId;
nextInstanceId += 1;
return id;
}
function decode(base64Data) {
if (base64Data !== "") {
return base64.decode(base64Data);
}
return base64Data;
}
function formatRecord(record) {
return {
type: record.type || null,
typeNameFormat: recordTypes[record.typeNameFormat],
identifier: record.identifier || null,
payload: record.payload || null,
};
}
function formatMessage(message) {
return {
records: message.records.map(formatRecord),
};
}
const eventEmitter = new NativeEventEmitter(nativeModule);
eventEmitter.addListener('NDEFMessages', (event) => {
console.log({ event });
_nfcNDEFReaderSessions[event.sessionId].emit('NDEFMessages', event.messages.map(formatMessage));
});
export class NFCNDEFReaderSession {
constructor({ alertMessage = null, invalidateAfterFirstRead = false } = {}) {
// ID generated to multiplex session messages over native event emitter
this.id = genInstanceId();
// iOS NFCNDEFReaderSession options
this.alertMessage = alertMessage
this.invalidateAfterFirstRead = invalidateAfterFirstRead;
// Event listeners for this session
this.listenersForType = {
NDEFMessages: [],
};
_nfcNDEFReaderSessions[this.id] = this;
nativeModule.createNFCNDEFReaderSession(this.id, this.invalidateAfterFirstRead, this.alertMessage);
}
static readingAvailable = nativeModule.NFCNDEFReaderSession_readingAvailable;
static readTag({ alertMessage } = {}) {
const session = new NFCNDEFReaderSession({ alertMessage, invalidateAfterFirstRead: true });
let listener;
return new Promise((resolve, reject) => {
const listener = (messages) => {
session.removeEventListener('NDEFMessages', listener);
resolve(messages);
session.release();
}
session.addEventListener('NDEFMessages', listener);
session.begin();
});
}
ensureExists() {
if (!_nfcNDEFReaderSessions[this.id]) {
throw new Error('Session does not exists anymore.')
}
}
release() {
nativeModule.NFCNDEFReaderSession_release(this.id);
delete _nfcNDEFReaderSessions[this.id];
}
begin() {
this.ensureExists();
nativeModule.NFCNDEFReaderSession_begin(this.id);
}
invalidate() {
this.ensureExists();
nativeModule.NFCNDEFReaderSession_invalidate(this.id);
}
setAlertMessage(alertMessage) {
this.ensureExists();
this.alertMessage = alertMessage;
nativeModule.NFCNDEFReaderSession_setAlertMessage(this.id, this.alertMessage);
}
addEventListener(eventType, listener) {
const listeners = this.listenersForType[eventType];
if (!Array.isArray(listeners)) {
throw new Error(`Event type ${eventType} is not supported`);
}
this.listenersForType[eventType].push(listener);
return true;
}
removeEventListener(eventType, listener) {
const listeners = this.listenersForType[eventType];
if (!Array.isArray(listeners)) {
throw new Error(`Event type ${eventType} is not supported`);
}
const listenerIndex = listeners.indexOf(listener);
if (listenerIndex === -1) {
throw new Error('Cannot find event listener');
}
listeners.splice(listenerIndex, 1);
return true;
}
removeAllListeners(eventType) {
this.listenersForType[eventType] = [];
return true;
}
emit(eventType, event) {
for (const listener of this.listenersForType[eventType]) {
listener(event);
}
}
}