MvvmCross.Plugin.BLE is a plugin designed to manage Bluetooth Low Energy (BLE) connections across multiple platforms, including Android, iOS, macOS, and Windows. It is part of the MvvmCross framework, which is a cross-platform MVVM framework for .NET. Here's how it manages BLE device connections and disconnections:
Connection Management
1. Initialization: To start using the plugin, you need to initialize it by resolving the `IBluetoothLE` and `IAdapter` interfaces. These interfaces are registered as lazy initialized singletons in the MvvmCross framework, allowing them to be injected into your view models or services as needed[1][3].
csharp
var ble = Mvx.Resolve();
var adapter = Mvx.Resolve();
2. Device Scanning: The plugin provides methods to scan for nearby BLE devices. You can start scanning using the `StartScanningForDevicesAsync` method on the adapter. Devices discovered during scanning are typically added to a list for further interaction[2].
csharp
await adapter.StartScanningForDevicesAsync();
3. Device Connection: Once a device is discovered, you can connect to it using the `ConnectDeviceAsync` method. This method returns a task that completes when the connection is established or fails[1].
Disconnection Management
1. Intentional Disconnection: You can intentionally disconnect from a device using the `DisconnectDeviceAsync` method. This method is typically used when you want to end a connection explicitly[2].
2. Automatic Disconnection: BLE devices can disconnect automatically due to various reasons such as signal loss, device going out of range, or supervisor timeout expiration. In such cases, the plugin does not explicitly handle disconnections but relies on the underlying platform's BLE stack to notify the application of disconnection events[4].
3. Monitoring Disconnection Events: While the plugin itself does not provide specific events for disconnection detection, you can monitor the state of the Bluetooth adapter or device connection to detect when a disconnection occurs. For example, you can listen for changes in the Bluetooth state or implement custom logic to detect disconnections based on device availability[1][4].
Handling Disconnections
To handle disconnections effectively, you might need to implement custom logic to detect when a device is no longer connected and take appropriate actions, such as removing it from a list of connected devices or attempting to reconnect.
csharp
// Example of handling disconnections by monitoring device availability
adapter.DeviceDiscovered += (s, a) => {
myDeviceList.Add(a.Device);
};
// Custom logic to detect disconnections
// This might involve periodically checking if devices are still connected
// or listening for specific events indicating disconnection
In summary, MvvmCross.Plugin.BLE provides a framework for managing BLE connections but relies on custom implementation for handling disconnections and monitoring device availability.
Citations:[1] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/
[2] https://stackoverflow.com/q/47031151
[3] https://github.com/mrozema/xamarin-bluetooth-le
[4] https://www.argenox.com/blog/understanding-ble-disconnections/
[5] https://stackoverflow.com/questions/36522920
[6] https://stackoverflow.com/questions/36386382/making-ble-stop-disconnecting-after-30-sec-on-mvvmcross
[7] https://stackoverflow.com/questions/38327176/xamarin-forms-plugins-ble-mvvmcross-plugins-ble
[8] https://github.com/dotintent/react-native-ble-plx/issues/1197
[9] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/2.2.0-pre5
[10] https://learn.microsoft.com/en-us/answers/questions/1518375/which-package-should-i-utilize-for-bluetooth-low-e