Enable near field communication

August 13, 2015

Near field communication (NFC) enables a Windows Embedded 8.1 Handheld powered device to communicate with an NFC tag or another NFC-enabled transmitting device. This section describes the components of an NFC tag that you should follow so that the tag works with your devices.

The NFC plug-in enables the administrator to provide a provisioning XML file during the out-of-box experience (OOBE) phase. The NFC plug-in allows an administrator to transfer provisioning information to persistent storage by tapping an unprovisioned Handheld 8.1 powered device to an NFC tag or NFC-enabled device. To use NFC for pre-provisioning a device, you must either prepare your own NFC tags by writing your provisioning XML file to a tag in the manner described in this section, or build the infrastructure needed to transmit a provisioning XML file between an NFC-enabled device and a Handheld 8.1 powered device during the OOBE phase.

Components of an NFC tag and an NFC-enabled device tag

This section describes the components of an NFC tag and an NFC-enabled device tag. Use an NFC tag for minimal provisioning and use an NFC-enabled device tag for larger provisioning XML files.

NFC tag components

NFC tags are suitable for very light applications where minimal provisioning is required. The size of NFC tags that contain provisioning XML files is typically 4 KB to 10 KB.

To write to an NFC tag, you will need to use an NFC Writer tool, or you can use the ProximityDevice class API to write your own custom tool to transfer your provisioning XML file to your NFC tag. The tool must publish a binary message (write) a Chunk data type to your NFC tag.

The following table describes the information that is required when writing to an NFC tag.

Required field

Description

Type

Windows.WEH.PreStageProv.Chunk

The receiving device uses this information to understand information in the Data field.

Data

Tag data in UTF-8 format that has the Byte Order Mark (BOM) removed.

The following example shows how to write to an NFC tag. This example assumes that the tag is already in range of the writing device.

private void WriteProvXMLFileToTag(String provXMLFile)
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

    if (proximityDevice != null)
    {
        var dataWriter = new Windows.Storage.Streams.DataWriter();
        dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
        dataWriter.WriteString(provXMLFile);
        var chunkPubId = proximityDevice.PublishBinaryMessage(
                     "Windows:WriteTag.WEH.PreStageProv.Chunk", 
                     dataWriter.DetachBuffer());
    }
}

NFC-enabled device tag components

Provisioning from an NFC-enabled source device allows for larger provisioning XML files than can be transferred using an NFC tag. When provisioning from an NFC-enabled device, the total file size must not exceed 128 KB. Be aware that the larger the NFC file is, the longer it will take to transfer the provisioning file. Depending on your NFC hardware, the transfer time for a 128 KB file will vary between 2.5 seconds and 10 seconds.

To provision from an NFC-enabled source device, use ProximityDevice class API to write your own custom tool that transfers your provisioning XML file in chunks to your target Handheld 8.1 powered device. The tool must publish binary messages (transmit) a Header message, followed by one or more Chunk messages. The Header specifies the total amount of data that will be transferred to the target device; the Chunks must contain UTF-8 formatted provisioning data where the BOM is removed, as shown in the NFC tag components section.

The following table shows the header format.

Required field

Description

Type

Windows.WEH. PreStageProv.Header

Data

A string that is two UTF-8 semicolon delimited data-value pairs that identify the header version and the total number of bytes (1 through 131072). The following example shows the format:

Vers=1.0;Len=<nnnn>

The target device caches the header information and then waits for the specified amount of data to arrive from the transmitter. The largest block of data that can be transmitted by NFC is 10 KB, so for large files the data must be transferred in separate chunks that are up to 10 KB in size. The receiver tallies the chunks and the content is reassembled. When all data has been received, the Handheld 8.1 powered device processes the provisioning data.

Although this method of transmitting and receiving protects against loss of data transmission, communication can be lost if the transmitting and receiving devices are out of range during the transmission or if the transmitter stops sending data. Communication between the two devices resynchronizes when a new header is transferred or when proximity is re-established.

The following example shows how to transmit a provisioning XML file to a target device. This example assumes that the devices are already in contact.

private void TransmitProvXMLFile(String provXMLFile)
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

    if (proximityDevice != null)
    {
         // Publish the header
         var dataWriter = new Windows.Storage.Streams.DataWriter();
         dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
         dataWriter.WriteString("Vers=1.0;Len="+provXMLFile.Length.ToString()+";");
         proximityDevice.PublishBinaryMessage(
                 "Windows.WEH.PreStageProv.Header", 
                 dataWriter.DetachBuffer());

         // Publish the data in chunks
         int maxMsgBytes = (int) proximityDevice.MaxMessageBytes;
         while (provXMLFile.Length > 0)
         {
             // Determine the maximum amount of data to send.
             int transmitSize=Math.Min(provXMLFile.Length,maxMsgBytes);

             // Prepare the chunk for transmission to peer device
             String fileChunk = provXMLFile.Substring(0, transmitSize);
             dataWriter = new Windows.Storage.Streams.DataWriter();
             dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
             dataWriter.WriteString(fileChunk);

             // Publish chunk to peer
             proximityDevice.PublishBinaryMessage(
                     "Windows.WEH.PreStageProv.Chunk", 
                     dataWriter.DetachBuffer());

             // Reduce the source data
             provXMLFile =  provXMLFile.Remove(0, transmitSize);
         }
    }

For more information about the ProximityDevice class API, see ProximityDevice class on the Developer Center.

Enable or disable NFC capabilities

The administrator can control whether to enable NFC capabilities on the device. When NFC is allowed, the user can change several settings by using Settings/NFC. When disabled, a message appears on the Settings page that NFC is disabled by company policy and the user cannot change the settings.

To disable NFC, set AllowNFC to 0; otherwise, set it to 1.

The following example shows how to allow an NFC tag.

<Add> 
   <CmdID>3</CmdID> 
   <Item> 
      <Target> 
         <LocURI>./Vendor/MSFT/PolicyManager/My/Connectivity/AllowNFC</LocURI> 
      </Target> 
      <Meta> 
         <Format xmlns="syncml:metinf">int</Format> 
      </Meta> 
      <Data>1</Data> 
   </Item> 
</Add> 

The following example shows how to disallow an NFC tag.

<Add> 
   <CmdID>3</CmdID> 
   <Item> 
      <Target> 
         <LocURI>./Vendor/MSFT/PolicyManager/My/Connectivity/AllowNFC</LocURI> 
      </Target> 
      <Meta> 
         <Format xmlns="syncml:metinf">int</Format> 
      </Meta> 
      <Data>0</Data> 
   </Item> 
</Add> 

The following example shows how to query the current policy value.

<Get> 
   <CmdID>3</CmdID> 
   <Item> 
      <Target> 
         <LocURI>./Vendor/MSFT/PolicyManager/Device/Connectivity/AllowNFC</LocURI> 
      </Target> 
   </Item> 
</Get> 

See also

Other resources

ProximityDevice class