Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Add reference to Device instance inside an GattCallback instance.
Browse files Browse the repository at this point in the history
* Eliminte the need to create new device instance
* Wire up RSSI to the device
* Get DeviceState from the profileState given by GattCallback
  • Loading branch information
smoy committed May 15, 2015
1 parent 4f3f153 commit d34e05c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ protected bool DeviceExistsInDiscoveredList(BluetoothDevice device)

public void ConnectToDevice (IDevice device)
{
var gattCallback = new GattCallback ();
var androidBleDevice = (Device)device;
var gattCallback = new GattCallback (androidBleDevice);
gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => {
this._connectedDevices.Add ( e.Device);
this.DeviceConnected (this, e);
};

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._connectedDevices.Remove(e.Device);
this.DeviceDisconnected (this, e);
};

var androidBleDevice = (Device)device;
androidBleDevice._gattCallback = gattCallback;
androidBleDevice.GattCallback = gattCallback;
androidBleDevice._gatt = ((BluetoothDevice)device.NativeDevice).ConnectGatt (Android.App.Application.Context, true, gattCallback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,16 @@ public class Device : DeviceBase
/// we also track this because of gogole's weird API. the gatt callback is where
/// we'll get notified when services are enumerated
/// </summary>
internal GattCallback _gattCallback;
private GattCallback _gattCallback;
internal ProfileState _profileState;

public Device (BluetoothDevice nativeDevice, BluetoothGatt gatt,
GattCallback gattCallback, int rssi) : base ()
{
this._nativeDevice = nativeDevice;
this._gatt = gatt;
this._gattCallback = gattCallback;
this.GattCallback = gattCallback;
this._rssi = rssi;

// when the services are discovered on the gatt callback, cache them here
if (this._gattCallback != null) {
this._gattCallback.ServicesDiscovered += (s, e) => {
var services = this._gatt.Services;
this._services = new List<IService> ();
foreach (var item in services) {
this._services.Add (new Service (item, this._gatt, this._gattCallback));
}
this.ServicesDiscovered (this, e);
};
}
}

public override Guid ID {
Expand Down Expand Up @@ -70,7 +59,7 @@ public override int Rssi {
get {
return this._rssi;
}
} protected int _rssi;
} internal int _rssi;

public override object NativeDevice
{
Expand All @@ -79,9 +68,6 @@ public override object NativeDevice
}
}

// TODO: investigate the validity of this. Android API seems to indicate that the
// bond state is available, rather than the connected state, which are two different
// things. you can be bonded but not connected.
public override DeviceState State {
get {
return this.GetState ();
Expand Down Expand Up @@ -113,18 +99,35 @@ public void Disconnect ()

protected DeviceState GetState()
{
switch (this._nativeDevice.BondState) {
case Bond.Bonded:
switch (this._profileState) {
case ProfileState.Connected:
return DeviceState.Connected;
case Bond.Bonding:
case ProfileState.Connecting:
return DeviceState.Connecting;
case Bond.None:
case ProfileState.Disconnected:
default:
return DeviceState.Disconnected;
}
}


internal GattCallback GattCallback
{
set
{
this._gattCallback = value;
// when the services are discovered on the gatt callback, cache them here
if (this._gattCallback != null) {
this._gattCallback.ServicesDiscovered += (s, e) => {
var services = this._gatt.Services;
this._services = new List<IService> ();
foreach (var item in services) {
this._services.Add (new Service (item, this._gatt, this._gattCallback));
}
this.ServicesDiscovered (this, e);
};
}
}
}
#endregion
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ public class GattCallback : BluetoothGattCallback
public event EventHandler<ServicesDiscoveredEventArgs> ServicesDiscovered = delegate {};
public event EventHandler<CharacteristicReadEventArgs> CharacteristicValueUpdated = delegate {};

private Device _device;

public GattCallback(Device device)
{
this._device = device;
}

public override void OnConnectionStateChange (BluetoothGatt gatt, GattStatus status, ProfileState newState)
{
Console.WriteLine ("OnConnectionStateChange: ");
base.OnConnectionStateChange (gatt, status, newState);

//TODO: need to pull the cached RSSI in here, or read it (requires the callback)
Device device = new Device (gatt.Device, gatt, this, 0);
Console.WriteLine ("OnConnectionStateChange: ");

this._device._profileState = newState;
switch (newState) {
// disconnected
case ProfileState.Disconnected:
Console.WriteLine ("disconnected");
this.DeviceDisconnected (this, new DeviceConnectionEventArgs () { Device = device });
this.DeviceDisconnected (this, new DeviceConnectionEventArgs () { Device = this._device });
break;
// connecting
case ProfileState.Connecting:
Expand All @@ -35,7 +41,7 @@ public override void OnConnectionStateChange (BluetoothGatt gatt, GattStatus sta
// connected
case ProfileState.Connected:
Console.WriteLine ("Connected");
this.DeviceConnected (this, new DeviceConnectionEventArgs () { Device = device });
this.DeviceConnected (this, new DeviceConnectionEventArgs () { Device = this._device });
break;
// disconnecting
case ProfileState.Disconnecting:
Expand Down Expand Up @@ -82,6 +88,12 @@ public override void OnCharacteristicChanged (BluetoothGatt gatt, BluetoothGattC
Characteristic = new Characteristic (characteristic, gatt, this) }
);
}

public override void OnReadRemoteRssi (BluetoothGatt gatt, int rssi, GattStatus status)
{
base.OnReadRemoteRssi (gatt, rssi, status);
this._device._rssi = rssi;
}
}
}

0 comments on commit d34e05c

Please sign in to comment.