你的应用可以使用无线网络、蓝牙和云连接来发现使用与发现设备相同的Microsoft帐户登录的 Windows 设备。 远程设备无需安装任何特殊软件即可被发现。
注释
本指南假定你已通过按照 启动远程应用中的步骤,获得了对远程系统功能的访问权限。
筛选可发现设备列表
可以通过将 RemoteSystemWatcher 与筛选器结合使用来缩小可发现设备集的范围。 筛选器可以检测发现类型(近端网络、本地网络、云连接)、设备类型(桌面、移动设备、Xbox、Hub 和全息设备),以及可用性状态(设备是否能够使用远程系统功能的状态)。
筛选器对象必须在初始化 RemoteSystemWatcher 对象之前或初始化时构造,因为它们作为参数传递到其构造函数中。 以下代码创建每种可用的类型的筛选器,然后将其添加到列表中。
注释
这些示例中的代码要求你在文件中有一个 using Windows.System.RemoteSystems 语句。
private List
{
// construct an empty list
List
// 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
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
// 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
{
// 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".
}
若要更改此设置,用户必须打开“设置”。 在 系统>共享体验>跨设备共享 菜单中,有一个下拉列表框,用户可以在其中指定其系统可以共享的设备。
相关主题
连接的应用和设备(Project Rome)
启动远程应用
远程系统 API 参考
远程系统示例