Search code examples
xamarinbluetoothbluetooth-lowenergymvvmcrossxamarin.forms

How do I solve a null exception when I try to create an adapter in MvvmCross Bluetooth-LE (Xamarin Forms)?


var adapter = Mvx.Resolve<MvvmCross.Plugins.BLE.Bluetooth.LE.IAdapter>();
App.SetAdapter(adapter);

I'm trying to use the BLE Plugin for MvvMCross but it returns a NullReferenceException.


Solution

  • You have to install package (the plugin) in each project. The PCL, the Android and the iOS project. The Android and iOS packages create a bootstrap class which loads the plugin. Please ensure, that you also add the permissions to your AndroidManifest.

    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    

    We will add this to the documentation with the 1.0 release. Please ensure that you call Mvx.Resolve after LoadApplication(...). Before this call, the Plugin will not be loaded and you can't resolve IAdapter or any other MvvMCross interface.