【发布时间】:2015-10-15 13:25:52
【问题描述】:
我正在使用 Android 信标库。从 Marshmallow 开始,我看到了以下错误,正如预期和记录的那样。
权限拒绝:需要 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限才能获取扫描结果
我在 Fragment 中有测距代码,当 Fragment 出现在屏幕上时,会引发 RuntimeException,正如预期的那样,因为我没有提示许可。如果我点击主页按钮,则会调用暂停,并且我正在解除对 beaconManager 的绑定,并且不再像预期的那样再次抛出异常。从之前的应用列表返回到 Activity,每次扫描都会再次抛出 RuntimeException。
为什么我在AndroidManifest中添加了权限,但在前台还是抛出异常?
根据文档 HERE,后台扫描只需要提示用户位置权限吗?
当您尝试在后台进行蓝牙扫描时,您会在 LogCat 中收到以下错误,并且不会检测到任何信标
这是否意味着仅后台扫描,还是我误解了文档,无论如何您都必须提示?如果可以避免的话,我想避免在应用程序内出现额外的(可能会让紧张的用户感到反感)提示!
我的清单包含 -
<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 权限才能获取扫描结果
【问题讨论】:
标签: android android-6.0-marshmallow ibeacon-android altbeacon