private static byte[] GetBytes(NfcProvHeader header)
        {
            int size = Marshal.SizeOf(header);

            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(header, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return(arr);
        }
        /// <summary>
        /// Function to format provisioning package into chunk data with a NfcProvHeader prefix.
        /// </summary>
        /// <param name="content">IBuffer to the provisioning package content.</param>
        /// <param name="device">ProximityDevice instance passed in the messageHandler.</param>
        /// <param name="messageHandler">Delegate handler to consume the chunk data.</param>
        private static async Task ConvertToNfcMessageAsync(IBuffer content, ProximityDevice device, Func <ProximityDevice, IBuffer, CancellationToken, Task> messageHandlerAsync, CancellationToken ct)
        {
            var  maxMessageSize = device.MaxMessageBytes;
            var  headerSize     = GetNfcProvHeaderSize();
            uint chunkSize      = maxMessageSize - (uint)headerSize;

            // Calculate the chunk count needed for this transfer.
            uint chunks = content.Length / chunkSize;

            if ((content.Length % chunkSize) > 0)
            {
                ++chunks;
            }

            // Loop and compose the payload per chunk data.
            for (uint posToRead = 0; posToRead < content.Length; posToRead += chunkSize)
            {
                var dataWriter = new DataWriter();
                var header     = new NfcProvHeader();

                header.version = NFC_PROV_MESSAGE_CURRENT_VERSION;  // Currently the supported version is 0x00.
                header.leading = NFC_PROV_MESSAGE_LEADING_BYTE;     // The leading byte should be always 0xFF.
                header.index   = (byte)(posToRead / chunkSize);     // To specify which chunk to this message.
                header.total   = (byte)chunks;                      // To specify the total message count for this transfer.

                int bufferSizeToAlloc = (int)maxMessageSize;
                if (header.total == (header.index + 1))
                {
                    bufferSizeToAlloc = headerSize + (int)(content.Length - posToRead);
                }

                // Write the prefix first and the chunk data of the provisioning package.
                dataWriter.WriteBytes(GetBytes(header));
                dataWriter.WriteBuffer(content, posToRead, (uint)(bufferSizeToAlloc - headerSize));

                // Invoke the delegate handler to handle the NFC message.
                await messageHandlerAsync(device, dataWriter.DetachBuffer(), ct);
            }
        }
        private static int GetNfcProvHeaderSize()
        {
            var header = new NfcProvHeader();

            return(Marshal.SizeOf(header));
        }
 private static int GetNfcProvHeaderSize()
 {
     var header = new NfcProvHeader();
     return Marshal.SizeOf(header);
 }
        private static byte[] GetBytes(NfcProvHeader header)
        {
            int size = Marshal.SizeOf(header);
            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(header, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return arr;
        }
        /// <summary>
        /// Function to format provisioning package into chunk data with a NfcProvHeader prefix.
        /// </summary>
        /// <param name="content">IBuffer to the provisioning package content.</param>
        /// <param name="device">ProximityDevice instance passed in the messageHandler.</param>
        /// <param name="messageHandler">Delegate handler to consume the chunk data.</param>
        private static async Task ConvertToNfcMessageAsync(IBuffer content, ProximityDevice device, Func<ProximityDevice, IBuffer, CancellationToken, Task> messageHandlerAsync, CancellationToken ct)
        {
            var maxMessageSize = device.MaxMessageBytes;
            var headerSize = GetNfcProvHeaderSize();
            uint chunkSize = maxMessageSize - (uint)headerSize;

            // Calculate the chunk count needed for this transfer.
            uint chunks = content.Length / chunkSize;
            if ((content.Length % chunkSize) > 0)
            {
                ++chunks;
            }

            // Loop and compose the payload per chunk data.
            for (uint posToRead = 0; posToRead < content.Length; posToRead += chunkSize)
            {
                var dataWriter = new DataWriter();
                var header = new NfcProvHeader();

                header.version = NFC_PROV_MESSAGE_CURRENT_VERSION;  // Currently the supported version is 0x00.
                header.leading = NFC_PROV_MESSAGE_LEADING_BYTE;     // The leading byte should be always 0xFF.
                header.index = (byte)(posToRead / chunkSize);       // To specify which chunk to this message.
                header.total = (byte)chunks;                        // To specify the total message count for this transfer.

                int bufferSizeToAlloc = (int)maxMessageSize;
                if (header.total == (header.index + 1))
                {
                    bufferSizeToAlloc = headerSize + (int)(content.Length - posToRead);
                }

                // Write the prefix first and the chunk data of the provisioning package.
                dataWriter.WriteBytes(GetBytes(header));
                dataWriter.WriteBuffer(content, posToRead, (uint)(bufferSizeToAlloc - headerSize));

                // Invoke the delegate handler to handle the NFC message.
                await messageHandlerAsync(device, dataWriter.DetachBuffer(), ct);
            }
        }