Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android 12 and above app crashes #474

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PhoneGap NFC Plugin
PhoneGap NFC Plugin (With Fix for SDK 34)
==========================

The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phonegap-nfc",
"version": "1.2.0",
"version": "1.3.0",
"description": "Near Field Communication (NFC) Plugin. Read and write NDEF messages to NFC tags and share NDEF messages with peers.",
"cordova": {
"id": "phonegap-nfc",
Expand Down
22 changes: 2 additions & 20 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="phonegap-nfc"
version="1.2.0">
version="1.3.0">

<name>NFC</name>

Expand All @@ -14,11 +14,11 @@
<repo>https://github.com/chariotsolutions/phonegap-nfc.git</repo>
<issue>https://github.com/chariotsolutions/phonegap-nfc/issues</issue>

<platform name="android">
<js-module src="www/phonegap-nfc.js" name="NFC">
<runs />
</js-module>

<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="NfcPlugin">
<param name="android-package" value="com.chariotsolutions.nfc.plugin.NfcPlugin"/>
Expand All @@ -35,14 +35,9 @@
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" android:required="false"/>
</config-file>

</platform>

<platform name="wp8">
<js-module src="www/phonegap-nfc.js" name="NFC">
<runs />
</js-module>

<config-file target="config.xml" parent="/*">
<feature name="NfcPlugin">
<param name="wp-package" value="NfcPlugin"/>
Expand All @@ -55,15 +50,10 @@

<source-file src="src/windows-phone-8/Ndef.cs" />
<source-file src="src/windows-phone-8/NfcPlugin.cs" />

</platform>

<!-- windows -->
<platform name="windows">
<js-module src="www/phonegap-nfc.js" name="NFC">
<runs />
</js-module>

<config-file target="config.xml" parent="/*">
<feature name="NfcPlugin">
<param name="wp-package" value="NfcPlugin"/>
Expand All @@ -89,9 +79,6 @@
</platform>

<platform name="blackberry10">
<js-module src="www/phonegap-nfc.js" name="NFC">
<runs />
</js-module>
<!-- override defaults for BB10 -->
<js-module src="www/phonegap-nfc-blackberry.js" name="NFCBB10">
<runs />
Expand All @@ -107,10 +94,6 @@
</platform>

<platform name="ios">
<js-module src="www/phonegap-nfc.js" name="NFC">
<runs />
</js-module>

<config-file parent="/*" target="config.xml">
<feature name="NfcPlugin">
<param name="ios-package" value="NfcPlugin" />
Expand Down Expand Up @@ -143,5 +126,4 @@
</config-file>
</platform>


</plugin>
168 changes: 11 additions & 157 deletions src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.net.Uri;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.Ndef;
Expand All @@ -35,7 +33,7 @@
import android.os.Parcelable;
import android.util.Log;

public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCompleteCallback {
public class NfcPlugin extends CordovaPlugin {
private static final String REGISTER_MIME_TYPE = "registerMimeType";
private static final String REMOVE_MIME_TYPE = "removeMimeType";
private static final String REGISTER_NDEF = "registerNdef";
Expand All @@ -46,10 +44,6 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
private static final String WRITE_TAG = "writeTag";
private static final String MAKE_READ_ONLY = "makeReadOnly";
private static final String ERASE_TAG = "eraseTag";
private static final String SHARE_TAG = "shareTag";
private static final String UNSHARE_TAG = "unshareTag";
private static final String HANDOVER = "handover"; // Android Beam
private static final String STOP_HANDOVER = "stopHandover";
private static final String ENABLED = "enabled";
private static final String INIT = "init";
private static final String SHOW_SETTINGS = "showSettings";
Expand All @@ -74,21 +68,17 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
private static final String STATUS_NFC_OK = "NFC_OK";
private static final String STATUS_NO_NFC = "NO_NFC";
private static final String STATUS_NFC_DISABLED = "NFC_DISABLED";
private static final String STATUS_NDEF_PUSH_DISABLED = "NDEF_PUSH_DISABLED";

private static final String TAG = "NfcPlugin";
private final List<IntentFilter> intentFilters = new ArrayList<>();
private final ArrayList<String[]> techLists = new ArrayList<>();

private NdefMessage p2pMessage = null;
private PendingIntent pendingIntent = null;

private Intent savedIntent = null;

private CallbackContext readerModeCallback;
private CallbackContext channelCallback;
private CallbackContext shareTagCallback;
private CallbackContext handoverCallback;

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Expand Down Expand Up @@ -155,18 +145,6 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
} else if (action.equalsIgnoreCase(ERASE_TAG)) {
eraseTag(callbackContext);

} else if (action.equalsIgnoreCase(SHARE_TAG)) {
shareTag(data, callbackContext);

} else if (action.equalsIgnoreCase(UNSHARE_TAG)) {
unshareTag(callbackContext);

} else if (action.equalsIgnoreCase(HANDOVER)) {
handover(data, callbackContext);

} else if (action.equalsIgnoreCase(STOP_HANDOVER)) {
stopHandover(callbackContext);

} else if (action.equalsIgnoreCase(INIT)) {
init(callbackContext);

Expand Down Expand Up @@ -289,13 +267,6 @@ private void removeNdef(CallbackContext callbackContext) {
callbackContext.success();
}

private void unshareTag(CallbackContext callbackContext) {
p2pMessage = null;
stopNdefPush();
shareTagCallback = null;
callbackContext.success();
}

private void init(CallbackContext callbackContext) {
Log.d(TAG, "Enabling plugin " + getIntent());

Expand Down Expand Up @@ -438,43 +409,13 @@ private void makeReadOnly(final CallbackContext callbackContext) {
});
}

private void shareTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
this.p2pMessage = new NdefMessage(records);

startNdefPush(callbackContext);
}

// setBeamPushUris
// Every Uri you provide must have either scheme 'file' or scheme 'content'.
// Note that this takes priority over setNdefPush
//
// See http://developer.android.com/reference/android/nfc/NfcAdapter.html#setBeamPushUris(android.net.Uri[],%20android.app.Activity)
private void handover(JSONArray data, CallbackContext callbackContext) throws JSONException {

Uri[] uri = new Uri[data.length()];

for (int i = 0; i < data.length(); i++) {
uri[i] = Uri.parse(data.getString(i));
}

startNdefBeam(callbackContext, uri);
}

private void stopHandover(CallbackContext callbackContext) {
stopNdefBeam();
handoverCallback = null;
callbackContext.success();
}

private void showSettings(CallbackContext callbackContext) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
Intent intent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
getActivity().startActivity(intent);
} else {
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
getActivity().startActivity(intent);
}
Intent intent = new Intent(
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN ?
android.provider.Settings.ACTION_NFC_SETTINGS :
android.provider.Settings.ACTION_WIRELESS_SETTINGS
);
getActivity().startActivity(intent);
callbackContext.success();
}

Expand All @@ -483,7 +424,10 @@ private void createPendingIntent() {
Activity activity = getActivity();
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
pendingIntent = PendingIntent.getActivity(
activity, 0, intent,
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0
);
}
}

Expand Down Expand Up @@ -546,9 +490,6 @@ private void startNfc() {
nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), intentFilters, techLists);
}

if (p2pMessage != null) {
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
}
} catch (IllegalStateException e) {
// issue 110 - user exits app with home button while nfc is initializing
Log.w(TAG, "Illegal State Exception starting NFC. Assuming application is terminating.");
Expand All @@ -575,77 +516,6 @@ private void stopNfc() {
});
}

private void startNdefBeam(final CallbackContext callbackContext, final Uri[] uris) {
getActivity().runOnUiThread(() -> {

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());

if (nfcAdapter == null) {
callbackContext.error(STATUS_NO_NFC);
} else if (!nfcAdapter.isNdefPushEnabled()) {
callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
} else {
nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());
try {
nfcAdapter.setBeamPushUris(uris, getActivity());

PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
handoverCallback = callbackContext;
callbackContext.sendPluginResult(result);

} catch (IllegalArgumentException e) {
callbackContext.error(e.getMessage());
}
}
});
}

private void startNdefPush(final CallbackContext callbackContext) {
getActivity().runOnUiThread(() -> {

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());

if (nfcAdapter == null) {
callbackContext.error(STATUS_NO_NFC);
} else if (!nfcAdapter.isNdefPushEnabled()) {
callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
} else {
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());

PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
shareTagCallback = callbackContext;
callbackContext.sendPluginResult(result);
}
});
}

private void stopNdefPush() {
getActivity().runOnUiThread(() -> {

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());

if (nfcAdapter != null) {
nfcAdapter.setNdefPushMessage(null, getActivity());
}

});
}

private void stopNdefBeam() {
getActivity().runOnUiThread(() -> {

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());

if (nfcAdapter != null) {
nfcAdapter.setBeamPushUris(null, getActivity());
}

});
}

private void addToTechList(String[] techs) {
techLists.add(techs);
}
Expand Down Expand Up @@ -835,22 +705,6 @@ private void setIntent(Intent intent) {
getActivity().setIntent(intent);
}

@Override
public void onNdefPushComplete(NfcEvent event) {

// handover (beam) take precedence over share tag (ndef push)
if (handoverCallback != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, "Beamed Message to Peer");
result.setKeepCallback(true);
handoverCallback.sendPluginResult(result);
} else if (shareTagCallback != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, "Shared Message with Peer");
result.setKeepCallback(true);
shareTagCallback.sendPluginResult(result);
}

}

/**
* Enable I/O operations to the tag from this TagTechnology object.
* *
Expand Down
Loading