title: Xamarin app crashes when disconnecting BLE device [DisconnectDevice]
body: <p>I have a Xamarin forms app that uses the <a href="https://github.com/xamarin/Monkey.Robotics/tree/master/Source/Platform%20Stacks" rel="nofollow">Monkey.Robotics.Core</a> to connect to a BLE device and read data from it.
  I have a xaml.cs page that discovers the devices (in the method StartUpJobs):</p>
  
  <pre><code>public partial class DeviceList : ContentPage
  {   
      IAdapter adapter;
      ObservableCollection&lt;IDevice&gt; devices;
  
      IAdapter serviceListadapter;
      IDevice serviceListdevice;
      ObservableCollection&lt;IService&gt; services;
  
      IAdapter adapterCharacteristics;
      IDevice deviceCharacteristics;
      IService serviceCharacteristics;
      ObservableCollection&lt;ICharacteristic&gt; characteristics;
  
      IDevice currentDevice;
  
      public DeviceList (IAdapter adapter)
      {
          InitializeComponent ();
  
          StartUpJobs(adapter);
          MessagingCenter.Subscribe&lt;CharacteristicDetail_TISensor&gt;(this, "DisconnectDeviceAndRefreshDevices", (sender) =&gt;
          {
              //Prints the connected device name - just to check for myself.
              DisplayAlert("Props", "Name: " + currentDevice.Name, "ok"); 
              adapter.DisconnectDevice(currentDevice);
              StartUpJobs(adapter);
          });
      }
  
      public void StartUpJobs(IAdapter adapter)
      {
          List&lt;string&gt; FoundDevicesList = new List&lt;string&gt;();
          this.adapter = adapter;
          this.devices = new ObservableCollection&lt;IDevice&gt;();
          listView.ItemsSource = devices;
          //Start the scan on startup.
          StartScanning(0x180D.UuidFromPartial());
          adapter.DeviceDiscovered += (object sender, DeviceDiscoveredEventArgs e) =&gt; Device.BeginInvokeOnMainThread(() =&gt; {
              if (!FoundDevicesList.Contains(e.Device.Name))
              {
                  devices.Add(e.Device);
              }
              FoundDevicesList.Add(e.Device.Name);
          }
      );
  </code></pre>
  
  <p>When it is launched it will start the method StartUpJobs(adapter) that finds all the devices and they will be listed up in a <code>ListView</code>. When the user clicks on an item all the details get fetched:</p>
  
  <pre><code>public void OnItemSelected (object sender, SelectedItemChangedEventArgs e) 
  {
          if (((ListView)sender).SelectedItem == null) {
              return;
          }
          var device = e.SelectedItem as IDevice;
          ((ListView)sender).SelectedItem = null; // clear selection
          ServiceList(adapter, device);
  }
  </code></pre>
  
  <p>This function sends the user to another view where he can view live sync-data etc.. when the user now clicks back I want the BLE device to be disconnected, the user gets re-directed to the first page and the <code>ListView</code> with data gets filled again. When the user clicks on the back button in Characteristics_Detail.xaml.cs the following code will be triggered:</p>
  
  <pre><code>    protected override bool OnBackButtonPressed()
      {
          base.OnBackButtonPressed();
          Xamarin.Forms.MessagingCenter.Send&lt;CharacteristicDetail_TISensor&gt;(this, "DisconnectDeviceAndRefreshDevices");
          Navigation.PopModalAsync();
          return false;
      }
  </code></pre>
  
  <p>The MessagingCenter will send me back to the main method in DeviceList.xaml.cs:</p>
  
  <pre><code>    public DeviceList (IAdapter adapter)
      {
          InitializeComponent ();
  
          StartUpJobs(adapter);
          MessagingCenter.Subscribe&lt;CharacteristicDetail_TISensor&gt;(this, "DisconnectDeviceAndRefreshDevices", (sender) =&gt;
          {
              //Prints the connected device name - just to check for myself.
              DisplayAlert("Props", "Name: " + currentDevice.Name, "ok");
              //This line will crash:
              adapter.DisconnectDevice(currentDevice);
              StartUpJobs(adapter);
          });
      }
  </code></pre>
  
  <p>Which then causes the <code>adapter.DisconnectDevice</code> to crash. The error that is printed:</p>
  
  <pre><code>[MonoDroid] UNHANDLED EXCEPTION:
  [MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object
  [MonoDroid] at Robotics.Mobile.Core.Bluetooth.LE.Device.Disconnect () &lt;0x00018&gt;
  [MonoDroid] at Robotics.Mobile.Core.Bluetooth.LE.Adapter.DisconnectDevice (Robotics.Mobile.Core.Bluetooth.LE.IDevice) &lt;0x0004b&gt;
  [MonoDroid] at Robotics.Mobile.BtLEExplorer.DeviceList/&lt;&gt;c__DisplayClass2.&lt;.ctor&gt;b__0 (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor) &lt;0x0007f&gt;
  [MonoDroid] at Xamarin.Forms.MessagingCenter/&lt;&gt;c__DisplayClass4`1&lt;Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor&gt;.&lt;Subscribe&gt;b__3 (object,object) &lt;0x00097&gt;
  [MonoDroid] at Xamarin.Forms.MessagingCenter.InnerSend (string,System.Type,System.Type,object,object) &lt;0x0013b&gt;
  [MonoDroid] at Xamarin.Forms.MessagingCenter.Send&lt;Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor&gt; (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor,string) &lt;0x00047&gt;
  [MonoDroid] at Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor.OnBackButtonPressed () &lt;0x0002b&gt;
  [MonoDroid] at Xamarin.Forms.Page.SendBackButtonPressed () &lt;0x0001f&gt;
  [MonoDroid] at Xamarin.Forms.NavigationPage.OnBackButtonPressed () &lt;0x0001f&gt;
  [MonoDroid] at Xamarin.Forms.Page.SendBackButtonPressed () &lt;0x0001f&gt;
  [MonoDroid] at Xamarin.Forms.Platform.Android.Platform.HandleBackPressed (object,System.EventArgs) &lt;0x0005f&gt;
  [MonoDroid] at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnBackPressed () &lt;0x0004b&gt;
  [MonoDroid] at Android.App.Activity.n_OnBackPressed (intptr,intptr) &lt;0x00037&gt;
  [MonoDroid] at (wrapper dynamic-method) object.2c89333f-7791-415b-86be-ca6a8458701f (intptr,intptr) &lt;0x0003b&gt;
  [art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
  [InputEventSender] Exception dispatching finished signal.
  [MessageQueue-JNI] Exception in MessageQueue callback: handleReceiveCallback
  [MessageQueue-JNI] md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
  [MessageQueue-JNI] at Robotics.Mobile.Core.Bluetooth.LE.Device.Disconnect () &lt;0x00018&gt;
  [MessageQueue-JNI] at Robotics.Mobile.Core.Bluetooth.LE.Adapter.DisconnectDevice (Robotics.Mobile.Core.Bluetooth.LE.IDevice) &lt;0x0004b&gt;
  [MessageQueue-JNI] at Robotics.Mobile.BtLEExplorer.DeviceList/&lt;&gt;c__DisplayClass2.&lt;.ctor&gt;b__0 (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor) &lt;0x0007f&gt;
  [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter/&lt;&gt;c__DisplayClass4`1&lt;Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor&gt;.&lt;Subscribe&gt;b__3 (object,object) &lt;0x00097&gt;
  [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter.InnerSend (string,System.Type,System.Type,object,object) &lt;0x0013b&gt;
  [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter.Send&lt;Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor&gt; (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor,string) &lt;0x00047&gt;
  [MessageQueue-JNI] at Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor.OnBackButtonPressed () &lt;0x0002b&gt;
  [MessageQueue-JNI] at Xamarin.Forms.Page.SendBackButtonPressed () &lt;0x0001f&gt;
  [MessageQueue-JNI] at Xamarin.Forms.NavigationPage.OnBackButtonPressed () &lt;0x0001f&gt;
  [MessageQueue-JNI] at Xamarin.Forms.Page.SendBackButtonPressed () &lt;0x0001f&gt;
  [MessageQueue-JNI] at Xamarin.Forms.Platform.Android.Platform.HandleBackPressed (object,System.EventArgs) &lt;0x0005f&gt;
  [MessageQueue-JNI] at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnBackPressed () &lt;0x0004b&gt;
  [MessageQueue-JNI] at Android.App.Activity.n_OnBackPressed (intptr,intptr) &lt;0x00037&gt;
  [MessageQueue-JNI] at (wrapper dynamic-method) object.2c89333f-7791-415b-86be-ca6a8458701f (intptr,intptr) &lt;0x0003b&gt;
  </code></pre>
  
  <p>I just don't understand why since <code>currentDevice</code> <strong>does</strong> contain the device its details, in the expected <code>IDevice</code> format. My <code>DisplayAlert</code> proves me that the (correct) device is in the variable. So what am I doing wrong and why is it crashing?</p>
  
  <p>Thanks,
  Yenthe</p>