From 8579a5fd360c646c3f1560e9a10e9e5904c8d580 Mon Sep 17 00:00:00 2001 From: Steven Moy Date: Wed, 13 May 2015 19:31:29 -0700 Subject: [PATCH] Correct bookkeeping of connected devices Prior to the change, ConnectedDevices will not be updated upon device disconnection. We change from a list to a dictionary in which the device ID is map to the I Upon disconnected event, we use the device ID to remove the underlying data structure. --- .../Bluetooth/LE/Adapter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Adapter.cs b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Adapter.cs index 62d9010..37f53e7 100644 --- a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Adapter.cs +++ b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Adapter.cs @@ -20,6 +20,7 @@ public class Adapter : Java.Lang.Object, BluetoothAdapter.ILeScanCallback, IAdap protected BluetoothManager _manager; protected BluetoothAdapter _adapter; protected GattCallback _gattCallback; + protected IDictionary _guidToDevice = new Dictionary(); public bool IsScanning { get { return this._isScanning; } @@ -33,10 +34,11 @@ public IList DiscoveredDevices { public IList ConnectedDevices { get { - return this._connectedDevices; + var devices = new List (_guidToDevice.Count); + devices.AddRange (this._guidToDevice.Values); + return devices; } - } protected IList _connectedDevices = new List(); - + } public Adapter () { @@ -48,14 +50,12 @@ public Adapter () this._gattCallback = new GattCallback (this); this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => { - this._connectedDevices.Add ( e.Device); + this._guidToDevice.Add (e.Device.ID, e.Device); this.DeviceConnected (this, e); }; this._gattCallback.DeviceDisconnected += (object sender, DeviceConnectionEventArgs e) => { - // TODO: remove the disconnected device from the _connectedDevices list - // i don't think this will actually work, because i'm created a new underlying device here. - //if(this._connectedDevices.Contains( + this._guidToDevice.Remove (e.Device.ID); this.DeviceDisconnected (this, e); }; }