2016-04-22 117 views
2

我正在开发一个连接应用程序,该应用程序连接到带有编译的SDK 23的蓝牙设备。我在申请蓝牙多重权限时遇到问题。这是我迄今所做的:在Android中请求多个蓝牙权限棉花糖

@Override 
public void onStart() { 
    super.onStart(); 
    if (D) 
     Log.e(TAG, "++ ON START ++"); 


    if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this, 
      Manifest.permission.BLUETOOTH) 
      != PackageManager.PERMISSION_GRANTED) { 
    } else { 
     ActivityCompat.requestPermissions(MyBlueToothClientActivity.this, 
       new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN}, 
       REQUEST_ENABLE_BT); 
    } 


    if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this, 
      Manifest.permission.BLUETOOTH) 
      != PackageManager.PERMISSION_GRANTED) { 
    } else { 

     ActivityCompat.requestPermissions(MyBlueToothClientActivity.this, 
       new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN}, 
       REQUEST_CONNECT_DEVICE_INSECURE); 
    } 
} 


@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case REQUEST_ENABLE_BT: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

    // permission was granted, yay! 
       Intent enableIntent = new Intent(
         BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 


      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
       if (CommonData.mChatService == null) 
        setupChat(); 

       Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show(); 
      } 
      return; 
     } 

     case REQUEST_CONNECT_DEVICE_INSECURE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! 
       Intent enableIntent = new Intent(
         BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableIntent, REQUEST_CONNECT_DEVICE_INSECURE); 


      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
       if (CommonData.mChatService == null) 
        setupChat(); 

       Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show(); 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

虽然我能够得到的对话框请求启用蓝牙,我没有得到第二许可,即连接到设备。在logcat中,我得到:

01-01 06:41:24.334 25473-25473 E/BluetoothChat: ++ ON START ++ 
    01-01 06:41:24.344 25473-25473 W/Activity: Can reqeust only one set of permissions at a time 

而且由于我无法连接到设备,我只是在这里卡住了。而且这个代码在Android版本上运行良好,直到棒棒糖,只是导致棉花糖版本的问题。

回答

6

BLUETOOTHBLUETOOTH_ADMINnormal permissions,因此它们被自动授予。在运行时只需要请求table of dangerous permissions中的权限。

然而,作为中提到的Android 6.0 changes: Access to Hardware Identifier

访问通过蓝牙和Wi-Fi扫描附近的外部装置的硬件识别码,您的应用程序现在必须有ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION权限:

如果你使用任何的这些方法,你需要在运行时,要求至少ACCESS_COARSE_LOCATION(因为它一个危险的权限)。

+0

哦。那我不需要为他们请求吗?如果不是,我该如何处理它们的版本低于棉花糖?另外,如果我需要使用其他权限,如READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE,我该如何请求它们,因为我可能会在那里得到相同的错误? – Mirhawk

+0

我已经更新了我的答案,我认为这可能是您的问题(如果您的目标为6.0+,则需要蓝牙扫描的位置权限)。无论如何,你可以同时请求多个权限(和你一样),但你不能做的就是连续调用'requestPermissions()'多次。 你应该结合你的if语句,并且在你想请求的每个权限上调用'checkSelfPermissions()',如果它们中的任何一个都没有被授予,那么就请求它们全部(系统不会提示用户接受他们的权限)已经接受)。 – ianhanniballake