If you prefer to implement your own permissions check, you can still use the DigiBLEManager to take advantage of some of the methods it provides to check interface status and validate permissions:

Method Description

GetBluetoothState()

Returns the current Bluetooth state.

IsBluetoothEnabled()

Returns whether the Bluetooth adapter is enabled or not.

IsGPSEnabled()

Returns whether the GPS is enabled or not.

RequestBluetoothPermissions()

Requests Bluetooth permissions. The method returns the permissions status result immediately if they were previously granted or denied by the user. Otherwise, it will request them to the user.

RequestLocationPermissions()

Requests location permissions. The method returns the permissions status result immediately if they were previously granted or denied by the user. Otherwise, it will request them to the user.

Location permissions were required in Android 11 and lower versions in order to work with the Bluetooth interface. For that reason, the library offers some methods to check the GPS and location permissions status.

The following code displays a possible implementation of the manual permissions check:

/// <summary>
/// Checks and returns whether Bluetooth permissions are granted or not.
/// </summary>
/// <returns><c>true</c> if permissions are granted, <c>false</c> otherwise.</returns>
public static void ValidateBluetoothStatus()
{
    // Instantiate the Digi Bluetooth Low Energy manager.
    DigiBLEManager bleManager = new DigiBLEManager();

    // Check if Bluetooth is enabled.
    var bluetoothEnabledTask = bleManager.IsBluetoothEnabled();
    bluetoothEnabledTask.Wait();
    if (!bluetoothEnabledTask.Result)
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            Shell.Current.DisplayAlert("Bluetooth disabled", "Bluetooth interface is disabled, please enable it", "OK");
        });
        return false;
    }

    if (DeviceInfo.Current.Platform == DevicePlatform.Android)
    {
        if (DeviceInfo.Version.Major <= 11)
        {
            // Check if the GPS is enabled.
            if (!bleManager.IsGPSEnabled())
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Shell.Current.DisplayAlert("GPS disabled", "GPS is disabled, please enable it", "OK");
                });
                return false;
            }
            // Check location permissions.
            var locationPermissionTask = bleManager.RequestLocationPermission();
            locationPermissionTask.Wait();
            if (!locationPermissionTask.Result)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Shell.Current.DisplayAlert("Permissions not granted". "Location permissions are required, please grant them", "OK");
                });
                return false;
            }
        }
        else if (DeviceInfo.Version.Major > 11)
        {
            // Check Bluetooth permissions.
            var bluetoothPermissionTask = bleManager.RequestBluetoothPermission();
            bluetoothPermissionTask.Wait();
            if (!bluetoothPermissionTask.Result)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Shell.Current.DisplayAlert("Permissions not granted". "Bluetooth permissions are required, please grant them", "OK");
                });
                return false;
            }
        }
    }
    else if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
    {
        // Check Bluetooth permissions.
        var bluetoothPermissionTask = bleManager.RequestBluetoothPermission();
        bluetoothPermissionTask.Wait();
        if (!bluetoothPermissionTask.Result)
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                Shell.Current.DisplayAlert("Permissions not granted". "Bluetooth permissions are required, please grant them", "OK");
            });
            return false;
        }
    }
    return true;
}