Skip to content

Instantly share code, notes, and snippets.

@chrisparnin
Last active April 26, 2017 14:55
Show Gist options
  • Save chrisparnin/99d3ec16dada7c11cc89 to your computer and use it in GitHub Desktop.
Save chrisparnin/99d3ec16dada7c11cc89 to your computer and use it in GitHub Desktop.
Get GSR and Heart Rate in Universal App from Microsoft Band 2
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);
}
}
}
}
@chrisparnin
Copy link
Author

This works assuming you've previously paired your band with the PC.

@chrisparnin
Copy link
Author

GSR:1439 2/18/16 1:01:21 AM -05:00
HR:73 2/18/16 1:01:21 AM -05:00
HR:73 2/18/16 1:01:22 AM -05:00
HR:73 2/18/16 1:01:23 AM -05:00
HR:73 2/18/16 1:01:24 AM -05:00
HR:73 2/18/16 1:01:25 AM -05:00
GSR:1433 2/18/16 1:01:26 AM -05:00
HR:73 2/18/16 1:01:26 AM -05:00
HR:73 2/18/16 1:01:27 AM -05:00
HR:73 2/18/16 1:01:28 AM -05:00
HR:73 2/18/16 1:01:29 AM -05:00
HR:73 2/18/16 1:01:30 AM -05:00
GSR:1441 2/18/16 1:01:31 AM -05:00
HR:72 2/18/16 1:01:31 AM -05:00
HR:72 2/18/16 1:01:32 AM -05:00
HR:72 2/18/16 1:01:33 AM -05:00
HR:72 2/18/16 1:01:34 AM -05:00
HR:72 2/18/16 1:01:35 AM -05:00
GSR:1426 2/18/16 1:01:36 AM -05:00
HR:72 2/18/16 1:01:36 AM -05:00
HR:72 2/18/16 1:01:37 AM -05:00
HR:71 2/18/16 1:01:38 AM -05:00

@chrisparnin
Copy link
Author

Including RR:

HR:75 2/20/16 8:39:27 PM -05:00
RR:0.895968 2/20/16 8:39:27 PM -05:00
HR:72 2/20/16 8:39:28 PM -05:00
RR:0.879376 2/20/16 8:39:28 PM -05:00
GSR:1377 2/20/16 8:39:28 PM -05:00
RR:0.730048 2/20/16 8:39:28 PM -05:00
HR:73 2/20/16 8:39:29 PM -05:00
RR:0.763232 2/20/16 8:39:29 PM -05:00
HR:73 2/20/16 8:39:30 PM -05:00
RR:0.862784 2/20/16 8:39:30 PM -05:00
HR:73 2/20/16 8:39:31 PM -05:00
RR:0.91256 2/20/16 8:39:31 PM -05:00
HR:73 2/20/16 8:39:32 PM -05:00
RR:0.895968 2/20/16 8:39:32 PM -05:00
HR:72 2/20/16 8:39:33 PM -05:00
RR:0.846192 2/20/16 8:39:33 PM -05:00
GSR:1399 2/20/16 8:39:33 PM -05:00

@dorflingers
Copy link

dorflingers commented Apr 27, 2016

Hi chrisparnin! I have tried to implement your code above (and it compiles), but as I am new to visual studio I don't know where to read the output, HR and GSR etc. Can you help?

@chrisparnin
Copy link
Author

chrisparnin commented May 17, 2016

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment