Last active
April 26, 2017 14:55
-
-
Save chrisparnin/99d3ec16dada7c11cc89 to your computer and use it in GitHub Desktop.
Get GSR and Heart Rate in Universal App from Microsoft Band 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Band; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace App1 | |
{ | |
class GSR | |
{ | |
public async void GetBand() | |
{ | |
IBandInfo[] pairedBands = await | |
BandClientManager.Instance.GetBandsAsync(); | |
if (pairedBands.Length == 0) | |
return; | |
try | |
{ | |
await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => | |
{ | |
using (IBandClient client = await BandClientManager.Instance.ConnectAsync(pairedBands[0])) | |
{ | |
var sensorManager = client.SensorManager; | |
// get the heart rate sensor | |
var heartRate = sensorManager.HeartRate; | |
Debug.WriteLine(string.Join(",", heartRate.SupportedReportingIntervals.Select(r => r.ToString()))); | |
heartRate.ReadingChanged += (o, args) => { | |
var quality = args.SensorReading.Quality; | |
Debug.WriteLine("HR:"+args.SensorReading.HeartRate + " " + args.SensorReading.Timestamp); | |
}; | |
if (heartRate.GetCurrentUserConsent() != UserConsent.Granted) | |
{ | |
await client.SensorManager.HeartRate.RequestUserConsentAsync(); | |
} | |
// GSR sensor | |
var gsr = sensorManager.Gsr; | |
Debug.WriteLine(string.Join(",", gsr.SupportedReportingIntervals.Select(r => r.ToString()))); | |
gsr.ReadingChanged += (o, args) => | |
{ | |
Debug.WriteLine("GSR:"+args.SensorReading.Resistance + " " + args.SensorReading.Timestamp); | |
}; | |
if (gsr.GetCurrentUserConsent() != UserConsent.Granted) | |
{ | |
await client.SensorManager.Gsr.RequestUserConsentAsync(); | |
} | |
// RR | |
var rr = sensorManager.RRInterval; | |
Debug.WriteLine(string.Join(",", rr.SupportedReportingIntervals.Select(r => r.ToString()))); | |
rr.ReadingChanged += (o, args) => | |
{ | |
Debug.WriteLine("RR:" + args.SensorReading.Interval + " " + args.SensorReading.Timestamp); | |
}; | |
if (rr.GetCurrentUserConsent() != UserConsent.Granted) | |
{ | |
await client.SensorManager.RRInterval.RequestUserConsentAsync(); | |
} | |
bool running = await client.SensorManager.HeartRate.StartReadingsAsync(); | |
bool gsrRunning = await client.SensorManager.Gsr.StartReadingsAsync(); | |
bool rrRunning = await client.SensorManager.RRInterval.StartReadingsAsync(); | |
while( running || gsrRunning || rrRunning) | |
{ | |
await Task.Delay(TimeSpan.FromSeconds(1)); | |
} | |
} | |
}); | |
} | |
catch (BandException ex) | |
{ | |
Debug.Write(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dorflingers -- woah didn't see your comment til now.
If you go to Debug -> Output, it should be there.
Just checked in solution that will save to a CSV, too.
https://github.com/BioStack/Sensors101/tree/master/Connectors/BandConnectorWinApp