private void Initialize()
        {
            //blobHelper = new BlobHelper(blobAccountName, blobAccountKey);

            service = new Bluetooth();
            service.OnConnect +=
                async connection =>
                {
                    await SendResult(new SystemResultMessage("CONNECT"), "!!");
                };

            service.OnDisconnected += connection =>
            {
                this.Disconnect();
                // say disconnected... ? reconnecting ? onscreen or allow bt symbol to be all?
            };

            service.Initialize( !appSettings.MissingBackButton );
            ////service = new ServerClient(remoteHost, remoteService);
            ////service.Initialize(); 

            RefreshConnections();

            destinations = new List<IDestination>();

            //ConnectionList = new Connections();
            //connectList.ItemsSource = ConnectionList;

            DataContext = model;
            model.Sensors = new Sensors(dispatcher);
            //SensorStack.ItemsSource = model.Sensors.ItemsList;

            MessageFactory.LoadClasses();

            Sensors = model.Sensors;

            // the Azure BLOB storage helper is handed to both Camera and audio for stream options
            // if it is not used, no upload will to Azure BLOB will be done
            camera = new Camera();
            audio = new Audio();

            CheckSensors();
            Sensors.OnSensorUpdated += Sensors_OnSensorUpdated;
            Sensors.Start();

            NavigationCacheMode = NavigationCacheMode.Required;

            canvas.PointerPressed += async (s, a) => await SendEvent(s, a, "pressed");
            canvas.PointerReleased += async (s, a) => await SendEvent(s, a, "released");

            Display.PointerPressed += async (s, a) => await SendEvent(s, a, "pressed");
            Display.PointerReleased += async (s, a) => await SendEvent(s, a, "released");

            screen = new Screen(this);
            web = new Web(this);
            CheckAlwaysRunning();

            isRunning = true;
#pragma warning disable 4014
            Task.Run(() => { AutoReconnect(); });
            Task.Run(() => { ProcessMessages(); });
#pragma warning restore 4014
        }
        public async void SetService()
        {
            if (this.appSettings.ConnectionIndex < 0)
            {
                return;
            }

            ServiceBase.OnConnectHandler OnConnection = async connection =>
                {
                    if (this.IsWelcomeMessageShowing)
                    {
                        this.IsWelcomeMessageShowing = false;
                        await
                            this.dispatcher.RunAsync(
                                CoreDispatcherPriority.Normal, 
                                () => { this.backgroundImage.ClearValue(Image.SourceProperty); });
                    }

                    await this.SendResult(new SystemResultMessage("CONNECT"), "!!");
                };

            ServiceBase.OnConnectHandler OnDisconnected = connection => { this.Disconnect(); };

            if (this.lastConnection != this.appSettings.ConnectionIndex)
            {
                await
                    this.dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal, 
                        () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.Disconnecting; });

                this.Disconnect();
                this.lastConnection = this.appSettings.ConnectionIndex;

                await
                    this.dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal, 
                        () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.NotConnected; });
            }

            if (this.service != null)
            {
                this.service.OnConnect -= OnConnection;
                this.service.OnDisconnected -= OnDisconnected;
            }

            switch (this.appSettings.ConnectionIndex)
            {
                case AppSettings.CONNECTION_BLUETOOTH:
                    {
                        this.service = this.services.ContainsKey("Bluetooth")
                                           ? this.services["Bluetooth"]
                                           : new Bluetooth();
                        this.services["Bluetooth"] = this.service;
                        App.Telemetry.Context.Properties["connection.type"] = "Bluetooth";

                        break;
                    }

                case AppSettings.CONNECTION_WIFI:
                case AppSettings.CONNECTION_MANUAL:
                    {
                        this.service = this.services.ContainsKey("Wifi")
                                           ? this.services["Wifi"]
                                           : new Wifi(AppSettings.BroadcastPort);
                        this.services["Wifi"] = this.service;
                        App.Telemetry.Context.Properties["connection.type"] = "Wifi";

                        if (this.appSettings.ConnectionIndex == 2
                            && !string.IsNullOrWhiteSpace(this.appSettings.Hostname))
                        {
                            this.service.SetClient(
                                "added", 
                                new Connection(
                                    "added", 
                                    new RemotePeer(
                                        null, 
                                        new HostName(this.appSettings.Hostname), 
                                        AppSettings.BroadcastPort.ToString())));
                        }

                        break;
                    }

                case AppSettings.CONNECTION_USB:
                    {
                        this.service = new USB();
                        App.Telemetry.Context.Properties["connection.type"] = "USB";
                        break;
                    }

                default:
                    {
                        throw new NotImplementedException(
                            "Connection type (" + this.appSettings.ConnectionIndex + ") not supported");
                    }
            }

            this.service.OnConnect += OnConnection;
            this.service.OnDisconnected += OnDisconnected;
            this.service.ThreadedException += this.Service_ThreadedException;

            if (!this.service.IsConnected)
            {
                this.service.Initialize( !this.appSettings.MissingBackButton );
            }

            this.service.ListenForBeacons();
            this.RefreshConnections();
        }
        public void SetService()
        {
            if (appSettings.ConnectionIndex < 0)
            {
                return;
            }

            ServiceBase.OnConnectHandler OnConnection = async connection =>
            {
                await SendResult(new SystemResultMessage("CONNECT"), "!!");
            };

            ServiceBase.OnConnectHandler OnDisconnected = connection =>
            {
                this.Disconnect();
            };

            if (service != null)
            {
                service.OnConnect -= OnConnection;
                service.OnDisconnected -= OnDisconnected;
            }

            if (appSettings.ConnectionIndex == 0)
            {
                service = services.ContainsKey("Bluetooth") ? services["Bluetooth"] : new Bluetooth();
                services["Bluetooth"] = service;
            }
            else
            {
                service = services.ContainsKey("Wifi") ? services["Wifi"] : new Wifi();
                services["Wifi"] = service;

                if (appSettings.ConnectionIndex == 2 && !string.IsNullOrWhiteSpace(appSettings.Hostname))
                {
                    service.SetClient("added",
                        new Connection("added",
                            new RemotePeer(null, new HostName(appSettings.Hostname), "1235")));
                }
            }

            service.OnConnect += OnConnection;
            service.OnDisconnected += OnDisconnected;

            service.Initialize(!appSettings.MissingBackButton);

            service.ListenForBeacons();
            RefreshConnections();
        }