Donner les autorisations pour l'accès au Bluetooth BLE avec MAUI

Added by Alexandre Journaux 5 months ago

Avec MAUI, il y a 2 étapes pour donner les autorisations d'accès au Bluetooth

1. Renseigner AndroidManifest.xml

Le fichier AndroidManifest.xml se trouve dans Platforms/Android

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:supportsRtl="true" android:icon="@mipmap/ae"></application>
    <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="34" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>

2. Tester et donner les autorisations dans MainActivity

Le fichier MainActivité.cs se trouve dans Platforms/Android

public class MainActivity : MauiAppCompatActivity
{

    private static String[] PERMISSIONS_STORAGE = {
        Manifest.Permission.ReadExternalStorage,
        Manifest.Permission.WriteExternalStorage,
        Manifest.Permission.AccessFineLocation,
        Manifest.Permission.AccessCoarseLocation,
        Manifest.Permission.AccessLocationExtraCommands,
        Manifest.Permission.BluetoothScan,
        Manifest.Permission.BluetoothConnect,
        Manifest.Permission.BluetoothPrivileged
};

    private static String[] PERMISSIONS_BLUETOOTH = {
        Manifest.Permission.AccessFineLocation,
        Manifest.Permission.AccessCoarseLocation,
        Manifest.Permission.AccessLocationExtraCommands,
        Manifest.Permission.BluetoothScan,
        Manifest.Permission.BluetoothConnect,
        Manifest.Permission.BluetoothPrivileged
};

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Permission permission1 = ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage);
        Permission permission2 = ActivityCompat.CheckSelfPermission(this, Manifest.Permission.BluetoothScan);
        if (permission1 != Permission.Granted)
        {
            // We don't have permission so prompt the user
            ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGE, 1);
        } else if (permission2 != Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, PERMISSIONS_BLUETOOTH, 1);
        }
    }
}

Solution trouvée sur : https://stackoverflow.com/questions/70245463/java-lang-securityexception-need-android-permission-bluetooth-connect-permissio