Skip to content

Commit

Permalink
Changed BtHrp start logic.
Browse files Browse the repository at this point in the history
Reverted to a revised async/await implementation.
  • Loading branch information
uwburn committed Jul 5, 2017
1 parent 43069fc commit fbcdc06
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 68 deletions.
Binary file modified Cardia.v12.suo
Binary file not shown.
9 changes: 6 additions & 3 deletions Cardia/PresentationModel/Cardia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,13 @@ public void Start()
return;
}

signalGenerator.Start();
if (hrm.Running)
{
signalGenerator.Start();

if (Started != null)
Started(this);
if (Started != null)
Started(this);
}
}

public void Stop()
Expand Down
128 changes: 63 additions & 65 deletions HRM/HRP/BtHrp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public DeviceInformation Device
public delegate void DeviceChangedEventHandler(object sender, DeviceInformation device);
public event DeviceChangedEventHandler DeviceChanged;

public override void Start()
public override async void Start()
{
if (Running)
return;
Expand All @@ -188,98 +188,95 @@ public override void Start()

lastReceivedDate = DateTime.Now;

ConfigureServiceForNotificationsAsync();

timeoutTimer.Start();
Running = true;

ConfigureServiceForNotificationsAsync();
}

private void ConfigureServiceForNotificationsAsync()
{
new Thread(async () =>
private async void ConfigureServiceForNotificationsAsync()
{
try
{
Thread.CurrentThread.IsBackground = true;
try
{
#if DEBUG
logger.Debug("Getting GattDeviceService " + device.Name + "with id " + device.Id);
logger.Debug("Getting GattDeviceService " + device.Name + " with id " + device.Id);
#endif
service = await GattDeviceService.FromIdAsync(device.Id);
if (initDelay > 0)
await Task.Delay(initDelay);

service = await GattDeviceService.FromIdAsync(device.Id);
Thread.Sleep(initDelay);
// Obtain the characteristic for which notifications are to be received
// Obtain the characteristic for which notifications are to be received
#if DEBUG
logger.Debug("Getting HeartRateMeasurement GattCharacteristic " + characteristicIndex);
logger.Debug("Getting HeartRateMeasurement GattCharacteristic " + characteristicIndex);
#endif
characteristic = service.GetCharacteristics(GattCharacteristicUuids.HeartRateMeasurement)[characteristicIndex];
characteristic = service.GetCharacteristics(GattCharacteristicUuids.HeartRateMeasurement)[characteristicIndex];

// While encryption is not required by all devices, if encryption is supported by the device,
// it can be enabled by setting the ProtectionLevel property of the Characteristic object.
// All subsequent operations on the characteristic will work over an encrypted link.
// While encryption is not required by all devices, if encryption is supported by the device,
// it can be enabled by setting the ProtectionLevel property of the Characteristic object.
// All subsequent operations on the characteristic will work over an encrypted link.
#if DEBUG
logger.Debug("Setting EncryptionRequired protection level on GattCharacteristic");
logger.Debug("Setting EncryptionRequired protection level on GattCharacteristic");
#endif
characteristic.ProtectionLevel = GattProtectionLevel.EncryptionRequired;
characteristic.ProtectionLevel = GattProtectionLevel.EncryptionRequired;

// Register the event handler for receiving notifications
Thread.Sleep(initDelay);
// Register the event handler for receiving notifications
if (initDelay > 0)
await Task.Delay(initDelay);
#if DEBUG
logger.Debug("Registering event handler onction level on GattCharacteristic");
logger.Debug("Registering event handler onction level on GattCharacteristic");
#endif
characteristic.ValueChanged += Characteristic_ValueChanged;
characteristic.ValueChanged += Characteristic_ValueChanged;

// In order to avoid unnecessary communication with the device, determine if the device is already
// correctly configured to send notifications.
// By default ReadClientCharacteristicConfigurationDescriptorAsync will attempt to get the current
// value from the system cache and communication with the device is not typically required.
// In order to avoid unnecessary communication with the device, determine if the device is already
// correctly configured to send notifications.
// By default ReadClientCharacteristicConfigurationDescriptorAsync will attempt to get the current
// value from the system cache and communication with the device is not typically required.
#if DEBUG
logger.Debug("Reading GattCharacteristic configuration descriptor");
logger.Debug("Reading GattCharacteristic configuration descriptor");
#endif
var currentDescriptorValue = await characteristic.ReadClientCharacteristicConfigurationDescriptorAsync();
var currentDescriptorValue = await characteristic.ReadClientCharacteristicConfigurationDescriptorAsync();

if ((currentDescriptorValue.Status != GattCommunicationStatus.Success) ||
(currentDescriptorValue.ClientCharacteristicConfigurationDescriptor != CHARACTERISTIC_NOTIFICATION_TYPE))
{
// Set the Client Characteristic Configuration Descriptor to enable the device to send notifications
// when the Characteristic value changes
if ((currentDescriptorValue.Status != GattCommunicationStatus.Success) ||
(currentDescriptorValue.ClientCharacteristicConfigurationDescriptor != CHARACTERISTIC_NOTIFICATION_TYPE))
{
// Set the Client Characteristic Configuration Descriptor to enable the device to send notifications
// when the Characteristic value changes
#if DEBUG
logger.Debug("Setting GattCharacteristic configuration descriptor to enable notifications");
logger.Debug("Setting GattCharacteristic configuration descriptor to enable notifications");
#endif

GattCommunicationStatus status =
await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
CHARACTERISTIC_NOTIFICATION_TYPE);
GattCommunicationStatus status =
await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
CHARACTERISTIC_NOTIFICATION_TYPE);

if (status == GattCommunicationStatus.Unreachable)
{
if (status == GattCommunicationStatus.Unreachable)
{
#if DEBUG
logger.Debug("Device unreachable");
logger.Debug("Device unreachable");
#endif

// Register a PnpObjectWatcher to detect when a connection to the device is established,
// such that the application can retry device configuration.
StartDeviceConnectionWatcher();
}
// Register a PnpObjectWatcher to detect when a connection to the device is established,
// such that the application can retry device configuration.
StartDeviceConnectionWatcher();
}
else
{
}
else
{
#if DEBUG
logger.Debug("Configuration successfull");
logger.Debug("Configuration successfull");
#endif
}
}
catch (Exception e)
{
}
catch (Exception e)
{
#if DEBUG
logger.Warn("Error configuring HRP device", e);
logger.Warn("Error configuring HRP device", e);
#endif

Stop();
FireTimeout("Bluetooth HRP device initialization failed");
//throw new Exception("Bluetooth HRP device initialization failed");
}
}).Start();
Stop();
FireTimeout("Bluetooth HRP device initialization failed");
//throw new Exception("Bluetooth HRP device initialization failed");
}
}

private void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
Expand Down Expand Up @@ -552,14 +549,9 @@ public override void Reset()

public override void Dispose()
{
if (service != null)
{
service.Dispose();
service = null;
}

if (characteristic != null)
{
characteristic.ValueChanged -= Characteristic_ValueChanged;
characteristic = null;
}

Expand All @@ -568,6 +560,12 @@ public override void Dispose()
watcher.Stop();
watcher = null;
}

if (service != null)
{
service.Dispose();
service = null;
}
}
}
}

0 comments on commit fbcdc06

Please sign in to comment.