ProximityDevice ProximityDevice ProximityDevice ProximityDevice ProximityDevice Class

Definition

Enables you to publish messages to proximate devices or subscribe to messages from proximate devices.

public : sealed class ProximityDevice : IProximityDevice
struct winrt::Windows::Networking::Proximity::ProximityDevice : IProximityDevice
public sealed class ProximityDevice : IProximityDevice
Public NotInheritable Class ProximityDevice Implements IProximityDevice
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)
Capabilities
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

Examples

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;
        });
}
function id(elementId) {
    return document.getElementById(elementId);
}

var proximityDevice;

function initializeProximityDevice() {
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.getDefault();

    if (proximityDevice) {
        proximityDevice.addEventListener("devicearrived", proximityDeviceArrived);
        proximityDevice.addEventListener("devicedeparted", proximityDeviceDeparted);

        id("messageDiv").innerHTML += "Proximity device initialized.<br />";
    }
    else {
        id("messageDiv").innerHTML += "Failed to initialized proximity device.<br />";
    }
}

function proximityDeviceArrived(device) {
    id("messageDiv").innerHTML += "Proximate device arrived. id = " + device.deviceId + "<br />";
}

function proximityDeviceDeparted(device) {
    id("messageDiv").innerHTML += "Proximate device departed. id = " + device.deviceId + "<br />";
}
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

Remarks

The ProximityDevice class enables applications to communicate with running applications on devices, typically within a range of 3 centimeters to 4 centimeters.

You can create an instance of ProximityDevice by using the GetDefault or FromId static method.

The ProximityDevice class uses publish/subscribe semantics and is useful for advertising and receiving small blocks of data. For larger amounts of data, or for persistent communications, use the PeerFinder and StreamSocket classes. For UWP apps, publications and subscriptions are active only if the calling app is in the foreground.

Important

The proximity APIs do not provide authentication, encryption, or message integrity. Do not use proximity to exchange user sensitive information such as passwords, financial data, text messages, email, photographs, or government id numbers.

Properties

BitsPerSecond BitsPerSecond BitsPerSecond BitsPerSecond BitsPerSecond

Gets the transfer rate of a proximity device.

DeviceId DeviceId DeviceId DeviceId DeviceId

Gets the DeviceInformation Id for a proximity device.

MaxMessageBytes MaxMessageBytes MaxMessageBytes MaxMessageBytes MaxMessageBytes

Gets the maximum size of a published message that this proximity device supports.

Methods

FromId(String) FromId(String) FromId(String) FromId(String) FromId(String)

Creates an instance of a ProximityDevice class and activates the specified proximity device interface.

GetDefault() GetDefault() GetDefault() GetDefault() GetDefault()

Creates an instance of a ProximityDevice class and activates the default proximity provider.

GetDeviceSelector() GetDeviceSelector() GetDeviceSelector() GetDeviceSelector() GetDeviceSelector()

Returns the class selection string that you can use to enumerate proximity devices.

PublishBinaryMessage(String, IBuffer) PublishBinaryMessage(String, IBuffer) PublishBinaryMessage(String, IBuffer) PublishBinaryMessage(String, IBuffer) PublishBinaryMessage(String, IBuffer)

Publishes a message that contains binary data to subscribers of the specified message type.

PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler) PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler) PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler) PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler) PublishBinaryMessage(String, IBuffer, MessageTransmittedHandler)

Publishes a message that contains binary data to subscribers of the specified message type. The specified handler is called when the message has been transmitted.

PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String)

Publishes a message to subscribers of the specified message type.

PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler)

Publishes a message to subscribers of the specified message type. The specified handler is called when the message has been transmitted.

PublishUriMessage(Uri) PublishUriMessage(Uri) PublishUriMessage(Uri) PublishUriMessage(Uri) PublishUriMessage(Uri)

Publishes a Uniform Resource Identifier (URI) to a proximate device.

PublishUriMessage(Uri, MessageTransmittedHandler) PublishUriMessage(Uri, MessageTransmittedHandler) PublishUriMessage(Uri, MessageTransmittedHandler) PublishUriMessage(Uri, MessageTransmittedHandler) PublishUriMessage(Uri, MessageTransmittedHandler)

Publishes a Uniform Resource Identifier (URI) to a proximate device. The specified handler is called when the message has been transmitted.

StopPublishingMessage(Int64) StopPublishingMessage(Int64) StopPublishingMessage(Int64) StopPublishingMessage(Int64) StopPublishingMessage(Int64)

Stops publishing a message.

StopSubscribingForMessage(Int64) StopSubscribingForMessage(Int64) StopSubscribingForMessage(Int64) StopSubscribingForMessage(Int64) StopSubscribingForMessage(Int64)

Cancels a message subscription.

SubscribeForMessage(String, MessageReceivedHandler) SubscribeForMessage(String, MessageReceivedHandler) SubscribeForMessage(String, MessageReceivedHandler) SubscribeForMessage(String, MessageReceivedHandler) SubscribeForMessage(String, MessageReceivedHandler)

Creates a subscription for a specified message type.

Events

DeviceArrived DeviceArrived DeviceArrived DeviceArrived DeviceArrived

Occurs when a device enters the proximate range.

DeviceDeparted DeviceDeparted DeviceDeparted DeviceDeparted DeviceDeparted

Occurs when a device leaves the proximate range.

See Also