Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any specific permissions needed for Bluetooth Low Energy (BLE) in MvvmCross


Are there any specific permissions needed for Bluetooth Low Energy (BLE) in MvvmCross


When developing a Bluetooth Low Energy (BLE) application using MvvmCross, specific permissions are required to ensure proper functionality across different platforms. Here's a detailed overview of the necessary permissions and configurations:

Android

For Android, you need to declare several permissions in your `AndroidManifest.xml` file. These include:

- Legacy Permissions: For versions older than Android 12 (API 31), you need to include:
- `android.permission.BLUETOOTH`
- `android.permission.BLUETOOTH_ADMIN`
- `android.permission.ACCESS_COARSE_LOCATION`
- `android.permission.ACCESS_FINE_LOCATION`

These permissions are typically declared with a `maxSdkVersion` attribute set to 30 to ensure they are only applied to versions older than Android 12.

- Android 12 and Later: For newer versions, you must request runtime permissions for:
- `android.permission.BLUETOOTH_SCAN`
- `android.permission.BLUETOOTH_CONNECT`
- Optionally, `android.permission.BLUETOOTH_ADVERTISE` if your app needs to advertise itself as a BLE device.

These permissions are essential for scanning and connecting to BLE devices.

To handle runtime permissions, you can use extension functions like those described in[3]. For example, you can check if your app has the required permissions using a method like `hasRequiredBluetoothPermissions()`.

iOS

On iOS, you need to add specific keys to your `Info.plist` file:

- UIBackgroundModes: Include `bluetooth-central` and `bluetooth-peripheral` to allow your app to run in the background when using BLE.
- NSBluetoothPeripheralUsageDescription and NSBluetoothAlwaysUsageDescription: Provide custom messages explaining why your app needs access to Bluetooth.

macOS

For macOS (version 11 and above), add the following key to your `Info.plist`:

- NSBluetoothAlwaysUsageDescription: Include a custom message explaining why your app needs Bluetooth access.

MvvmCross Plugin

The MvvmCross.Plugin.BLE provides a cross-platform way to access BLE functionality. It supports Android, iOS, macOS, and UWP. You can install it via NuGet using `Install-Package MvvmCross.Plugin.BLE`. This plugin simplifies the process of handling BLE operations across different platforms by providing a unified API.

To use the plugin effectively, ensure you have the necessary permissions declared in your project's configuration files (`AndroidManifest.xml` for Android and `Info.plist` for iOS and macOS). The plugin itself handles the underlying BLE operations, allowing you to focus on the application logic.

Usage Example

After setting up the necessary permissions and installing the MvvmCross.Plugin.BLE, you can use it in your MvvmCross application like this:

csharp
var ble = Mvx.Resolve();
var adapter = Mvx.Resolve();

This allows you to access BLE functionality through the `IBluetoothLE` and `IAdapter` interfaces, which are resolved by MvvmCross's dependency injection mechanism.

Citations:
[1] https://learn.microsoft.com/en-us/answers/questions/957449/bluetooth-dependency-injection
[2] https://github.com/xabre/xamarin-bluetooth-le/issues/524
[3] https://punchthrough.com/mastering-permissions-for-bluetooth-low-energy-android/
[4] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/
[5] https://learn.microsoft.com/en-us/answers/questions/1518375/which-package-should-i-utilize-for-bluetooth-low-e
[6] https://developer.android.com/develop/connectivity/bluetooth/bt-permissions
[7] https://github.com/mrozema/xamarin-bluetooth-le
[8] https://stackoverflow.com/questions/21315172/bluetooth-connectivity-using-mvvmcross
[9] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/2.2.0-pre5