Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand All @@ -31,6 +32,8 @@ public class ReactNativeNFCModule extends ReactContextBaseJavaModule implements

private boolean startupIntentProcessed = false;

private NfcAdapter mNfcAdapter;

public ReactNativeNFCModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
Expand All @@ -54,9 +57,9 @@ public void onNewIntent(Intent intent) {
private void handleIntent(Intent intent, boolean startupIntent) {
if (intent != null && intent.getAction() != null) {

switch (intent.getAction()){
// switch (intent.getAction()){

case NfcAdapter.ACTION_NDEF_DISCOVERED:
// case NfcAdapter.ACTION_NDEF_DISCOVERED:
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

if (rawMessages != null) {
Expand All @@ -66,16 +69,16 @@ private void handleIntent(Intent intent, boolean startupIntent) {
}
processNdefMessages(messages,startupIntent);
}
break;
// 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:
// case NfcAdapter.ACTION_TAG_DISCOVERED:
// case NfcAdapter.ACTION_TECH_DISCOVERED:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
processTag(tag,startupIntent);
break;
// break;

}
// }
}
}

Expand Down Expand Up @@ -123,14 +126,35 @@ public void onHostResume() {
}
startupIntentProcessed = true;
}

if (mNfcAdapter != null) {
setupForegroundDispatch(getCurrentActivity(), mNfcAdapter);
} else {
mNfcAdapter = NfcAdapter.getDefaultAdapter(getReactApplicationContext());
}
}

@Override
public void onHostPause() {}
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> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class TagParser {

public static WritableMap parse(Tag tag){
if (tag == null) return null;
WritableMap result = new WritableNativeMap();

result.putString("type", NfcDataType.TAG.name());
Expand Down