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

checkStatus command #3

Open
wants to merge 2 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The AID for your application must be passed as a variable when installing the pl
- [hce.registerCommandCallback](hceregistercommandcallback)
- [hce.sendResponse](hcesendresponse)
- [hce.registerDeactivatedCallback](hceregisterdeactivatedcallback)
- [hce.checkStatus](checkStatus)

## hce.registerCommandCallback

Expand Down Expand Up @@ -96,6 +97,20 @@ See [HostApduService.onDeactivated](http://developer.android.com/reference/andro
console.log("Deactivated. Reason code = " + reason);
}

## hce.checkStatus

Check if HCE is available and enabled.

hce.checkStatus(result);

#### Parameters
- __success__: Result callback function that is with result of the check
- __failure__: Error callback function, invoked when error occurs. [optional]


#### Description
Function `checkStatus` allows your JavaScript code to check if HCE is available on the device and if NFC is turned ON.

# HCE Util

> The hce.util object provides utility function for APDU operations.
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-feature android:name="android.hardware.nfc.hce" android:required="true" />
<uses-feature android:name="android.hardware.nfc.hce" android:required="false" />
<uses-permission android:name="android.permission.NFC" />

</config-file>
Expand Down
15 changes: 15 additions & 0 deletions src/android/CordovaApduService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.megster.cordova.hce;

import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -16,6 +19,11 @@ public class CordovaApduService extends HostApduService {
private static HCEPlugin hcePlugin;
private static CordovaApduService cordovaApduService;

public CordovaApduService() {
Log.i(TAG,"starting CordovaApduService");
cordovaApduService = this;
}

static void setHCEPlugin(HCEPlugin _hcePlugin) {
hcePlugin = _hcePlugin;
}
Expand Down Expand Up @@ -83,6 +91,13 @@ public void onDeactivated(int reason) {

}

static HCEPlugin.Status checkStatus() {
if (cordovaApduService != null) {

}
return HCEPlugin.Status.NOT_AVAILABLE;
}

/**
* Utility method to convert a byte array to a hexadecimal string.
*
Expand Down
30 changes: 29 additions & 1 deletion src/android/HCEPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.megster.cordova.hce;

import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.util.Log;

import org.apache.cordova.CallbackContext;
Expand All @@ -18,11 +20,14 @@ public class HCEPlugin extends CordovaPlugin {
private static final String REGISTER_COMMAND_CALLBACK = "registerCommandCallback";
private static final String SEND_RESPONSE = "sendResponse";
private static final String REGISTER_DEACTIVATED_CALLBACK = "registerDeactivatedCallback";
private static final String REGISTER_CHECK_STATUS = "checkStatus";
private static final String TAG = "HCEPlugin";

private CallbackContext onCommandCallback;
private CallbackContext onDeactivatedCallback;



@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

Expand Down Expand Up @@ -58,6 +63,23 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);

} else if (action.equalsIgnoreCase(REGISTER_CHECK_STATUS)) {
Log.d(TAG, "REGISTER_CHECK_STATUS 1");

PackageManager pm = this.cordova.getActivity().getPackageManager();
Status st = Status.NOT_AVAILABLE;
if ( pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION) ) {
st = Status.OFF;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this.cordova.getActivity());
if (nfcAdapter.isEnabled()) {
st = Status.OK;
}
}
PluginResult result = new PluginResult(PluginResult.Status.OK, st.toString());
Log.d(TAG, "REGISTER_CHECK_STATUS 2");
callbackContext.sendPluginResult(result);
Log.d(TAG, "REGISTER_CHECK_STATUS 3");

} else {

return false;
Expand All @@ -84,4 +106,10 @@ public void sendCommand(byte[] command) {
onCommandCallback.sendPluginResult(result);
}
}
}

public enum Status {
NOT_AVAILABLE,
OK,
OFF
}
}
6 changes: 6 additions & 0 deletions www/hce.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ module.exports = {
// http://developer.android.com/reference/android/nfc/cardemulation/HostApduService.html#onDeactivated(int)
registerDeactivatedCallback: function(success, failure) {
cordova.exec(success, failure, 'HCE', 'registerDeactivatedCallback', []);
},

// Check HCE status
// The result callback will be called with parameter value one of: NOT_AVAILABLE, OK, OFF
checkStatus: function (result, failure) {
cordova.exec(result, failure, 'HCE', 'checkStatus', []);
}

};