Skip to content

Commit

Permalink
Check to see if Connection/Disconnect event corresponds to the device…
Browse files Browse the repository at this point in the history
… in interest.

* Robustify removing delegates upon device disconnection
  • Loading branch information
smoy committed May 15, 2015
1 parent 8ccc702 commit c538bb5
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ServiceList (IAdapter adapter, IDevice device)
this.services = new ObservableCollection<IService> ();
listView.ItemsSource = services;
adapter.DeviceConnected += this.OnDeviceConnected;
adapter.DeviceDisconnected += this.OnDeviceDisconnected;
DisconnectButton.Activated += (sender, e) => {
adapter.DisconnectDevice (device);
Navigation.PopToRootAsync(); // disconnect means start over
Expand Down Expand Up @@ -50,11 +51,29 @@ protected override void OnAppearing ()
((ListView)sender).SelectedItem = null; // clear selection
}

public void OnDeviceConnected(object sender, EventArgs args)
public void OnDeviceConnected(object sender, DeviceConnectionEventArgs args)
{
this.adapter.DeviceConnected -= this.OnDeviceConnected;
this.device.ServicesDiscovered += this.ServicesDiscovered;
this.device.DiscoverServices ();
if (args.Device == this.device) {
this.adapter.DeviceConnected -= this.OnDeviceConnected;
this.device.ServicesDiscovered += this.ServicesDiscovered;
this.device.DiscoverServices ();
}
}

public void OnDeviceDisconnected(object sender, DeviceConnectionEventArgs args)
{
if (args.Device == this.device) {
this.adapter.DeviceDisconnected -= this.OnDeviceDisconnected;
// For robustness reason, its possible to be disconnected from device
// right after adding the OnConnected delgate in constructor.
// We do not want DeviceConnected delegate become a hidden bomb.
this.adapter.DeviceConnected -= this.OnDeviceConnected;
// For robustness reason, its possible we go from device
// connected to device disconnected without going through
// service discovered. We do not want the service discover
// delegate become a hidden bomb.
this.device.ServicesDiscovered -= this.ServicesDiscovered;
}
}

public void ServicesDiscovered(object sender, EventArgs args)
Expand Down

0 comments on commit c538bb5

Please sign in to comment.