问题
Here is sample code that I'm using to detect enter region events:
public class BeaconApplication extends android.app.Application implements BootstrapNotifier {
private static final String TAG = "TAGTAG";
@Override public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
new BackgroundPowerSaver(this);
new RegionBootstrap(this, new Region(getPackageName(), null, null, null));
}
@Override public void didDetermineStateForRegion(int arg0, Region arg1) {
Log.d(TAG, "didDetermineStateForRegion");
}
@Override public void didEnterRegion(Region arg0) {
Log.d(TAG, "didEnterRegion");
}
@Override public void didExitRegion(Region arg0) {
Log.d(TAG, "didExitRegion");
}
}
The problem in that that if I'm using the following build.gradle configuration then everything works like expected
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig {
applicationId "com.test"
minSdkVersion 18
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
LogCat:
D/TAGTAG: App started up
D/TAGTAG: didDetermineStateForRegion
D/TAGTAG: didEnterRegion
But if I'm changing compileSdkVersion
to the actual version then nothing work
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.test"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
LogCat:
D/TAGTAG: App started up
回答1:
Starting with Android 6.0 Marshmallow, apps that scan for Bluetooth LE devices (including beacons) must obtain dynamic location permissions before they are allowed to do so. For legacy purposes, apps running on that target older Android SDKs (before API 23) are still allowed to scan for Bluetooth LE devices, but only in the foreground. This is the reason your app works if you target SDK 21, but not SDK 23.
To fix this when targeting SDK 23, you simply need to add the dynamic permissions request.
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Android M Permission check
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override?
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);?
}
});
builder.show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
}
});
builder.show();
}
return;
}
}
}
Detailed instructions on how to do this are available here: https://altbeacon.github.io/android-beacon-library/requesting_permission.html
I wrote a blog post about this topic here: http://developer.radiusnetworks.com/2015/09/29/is-your-beacon-app-ready-for-android-6.html
EDIT: As @Near1999 noted in a comment below, some Android 5+ builds also will not detect BLE devices unless Location Services are turned on in settings. Apparently, this restriction also only applies if targeting SDK 23+. See here for more info: https://github.com/AltBeacon/android-beacon-library/issues/301
来源:https://stackoverflow.com/questions/35988149/altbeacon-library-background-service