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

Remove not supported Peer2Peer NFC communication for Android on SDK 34 support #495

Closed
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
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
= 1.2.0-j5.2 =
Remove peer to peer communication for Android since it is not supported in Android SDK 34 anymore

= 1.2.0 =
iOS allow tag read and write in same session #419

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.2.0-j5.2",
"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
2 changes: 1 addition & 1 deletion 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.2.0-j5.2">

<name>NFC</name>

Expand Down
183 changes: 94 additions & 89 deletions src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.TagTechnology;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
Expand Down Expand Up @@ -155,17 +156,17 @@ 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(SHARE_TAG)) {
// shareTag(data, callbackContext);

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

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

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

} else if (action.equalsIgnoreCase(INIT)) {
init(callbackContext);
Expand Down Expand Up @@ -289,12 +290,12 @@ private void removeNdef(CallbackContext callbackContext) {
callbackContext.success();
}

private void unshareTag(CallbackContext callbackContext) {
p2pMessage = null;
stopNdefPush();
shareTagCallback = null;
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,34 +439,34 @@ 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);
// private void shareTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
// NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
// this.p2pMessage = new NdefMessage(records);

startNdefPush(callbackContext);
}
// 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 {
// private void handover(JSONArray data, CallbackContext callbackContext) throws JSONException {

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

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

startNdefBeam(callbackContext, uri);
}
// startNdefBeam(callbackContext, uri);
// }

private void stopHandover(CallbackContext callbackContext) {
stopNdefBeam();
handoverCallback = null;
callbackContext.success();
}
// 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) {
Expand All @@ -483,7 +484,11 @@ 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);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
}

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

if (p2pMessage != null) {
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
}
// 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,76 +580,76 @@ private void stopNfc() {
});
}

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

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
// 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());
// 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);
// PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
// result.setKeepCallback(true);
// handoverCallback = callbackContext;
// callbackContext.sendPluginResult(result);

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

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

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
// 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());
// 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);
}
});
}
// PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
// result.setKeepCallback(true);
// shareTagCallback = callbackContext;
// callbackContext.sendPluginResult(result);
// }
// });
// }

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

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

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

});
}
// });
// }

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

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

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

});
}
// });
// }

private void addToTechList(String[] techs) {
techLists.add(techs);
Expand Down
16 changes: 8 additions & 8 deletions src/ios/NfcPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// (c) 2107-2020 Don Coleman

#import "NfcPlugin.h"
#import <CoreNFC/CoreNFC.h>

@interface NfcPlugin() {
NSString* sessionCallbackId;
Expand Down Expand Up @@ -124,14 +125,13 @@ - (void)writeTag:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(13.0)){
} else { // create a new session
if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");

self.nfcSession = [[NFCTagReaderSession new]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];
self.nfcSession = [[NFCTagReaderSession alloc] initWithPollingOption: (NFCPollingISO14443 | NFCPollingISO15693)
delegate: self
queue: dispatch_get_main_queue()];

} else {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
self.nfcSession = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
}
}

Expand Down Expand Up @@ -306,20 +306,20 @@ - (void)startScanSession:(CDVInvokedUrlCommand*)command {

if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCTagReaderSession new]
self.nfcSession = [[NFCTagReaderSession alloc]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];
} else {
NSLog(@"Using NFCNDEFReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
}
sessionCallbackId = [command.callbackId copy];
self.nfcSession.alertMessage = @"Hold near NFC tag to scan.";
[self.nfcSession beginSession];

} else if (@available(iOS 11.0, *)) {
NSLog(@"iOS < 13, using NFCNDEFReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
sessionCallbackId = [command.callbackId copy];
self.nfcSession.alertMessage = @"Hold near NFC tag to scan.";
[self.nfcSession beginSession];
Expand Down
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.chariotsolutions.nfc.plugin.tests",
"version": "0.0.1-dev",
"version": "1.2.0-j5.2",
"description": "Tests for phonegap-nfc",
"cordova": {
"id": "phonegap-nfc-tests",
Expand Down
4 changes: 2 additions & 2 deletions tests/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="phonegap-nfc-tests"
version="0.0.1-dev">
version="1.2.0-j5.2">
<name>PhoneGap NFC Plugin Tests</name>
<license>Apache 2.0</license>

<js-module src="tests.js" name="tests">
</js-module>
</plugin>
</plugin>