private async Task run_band(int time = 999) { // Declare Objects MSBand2 band = new MSBand2(); bool connect = false; connection_text.Text = "Connecting TCP..."; do { await Task.Delay(1); tcpClient.create_socket(); connect = await tcpClient.connect(Tcp_Client.GetLocalIp()); }while (!connect); connection_text.Text = "TCP Connected.\nConnecting Band..."; await band.everything(time, connection_text, tcpClient); connection_text.Text = "done"; }
public async Task <int> everything(int RunTime, TextBlock OutputText, Tcp_Client tcpClient) { bool connected = true; OutputText.Visibility = Visibility.Visible; try { // Get the list of Microsoft Bands paired to the phone. IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync(); if (pairedBands.Length < 1) { return(-1); } // Connect to Microsoft Band. using (bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0])) { bool heartRateConsentGranted; // Check whether the user has granted access to the HeartRate sensor. if (bandClient.SensorManager.HeartRate.GetCurrentUserConsent() == UserConsent.Granted) { heartRateConsentGranted = true; } else { heartRateConsentGranted = await bandClient.SensorManager.HeartRate.RequestUserConsentAsync(); } if (!heartRateConsentGranted) { return(-1); } bool gsrConsentGranted; // Check whether the user has granted access to the HeartRate sensor. if (bandClient.SensorManager.Gsr.GetCurrentUserConsent() == UserConsent.Granted) { gsrConsentGranted = true; } else { gsrConsentGranted = await bandClient.SensorManager.Gsr.RequestUserConsentAsync(); } if (!gsrConsentGranted) { return(-1); } // Subscribe to HeartRate data. bandClient.SensorManager.HeartRate.ReadingChanged += async(s, args) => { await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { var data_string = string.Format("{0},Heartrate,{1,0:D3};", DateTime.Now.ToString(timeFormat), args.SensorReading.HeartRate); OutputText.Text = data_string; try { await tcpClient.send(string.Format(data_string)); } catch (Exception) { connected = false; OutputText.Text = "TCP Disconnected"; return; } }); }; await bandClient.SensorManager.HeartRate.StartReadingsAsync(); // Subscribe to GSR data. bandClient.SensorManager.Gsr.ReadingChanged += async(s, args) => { await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { var data_string = string.Format("{1},gsr,{0,0:D9};", args.SensorReading.Resistance, DateTime.Now.ToString(timeFormat)); OutputText.Text = data_string; try { await tcpClient.send(string.Format(data_string)); } catch (Exception) { connected = false; OutputText.Text = "TCP Disconnected"; return; } }); }; await bandClient.SensorManager.Gsr.StartReadingsAsync(); // Run time to collect samples //await Task.Delay(TimeSpan.FromSeconds(RunTime)); while (connected) { await Task.Delay(5); } OutputText.Text = "Band Time Complete"; // Shut off Sensors await bandClient.SensorManager.HeartRate.StopReadingsAsync(); await bandClient.SensorManager.Gsr.StopReadingsAsync(); return(0); } } catch (Exception) { return(-1); } }