很多蓝牙设备搜到不到,怎么办

很多蓝牙设备搜到不到?

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();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值