Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the permissions required to use Bluetooth in an MvvmCross app


What are the permissions required to use Bluetooth in an MvvmCross app


To use Bluetooth in an MvvmCross app, particularly on Android, you need to declare several permissions in your app's `AndroidManifest.xml` file. The specific permissions required depend on the Android version your app targets and the type of Bluetooth functionality you need.

For Android 12 and Higher

If your app targets Android 12 (API level 31) or higher, you need to declare the following permissions:

- `BLUETOOTH_SCAN`: This permission is necessary if your app needs to scan for nearby Bluetooth devices, such as BLE peripherals.
- `BLUETOOTH_CONNECT`: This permission is required if your app communicates with already-paired Bluetooth devices.
- `BLUETOOTH_ADVERTISE`: This permission is needed if your app makes the current device discoverable to other Bluetooth devices.

These permissions are requested at runtime. Here's how you might declare them in your `AndroidManifest.xml`:

xml



For Android 11 and Lower

If your app targets Android 11 (API level 30) or lower, you need to declare the following permissions:

- `BLUETOOTH`: This permission is necessary for any Bluetooth communication.
- `BLUETOOTH_ADMIN`: This permission is required to discover local Bluetooth devices or manipulate Bluetooth settings.
- `ACCESS_FINE_LOCATION`: This permission is needed because Bluetooth scans could potentially be used to gather location information. This permission must be requested at runtime.

Here's how you might declare these permissions in your `AndroidManifest.xml`:

xml



Runtime Permission Handling

For Android 6.0 (API level 23) and higher, you must request runtime permissions for location and Bluetooth. You can use MvvmCross plugins or custom code to handle these runtime permissions.

MvvmCross Plugin for BLE

The MvvmCross.Plugin.BLE can simplify handling Bluetooth Low Energy (BLE) functionality across platforms. However, you still need to manually declare the necessary permissions in your `AndroidManifest.xml` file and handle runtime permissions as required by Android.

Example of Requesting Permissions

To request permissions at runtime, you can use code similar to the following in your Android-specific part of the MvvmCross app:

csharp
var permissions = new List();

if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
{
    permissions.Add(Manifest.Permission.BluetoothScan);
    permissions.Add(Manifest.Permission.BluetoothConnect);
    permissions.Add(Manifest.Permission.BluetoothAdvertise);
}
else
{
    permissions.Add(Manifest.Permission.AccessFineLocation);
}

var requestPermissions = permissions.ToArray();
MainActivity.Instance.RequestPermissions(requestPermissions, MainActivity.RequestCode);

This code checks the Android version and requests the appropriate permissions.

Conclusion

In summary, using Bluetooth in an MvvmCross app on Android requires declaring the necessary permissions in your `AndroidManifest.xml` and handling runtime permissions based on the Android version. For Android 12 and higher, use `BLUETOOTH_SCAN`, `BLUETOOTH_CONNECT`, and `BLUETOOTH_ADVERTISE`. For Android 11 and lower, use `BLUETOOTH`, `BLUETOOTH_ADMIN`, and `ACCESS_FINE_LOCATION`. Always ensure you request these permissions at runtime as required by Android's security model.

Citations:
[1] https://punchthrough.com/mastering-permissions-for-bluetooth-low-energy-android/
[2] https://www.youtube.com/watch?v=Ynuyxtg0R5c
[3] https://community.appinventor.mit.edu/t/need-help-in-bluetooth-permission-for-an-app-on-android-12/76963
[4] https://learn.microsoft.com/en-us/answers/questions/957449/bluetooth-dependency-injection
[5] https://github.com/dotnet/maui/issues/12264
[6] https://stackoverflow.com/questions/58102526/how-to-implement-permission-request-in-xamarin-mvvmcross
[7] https://www.youtube.com/watch?v=9GljgwfpiiE
[8] https://learn.microsoft.com/en-us/answers/questions/994871/i-need-help-with-android-permission-bluetooth-scan
[9] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/
[10] https://developer.android.com/develop/connectivity/bluetooth/bt-permissions