Android 6.0以后蓝牙扫描及连接不上解决方案

1.AndroidManifest.xml文件中添加两个权限:如下

 

   
  

 

2.在Activity中调用 requestPermissions() 方法来请求权限,系统会弹出需要请求权限的对话框

 

3.重写Activity的onRequestPermissionsResult()方法,接收权限是否请求的请求状态


全部示例代码如下:

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;  
  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super(savedInstanceState);


    setContextView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
        // Android M Permission check  
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {  
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},         
            PERMISSION_REQUEST_COARSE_LOCATION);  
        }  
    }  
}  
  
@Override  
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {  
    switch (requestCode) {  
        case PERMISSION_REQUEST_COARSE_LOCATION:  
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
                // TODO request success  
            }  
        break;  
    }  
}  

 

 

你可能感兴趣的:(Android)