很多蓝牙设备搜到不到?
1、蓝牙地理位置权限没打开
2、需要手动打开GPS,有的手机叫做定位,还有的手机叫位置信息(就是下拉状态栏上面的有一个地理位置相关的图标)
解决方案:
1、动态申请地理位置权限
private void initPermission() { RxPermissions rxPermission = new RxPermissions(BLEDeviceScanActivity.this); rxPermission .requestEach( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION) .subscribe(new Consumer<Permission>() { @Override public void accept(Permission permission) throws Exception { if (permission.granted) { // 用户已经同意该权限 L.e("Exception 用户已经同意该权限 *** " + permission.name + " is granted."); } else if (permission.shouldShowRequestPermissionRationale) { // 用户拒绝了该权限,没有选中『不再询问』(Never ask again), //那么下次再次启动时,还会提示请求权限的对话框,并再次申请索要权限 initPermission(); L.e("Exception 没有选中『不再询问』 *** " + permission.name) } else { // 用户拒绝了该权限,并且选中『不再询问』,则不会再提醒索要权限 L.e("Exception 不再询问 *** " + permission.name) } } }); }
2、判断是否手动打开定位权限,如若没有打开,再申请手动打开
/** * 判断定位服务是否开启 * * @param * @return true 表示开启 */ public static boolean isLocationEnabled(Context context) { int locationMode = 0; String locationProviders; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; } else { locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } }
//如果未开启定位服务,提示用户去开启 if (!isLocationEnabled(this)) { toOpenGPS(this); }
/** * 提示用户去开启定位服务 **/ public void toOpenGPS(Context context) { AlertDialog.Builder dialog = new AlertDialog.Builder(Util.getThemeContext(context)); dialog.setMessage(getString(R.string.location_none)); dialog.setPositiveButton(context.getString(R.string.to_start), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivity(intent); dialog.dismiss(); } }); dialog.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show(); }