Skip to content

Commit

Permalink
Refactor out references to gatt and gattCallback. Instead reference d…
Browse files Browse the repository at this point in the history
…evice instance instead.

* This is in preparation to allow read/write of characteristic to check
if a device is disconnected before attempting those read/write actions.
  • Loading branch information
smoy committed May 15, 2015
1 parent babab5f commit 4518d95
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,17 @@ public class Characteristic : ICharacteristic


protected BluetoothGattCharacteristic _nativeCharacteristic;
/// <summary>
/// we have to keep a reference to this because Android's api is weird and requires
/// the GattServer in order to do nearly anything, including enumerating services
/// </summary>
protected BluetoothGatt _gatt;
/// <summary>
/// we also track this because of gogole's weird API. the gatt callback is where
/// we'll get notified when services are enumerated
/// </summary>
protected GattCallback _gattCallback;


public Characteristic (BluetoothGattCharacteristic nativeCharacteristic, BluetoothGatt gatt, GattCallback gattCallback)
protected Device _device;


public Characteristic (BluetoothGattCharacteristic nativeCharacteristic, Device device)
{
this._nativeCharacteristic = nativeCharacteristic;
this._gatt = gatt;
this._gattCallback = gattCallback;
this._device = device;

if (this._gattCallback != null) {
if (this._device.GattCallback != null) {
// wire up the characteristic value updating on the gattcallback
this._gattCallback.CharacteristicValueUpdated += (object sender, CharacteristicReadEventArgs e) => {
this._device.GattCallback.CharacteristicValueUpdated += (object sender, CharacteristicReadEventArgs e) => {
// it may be other characteristics, so we need to test
if(e.Characteristic.ID == this.ID) {
// update our underlying characteristic (this one will have a value)
Expand Down Expand Up @@ -113,7 +103,7 @@ public void Write (byte[] data)

var c = _nativeCharacteristic;
c.SetValue (data);
this._gatt.WriteCharacteristic (c);
this._device._gatt.WriteCharacteristic (c);
Console.WriteLine(".....Write");
}

Expand All @@ -132,20 +122,20 @@ public Task<ICharacteristic> ReadAsync()
// it may be other characteristics, so we need to test
var c = e.Characteristic;
tcs.SetResult(c);
if (this._gattCallback != null) {
if (this._device.GattCallback != null) {
// wire up the characteristic value updating on the gattcallback
this._gattCallback.CharacteristicValueUpdated -= updated;
this._device.GattCallback.CharacteristicValueUpdated -= updated;
}
};


if (this._gattCallback != null) {
if (this._device.GattCallback != null) {
// wire up the characteristic value updating on the gattcallback
this._gattCallback.CharacteristicValueUpdated += updated;
this._device.GattCallback.CharacteristicValueUpdated += updated;
}

Console.WriteLine(".....ReadAsync");
this._gatt.ReadCharacteristic (this._nativeCharacteristic);
this._device._gatt.ReadCharacteristic (this._nativeCharacteristic);

return tcs.Task;
}
Expand All @@ -156,12 +146,12 @@ public void StartUpdates ()
bool successful = false;
if (CanRead) {
Console.WriteLine ("Characteristic.RequestValue, PropertyType = Read, requesting updates");
successful = this._gatt.ReadCharacteristic (this._nativeCharacteristic);
successful = this._device._gatt.ReadCharacteristic (this._nativeCharacteristic);
}
if (CanUpdate) {
Console.WriteLine ("Characteristic.RequestValue, PropertyType = Notify, requesting updates");

successful = this._gatt.SetCharacteristicNotification (this._nativeCharacteristic, true);
successful = this._device._gatt.SetCharacteristicNotification (this._nativeCharacteristic, true);

// [TO20131211@1634] It seems that setting the notification above isn't enough. You have to set the NOTIFY
// descriptor as well, otherwise the receiver will never get the updates. I just grabbed the first (and only)
Expand All @@ -177,7 +167,7 @@ public void StartUpdates ()
if (_nativeCharacteristic.Descriptors.Count > 0) {
BluetoothGattDescriptor descriptor = _nativeCharacteristic.Descriptors [0];
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray ());
_gatt.WriteDescriptor (descriptor);
this._device._gatt.WriteDescriptor (descriptor);
} else {
Console.WriteLine ("RequestValue, FAILED: _nativeCharacteristic.Descriptors was empty, not sure why");
}
Expand All @@ -190,7 +180,7 @@ public void StopUpdates ()
{
bool successful = false;
if (CanUpdate) {
successful = this._gatt.SetCharacteristicNotification (this._nativeCharacteristic, false);
successful = this._device._gatt.SetCharacteristicNotification (this._nativeCharacteristic, false);
//TODO: determine whether
Console.WriteLine ("Characteristic.RequestValue, PropertyType = Notify, STOP updates");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ protected DeviceState GetState()

internal GattCallback GattCallback
{
get
{
return this._gattCallback;
}

set
{
this._gattCallback = value;
Expand All @@ -131,7 +136,7 @@ internal GattCallback GattCallback
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._services.Add (new Service (item, this));
}
this.ServicesDiscovered (this, e);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override void OnCharacteristicRead (BluetoothGatt gatt, BluetoothGattChar
Console.WriteLine ("OnCharacteristicRead: " + characteristic.GetStringValue (0));

this.CharacteristicValueUpdated (this, new CharacteristicReadEventArgs () {
Characteristic = new Characteristic (characteristic, gatt, this) }
Characteristic = new Characteristic (characteristic, this._device) }
);
}

Expand All @@ -85,7 +85,7 @@ public override void OnCharacteristicChanged (BluetoothGatt gatt, BluetoothGattC
Console.WriteLine ("OnCharacteristicChanged: " + characteristic.GetStringValue (0));

this.CharacteristicValueUpdated (this, new CharacteristicReadEventArgs () {
Characteristic = new Characteristic (characteristic, gatt, this) }
Characteristic = new Characteristic (characteristic, this._device) }
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@ namespace Robotics.Mobile.Core.Bluetooth.LE
public class Service : IService
{
protected BluetoothGattService _nativeService;
/// <summary>
/// we have to keep a reference to this because Android's api is weird and requires
/// the GattServer in order to do nearly anything, including enumerating services
/// </summary>
protected BluetoothGatt _gatt;
/// <summary>
/// we also track this because of gogole's weird API. the gatt callback is where
/// we'll get notified when services are enumerated
/// </summary>
protected GattCallback _gattCallback;
protected Device _device;

public Service (BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
public Service (BluetoothGattService nativeService, Device device)
{
this._nativeService = nativeService;
this._gatt = gatt;
this._gattCallback = _gattCallback;
this._device = device;
}

public Guid ID {
Expand Down Expand Up @@ -54,7 +44,7 @@ public Service (BluetoothGattService nativeService, BluetoothGatt gatt, GattCall
if (this._characteristics == null) {
this._characteristics = new List<ICharacteristic> ();
foreach (var item in this._nativeService.Characteristics) {
this._characteristics.Add (new Characteristic (item, this._gatt, this._gattCallback));
this._characteristics.Add (new Characteristic (item, this._device));
}
}
return this._characteristics;
Expand All @@ -66,7 +56,7 @@ public ICharacteristic FindCharacteristic (KnownCharacteristic characteristic)
//TODO: why don't we look in the internal list _chacateristics?
foreach (var item in this._nativeService.Characteristics) {
if ( string.Equals(item.Uuid.ToString(), characteristic.ID.ToString()) ) {
return new Characteristic(item, this._gatt, this._gattCallback);
return new Characteristic(item, this._device);
}
}
return null;
Expand Down

0 comments on commit 4518d95

Please sign in to comment.