Esempio n. 1
0
        public MainPage()
        {
            this.DataContext = new MainPageViewModel();
            mqttService      = new MqttService();


            devicePicker = new DevicePicker();
            this.devicePicker.DeviceSelected += async(devicePicker, args) =>
            {
                var device = args.SelectedDevice;
                devicePicker.Hide();

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    try
                    {
                        await mqttService.StartAsync();


                        await PairDeviceIfNecessary(device);

                        await ConnectIGrill(device);
                        Settings.SelectedDeviceId = device.Id;
                    } catch (Exception ex)
                    {
                    }
                });
            };
            devicePicker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false));
            devicePicker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true));

            this.InitializeComponent();
        }
Esempio n. 2
0
        private async void Picker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            string deviceId = args.SelectedDevice.Id;

            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                //Update the display status for the selected device to connecting.
                try { picker.SetDisplayStatus(args.SelectedDevice, "Connecting", DevicePickerDisplayStatusOptions.ShowProgress); } catch { }

                //The selectedDeviceInfo instance is needed to be able to update the picker.
                DeviceInformation selectedDeviceInfo = args.SelectedDevice;
#if DEBUG
                // The args.SelectedCastingDevice is proxied from the picker process. The picker process is
                // dismissmed as soon as you break into the debugger. Creating a non-proxied version
                // allows debugging since the proxied version stops working once the picker is dismissed.
                selectedDeviceInfo = await DeviceInformation.CreateFromIdAsync(args.SelectedDevice.Id);
#endif

                bool castSucceeded = false;

                // If the ProjectionManager API did not work and the device id will have 'dial' in it.
                castSucceeded = await TryLaunchDialAppAsync(selectedDeviceInfo);

                // If it doesn't try the ProjectionManager API.
                if (!castSucceeded)
                {
                    castSucceeded = await TryProjectionManagerCastAsync(selectedDeviceInfo);
                }

                //If DIAL and ProjectionManager did not work for the selected device, try the CAST API
                if (!castSucceeded)
                {
                    castSucceeded = await TryCastMediaElementAsync(selectedDeviceInfo);
                }

                if (castSucceeded)
                {
                    //Update the display status for the selected device.  Try Catch in case the picker is not visible anymore.
                    try { picker.SetDisplayStatus(selectedDeviceInfo, "Connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); } catch { }

                    // Hide the picker now that all the work is completed. Try Catch in case the picker is not visible anymore.
                    try { picker.Hide(); } catch { }
                }
                else
                {
                    //Show a retry button when connecting to the selected device failed.
                    try { picker.SetDisplayStatus(selectedDeviceInfo, "Connecting failed", DevicePickerDisplayStatusOptions.ShowRetryButton); } catch { }
                }
            });
        }
        private async void Picker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            string deviceId = args.SelectedDevice.Id;

            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                //Update the display status for the selected device to connecting.
                picker.SetDisplayStatus(args.SelectedDevice, "Connecting", DevicePickerDisplayStatusOptions.ShowProgress);

                // BUG: In order to support debugging it is best to retreive the device from the local app instead
                // of continuing to use the DeviceInformation instance that is proxied from the picker.
                DeviceInformation device = await DeviceInformation.CreateFromIdAsync(deviceId);

                bool castSucceeded = false;

                // The dial AssociationEndpoint ID will have 'dial' in the string.
                // If it doesn't try the ProjectionManager API.
                if (deviceId.IndexOf("dial", StringComparison.OrdinalIgnoreCase) == -1)
                {
                    castSucceeded = await TryProjectionManagerCastAsync(device);
                }
                // If the ProjectionManager API did not work and the device id will have 'dial' in it.
                if (!castSucceeded && deviceId.IndexOf("dial", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    castSucceeded = await TryLaunchDialAppAsync(device);
                }

                //If DIAL and ProjectionManager did not work for the selected device, try the CAST API
                if (!castSucceeded)
                {
                    castSucceeded = await TryCastMediaElementAsync(device);
                }

                if (castSucceeded)
                {
                    //Update the display status for the selected device.  Try Catch in case the picker is not visible anymore.
                    try { picker.SetDisplayStatus(device, "Connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); } catch { }

                    // Hide the picker now that all the work is completed. Try Catch in case the picker is not visible anymore.
                    try { picker.Hide(); } catch { }
                }
                else
                {
                    //Show a retry button when connecting to the selected device failed.
                    try { picker.SetDisplayStatus(device, "Connecting failed", DevicePickerDisplayStatusOptions.ShowRetryButton); } catch { }
                }
            });
        }
Esempio n. 4
0
        private async void Picker_DisconnectButtonClicked(DevicePicker sender, DeviceDisconnectButtonClickedEventArgs args)
        {
            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                rootPage.NotifyUser("Disconnect Button clicked", NotifyType.StatusMessage);

                //Update the display status for the selected device.
                sender.SetDisplayStatus(args.Device, "Disconnecting", DevicePickerDisplayStatusOptions.ShowProgress);

                bool disconnected = false;
                if (this.activeCastConnectionHandler is ProjectionViewBroker)
                {
                    disconnected = TryStopProjectionManagerAsync((ProjectionViewBroker)activeCastConnectionHandler);
                }
                if (this.activeCastConnectionHandler is DialApp)
                {
                    disconnected = await TryStopDialAppAsync((DialApp)activeCastConnectionHandler);
                }
                if (this.activeCastConnectionHandler is CastingConnection)
                {
                    disconnected = await TryDisconnectCastingSessionAsync((CastingConnection)activeCastConnectionHandler);
                }

                if (disconnected)
                {
                    //Update the display status for the selected device.
                    try { sender.SetDisplayStatus(args.Device, "Disconnected", DevicePickerDisplayStatusOptions.None); } catch { }
                    // Set the active device variables to null
                    activeDevice = null;
                    activeCastConnectionHandler = null;

                    //Hide the picker
                    sender.Hide();
                }
                else
                {
                    //Update the display status for the selected device.
                    sender.SetDisplayStatus(args.Device, "Disconnect failed", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                }
            });
        }
Esempio n. 5
0
 /// <summary>
 /// Obsługuje zdarzenie wyboru urządzenia. Obsługa polega na sprawdzeniu czy wybrane urządzenie jest sparowane.
 /// W przypadku, gdy wynik sprawdzenia jest pozytywny wywoływana jest metoda próbująca nawiązać połączenie z wybranym urządzeniem.
 /// W przeciwnym razie, jeśli jest taka możliwość wywoływana jest metoda parująca urządzenia.
 /// </summary>
 /// <param name="pckr"></param>
 /// <param name="args"></param>
 private void _selectedDeviceHandler(DevicePicker pckr, DeviceSelectedEventArgs args)
 {
     pckr.Hide();
     if (args.SelectedDevice.Pairing.IsPaired)
     {
         _connectToDevice(args.SelectedDevice);
     }
     else
     {
         if (args.SelectedDevice.Pairing.CanPair)
         {
             PopUp.Show(StringConsts.RobotNotPairedButCanPair, StringConsts.Error);
             _pairDevices(args.SelectedDevice);
         }
         else
         {
             PopUp.Show(StringConsts.RobotNotPairedAndCannotPair, StringConsts.Error);
         }
     }
 }
Esempio n. 6
0
        private async Task StopDialConnection(DevicePicker picker, DeviceInformation device)
        {
            try
            {
                // 取得被選擇的 dial device
                DialDevice selectedDialDevice = await DialDevice.FromIdAsync(device.Id);

                // 更新 picker status
                picker.SetDisplayStatus(device, "connecting", DevicePickerDisplayStatusOptions.ShowProgress);
                // 取得 dial app
                DialApp app = selectedDialDevice.GetDialApp("castingsample");

                // 請求斷綫
                DialAppStopResult result = await app.StopAsync();

                if (result == DialAppStopResult.Stopped)
                {
                    picker.SetDisplayStatus(device, "Disconnected", DevicePickerDisplayStatusOptions.None);
                    activeDevice = null;
                    picker.Hide();
                }
                else
                {
                    if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure)
                    {
                        // 如果失敗的話要記得多 retry 的機制
                        picker.SetDisplayStatus(device, "Error", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                    }
                    else
                    {
                        // 如果設備沒有支援 Stop 機制,則直接清楚連綫就好
                        activeDevice = null;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Esempio n. 7
0
        private async Task SendDialParameter(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            try
            {
                // 設定遠端設備現在要準備連綫
                sender.SetDisplayStatus(args.SelectedDevice, "connecting", DevicePickerDisplayStatusOptions.ShowProgress);

                // 取得遠端設備中支援指定 app name 的 App
                DialDevice dialDevice = await DialDevice.FromIdAsync(args.SelectedDevice.Id);

                DialApp app = dialDevice.GetDialApp("castingsample");

                if (app == null)
                {
                    // 嘗試建立 DIAL device,如果失敗代表那個設備不支援 DIAL
                    sender.SetDisplayStatus(args.SelectedDevice, "The app is not exist in the device.", DevicePickerDisplayStatusOptions.ShowRetryButton);
                }
                else
                {
                    // 請求送出參數到遠端設備的 App
                    DialAppLaunchResult result = await app.RequestLaunchAsync("Test");

                    if (result == DialAppLaunchResult.Launched)
                    {
                        activeDevice = args.SelectedDevice;
                        sender.SetDisplayStatus(args.SelectedDevice, "connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                        sender.Hide();
                    }
                    else
                    {
                        sender.SetDisplayStatus(args.SelectedDevice, "Device Error", DevicePickerDisplayStatusOptions.ShowRetryButton);
                    }
                }
            }
            catch (Exception ex)
            {
                sender.SetDisplayStatus(args.SelectedDevice, ex.Message, DevicePickerDisplayStatusOptions.None);
            }
        }
        private async void Picker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                try
                {
                    // Set status to Connecting
                    picker.SetDisplayStatus(args.SelectedDevice, "Connecting", DevicePickerDisplayStatusOptions.ShowProgress);

                    // Getting the selected device improves debugging
                    DeviceInformation selectedDevice = args.SelectedDevice;

                    thisViewId = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Id;

                    // If projection is already in progress, then it could be shown on the monitor again
                    // Otherwise, we need to create a new view to show the presentation
                    if (rootPage.ProjectionViewPageControl == null)
                    {
                        // First, create a new, blank view
                        var thisDispatcher = Window.Current.Dispatcher;
                        await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            // ViewLifetimeControl is a wrapper to make sure the view is closed only
                            // when the app is done with it
                            rootPage.ProjectionViewPageControl = ViewLifetimeControl.CreateForCurrentView();

                            // Assemble some data necessary for the new page
                            pvb.MainPageDispatcher        = thisDispatcher;
                            pvb.ProjectionViewPageControl = rootPage.ProjectionViewPageControl;
                            pvb.MainViewId = thisViewId;

                            // Display the page in the view. Note that the view will not become visible
                            // until "StartProjectingAsync" is called
                            var rootFrame = new Frame();
                            rootFrame.Navigate(typeof(ProjectionViewPage), pvb);
                            Window.Current.Content = rootFrame;

                            Window.Current.Activate();
                        });
                    }

                    try
                    {
                        // Start/StopViewInUse are used to signal that the app is interacting with the
                        // view, so it shouldn't be closed yet, even if the user loses access to it
                        rootPage.ProjectionViewPageControl.StartViewInUse();

                        try
                        {
                            await ProjectionManager.StartProjectingAsync(rootPage.ProjectionViewPageControl.Id, thisViewId, selectedDevice);
                        }
                        catch (Exception ex)
                        {
                            if (!ProjectionManager.ProjectionDisplayAvailable || pvb.ProjectedPage == null)
                            {
                                throw ex;
                            }
                        }

                        // ProjectionManager currently can throw an exception even when projection has started.\
                        // Re-throw the exception when projection has not been started after calling StartProjectingAsync
                        if (ProjectionManager.ProjectionDisplayAvailable && pvb.ProjectedPage != null)
                        {
                            this.player.Pause();
                            await pvb.ProjectedPage.SetMediaSource(this.player.Source, this.player.Position);
                            activeDevice = selectedDevice;
                            // Set status to Connected
                            picker.SetDisplayStatus(args.SelectedDevice, "Connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                            picker.Hide();
                        }
                        else
                        {
                            rootPage.NotifyUser(string.Format("Projection has failed to '{0}'", selectedDevice.Name), NotifyType.ErrorMessage);
                            // Set status to Failed
                            picker.SetDisplayStatus(args.SelectedDevice, "Connection Failed", DevicePickerDisplayStatusOptions.ShowRetryButton);
                        }
                    }
                    catch (Exception)
                    {
                        rootPage.NotifyUser(string.Format("Projection has failed to '{0}'", selectedDevice.Name), NotifyType.ErrorMessage);
                        // Set status to Failed
                        try { picker.SetDisplayStatus(args.SelectedDevice, "Connection Failed", DevicePickerDisplayStatusOptions.ShowRetryButton); } catch { }
                    }
                }
                catch (Exception ex)
                {
                    UnhandledExceptionPage.ShowUnhandledException(ex);
                }
            });
        }
Esempio n. 9
0
 private void HideDevicePickerButton_Click(object sender, RoutedEventArgs e)
 {
     devicePicker.Hide();
 }
        private async Task StopDialConnection(DevicePicker picker, DeviceInformation device)
        {
            try
            {
                // 取得被選擇的 dial device
                DialDevice selectedDialDevice = await DialDevice.FromIdAsync(device.Id);
                // 更新 picker status
                picker.SetDisplayStatus(device, "connecting", DevicePickerDisplayStatusOptions.ShowProgress);
                // 取得 dial app 
                DialApp app = selectedDialDevice.GetDialApp("castingsample");

                // 請求斷綫
                DialAppStopResult result = await app.StopAsync();

                if (result == DialAppStopResult.Stopped)
                {
                    picker.SetDisplayStatus(device, "Disconnected", DevicePickerDisplayStatusOptions.None);
                    activeDevice = null;
                    picker.Hide();
                }
                else
                {
                    if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure)
                    {
                        // 如果失敗的話要記得多 retry 的機制
                        picker.SetDisplayStatus(device, "Error", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                    }
                    else
                    {
                        // 如果設備沒有支援 Stop 機制,則直接清楚連綫就好
                        activeDevice = null;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private async Task SendDialParameter(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            try
            {
                // 設定遠端設備現在要準備連綫
                sender.SetDisplayStatus(args.SelectedDevice, "connecting", DevicePickerDisplayStatusOptions.ShowProgress);

                // 取得遠端設備中支援指定 app name 的 App              
                DialDevice dialDevice = await DialDevice.FromIdAsync(args.SelectedDevice.Id);
                DialApp app = dialDevice.GetDialApp("castingsample");

                if (app == null)
                {
                    // 嘗試建立 DIAL device,如果失敗代表那個設備不支援 DIAL
                    sender.SetDisplayStatus(args.SelectedDevice, "The app is not exist in the device.", DevicePickerDisplayStatusOptions.ShowRetryButton);
                }
                else
                {
                    // 請求送出參數到遠端設備的 App 
                    DialAppLaunchResult result = await app.RequestLaunchAsync("Test");

                    if (result == DialAppLaunchResult.Launched)
                    {
                        activeDevice = args.SelectedDevice;
                        sender.SetDisplayStatus(args.SelectedDevice, "connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                        sender.Hide();
                    }
                    else
                    {
                        sender.SetDisplayStatus(args.SelectedDevice, "Device Error", DevicePickerDisplayStatusOptions.ShowRetryButton);
                    }
                }
            }
            catch (Exception ex)
            {
                sender.SetDisplayStatus(args.SelectedDevice, ex.Message, DevicePickerDisplayStatusOptions.None);
            }
        }
        private async void Picker_DisconnectButtonClicked(DevicePicker sender, DeviceDisconnectButtonClickedEventArgs args)
        {
            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
            {
                rootPage.NotifyUser("Disconnect Button clicked", NotifyType.StatusMessage);

                //Update the display status for the selected device.
                sender.SetDisplayStatus(args.Device, "Disconnecting", DevicePickerDisplayStatusOptions.ShowProgress);

                bool disconnected = false;
                if (this.activeCastConnectionHandler is ProjectionViewBroker)
                    disconnected = TryStopProjectionManagerAsync((ProjectionViewBroker)activeCastConnectionHandler);
                if (this.activeCastConnectionHandler is DialApp)
                    disconnected = await TryStopDialAppAsync((DialApp)activeCastConnectionHandler);
                if (this.activeCastConnectionHandler is CastingConnection)
                    disconnected = await TryDisconnectCastingSessionAsync((CastingConnection)activeCastConnectionHandler);

                if (disconnected)
                {
                    //Update the display status for the selected device.
                    try { sender.SetDisplayStatus(args.Device, "Disconnected", DevicePickerDisplayStatusOptions.None); } catch { }
                    // Set the active device variables to null
                    activeDevice = null;
                    activeCastConnectionHandler = null;

                    //Hide the picker
                    sender.Hide();
                }
                else
                {
                    //Update the display status for the selected device.
                    sender.SetDisplayStatus(args.Device, "Disconnect failed", DevicePickerDisplayStatusOptions.ShowDisconnectButton);
                }
            });
        }