Represents a Hue bridge.
        /// <summary>
        /// The entry point of a background task.
        /// </summary>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            backgroundTaskInstance = taskInstance;
            var details = taskInstance.TriggerDetails as BluetoothLEAdvertisementWatcherTriggerDetails;
            if (details != null)
            {
                _deferral = backgroundTaskInstance.GetDeferral();
                taskInstance.Canceled += (s, e) => _deferral.Complete();

                var localStorage = ApplicationData.Current.LocalSettings.Values;
                _bridge = new Bridge(localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString()); 
                try
                {
                    _lights = await _bridge.GetLightsAsync();
                }
                catch (Exception)
                {
                    _deferral.Complete();
                    return; 
                }
                foreach(var item in details.Advertisements)
                {
                    Debug.WriteLine(item.RawSignalStrengthInDBm);
                }

                // -127 is a BTLE magic number that indicates out of range. If we hit this, 
                // turn off the lights. Send the command regardless if they are on/off
                // just to be safe, since it will only be sent once.
                if (details.Advertisements.Any(x => x.RawSignalStrengthInDBm == -127))
                {
                    foreach (Light light in _lights)
                    {
                        light.State.On = false;
                        await Task.Delay(250);
                    }
                }
                // If there is no magic number, we are in range. Toggle any lights reporting
                // as off to on. Do not spam the command to lights arleady on. 
                else
                {
                    foreach (Light light in _lights.Where(x => !x.State.On))
                    {
                        light.State.On = true;
                        await Task.Delay(250);
                    }
                }
                // Wait 1 second before exiting to ensure all HTTP requests have sent.
                await Task.Delay(1000);
                _deferral.Complete();
            }
        }
 /// <summary>
 /// Fires when the page is navigated to, which occurs after the extended
 /// splash screen has finished loading all Hue resources. 
 /// </summary>
 protected override async void OnNavigatedTo(NavigationEventArgs e)
 {
     HuePayload args = e.Parameter as HuePayload; 
     if (null != args)
     {
         _bridge = args.Bridge;
         Lights = new ObservableCollection<Light>(args.Lights); 
     }
     _taskRegistration = BackgroundTaskRegistration.AllTasks.Values
         .FirstOrDefault(x => x.Name == _taskName);
     BluetoothWatcherToggle.IsChecked = null != _taskRegistration;
     
     var radios = await Radio.GetRadiosAsync();
     BluetoothWatcherToggle.IsEnabled = radios.Any(radio => 
         radio.Kind == RadioKind.Bluetooth && radio.State == RadioState.On);
     base.OnNavigatedTo(e);
 }
        /// <summary>
        /// Tries to find the bridge using multiple methods. Note: this is not an exhaustive list of discovery
        /// options. For complete guidance, see http://www.developers.meethue.com/documentation/hue-bridge-discovery
        /// </summary>
        private async Task FindBridgeAsync()
        {
            try
            {
                // First attempt: local storage cache.
                var localStorage = ApplicationData.Current.LocalSettings.Values;
                if (localStorage.ContainsKey("bridgeIp") && localStorage.ContainsKey("userId"))
                {
                    _bridge = new Bridge(
                        localStorage["bridgeIp"].ToString(), 
                        localStorage["userId"].ToString());
                    if (await PrepareBridgeAsync())
                    {
                        return;
                    }
                }

                // Second attempt: Hue N-UPnP service.
                _bridge = await Bridge.FindAsync();
                if (await PrepareBridgeAsync())
                {
                    return;
                }

                // Third attempt: Re-try Hue N-UPnP service.
                await DispatchAwaitableUITask(async () =>
                    await RetryBridgeSearchPopup.ShowAsync());
                _bridge = await Bridge.FindAsync();
                if (await PrepareBridgeAsync())
                {
                    return;
                }

                // Final attempt: manual entry.
                await DispatchAwaitableUITask(async () =>
                {
                    await BridgeEntryPopup.ShowAsync();
                    _bridge = new Bridge(BridgEntryIp.Text);
                });
                if (await PrepareBridgeAsync())
                {
                    return;
                }
            }
            catch (Exception e)
            {
                await ReportErrorAsync(
                    "We encountered an unexpected problem trying to find your bridge: " + e);
            }
            await ReportErrorAsync("We couldn't find your bridge. Make sure it's powered on, " +
                "has 3 blue lights illuminated, on the same network as this device, " +
                "and that you're connected to the Internet.");
        }
        /// <summary>
        /// Helper method for initalizing the voice service, bridge, and lights. Returns if successful. 
        /// </summary>
        private async Task<bool> InitializeAsync(AppServiceTriggerDetails triggerDetails)
        {
            _voiceServiceConnection = 
                VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
            _voiceServiceConnection.VoiceCommandCompleted += (s, e) => _deferral.Complete();

            _voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync();
            _colors = HsbColor.CreateAll().ToDictionary(x => x.Name);

            var localStorage = ApplicationData.Current.LocalSettings.Values;
            _bridge = new Bridge(
                localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString());
            try
            {
                _lights = await _bridge.GetLightsAsync();
            }
            catch (Exception)
            {
                var response = CreateCortanaResponse("Sorry, I couldn't connect to your bridge.");
                await _voiceServiceConnection.ReportFailureAsync(response);
                return false;
            }
            if (!_lights.Any())
            {
                var response = CreateCortanaResponse("Sorry, I couldn't find any lights.");
                await _voiceServiceConnection.ReportFailureAsync(response);
                return false;
            }
            return true;
        }