public async Task <IEnumerable <BandDeviceInfo> > getBands() { //IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync(); var bands = await bandClientManager.GetPairedBandsAsync(); return(bands); }
protected static void ConnectClient(CancellationToken cancellationToken = default(CancellationToken)) { if (!SensusServiceHelper.Get().EnableBluetooth(true, "Sensus uses Bluetooth to collect data from your Microsoft Band, which is being used in one of your studies.")) { throw new MicrosoftBandClientConnectException("Bluetooth not enabled."); } lock (BAND_CLIENT_LOCKER) { if (!BAND_CLIENT_CONNECTING) { BAND_CLIENT_CONNECTING = true; BAND_CLIENT_CONNECT_WAIT.Reset(); Task.Run(async() => { try { if (BandClient?.IsConnected ?? false) { return; } int connectAttempt = 0; while (++connectAttempt <= BAND_CLIENT_CONNECT_ATTEMPTS && (BandClient == null || !BandClient.IsConnected) && !cancellationToken.IsCancellationRequested) { try { SensusServiceHelper.Get().Logger.Log("Connect attempt " + connectAttempt + " of " + BAND_CLIENT_CONNECT_ATTEMPTS + ".", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); BandClientManager bandManager = BandClientManager.Instance; BandDeviceInfo band = (await bandManager.GetPairedBandsAsync()).FirstOrDefault(); if (band == null) { SensusServiceHelper.Get().Logger.Log("No paired Bands.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); Thread.Sleep(BAND_CLIENT_CONNECT_TIMEOUT_MS); } else { Task <BandClient> connectTask = bandManager.ConnectAsync(band); if (await Task.WhenAny(connectTask, Task.Delay(BAND_CLIENT_CONNECT_TIMEOUT_MS)) == connectTask) { BandClient = await connectTask; if (BandClient.IsConnected) { SensusServiceHelper.Get().Logger.Log("Connected.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } else { SensusServiceHelper.Get().Logger.Log("Could not connect.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } } else { SensusServiceHelper.Get().Logger.Log("Timed out.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } } } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Exception while connecting to Band: " + ex.Message, LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } } } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to reuse/establish connected Band client: " + ex.Message, LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } finally { BAND_CLIENT_CONNECT_WAIT.Set(); BAND_CLIENT_CONNECTING = false; } }); } } BAND_CLIENT_CONNECT_WAIT.WaitOne(); if (BandClient == null || !BandClient.IsConnected) { throw new MicrosoftBandClientConnectException("Failed to connect to Band."); } }
protected static void ConnectClient(MicrosoftBandProbeBase configureProbeIfConnected = null) { if (!SensusServiceHelper.Get().EnableBluetooth(true, "Sensus uses Bluetooth to collect data from your Microsoft Band, which is being used in one of your studies.")) { return; } if (configureProbeIfConnected != null) { lock (CONFIGURE_PROBES_IF_CONNECTED) { CONFIGURE_PROBES_IF_CONNECTED.Add(configureProbeIfConnected); } } lock (BAND_CLIENT_LOCKER) { if (!BAND_CLIENT_CONNECTING) { BAND_CLIENT_CONNECTING = true; BAND_CLIENT_CONNECT_WAIT.Reset(); Task.Run(async() => { try { // if we already have a connection, configure any waiting probes if (BandClient?.IsConnected ?? false) { lock (CONFIGURE_PROBES_IF_CONNECTED) { foreach (MicrosoftBandProbeBase probe in CONFIGURE_PROBES_IF_CONNECTED) { try { probe.Configure(BandClient); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to configure probe on existing connection: " + ex.Message, LoggingLevel.Normal, probe.GetType()); } } CONFIGURE_PROBES_IF_CONNECTED.Clear(); } } // otherwise, attempt to connect else { int connectAttempt = 0; while (++connectAttempt <= BAND_CLIENT_CONNECT_ATTEMPTS && (BandClient == null || !BandClient.IsConnected)) { SensusServiceHelper.Get().Logger.Log("Connect attempt " + connectAttempt + " of " + BAND_CLIENT_CONNECT_ATTEMPTS + ".", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); BandClientManager bandManager = BandClientManager.Instance; BandDeviceInfo band = (await bandManager.GetPairedBandsAsync()).FirstOrDefault(); if (band == null) { SensusServiceHelper.Get().Logger.Log("No paired Bands.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); Thread.Sleep(BAND_CLIENT_CONNECT_TIMEOUT_MS); } else { Task <BandClient> connectTask = bandManager.ConnectAsync(band); if (await Task.WhenAny(connectTask, Task.Delay(BAND_CLIENT_CONNECT_TIMEOUT_MS)) == connectTask) { BandClient = await connectTask; } else { SensusServiceHelper.Get().Logger.Log("Timed out.", LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } } } // if we connected successfully, use the new client to (re)configure all probes that should be running. if (BandClient?.IsConnected ?? false) { lock (CONFIGURE_PROBES_IF_CONNECTED) { foreach (MicrosoftBandProbeBase probe in BandProbesThatShouldBeRunning) { try { probe.Configure(BandClient); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to start readings for Band probe: " + ex.Message, LoggingLevel.Normal, probe.GetType()); } } CONFIGURE_PROBES_IF_CONNECTED.Clear(); } } } } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to connect to Band: " + ex.Message, LoggingLevel.Normal, typeof(MicrosoftBandProbeBase)); } finally { BAND_CLIENT_CONNECT_WAIT.Set(); BAND_CLIENT_CONNECTING = false; } }); } } BAND_CLIENT_CONNECT_WAIT.WaitOne(); if (BandClient == null || !BandClient.IsConnected) { throw new MicrosoftBandClientConnectException("Failed to connect to Band."); } }
public async Task <IEnumerable <BandDeviceInfo> > getBands() { var bands = await bandClientManager.GetPairedBandsAsync(); return(bands); }