ProximityDevice 类

定义

使你能够将消息发布到近似设备或订阅来自近似设备的消息。

public ref class ProximityDevice sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class ProximityDevice final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class ProximityDevice
Public NotInheritable Class ProximityDevice
继承
Object Platform::Object IInspectable ProximityDevice
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
应用功能
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

示例

Windows.Networking.Proximity.ProximityDevice proximityDevice;

private void InitializeProximityDevice()
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

    if (proximityDevice != null) {
        proximityDevice.DeviceArrived += ProximityDeviceArrived;
        proximityDevice.DeviceDeparted += ProximityDeviceDeparted;

        WriteMessageText("Proximity device initialized.\n");
    }
    else
    {
        WriteMessageText("Failed to initialized proximity device.\n");
    }
}

private void ProximityDeviceArrived(Windows.Networking.Proximity.ProximityDevice device)
{
    WriteMessageText("Proximate device arrived. id = " + device.DeviceId + "\n");
}

private void ProximityDeviceDeparted(Windows.Networking.Proximity.ProximityDevice device)
{
    WriteMessageText("Proximate device departed. id = " + device.DeviceId + "\n");
}

// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;

async private void WriteMessageText(string message, bool overwrite = false)
{
    await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (overwrite)
                MessageBlock.Text = message;
            else
                MessageBlock.Text += message;
        });
}
Dim proximityDevice As Windows.Networking.Proximity.ProximityDevice

Private Sub InitializeProximityDevice()

    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault()

    If proximityDevice IsNot Nothing Then
        AddHandler proximityDevice.DeviceArrived, AddressOf ProximityDeviceArrived
        AddHandler proximityDevice.DeviceDeparted, AddressOf ProximityDeviceDeparted

        WriteMessageText("Proximity device initialized." & vbTab)
    Else
        WriteMessageText("Failed to initialized proximity device." & vbTab)
    End If
End Sub

Private Sub ProximityDeviceArrived(device As Windows.Networking.Proximity.ProximityDevice)
    WriteMessageText("Proximate device arrived. id = " & device.DeviceId & vbTab)
End Sub

Private Sub ProximityDeviceDeparted(device As Windows.Networking.Proximity.ProximityDevice)
    WriteMessageText("Proximate device departed. id = " & device.DeviceId & vbTab)
End Sub

' Write a message to MessageBlock on the UI thread.
Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False)
    Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        Sub()
            If overwrite Then
                MessageBlock.Text = message
            Else
                MessageBlock.Text &= message
            End If
        End Sub)
End Sub

注解

ProximityDevice 类使应用程序能够与设备上正在运行的应用程序通信,通常在 3 厘米到 4 厘米的范围内。

可以使用 GetDefaultFromId 静态方法创建 ProximityDevice 的实例。

ProximityDevice 类使用发布/订阅语义,可用于播发和接收小块数据。 对于较大的数据量或持久通信,请使用 PeerFinderStreamSocket 类。 对于 UWP 应用,仅当调用应用位于前台时,发布和订阅才处于活动状态。

重要

邻近感应 API 不提供身份验证、加密或消息完整性。 不要使用邻近感应来交换用户敏感信息,如密码、财务数据、短信、电子邮件、照片或政府 ID 号。

属性

BitsPerSecond

获取邻近设备的传输速率。

DeviceId

获取邻近设备的 DeviceInformation ID

MaxMessageBytes

获取此邻近感应设备支持的已发布消息的最大大小。

方法

FromId(String)

创建 ProximityDevice 类的实例并激活指定的邻近感应设备接口。

GetDefault()

创建 ProximityDevice 类的实例并激活默认邻近感应提供程序。

GetDeviceSelector()

返回可用于枚举邻近设备的类选择字符串。

PublishBinaryMessage(String, IBuffer)

向指定消息类型的订阅者发布包含二进制数据的消息。

PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler)

将包含二进制数据的消息发布到指定消息类型的订阅者。 传输消息后,将调用指定的处理程序。

PublishMessage(String, String)

将消息发布到指定消息类型的订阅者。

PublishMessage(String, String, MessageTransmittedHandler)

将消息发布到指定消息类型的订阅者。 传输消息后,将调用指定的处理程序。

PublishUriMessage(Uri)

将统一资源标识符 (URI) 发布到近似设备。

PublishUriMessage(Uri, MessageTransmittedHandler)

将统一资源标识符 (URI) 发布到近似设备。 传输消息后,将调用指定的处理程序。

StopPublishingMessage(Int64)

停止发布消息。

StopSubscribingForMessage(Int64)

取消消息订阅。

SubscribeForMessage(String, MessageReceivedHandler)

为指定的消息类型创建订阅。

事件

DeviceArrived

当设备进入近似范围时发生。

DeviceDeparted

当设备离开近似范围时发生。

适用于

另请参阅