public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view, typically from a nib.

            manager = BandClientManager.Instance;
        }
Example #2
0
        async public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            if (null != userInfo && userInfo.ContainsKey(new NSString("aps")))
            {
                NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;

                string alert = string.Empty;
                if (aps.ContainsKey(new NSString("alert")))
                {
                    var nsString = aps[new NSString("alert")] as NSString;
                    if (nsString != null)
                    {
                        alert = nsString.ToString();
                    }
                }

                if (!string.IsNullOrEmpty(alert))
                {
                    //Show a pop-up if the application is open
                    UIAlertView avAlert = new UIAlertView("CRM Item", alert, null, "OK", null);
                    avAlert.Show();

                    BandClient client = MainViewController.GetClient();

                    //Connect to Band if not already connected
                    if (client == null)
                    {
                        try
                        {
                            BandClientManager manager = MainViewController.GetManager();
                            client = manager.AttachedClients.FirstOrDefault();
                            if (client == null)
                            {
                                return;
                            }

                            client = MainViewController.GetClient();
                        }
                        catch (BandException) { }
                    }

                    if (client == null)
                    {
                        return;
                    }

                    //Send to Band
                    await client.NotificationManager.SendMessageTaskAsync(MainViewController.GetTileId(), "CRM Item",
                                                                          alert, DateTime.Now, true);
                }
            }
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view, typically from a nib.
            string userid = NSUserDefaults.StandardUserDefaults.StringForKey("CrmUserId");

            if (!string.IsNullOrEmpty(userid))
            {
                LoginButton.SetTitle("Logout", UIControlState.Normal);
            }

            _manager = BandClientManager.Instance;

            DismissKeyboardOnBackgroundTap();
        }
Example #4
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view, typically from a nib.

            manager = BandClientManager.Instance;

            manager.Connected += (sender, e) => {
                Output("Band connected.");
            };
            manager.ConnectionFailed += (sender, e) => {
                Output("Band connection failed.");
            };
            manager.Disconnected += (sender, e) => {
                Output("Band disconnected.");
            };
        }
        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.");
            }
        }
Example #7
0
 public BandService()
 {
     bandClientManager = BandClientManager.Instance;
 }
Example #8
0
 public Sdk()
 {
     bandClientManager = BandClientManager.Instance;
 }