安卓SDK的默认源码在Android 6.0 M上貌似有问题,经过修改,解决方法如下:
需要修改BeaconList.java
@@ -2,14 +2,19 @@ package com.aprilbrother.aprilbeacondemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import android.Manifest;
import android.app.Activity;
+import android.app.AlertDialog;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
+import android.content.DialogInterface;
import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
@@ -49,18 +54,61 @@ public class BeaconList extends Activity {
// “aa000000-0000-0000-0000-000000000000”,
// null, null);
private BeaconAdapter adapter;
private BeaconManager beaconManager;
private ArrayList myBeacons;
-
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); -
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(); -
} -
} init();}
-
@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; -
} -
} -
}
-
private void init() {
myBeacons = new ArrayList();
ListView lv = (ListView) findViewById(R.id.lv);
adapter = new BeaconAdapter(this);
lv.setAdapter(adapter);
原因和Android M新增的授权有关系,已测试通过。