我正在使用 Android Beacon 库。自 Marshmallow 以来,我看到了以下错误,正如预期和记录的那样。
权限拒绝:需要 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限才能获取扫描结果
我的测距代码位于片段中,当片段显示在屏幕上时,会抛出 RuntimeException,正如预期的那样,因为我没有提示许可。如果我点击主页按钮,则会调用 Pause,并且我将解除绑定 beaconManager,并且不再像预期的那样再次抛出异常。从之前的应用程序列表返回到 Activity,每次扫描都会再次抛出 RuntimeException。
为什么我在AndroidManifest中添加了权限,在前台还是抛出异常?
根据文档 这里,如果您在后台执行扫描,只需要提示用户位置权限?
当您尝试在后台进行蓝牙扫描时,您会在 LogCat 中收到以下错误,并且不会检测到信标
这是否意味着ONLY后台扫描,还是我误解了文档,无论如何你都必须提示?如果可以避免的话,我想避免应用程序内出现额外的许可提示(可能会让紧张的用户感到厌烦)!!
我的清单包含 -
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
我从片段中截取的代码是 -
public class NearMeNowFragment extends Fragment implements BeaconConsumer {
//Beacon manager stuff
private BeaconManager beaconManager;
<SNIP>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
beaconManager = BeaconManager.getInstanceForApplication(getActivity());
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.near_me_now, container, false);
//Set up beacon stuff if Bluetooth available
if (verifyBluetooth(false)) {
beaconManager.bind(this);
}
<SNIP>
return rootView;
}
/***************************
Beacon config methods
****************************
*/
@Override
public void onBeaconServiceConnect() {
//update all scan periods
beaconManager.setForegroundScanPeriod(1100l);
beaconManager.setForegroundBetweenScanPeriod(8900l);
beaconManager.setAndroidLScanningDisabled(true);
try {
beaconManager.updateScanPeriods();
} catch (RemoteException e) {
e.printStackTrace();
}
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
<SNIP>
//handling beacons here
...
} else {
Log.i(TAG, "ZERO BEACONS IN SCAN");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region(TAG, null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int mode) {
return getActivity().bindService(intent, serviceConnection, mode);
}
@Override
public void onPause() {
super.onPause();
if(beaconManager.isBound(this)){
beaconManager.unbind(this);
}
}
@Override
public void onResume() {
super.onResume();
beaconManager.bind(this);
}
@Override
public void onDestroy() {
super.onDestroy();
if(beaconManager.isBound(this)){
beaconManager.unbind(this);
}
}
}
我的实际 Logcat 错误是
W/Binder:从活页夹存根实现中捕获了 RuntimeException。 java.lang.SecurityException:需要 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限才能获取扫描结果
澄清一下要求是不同的,具体取决于您是否在 build.gradle 文件中设置了
targetSdkVersion 23
。
如果您的目标是 SDK 23 (Marshmallow) 或更高版本:
如果您的目标是 SDK 22 或更低版本:
注意:您还可以使用仅需要蓝牙权限而不需要位置权限的备用蓝牙格式,如我的博客文章此处
中所述当我在 Marshmallow 设备上测试我的应用程序时,我遇到了同样的问题。为了避免提示用户许可,我只是将 targetSdkVersion 更改为 22。compileSdkVersion 可以保留在 23。更改 build.gradle 后,您可能需要运行 Tools > Android > Sync Project with Gradle Files。如果您使用 Eclipse,targetSdkVersion 位于清单文件中。