【问题标题】:Android need user explicitly give approval to turn on BluetoothAndroid 需要用户明确批准才能打开蓝牙
【发布时间】:2019-12-15 21:11:55
【问题描述】:

我的APP在做室内定位。我们的解决方案是连续扫描 BLE 设备。但是我们遇到了第一个问题。 2-3 小时后,蓝牙 API 无法获得任何扫描结果。因此,我们尝试关闭蓝牙并再次打开以解决此问题。它适用于运行 Android 6.0 及更低版本的设备。但是对于Android 7.0及以上,当我们尝试通过调用“BluetoothAdapter.enable()”打开蓝牙时,它总是要求用户对此给予明确的批准,这使得我的APP无法在不涉及用户的情况下在后台运行.

Android 的文档说蓝牙权限不是危险权限,不是吗?为什么需要用户参与才能对此给予批准?

【问题讨论】:

  • 您的文档是什么版本的 Android?有时您可能需要在蓝牙中启用 Fine_access_location 权限。

标签: android bluetooth


【解决方案1】:

您至少需要在运行时调用ACCES_COARSE_LOCATION。这个答案可能会对你有所帮助。

Requesting multiple Bluetooth permissions in Android Marshmallow

(我仍然没有足够的积分来添加cmets,所以我将其发布为答案)

【讨论】:

    【解决方案2】:

    在棉花糖设备之上,您需要在运行时获得用户的许可。

    public void onCheckingPermission() {
    
        if (ActivityCompat.checkSelfPermission(ProfileMakingActivity.this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED
        ) {
            //  initQRCodeReaderView();
            //  Utils.log("Camera AllPermission Granted . ");
            requestPermission();
    
            Log.i("permission", "Requesting  AllPermission");
    
    
        } else {
            Log.i("permission", "permission Granted");
            getVcardString();
    
            //   Toast.makeText(getApplicationContext(),"Permission Granted",Toast.LENGTH_SHORT).show();
    
    
        }
    
    } 
    
    
        private void requestPermission() {
    
    
        if (ActivityCompat.shouldShowRequestPermissionRationale(ProfileMakingActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
    
            Snackbar.make(this.findViewById(android.R.id.content), "All permission are required to use this app",
                    Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View view) {
                    requestPermissions(new String[]{Manifest.permission.BLUETOOTH);
                }
            }).show();
        } else {
            Snackbar.make(this.findViewById(android.R.id.content), "Requesting contacts permission. Required for this app.",
                    Snackbar.LENGTH_SHORT).show();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.BLUETOOTH);
            }
        }
    
    
    
        @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode != MY_PERMISSIONS_REQUEST) {
            return;
        }
    
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Snackbar.make(this.findViewById(android.R.id.content), "permission was granted. Now you can all contacts", Snackbar.LENGTH_SHORT).show();
            getVcardString();
    
        } else {
            Snackbar.make(this.findViewById(android.R.id.content), "Need Allow Permisson to access the accounts", Snackbar.LENGTH_SHORT)
                    .show();
    
        }
    }
    
    }
    

    只需调用 onCheckingPermission();在创建中

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多