Windows 8 Consumer Preview and Visual Studio 11 Beta – Working with Proximity Device (Part 2/11)

February 29, 2012

This post is about proximity devices and scenarios enabled by using this functionality.

WinRT supports Near Field Communication (NFC) scenarios by providing native support using Windows.Networking.Proximity namespace. This namespace enable working with proximity device in two different modes – direct communication using sockets (ProximityStreamSocket class) and publish/subscribe mode (using ProximityMessage class). ProximityDevice class provides gateway to hardware NFC device component (if present in computer/tablet, drivers are installed and user permits using such type of devices).

To use Proximity device it must be manifested in Capabilities section of application manifest:

image

In both cases first you need to acquire the proximity device:

ProximityDevice _proximityDevice;

_proximityDevice = ProximityDevice.GetDefault();

 

//Check for proximity device presence

if (null == _proximityDevice)

{

    //Device is not present or not enabled. Have a "backup" plan

    //...

}

else

{

    //Device is presend and working

    //...

}

Using sockets requires using PeerFinder class which helps looking for devices, enables getting requests from arriving device and connection to them. Once two peers completer the connection cycle (PeerFinder.TriggeredConnectionState == TriggeredConnectState.Completed) event arguments provides an instance of ProximitryStreamSocket class which is used to transmit data between devices.

While this technique is relatively advanced and requires subscribing to multiple events and managing socketed connection, ProximityDevice class provides easier way to transmit messages between two PCs/Tablets. Using pub/sub model requires subscribing to known message type from one hand and publishing known message type from other hand. Once connection established and message delivered devices can stop publishing/listening to the messages:

long _subscribedMessageId = -1;

long _publishedMessageId = -1;

 

//...

//-----------------------------------------------------------------------------------------

 

//Subscribing to messages

if (_subscribedMessageId == -1)

{

    //Start listening for messages

    _subscribedMessageId = _proximityDevice.SubscribeForMessage("Windows.TheLoveMessage", (_proximityDevice, message) =>

    {

        //When message arrives proceed with application logic

        if (message.DataAsString.Contains("LOVE;"))

        {

            ///...

        }

 

        //Optionally, stop listening to messgaes if applicaton logic requires so

        _proximityDevice.StopSubscribingForMessage(_subscribedMessageId);

    });

}

else

    //Already subscribed to message...

 

//...

//-----------------------------------------------------------------------------------------

 

//Publish messages

if (_publishedMessageId == -1)

{

    //Start publishing messages

    _publishedMessageId = _proximityDevice.PublishMessage("Windows.TheLoveMessage", "LOVE;" + txtName.Text);

    

    //The rest of applciation logic after starting publishing the message

}

else

    //Already publishing the message...

 

//...

//-----------------------------------------------------------------------------------------

 

//When not needed anymore, stop publishing the message

_proximityDevice.StopPublishingMessage(_publishedMessageId);

In addition, ProximityDevice enables subscribing to DeviceArrived and DeviceDeparted events, which helps to maintain state of connected/disconnected devices and provides information about them:

//Get proximity device

_proximityDevice = ProximityDevice.GetDefault();

 

//If device initialized successfully

if (null != _proximityDevice)

{

    //Subscribe to DeviceArrived event

    _proximityDevice.DeviceArrived += (arrivedProximityDevice) =>

        {

            //...

        };

 

    //Subscribe to DeviceADeparted event

    _proximityDevice.DeviceDeparted += (departedProximityDevice) =>

        {

            //...

        };

}

Note: MessageType parameter in PublishMessage function must begin from “Windows.

That’s it for now.

 

Stay tuned for next part.

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. sheMarch 22, 2012 ב 3:54

    What is a proximity device?
    Is it Wifi? Bluetooth? NFC? OHHHH the suspense!!!

    Reply