Esempio n. 1
0
        // Discovery of existing sessions
        // Handles the addition, removal, or updating of a session
        public async void DiscoverSessions()
        {
            try
            {
                RemoteSystemAccessStatus status = await RemoteSystem.RequestAccessAsync();

                // Checking that remote system access is allowed - ensure capability in the project manifest is set
                if (status != RemoteSystemAccessStatus.Allowed)
                {
                    SendDebugMessage("Access is denied, ensure the \'bluetooth\' and \'remoteSystem\' capabilities are set.");
                    return;
                }

                //  Create watcher to observe for added, updated, or removed sessions
                m_sessionWatcher          = RemoteSystemSession.CreateWatcher();
                m_sessionWatcher.Added   += RemoteSystemSessionWatcher_RemoteSessionAdded;
                m_sessionWatcher.Removed += RemoteSystemSessionWatcher_RemoteSessionRemoved;
                m_sessionWatcher.Start();
                SendDebugMessage("Session discovery started.");
            }
            catch (Win32Exception)
            {
                SendDebugMessage("Session discovery failed.");
            }
        }
        public void OnDescoverSessionAsync(object sender, RoutedEventArgs e)
        {
            if (sessionWatcher != null)
            {
                return;
            }

            // 建立 Watcher 來查看有哪些 Sessions 被建立或是刪除
            sessionWatcher = RemoteSystemSession.CreateWatcher();

            sessionWatcher.Added += (s, a) => {
                // 將找到的 RemoteSystemInfo 加入 UI 顯示
                RemoteSystemSessionInfo sessionInfo = a.SessionInfo;

                var addedTask = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    sessionList.Add(sessionInfo);
                });
            };

            sessionWatcher.Removed += (s, a) => {
                // 將已經結束的 session 從 UI 移除
                var removedSession = a.SessionInfo;
                var exist          = sessionList.Where(x => x.ControllerDisplayName == removedSession.ControllerDisplayName && x.DisplayName == removedSession.DisplayName).FirstOrDefault();

                if (exist != null)
                {
                    var removedTask = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        sessionList.Remove(exist);
                    });
                }
            };

            sessionWatcher.Updated += (s, a) => {
                // 講更新的 RemoteSystemInfo 加入到 UI
                var updatedSession = a.SessionInfo;
                var exist          = sessionList.Where(x => x.ControllerDisplayName == updatedSession.ControllerDisplayName && x.DisplayName == updatedSession.DisplayName).FirstOrDefault();

                if (exist != null)
                {
                    var updatedTask = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        sessionList.Remove(exist);
                        sessionList.Add(updatedSession);
                    });
                }
            };

            sessionWatcher.Start();
        }
        public static async Task DiscoverSessionsAsync()
        {
            var status = await RemoteSystem.RequestAccessAsync();

            if (status == RemoteSystemAccessStatus.Allowed)
            {
                _sessionWatcher?.Stop();
                AvailableSessions.Clear();

                _sessionWatcher          = RemoteSystemSession.CreateWatcher();
                _sessionWatcher.Added   += OnSessionWatcherAdded;
                _sessionWatcher.Removed += OnSessionWatcherRemoved;
                _sessionWatcher.Updated += OnSessionWatcherUpdated;

                _sessionWatcher.Start();
            }
        }
        public async void DiscoverSessions()
        {
            Debug.WriteLine("InsideSessionDiscovery");
            try
            {
                RemoteSystemAccessStatus status = await RemoteSystem.RequestAccessAsync();

                if (status != RemoteSystemAccessStatus.Allowed)
                {
                    Debug.WriteLine("Access Denied");
                    return;
                }
                m_sessionWatcher        = RemoteSystemSession.CreateWatcher();
                m_sessionWatcher.Added += RemoteSystemSessionWatcher_RemoteSessionAdded;
                m_sessionWatcher.Start();
                Debug.WriteLine("Starting Discovery");
            }
            catch (Win32Exception)
            {
                Debug.WriteLine("Discovery Failed");
            }
        }