diff --git a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Characteristic.cs b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Characteristic.cs index ecbe337..9123adf 100644 --- a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Characteristic.cs +++ b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Characteristic.cs @@ -15,27 +15,17 @@ public class Characteristic : ICharacteristic protected BluetoothGattCharacteristic _nativeCharacteristic; - /// - /// 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 - /// - protected BluetoothGatt _gatt; - /// - /// we also track this because of gogole's weird API. the gatt callback is where - /// we'll get notified when services are enumerated - /// - 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) @@ -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"); } @@ -132,20 +122,20 @@ public Task 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; } @@ -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) @@ -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"); } @@ -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"); } diff --git a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Device.cs b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Device.cs index 6faa16f..14667be 100644 --- a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Device.cs +++ b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Device.cs @@ -122,6 +122,11 @@ protected DeviceState GetState() internal GattCallback GattCallback { + get + { + return this._gattCallback; + } + set { this._gattCallback = value; @@ -131,7 +136,7 @@ internal GattCallback GattCallback var services = this._gatt.Services; this._services = new List (); 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); }; diff --git a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/GattCallback.cs b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/GattCallback.cs index 84d8a63..8ac2754 100644 --- a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/GattCallback.cs +++ b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/GattCallback.cs @@ -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) } ); } @@ -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) } ); } diff --git a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Service.cs b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Service.cs index a4db02a..8236170 100644 --- a/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Service.cs +++ b/Source/Platform Stacks/Robotics.Mobile.Core.Droid/Bluetooth/LE/Service.cs @@ -7,22 +7,12 @@ namespace Robotics.Mobile.Core.Bluetooth.LE public class Service : IService { protected BluetoothGattService _nativeService; - /// - /// 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 - /// - protected BluetoothGatt _gatt; - /// - /// we also track this because of gogole's weird API. the gatt callback is where - /// we'll get notified when services are enumerated - /// - 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 { @@ -54,7 +44,7 @@ public IList Characteristics { if (this._characteristics == null) { this._characteristics = new List (); 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; @@ -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;