Using MvvmCross.Plugin.BLE without MvvmCross is technically possible but not straightforward. Here's a detailed explanation:
Overview of MvvmCross.Plugin.BLE
**MvvmCross.Plugin.BLE is designed to work within the MvvmCross framework, providing a cross-platform solution for accessing Bluetooth Low Energy (BLE) functionality. It registers `IBluetoothLE` and `IAdapter` as lazy-initialized singletons, which can be resolved using MvvmCross's dependency injection mechanism[1][3].
Using Without MvvmCross
If you want to use the BLE functionality without MvvmCross, you would need to manually manage the dependencies and initialization of the BLE components. This means you would have to:
1. Install the Plugin.BLE Package: Instead of using MvvmCross.Plugin.BLE, you can install the Plugin.BLE package, which is the underlying BLE implementation used by MvvmCross[2][3]. This package provides the core BLE functionality without the MvvmCross framework.
2. Manual Initialization: You would need to manually initialize and manage instances of `IBluetoothLE` and `IAdapter`. This involves creating instances of these interfaces or their concrete implementations and handling their lifecycles manually[8].
3. Dependency Management: Since MvvmCross handles dependency injection for you, without it, you would need to manually manage how these components are created and shared across your application.
Example Usage Without MvvmCross
Here's a basic example of how you might use Plugin.BLE without MvvmCross:
csharp
using Plugin.BLE;
public class MyBLEManager
{
private readonly IBluetoothLE _ble;
private readonly IAdapter _adapter;
public MyBLEManager()
{
_ble = CrossBluetoothLE.Current;
_adapter = CrossBluetoothLE.Current.Adapter;
}
// Use _ble and _adapter as needed
}
In this example, `CrossBluetoothLE.Current` is used to access the BLE functionality, similar to how MvvmCross resolves these services.
Conclusion
While it's possible to use the BLE functionality without MvvmCross by leveraging the Plugin.BLE package directly, you will need to handle the dependency management and initialization manually. This approach requires more effort compared to using the MvvmCross plugin, which provides a more streamlined integration with its dependency injection features.
Citations:[1] https://github.com/mrozema/xamarin-bluetooth-le
[2] https://learn.microsoft.com/en-us/answers/questions/1518375/which-package-should-i-utilize-for-bluetooth-low-e
[3] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/
[4] https://www.nuget.org/packages/MvvmCross.Plugin.BLE/2.2.0-pre4
[5] https://stackoverflow.com/questions/38327176/xamarin-forms-plugins-ble-mvvmcross-plugins-ble
[6] https://stackoverflow.com/questions/36522920
[7] https://learn.microsoft.com/en-us/answers/questions/285551/plugin-ble-(you-should-reference-the-nuget-package
[8] https://github.com/xabre/xamarin-bluetooth-le/issues/524