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..8895c5c 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,14 @@ else if ("beacons_writeConnectedMajor".equals(action)) { else if ("beacons_writeConnectedMinor".equals(action)) { writeConnectedMinor(args, callbackContext); } + else if ("beacons_requestAlwaysAuthorization".equals(action)) { + requestAuthorization(args, callbackContext); + } + else if ("beacons_requestWhenInUseAuthorization".equals(action)) { + requestAuthorization(args, callbackContext); + } else if ("bluetooth_bluetoothState".equals(action)) { - checkBluetoothState(args, callbackContext); + checkBluetoothState(args, callbackContext); } else { return false; @@ -218,8 +229,7 @@ public void sendResultForBluetoothEnabled(CallbackContext callbackContext) { if (mBeaconManager.isBluetoothEnabled()) { callbackContext.success(1); - } - else { + } else { callbackContext.success(0); } } @@ -624,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); @@ -709,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); @@ -717,7 +727,7 @@ private void writeConnectedMinor( if (minor == 0) { callbackContext.error("minor cannot be 0"); - return; + return; } BeaconConnection.WriteCallback writeCallback; @@ -735,7 +745,46 @@ public void onError(EstimoteDeviceException e) { }; mConnectedBeacon.writeMinor(minor, writeCallback); - } + } + } + + /** + * Helper method. + */ + private void requestAuthorization( + 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; + } + } } /**