ProximityDevice Class

Definition

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

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
Inheritance
Object Platform::Object IInspectable ProximityDevice
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)
App 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;
        });
}
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

Gets the transfer rate of a proximity device.

DeviceId

Gets the DeviceInformation Id for a proximity device.

MaxMessageBytes

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

Methods

FromId(String)

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

GetDefault()

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

GetDeviceSelector()

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

PublishBinaryMessage(String, IBuffer)

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

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)

Publishes a message to subscribers of the specified message type.

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)

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

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)

Stops publishing a message.

StopSubscribingForMessage(Int64)

Cancels a message subscription.

SubscribeForMessage(String, MessageReceivedHandler)

Creates a subscription for a specified message type.

Events

DeviceArrived

Occurs when a device enters the proximate range.

DeviceDeparted

Occurs when a device leaves the proximate range.

Applies to

See also