This repository was archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathReactNativeNFCModule.java
More file actions
207 lines (161 loc) · 6.97 KB
/
ReactNativeNFCModule.java
File metadata and controls
207 lines (161 loc) · 6.97 KB
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
package com.novadart.reactnativenfc;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.app.PendingIntent;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.novadart.reactnativenfc.parser.NdefParser;
import com.novadart.reactnativenfc.parser.TagParser;
public class ReactNativeNFCModule extends ReactContextBaseJavaModule implements ActivityEventListener,LifecycleEventListener {
private static final String EVENT_NFC_DISCOVERED = "__NFC_DISCOVERED";
// caches the last message received, to pass it to the listeners when it reconnects
private WritableMap startupNfcData;
private boolean startupNfcDataRetrieved = false;
private boolean startupIntentProcessed = false;
private NfcAdapter mNfcAdapter;
public ReactNativeNFCModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
reactContext.addLifecycleEventListener(this);
}
@Override
public String getName() {
return "ReactNativeNFC";
}
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {}
@Override
public void onNewIntent(Intent intent) {
handleIntent(intent,false);
}
private void handleIntent(Intent intent, boolean startupIntent) {
if (intent != null && intent.getAction() != null) {
// switch (intent.getAction()){
// case NfcAdapter.ACTION_NDEF_DISCOVERED:
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
}
processNdefMessages(messages,startupIntent);
}
// break;
// ACTION_TAG_DISCOVERED is an unlikely case, according to https://developer.android.com/guide/topics/connectivity/nfc/nfc.html
// case NfcAdapter.ACTION_TAG_DISCOVERED:
// case NfcAdapter.ACTION_TECH_DISCOVERED:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
processTag(tag,startupIntent);
// break;
// }
}
}
/**
* This method is used to retrieve the NFC data was acquired before the React Native App was loaded.
* It should be called only once, when the first listener is attached.
* Subsequent calls will return null;
*
* @param callback callback passed by javascript to retrieve the nfc data
*/
@ReactMethod
public void getStartUpNfcData(Callback callback){
if(!startupNfcDataRetrieved){
callback.invoke(DataUtils.cloneWritableMap(startupNfcData));
startupNfcData = null;
startupNfcDataRetrieved = true;
} else {
callback.invoke();
}
}
private void sendEvent(@Nullable WritableMap payload) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(EVENT_NFC_DISCOVERED, payload); }
private void processNdefMessages(NdefMessage[] messages, boolean startupIntent){
NdefProcessingTask task = new NdefProcessingTask(startupIntent);
task.execute(messages);
}
private void processTag(Tag tag, boolean startupIntent){
TagProcessingTask task = new TagProcessingTask(startupIntent);
task.execute(tag);
}
@Override
public void onHostResume() {
if(!startupIntentProcessed){
if(getReactApplicationContext().getCurrentActivity() != null){ // it shouldn't be null but you never know
// necessary because NFC might cause the activity to start and we need to catch that data too
handleIntent(getReactApplicationContext().getCurrentActivity().getIntent(),true);
}
startupIntentProcessed = true;
}
if (mNfcAdapter != null) {
setupForegroundDispatch(getCurrentActivity(), mNfcAdapter);
} else {
mNfcAdapter = NfcAdapter.getDefaultAdapter(getReactApplicationContext());
}
}
@Override
public void onHostPause() {
if (mNfcAdapter != null)
stopForegroundDispatch(getCurrentActivity(), mNfcAdapter);
}
@Override
public void onHostDestroy() {}
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
private class NdefProcessingTask extends AsyncTask<NdefMessage[],Void,WritableMap> {
private final boolean startupIntent;
NdefProcessingTask(boolean startupIntent) {
this.startupIntent = startupIntent;
}
@Override
protected WritableMap doInBackground(NdefMessage[]... params) {
NdefMessage[] messages = params[0];
return NdefParser.parse(messages);
}
@Override
protected void onPostExecute(WritableMap ndefData) {
if(startupIntent) {
startupNfcData = ndefData;
}
sendEvent(ndefData);
}
}
private class TagProcessingTask extends AsyncTask<Tag,Void,WritableMap> {
private final boolean startupIntent;
TagProcessingTask(boolean startupIntent) {
this.startupIntent = startupIntent;
}
@Override
protected WritableMap doInBackground(Tag... params) {
Tag tag = params[0];
return TagParser.parse(tag);
}
@Override
protected void onPostExecute(WritableMap tagData) {
if(startupIntent) {
startupNfcData = tagData;
}
sendEvent(tagData);
}
}
}