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

[WIP]Initial runtime permissions for android #111

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
16 changes: 10 additions & 6 deletions examples/beacon-finder/www/js/screen-monitor-regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 10 additions & 6 deletions examples/beacon-finder/www/js/screen-range-beacons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="com.estimote.sdk.service.BeaconService" android:exported="false"/>
Expand Down
63 changes: 56 additions & 7 deletions plugin/src/android/EstimoteBeacons.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -218,8 +229,7 @@ public void sendResultForBluetoothEnabled(CallbackContext callbackContext)
{
if (mBeaconManager.isBluetoothEnabled()) {
callbackContext.success(1);
}
else {
} else {
callbackContext.success(0);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -709,15 +719,15 @@ 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);
}

if (minor == 0) {
callbackContext.error("minor cannot be 0");
return;
return;
}

BeaconConnection.WriteCallback writeCallback;
Expand All @@ -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;
}
}
}

/**
Expand Down