private async void Instance_DeviceConnectionUpdated(bool isConnected, string error)
        {
            System.Diagnostics.Debug.WriteLine("Instance_DeviceConnectionUpdated : " + isConnected);
            if (isConnected)
            {
                await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    progressRing.Text = "Registering Background service";
                    System.Diagnostics.Debug.WriteLine("Device is now conneted, registering background task");

                    string retValue = BackgroundManager.RegisterBackgroundTask(BLEDeviceEngine.Instance.Characteristic);
                    SetWaitVisibility(false);

                    if (retValue != null)
                    {
                        ShowErrorDialog(retValue, "BackgroundTask error");
                        return;
                    }

                    System.Diagnostics.Debug.WriteLine("Navigating to HeartBeatPage");
                    this.Frame.Navigate(typeof(HeartBeatPage));
                });
            }
            else
            {
                SetWaitVisibility(false);
                ShowErrorDialog("Failed to connect to the device, reason : " + error, "Device Connection");
            }
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("OnNavigatedFrom");

            //remove background task event handlers
            BackgroundManager.UnRegisterBackgroundTaskEventHandlers(null, _progressEventHandler);
            base.OnNavigatedFrom(e);
        }
        /*
         * App to app launchn URI can be fetched with
         * ProtocolActivatedEventArgs launchArgs = e.Parameter as ProtocolActivatedEventArgs;
         * and we could do something about them if we would want to
         */
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            SetWaitVisibility(true);
            DeviceName.Text = AppSettings.SelectedDeviceName;

            _data.Clear();

            if (!BackgroundManager.RegisterBackgroundTaskEventHandlers(null, _progressEventHandler))
            {
                ShowErrorDialog("Plese go back and select device", "Device not selected");
                return;
            }

            // todo : we should have timer here, that brings error note if we don't get data from background task in x seconds
        }
        private void NavigateToStarPage(Frame rootFrame, System.Object parameter)
        {
            //if we have already active background task, we can go strait viewing values from it
            if (BackgroundManager.IsBackgroundTaskRegistered())
            {
                if (!rootFrame.Navigate(typeof(HeartBeatPage), parameter))
                {
                    throw new Exception("Failed to create initial page");
                }

                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            }
            else
            {
                if (!rootFrame.Navigate(typeof(MainPage), parameter))
                {
                    throw new Exception("Failed to create initial page");
                }

                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
            }
        }
        private async void App_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                return;
            }

            if (rootFrame.SourcePageType.Equals(typeof(HeartBeatPage)))
            {
                if (BackgroundManager.IsBackgroundTaskRegistered())
                {
                    e.Handled = true;

                    //Todo, consider movcing this to be handled inside the UI, not in external popup
                    // so we are going back from HeartBeatPage and we have active background task
                    // so we need to ask whether we want to keep it or not
                    var dlg = new MessageDialog("Stop Background heartbeat monitoring ?");
                    dlg.Commands.Add(new UICommand("Yes", null, "YES"));
                    dlg.Commands.Add(new UICommand("No", null, "NO"));
                    var op = await dlg.ShowAsync();

                    if ((string)op.Id == "YES")
                    {
                        BackgroundManager.UnregisterBackgroundTask();

                        if (rootFrame.CanGoBack)
                        {   // if we started with Mainpage, we'll just  go back
                            rootFrame.GoBack();
                        }
                        else
                        {   //but if we started with Heartbeat page, we actually need to navigate to the mainpage
                            rootFrame.Navigate(typeof(MainPage));
                        }
                    }
                    else
                    {
                        // lets let the background task run, while UI goes out
                        Exit();
                    }

                    return;
                }
                else if (rootFrame.CanGoBack)
                {//this would be error situation, where we went to HeartBeatPage without active background task
                    rootFrame.GoBack();
                }
            }

            if (rootFrame.SourcePageType.Equals(typeof(MainPage)))
            {
                e.Handled = true;
                // from mainview we simply exit
                Exit();
                return;
            }

            // if we are here, we should just go back if it is possible
            if (rootFrame.CanGoBack && !e.Handled)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }