探索遠端裝置
你的應用程式可以使用無線網路、藍牙和雲端連線來發現使用與發現裝置相同的 Microsoft 帳戶登入的 Windows 裝置。 遠端裝置無需安裝任何特殊軟體即可探索。
注意
本指南假設您已按照啟動遠端應用程式中的步驟取得存取遠端系統功能的權限。
您可以使用 RemoteSystemWatcher 搭配篩選來縮小可探索的裝置集。 過濾器可以偵測發現類型 (近端網路、本機網路、雲端連接)、裝置類型 (桌上型電腦、行動裝置、Xbox、集線器和全像) 和可用性狀態 (裝置可使用遠端系統功能的狀態)。
必須在初始化 RemoteSystemWatcher 物件之前或同時建構過濾器物件,因為它們會作為參數傳遞到其建構函式中。 以下程式碼會建立每種可用類型的篩選器,然後將它們新增到清單中。
注意
這些範例中的程式碼要求您的檔案中有 using Windows.System.RemoteSystems
陳述式。
private List<IRemoteSystemFilter> makeFilterList()
{
// construct an empty list
List<IRemoteSystemFilter> localListOfFilters = new List<IRemoteSystemFilter>();
// construct a discovery type filter that only allows "proximal" connections:
RemoteSystemDiscoveryTypeFilter discoveryFilter = new RemoteSystemDiscoveryTypeFilter(RemoteSystemDiscoveryType.Proximal);
// construct a device type filter that only allows desktop and mobile devices:
// For this kind of filter, we must first create an IIterable of strings representing the device types to allow.
// These strings are stored as static read-only properties of the RemoteSystemKinds class.
List<String> listOfTypes = new List<String>();
listOfTypes.Add(RemoteSystemKinds.Desktop);
listOfTypes.Add(RemoteSystemKinds.Phone);
// Put the list of device types into the constructor of the filter
RemoteSystemKindFilter kindFilter = new RemoteSystemKindFilter(listOfTypes);
// construct an availibility status filter that only allows devices marked as available:
RemoteSystemStatusTypeFilter statusFilter = new RemoteSystemStatusTypeFilter(RemoteSystemStatusType.Available);
// add the 3 filters to the listL
localListOfFilters.Add(discoveryFilter);
localListOfFilters.Add(kindFilter);
localListOfFilters.Add(statusFilter);
// return the list
return localListOfFilters;
}
注意
「近端」篩選值不保證實體鄰近度。 針對需要可靠實體鄰近性的情況,請在篩選中使用 RemoteSystemDiscoveryType.SpatiallyProximal 值。 目前,此篩選只允許藍牙探索到的裝置。 由於支援保證物理鄰近性的新發現機制和協議,因此它們也將包含在此處。
RemoteSystem 類別中還有一個屬性,用來指示發現的裝置實際上是否在物理鄰近範圍內:RemoteSystem.IsAvailableBySpatialProximity。
注意
如果您想要透過局域網路探索裝置 (由探索類型篩選選取專案決定),您的網路必須使用「私人」或「網域」設定檔。 您的裝置不會透過「公用」網路探索其他裝置。
建立 IRemoteSystemFilter 物件清單之後,就可以將它傳遞至 RemoteSystemWatcher 的建構函式。
// store filter list
List<IRemoteSystemFilter> listOfFilters = makeFilterList();
// construct watcher with the list
m_remoteSystemWatcher = RemoteSystem.CreateWatcher(listOfFilters);
呼叫此監看員的 Start 方法時,只有在偵測到符合下列所有準則的裝置時,才會引發 RemoteSystemAdded 事件:
- 可透過近端連線來探索
- 它是桌面或手機
- 它被分類為可用
從那裡開始,處理事件、擷取 RemoteSystem 物件以及連接到遠端裝置的過程與啟動遠端應用程式中的過程完全相同。 簡而言之,RemoteSystem 物件儲存為 RemoteSystemAddedEventArgs 物件的屬性,這些物件隨每個 RemoteSystemAdded 事件一起傳入。
某些裝置可能無法與使用者關聯或無法透過掃描發現,但如果發現應用程式使用直接位址,仍然可以存取它們。 HostName 類別用來代表遠端裝置的位址。 這通常以 IP 位址的形式儲存,但也允許使用其他幾種格式 (有關詳細資訊,請參閱 HostName 建構函式)。
如果提供了有效的 HostName 物件,則會擷取 RemoteSystem 物件。 如果地址資料無效,則會傳回 null
物件參考。
private async Task<RemoteSystem> getDeviceByAddressAsync(string IPaddress)
{
// construct a HostName object
Windows.Networking.HostName deviceHost = new Windows.Networking.HostName(IPaddress);
// create a RemoteSystem object with the HostName
RemoteSystem remotesys = await RemoteSystem.FindByHostNameAsync(deviceHost);
return remotesys;
}
雖然與探索篩選分開,但查詢裝置功能可能是發現過程的重要組成部分。 使用 RemoteSystem.GetCapabilitySupportedAsync 方法,您可以查詢發現的遠端系統是否支援某些功能,例如遠端會話連線或空間實體 (全像) 共用。 如需可查詢的功能清單,請參閱 KnownRemoteSystemCapabilities 類別。
// Check to see if the given remote system can accept LaunchUri requests
bool isRemoteSystemLaunchUriCapable = remoteSystem.GetCapabilitySupportedAsync(KnownRemoteSystemCapabilities.LaunchUri);
開發人員可以指定發現靠近客戶端裝置的所有裝置,而不僅僅是註冊到相同使用者的設備。 這是透過一個特殊的 IRemoteSystemFilter、RemoteSystemAuthorizationKindFilter 來實現的。 它的實作方式與其他篩選器類型類似:
// Construct a user type filter that includes anonymous devices
RemoteSystemAuthorizationKindFilter authorizationKindFilter = new RemoteSystemAuthorizationKindFilter(RemoteSystemAuthorizationKind.Anonymous);
// then add this filter to the RemoteSystemWatcher
- RemoteSystemAuthorizationKind 值為 Anonymous 將允許發現所有鄰近設備,甚至是來自不可信用戶的設備。
- SameUser 值會將發現篩選為僅註冊到與用戶端裝置相同的使用者的裝置。 此為預設行為。
除了在發現應用程式中指定上述篩選器之外,還必須將用戶端裝置本身配置為允許與其他使用者登入的裝置共用體驗。 這是可以使用 RemoteSystem 類別中的靜態方法查詢的系統設定:
if (!RemoteSystem.IsAuthorizationKindEnabled(RemoteSystemAuthorizationKind.Anonymous)) {
// The system is not authorized to connect to cross-user devices.
// Inform the user that they can discover more devices if they
// update the setting to "Anonymous".
}
若要變更此設定,使用者必須開啟 [設定] 應用程式。 在 [系統>共用體驗>跨裝置共用] 功能表中,有一個下拉式方塊,使用者可以指定其系統可以共用的裝置。