Export (0) Print
Expand All

How to: Make an Infrared File Transfer

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework provides classes for infrared communications between devices. This example demonstrates how to send and receive files between devices using infrared communications. You need two Pocket PCs, one to send the file and one to receive it.

This example creates an instance of IrDAClient and uses its DiscoverDevices method to discover infrared devices within range. This method returns an array of IrDADeviceInfo objects that supply information about each device.

This example provides code for sending and receiving a file, which can be demonstrated with a Send and Receive button. Minimally, you'll need to create a Send application for one device, and a Receive application for the other device.

The Send button sends a file only to a device that has been listening for requests to be sent a file. Therefore, the Receive button must be tapped on the receiving device before the Send button is tapped on the sending device. The following tasks are performed:

  • Obtain a stream of the file to send.

  • Create an IrDAClient instance using the service name determined for this application. Infrared connections are made by specifying a service name, which can be any value provided that the participating devices refer to the same name. The service name is "IrDATest" in this example.

  • Read the stream of the file into the IrDAClient stream that sends the file.

The Receive button creates an IrDAListener instance to listen for the device with the same service name as the IrDAClient instance on the sending device.

The following tasks are performed:

  • Create a stream for writing the transferred content to a receiving file in the My Documents folder.

  • Create an IrDAEndPoint instance with the device ID and service name for the sending device.

  • Create an IrDAListener instance from the IrDAEndPoint instance and start the listening service.

  • Create an IrDAClient instance from the IrDAListener instance using the AcceptIrDAClient method.

  • Read the underlying stream of the IrDAClient instance, which contains the data of the transferred file.

  • Write that data stream into the stream for the Receive.txt file.

To create the applications

  1. Create a Pocket PC application for the sending device and add a button to the form. Name the button Send.

  2. Create a file named Send.txt in the My Documents folder.

  3. Add the following code for the Send button's Click event.

    No code example is currently available or this language may not be supported.
  4. Create a Pocket PC application for the receiving device and add a button to the form. Name the button Receive.

  5. Add the following code for the Receive button's Click event.

    No code example is currently available or this language may not be supported.

To run the applications

  1. Deploy the applications to the devices and start them.

  2. Align the infrared ports of the devices.

  3. Tap the Receive button on the receiving device.

  4. Tap the Send button on the sending device.

  5. Check to see if Receive.txt has been created in the My Documents folder.

This example requires references to the following namespaces:

Show:
© 2014 Microsoft