Overview
The Windows.Networking.Proximity namespace provides APIs that enable your Windows Store apps to discover and communicate with nearby devices. This is achieved through technologies like Bluetooth and Near Field Communication (NFC).
This API allows for seamless data sharing, app launching, and device control between devices in close proximity, enhancing user experiences for scenarios like file sharing, multiplayer gaming, and quick device pairing.
Key Features
- Device Discovery: Automatically detects other devices in proximity that are advertising their presence.
- Data Publishing: Allows your app to publish data (e.g., text, URIs, custom objects) that other nearby devices can discover.
- Data Subscribing: Enables your app to subscribe to data published by other nearby devices.
- App Launching: Facilitates launching another app on a nearby device by tapping.
- Cross-Platform Potential: Works with other devices supporting proximity technologies, not just Windows devices.
Getting Started with Proximity
To use the Proximity API, you'll typically follow these steps:
- Check for Proximity Support: Verify if the device has proximity capabilities.
- Publish Data: Make your app discoverable by publishing specific data.
- Subscribe to Data: Listen for data published by other devices.
- Handle Events: Respond to connection events and incoming data.
Publishing Data
You can publish data that other devices can discover. For example, publishing a URI:
async void PublishDataAsync()
{
ProximityDevice device = ProximityDevice.GetDefault();
if (device == null)
{
// Proximity device not available
return;
}
string data = "{\"title\":\"Windows Proximity API Example\", \"url\":\"https://learn.microsoft.com/en-us/windows/uwp/networking/proximity-and-bluetooth-tutorial\"}";
long publishedId = device.PublishMessage("windows.com.example.proximity", data);
if (publishedId != 0)
{
System.Diagnostics.Debug.WriteLine("Data published successfully. ID: " + publishedId);
}
}
Subscribing to Data
Your app can subscribe to data published by other devices. The following code demonstrates subscribing to messages published with the same message type:
long subscribedId = 0;
void SubscribeToData()
{
ProximityDevice device = ProximityDevice.GetDefault();
if (device == null)
{
// Proximity device not available
return;
}
subscribedId = device.SubscribeForMessage("windows.com.example.proximity", (sender, message) =>
{
// Process the received message
string receivedData = message.SoftwareBehaviors.ToString();
System.Diagnostics.Debug.WriteLine("Received Data: " + receivedData);
// Update UI or perform actions based on receivedData
});
if (subscribedId != 0)
{
System.Diagnostics.Debug.WriteLine("Subscribed for messages. Subscription ID: " + subscribedId);
}
}
void Unsubscribe()
{
if (subscribedId != 0)
{
ProximityDevice device = ProximityDevice.GetDefault();
if (device != null)
{
device.UnsubscribeForMessage(subscribedId);
System.Diagnostics.Debug.WriteLine("Unsubscribed from messages.");
}
}
}
Important Considerations
- Permissions: Ensure your app has the necessary capabilities declared in its manifest (e.g.,
Proximity). - Device Availability: Always check for the availability of the proximity device using
ProximityDevice.GetDefault(). - Error Handling: Implement robust error handling for network operations and device interactions.
- Background Tasks: For continuous discovery or communication, consider using background tasks.