From 93c40f65cce25c2c069e671d5d73651defb0ba0e Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 30 Mar 2016 22:32:47 +0200 Subject: [PATCH 1/2] Initial runtime permissions for android --- .../www/js/screen-monitor-regions.js | 16 +++--- .../www/js/screen-range-beacons.js | 16 +++--- plugin.xml | 1 + plugin/src/android/EstimoteBeacons.java | 49 ++++++++++++++++++- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/examples/beacon-finder/www/js/screen-monitor-regions.js b/examples/beacon-finder/www/js/screen-monitor-regions.js index 6002d5e..af52bca 100644 --- a/examples/beacon-finder/www/js/screen-monitor-regions.js +++ b/examples/beacon-finder/www/js/screen-monitor-regions.js @@ -41,13 +41,17 @@ $('#id-screen-monitor-regions .style-item-list').empty(); // Request authorisation. - estimote.beacons.requestAlwaysAuthorization(); + estimote.beacons.requestAlwaysAuthorization(function(){ + // Start monitoring. + estimote.beacons.startMonitoringForRegion( + {}, // Empty region matches all beacons. + onMonitor, + onError); + }, function(e){ + alert(e); + }); + - // Start monitoring. - estimote.beacons.startMonitoringForRegion( - {}, // Empty region matches all beacons. - onMonitor, - onError); }; app.stopMonitoringRegions = function() diff --git a/examples/beacon-finder/www/js/screen-range-beacons.js b/examples/beacon-finder/www/js/screen-range-beacons.js index 97f16d2..e3c6df3 100644 --- a/examples/beacon-finder/www/js/screen-range-beacons.js +++ b/examples/beacon-finder/www/js/screen-range-beacons.js @@ -56,13 +56,17 @@ $('#id-screen-range-beacons .style-item-list').empty(); // Request authorisation. - estimote.beacons.requestAlwaysAuthorization(); + estimote.beacons.requestAlwaysAuthorization(function(){ + // Start ranging. + estimote.beacons.startRangingBeaconsInRegion( + {}, // Empty region matches all beacons. + onRange, + onError); + }, function(e){ + alert(e); // An error occured, like permission denied + }); + - // Start ranging. - estimote.beacons.startRangingBeaconsInRegion( - {}, // Empty region matches all beacons. - onRange, - onError); }; app.stopRangingBeacons = function() diff --git a/plugin.xml b/plugin.xml index 9aef841..2b3c161 100644 --- a/plugin.xml +++ b/plugin.xml @@ -22,6 +22,7 @@ + diff --git a/plugin/src/android/EstimoteBeacons.java b/plugin/src/android/EstimoteBeacons.java index 116faef..93bee09 100644 --- a/plugin/src/android/EstimoteBeacons.java +++ b/plugin/src/android/EstimoteBeacons.java @@ -8,9 +8,11 @@ import android.content.Context; import android.util.Log; +import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.content.Intent; +import android.content.pm.PackageManager; import com.estimote.sdk.*; import com.estimote.sdk.cloud.model.*; @@ -40,7 +42,9 @@ public class EstimoteBeacons extends CordovaPlugin private static final String LOGTAG = "EstimoteBeacons"; private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D"; private static final String ESTIMOTE_SAMPLE_REGION_ID = "EstimoteSampleRegion"; + private static final String LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION; private static final int REQUEST_ENABLE_BLUETOOTH = 1; + private static final int REQUEST_COARSE_LOCATION = 2; private BeaconManager mBeaconManager; private EstimoteSDK mEstimoteSDK; @@ -60,6 +64,7 @@ public class EstimoteBeacons extends CordovaPlugin private CallbackContext mBluetoothStateCallbackContext; private CallbackContext mBeaconConnectionCallback; private CallbackContext mBeaconDisconnectionCallback; + private CallbackContext mLocationStateCallbackContext; // todo: consider using pluginInitialize instead, per Cordova recommendation // https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java#L60-L61 @@ -163,8 +168,11 @@ else if ("beacons_writeConnectedMajor".equals(action)) { else if ("beacons_writeConnectedMinor".equals(action)) { writeConnectedMinor(args, callbackContext); } + else if ("beacons_requestAlwaysAuthorization".equals(action)) { + requestAlwaysAuthorization(args, callbackContext); + } else if ("bluetooth_bluetoothState".equals(action)) { - checkBluetoothState(args, callbackContext); + checkBluetoothState(args, callbackContext); } else { return false; @@ -738,6 +746,45 @@ public void onError(EstimoteDeviceException e) { } } + /** + * Helper method. + */ + private void requestAlwaysAuthorization( + CordovaArgs cordovaArgs, + CallbackContext callbackContext) + { + Log.i(LOGTAG, "requestAlwaysAuthorization"); + + if (null != mLocationStateCallbackContext) { + callbackContext.error("Location permission request already in progress"); + return; + } + + if(cordova.hasPermission(LOCATION)) { + callbackContext.success(); + } else { + mLocationStateCallbackContext = callbackContext; + cordova.requestPermission(this, REQUEST_COARSE_LOCATION, LOCATION); + } + } + + @Override + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) throws JSONException { + switch (requestCode) { + case REQUEST_COARSE_LOCATION: { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + mLocationStateCallbackContext.success(); + } else { + mLocationStateCallbackContext.error("Permission denied"); + } + mLocationStateCallbackContext = null; + return; + } + } + } + /** * Create JSON object representing beacon info. * From 79157c546ddb8d7a53431bdce7ca1dbf746e6d6e Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 31 Mar 2016 10:07:35 +0200 Subject: [PATCH 2/2] add requestWhenInUseAuthorization --- plugin/src/android/EstimoteBeacons.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plugin/src/android/EstimoteBeacons.java b/plugin/src/android/EstimoteBeacons.java index 93bee09..8895c5c 100644 --- a/plugin/src/android/EstimoteBeacons.java +++ b/plugin/src/android/EstimoteBeacons.java @@ -169,7 +169,10 @@ else if ("beacons_writeConnectedMinor".equals(action)) { writeConnectedMinor(args, callbackContext); } else if ("beacons_requestAlwaysAuthorization".equals(action)) { - requestAlwaysAuthorization(args, callbackContext); + requestAuthorization(args, callbackContext); + } + else if ("beacons_requestWhenInUseAuthorization".equals(action)) { + requestAuthorization(args, callbackContext); } else if ("bluetooth_bluetoothState".equals(action)) { checkBluetoothState(args, callbackContext); @@ -226,8 +229,7 @@ public void sendResultForBluetoothEnabled(CallbackContext callbackContext) { if (mBeaconManager.isBluetoothEnabled()) { callbackContext.success(1); - } - else { + } else { callbackContext.success(0); } } @@ -632,7 +634,7 @@ private void writeConnectedProximityUUID( Log.i(LOGTAG, mConnectedBeacon.getBeacon().getProximityUUID()); Log.i(LOGTAG, String.valueOf(uuid.equals(mConnectedBeacon.getBeacon().getProximityUUID()))); - // already correct, skip + // already correct, skip if (uuid.equals(mConnectedBeacon.getBeacon().getProximityUUID())) { PluginResult r = new PluginResult(PluginResult.Status.OK); callbackContext.sendPluginResult(r); @@ -717,7 +719,7 @@ private void writeConnectedMinor( if (mConnectedBeacon != null && mConnectedBeacon.isConnected()) { int minor = cordovaArgs.getInt(0); - // already correct, skip + // already correct, skip if (minor == mConnectedBeacon.getBeacon().getMinor()) { PluginResult r = new PluginResult(PluginResult.Status.OK); callbackContext.sendPluginResult(r); @@ -725,7 +727,7 @@ private void writeConnectedMinor( if (minor == 0) { callbackContext.error("minor cannot be 0"); - return; + return; } BeaconConnection.WriteCallback writeCallback; @@ -743,15 +745,15 @@ public void onError(EstimoteDeviceException e) { }; mConnectedBeacon.writeMinor(minor, writeCallback); - } + } } /** * Helper method. */ - private void requestAlwaysAuthorization( + private void requestAuthorization( CordovaArgs cordovaArgs, - CallbackContext callbackContext) + CallbackContext callbackContext) { Log.i(LOGTAG, "requestAlwaysAuthorization");