关闭

Android ble扫描版本高于6.0后需要定位权限

646人阅读 评论(0) 收藏 举报
分类:

参考博客:http://blog.csdn.net/sun6223508/article/details/50505936


private  final int REQUEST_FINE_LOCATION = 1;

private void mayRequestLocation() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int cllPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
        if (callPhonePermission != PackageManager.PERMISSION_GRANTED) {
          
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION))
                playToast(getString(R.string.ble_need));
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_FINE_LOCATION);
            return;
        }
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_FINE_LOCATION:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startScan();
            }
            break;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5211次
    • 积分:35
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:11篇
    • 译文:0篇
    • 评论:0条