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 app,只有在呼叫的應用程式位於前景時,發行集和訂閱才會作用中。

重要

鄰近 API 不提供驗證、加密或訊息完整性。 請勿使用鄰近性來交換使用者敏感性資訊,例如密碼、財務資料、簡訊、電子郵件、相片或政府識別碼。

屬性

BitsPerSecond

取得鄰近裝置的傳輸速率。

DeviceId

取得鄰近裝置的 DeviceInformation 識別碼

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

當裝置離開代理範圍時發生。

適用於

另請參閱