Android ble掃描版本高於6.0後需要定位許可權
阿新 • • 發佈:2018-11-07
參考部落格:http://blog.csdn.net/sun6223508/article/details/50505936
p
private final int REQUEST_FINE_LOCATION = 1; private void mayRequestLocation() { //檢查系統版本,6.0開始需要動態申請許可權 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)) { //提示使用者需要獲取位置許可權 } //開始許可權申請 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_FINE_LOCATION); return; } } } /** * 許可權申請結果處理 * @param requestCode * @param permissions * @param grantResults */ @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) { //使用者已授權,開始掃描藍芽 }else{ //使用者未授權,提示使用者 } break; } } }