/// <summary> /// Parses data received from heart rate sensors. /// </summary> /// <param name="bytes">String received from heart rate sensor.</param> /// <returns>Returns parsed data from the device.</returns> public HeartBeatSensorData Parse(byte[] bytes) { HeartBeatSensorData sensorData = new HeartBeatSensorData(); int offset; HeaderData header = GetHeader(bytes, out offset); if (header.IsValid) { sensorData.Firmware = header.Firmware; sensorData.Hardware = header.Hardware; PayloadData payload = GetData(bytes, ref offset); sensorData.IsAvailable = true; sensorData.Battery = payload.Battery; sensorData.HeartBeatPerMinute = payload.HeartRate; sensorData.TotalHeartBeats = payload.HeartBeatNumber; sensorData.HeartBeatTimestamps = payload.HeartBeatTimestamps; if (payload.ContainsStrideData) { sensorData.Speed = payload.Speed; sensorData.Distance = payload.Distance; sensorData.Stride = payload.Stride; } } return(sensorData); }
private void RaiseUpdateEvent(HeartBeatSensorData data, Exception error) { if (Updated != null) { Updated(this, new HeartBeatSensorUpdateEventArgs(data, error)); } }
public async void LoadAsync() { // While WinRT designer can show you actual available devices it can also cause weird stuff. (like "Element not found!" exception or VS crash) #if SILVERLIGHT if (DesignerProperties.IsInDesignTool) #elif NETFX_CORE if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) #endif { // Show some devices in design time. Devices.Add(new DeviceInformation { DisplayName = "Mock device" }); Devices.Add(new DeviceInformation { DisplayName = "HxM device" }); HeartBeatSensorData = new HeartBeatSensorData(); HeartBeatSensorData.Battery = 90; HeartBeatSensorData.Distance = 23.3452; HeartBeatSensorData.Firmware = "9500.550.V1f"; HeartBeatSensorData.Hardware = "9500.550.V1f"; HeartBeatSensorData.HeartBeatPerMinute = 67; HeartBeatSensorData.TotalHeartBeats = 234; HeartBeatSensorData.Stride = 123; HeartBeatSensorData.Speed = 5.456; HeartBeatSensorData.IsAvailable = true; HeartBeatSensorData.IsStrideAvailabe = true; OnPropertyChanged("HeartBeatSensorData"); return; } // Get devices in platform independent data structure. // Method sensorController.ConnectToDeviceAsync will figure out what to do with it. var devices = await devicesController.GetAllDevicesAsync(); foreach (var device in devices) { Devices.Add(device); } }
/// <summary> /// Starts processing sensor data from device. /// <remarks>You need to be connected to the device!</remarks> /// <remarks>This method will complete when called <see cref="Stop" /> method.</remarks> /// </summary> public async Task StartAsync() { int interval = (int)Math.Round(1000 / (float)updateFrequency); cancel = false; while (!cancel) { try { reader.InputStreamOptions = InputStreamOptions.Partial; // All messages should be size of 60 bytes however it is possible // that device was unable to keep up all the data and receives more than 60 bytes. // Also a different sensor can also have different message size. uint count = await reader.LoadAsync(200); // I had some weird issues with ReadBytes but ReadByte worked just fine. byte[] bytes = new byte[count]; for (int i = 0; i < count; ++i) { bytes[i] = reader.ReadByte(); } // Parse data and send new data to listeners. HeartBeatSensorData data = heartRateSensorParser.Parse(bytes); data.DeviceName = lastConnectedDeviceName; RaiseUpdateEvent(data, null); // Sleep until next interval await Task.Delay(interval); } catch (Exception ex) { RaiseUpdateEvent(null, ex); } } }