Runtime request for bluetooth scan permission - Android Bluetooth

Android examples for Bluetooth:Bluetooth Bond

Description

Runtime request for bluetooth scan permission

Demo Code


import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

public class Main{
    /**/*from   w  w w.j  a  v a  2 s  .  co  m*/
     * Runtime request for ACCESS_FINE_LOCATION. This is required on Android 6.0 and higher in order
     * to perform BLE scans.
     *
     * @param requestingActivity The activity requesting the permission.
     * @param requestCode        The request code used to deliver the user feedback to the calling
     *                           activity.
     */
    public static void requestScanningPermission(
            @NonNull final Activity requestingActivity,
            final int requestCode) {
        final String permission = Manifest.permission.ACCESS_FINE_LOCATION;
        if (ContextCompat.checkSelfPermission(requestingActivity,
                permission) != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(
                    requestingActivity, permission)) {
                ActivityCompat.requestPermissions(requestingActivity,
                        new String[] { permission }, requestCode);
            }
        }
    }
}

Related Tutorials