1. 程式人生 > >android6.0以上裝置連線不上藍芽問題

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); }

<!-- 藍芽分享所需的許可權 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!--6.0版本以上的藍芽許可權需要模糊定位許可權,僅作用於6.0+-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />