Method of opening location permission for Android 6.0 scanning devices that cannot be scanned by Ble

  • 2021-09-24 23:43:51
  • OfStack

Previously, Ble development was tested in the version below Android 6.0 system. Today, when testing devices with Android 6.0, it was found that the surrounding Ble devices could not be scanned. Later, it was discovered that 6.0 needed to request location permission during the application running, and also needed to open the location.

Dynamic Application for Location Permission ACCESS_COARSE_LOCATION

Add in the AndroidManifest. xml file


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Then apply during the running of the program


private static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 1;
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// If  API level  Is greater than or equal to  23(Android 6.0)  Hour 
 // Determine whether you have permission 
 if (ContextCompat.checkSelfPermission(this,
   Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  // Determine whether you need to explain to the user why you need to apply for the permission 
  if (ActivityCompat.shouldShowRequestPermissionRationale(this,
    Manifest.permission.ACCESS_COARSE_LOCATION)) {
   showToast(" Since Android 6.0 You need to open the location permission to search for it at first Ble Equipment ");
  }
  // Request permission 
  ActivityCompat.requestPermissions(this,
    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
    REQUEST_CODE_ACCESS_COARSE_LOCATION);
 }
}

After executing the above request permission, the system will pop up a prompt box for the user to choose whether to allow the permission to be changed. The result of the selection can be known in the back interface:


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 if (requestCode == REQUEST_CODE_ACCESS_COARSE_LOCATION) {
  if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
   // Users are allowed to change permissions, 0 Indicates permission, -1 Express rejection  PERMISSION_GRANTED = 0 ,  PERMISSION_DENIED = -1
   //permission was granted, yay! Do the contacts-related task you need to do.
   // Here, the processing where authorization is allowed is carried out 
  } else {
   //permission denied, boo! Disable the functionality that depends on this permission.
   // Permission is denied here 
  }
 } else {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 }
}

The above is how to apply for location permission during the application running. After finishing the above, you thought it was OK, but you may find that there is no egg use, and you still can't search the surrounding Ble devices, because it is possible that your location service (positioning GPS) is not turned on.

Open Positioning (Location)

First, check whether positioning is turned on. You can do this as follows:


/**
 * Location service if enable
 *
 * @param context
 * @return location is enable if return true, otherwise disable.
 */
public static final boolean isLocationEnable(Context context) {
 LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
 boolean networkProvider = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 boolean gpsProvider = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 if (networkProvider || gpsProvider) return true;
 return false;
}

If the location has been turned on, OK is very good, and ble devices can be searched; If the location is not turned on, the user is required to turn it on, as follows:


private static final int REQUEST_CODE_LOCATION_SETTINGS = 2;
...
private void setLocationService() {
 Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
 this.startActivityForResult(locationIntent, REQUEST_CODE_LOCATION_SETTINGS);
}

Enter the positioning setting interface, and let the user choose whether to open the positioning or not. The result of the selection is obtained:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == REQUEST_CODE_LOCATION_SETTINGS) {
  if (isLocationEnable(this)) {
   // Locate open processes 
  } else {
   // Locate the process that is still not open 
  }
 } else super.onActivityResult(requestCode, resultCode, data);
}

OK, 6.0 system is better for users' privacy protection, but it is troublesome for developers, but what can be done?


Related articles: