android6.0以上设备连接不上蓝牙问题

真tm恶心! 6.0以后想打开蓝牙,必先打开位置!


//启动权限检测
mCheckPermissionsPresenter = new CheckPermissionsPresenterImp(this);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    mCheckPermissionsPresenter.checkPermissions(this,
            Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE);
}else{
    mCheckPermissionsPresenter.checkPermissions(this,
            Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION);
}


@Override
public void onCheckPermissionsSuccess(){
    if (Build.VERSION.SDK_INT >= 23 ) {
        if(!isLocationOpen(getApplicationContext())){
            Intent enableLocate = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(enableLocate, REQUEST_LOCATION_PERMISSION);
            return;
        }
    }


/** *判断位置信息是否开启 */
public static boolean isLocationOpen(final Context context){
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    //gps定位
    boolean isGpsProvider = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    //网络定位
    boolean isNetWorkProvider = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    return isGpsProvider|| isNetWorkProvider;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_LOCATION_PERMISSION) {
        if (!isLocationOpen(getApplicationContext())) {
            Toast.makeText(LauncherActivity.this, "安卓6.0系统要求:如果要使用蓝牙设备,必须打开位置!", Toast.LENGTH_SHORT).show();
        }

        //...
       
    }
    super.onActivityResult(requestCode, resultCode, data);
}


android:name="android.permission.BLUETOOTH" />
android:name="android.permission.BLUETOOTH_ADMIN" />

android:name="android.permission.ACCESS_COARSE_LOCATION" />

android:name="android.permission.ACCESS_FINE_LOCATION" />



你可能感兴趣的:(工作问题)