diff --git a/README.md b/README.md index 4d0a79f..02af1d5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/plugin.xml b/plugin.xml index ade5786..5284df8 100644 --- a/plugin.xml +++ b/plugin.xml @@ -32,7 +32,7 @@ - + diff --git a/src/android/CordovaApduService.java b/src/android/CordovaApduService.java index 4d1b63e..9d7428f 100644 --- a/src/android/CordovaApduService.java +++ b/src/android/CordovaApduService.java @@ -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; @@ -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; } @@ -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. * diff --git a/src/android/HCEPlugin.java b/src/android/HCEPlugin.java index 17e91b4..74940a0 100644 --- a/src/android/HCEPlugin.java +++ b/src/android/HCEPlugin.java @@ -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; @@ -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 { @@ -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; @@ -84,4 +106,10 @@ public void sendCommand(byte[] command) { onCommandCallback.sendPluginResult(result); } } -} + + public enum Status { + NOT_AVAILABLE, + OK, + OFF + } +} \ No newline at end of file diff --git a/www/hce.js b/www/hce.js index f88c675..fa27452 100644 --- a/www/hce.js +++ b/www/hce.js @@ -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', []); } };