Skip to content

Commit

Permalink
Modernized the Android permission code.
Browse files Browse the repository at this point in the history
This change fixes a build error which looks like this:
Could not resolve all dependencies for configuration ':_debugCompile'.
Could not find any version that matches com.android.support:support-v4
  • Loading branch information
fredrikeldh committed Jun 16, 2016
1 parent f299d84 commit 1b664ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-ble",
"version": "1.3.1",
"version": "1.3.2",
"description": "Bluetooth Low Energy Cordova plugin",
"cordova": {
"id": "cordova-plugin-ble",
Expand Down
5 changes: 2 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest"
id="cordova-plugin-ble"
version="1.3.1">
version="1.3.2">

<engines>
<engine name="cordova" version=">=6.0.0" />
Expand All @@ -28,9 +28,8 @@
<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_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</config-file>
<framework src="com.android.support:support-v4:+" />
<source-file src="src/android/BLE.java" target-dir="src/com/evothings" />
</platform>

Expand Down
38 changes: 11 additions & 27 deletions src/android/BLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@
import android.util.Base64;
import android.os.ParcelUuid;
import android.util.Log;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback;
import android.content.pm.PackageManager;
import android.os.Build;
import android.Manifest;

public class BLE
extends CordovaPlugin
implements
LeScanCallback,
OnRequestPermissionsResultCallback
LeScanCallback
{
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

Expand Down Expand Up @@ -233,12 +229,10 @@ private void keepCallback(final CallbackContext callbackContext, byte[] message)
}
}

// Callback from ActivityCompat.requestPermissions().
// Callback from cordova.requestPermissions().
@Override
public void onRequestPermissionsResult(
int requestCode,
String permissions[],
int[] grantResults)
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException
{
//Log.i("@@@@@@@@", "onRequestPermissionsResult: " + permissions + " " + grantResults);

Expand Down Expand Up @@ -267,25 +261,15 @@ private void startScan(final CordovaArgs args, final CallbackContext callbackCon
mScanCallbackContext = callbackContext;
mScanArgs = args;

//Log.i("@@@@@@@@", "Checking permission platform: " + Build.VERSION.SDK_INT + " >= " + Build.VERSION_CODES.M);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
// Cordova Permission check
if (!cordova.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION))
{
//Log.i("@@@@@@@@", "Checking permission");

// Android M Permission check
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED)
{
//Log.i("@@@@@@@@", "PERMISSION NEEDED");
//Log.i("@@@@@@@@", "PERMISSION NEEDED");

// Permission needed. Ask user.
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
PERMISSION_REQUEST_COARSE_LOCATION);
return;
}
// Permission needed. Ask user.
cordova.requestPermission(this, PERMISSION_REQUEST_COARSE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION);
return;
}

// Permission ok, go ahead and start scanning.
Expand Down

0 comments on commit 1b664ca

Please sign in to comment.