How to Create an application to transfer files using
Infrared Communication in .NET Compact Framework
Connectivity
between smart devices and the personal computers are the necessity in this fast
moving world. This feature is possible using the classes that are provided in
the .Net Compact Framework. These classes that are used for the infrared communication
is added to the namespace System.Net.Sockets. The Infrared Data Association (IrDA)
has set some standards for socket programming and the Infrared communication programming
has to implement these set standards in the IrDA specifications.
The Windows socket has to be invoked using the platform invoke in your personal
computer since the full .Net Framework does not have the IrDA classes. This platform
invoke on your personal computer is essential for communication between the smart
devices and the personal computer. The protocol defined by IrDA is for point to
point communication between devices.
This communication can be done over
a short range only. Connecting multiple devices to the same host is also possible.
In that case peer connectivity is established. Functionalities such as client
server are implemented over the infrared connectivity. The server is usually the
personal computer. The client would be the smart device that initiates the connectivity.
The
IrDA protocol stack has a layer called the Information Access Service (IAS). This
IAS layer has the Server and the Client component. The server has the services
and the applications that are available for the IR connections that come to it.
The client discovers the connections that are available in the server.
Some
of the classes that are available are IrDACharacterSet, IrDAClient, IrDADeviceInfo,
IrDAEndPoint, IrDAHints, and IrDAListener. Each of these classes has its own purpose.
The IrDA devices have a character set that is supported by it. These character
set supported is found by using the IrDACharacterSet class. Access to the client
is provided by the IrDAClient class to send and receive data. The class IrDADeviceInfo
is used to find the connections available with the server for that particular
device.
The infrared port information is obtained using the IrDAEndPoint
class. The type of device and the connection type are found as an enumeration
list using the IrDAHints class. Monitoring the available connections for a particular
device is possible when a socket is placed in the listening state. A socket is
placed in such a state using the IrDAListener class. The start method of the class
is called in order to place the socket in a listening state. Otherwise it is not
available in the listening state.
To
create an application to transfer files using InfraRed communication you need
two devices which could be pocket PCs. One of the devices will be sending the
data while the other one will receive the data. You will be using the IrDAClient
class and its DiscoverDevices method to find the device that is available within
the range in which it can access. Information about the devices that are within
the range is got using the IrDADeviceInfo objects.
To
make the device communicate with each other you have to create a send application
that is used to send data and a receive application that would receive the data
that is sent by the device. The Send application would be in one device and the
Receive application would be in the other device. These are the minimum application
that is to be created for communication between the two devices. For this purpose
you could create a Send and Receive button which could be used to invoke the necessary
action.
When
you use the Send button to send a file to a device, the device to which it is
sent should be listening to the devices. To make this possible you have to click
the Receive button first so that the device is in the listening mode. Then click
the send button to send the file to the other device which has the Receive application
and is in the receive mode.
The
tasks that are performed when you click the Send button are:
·
Gets the file stream that is to be sent. · An instance of IrDAClient
is created with a service name for the application. · The file stream
is read into the IrDAClient for sending.
The
tasks that are performed when you click the Receive button are:
·
A stream is created to write the received file in a folder · An IrDAEndPoint
is created for the device that sends the file with the service name ·
The listening service is started with the IrDAListener · An IrDAClient
object is create using the method AcceptIrDAClient · Read the stream
that has the data · Write the received stream in to a text file
The
steps that are followed to create applications to send and receive are as follows.
First you can create an application for the sending device. The steps involved
in creating the Send application would be,
1. Creating a Pocket PC application 2.
Adding a Send button to the form in the application 3. Creating a text file
that has to be sent in a folder 4. Add code to the click event of the Send
button.
The
steps for the Receive application in the receiving device would be,
1.
Creating a Pocket PC application 2. Adding a Recieve button to the form in
the application 3. Add code to the click event of the Receive button.
The
algorithm behind the Send buttons click event should be as follows:
1.
A collection is created to discover the devices available for communication. 2.
The name of the first device that is detected is shown. If it couldnt find
it a message is displayed. 3. The file that is to be sent is opened and its
stream is got. 4. The file size is got and it is written to the stream. 5.
A buffer to read that file is created. 6. The file is read to the base stream.
The
algorithm behind the Receive buttons click event should be as follows:
1.
A collection is created to discover the devices available for communication. 2.
The first IrDA device is found and is connected to it. If no devices are found
a message is displayed. 3. A stream for writing is created. 4. The file
is read to the buffer created for writing the file. 5. The file is written
until there are no bytes to write.
To
run the applications thus created you have to align the InfraRed ports of the
devices. Before this you have to install the applications you have created in
the respective devices. Click the Receive button in the receiving device. Then
click the Send button in the device that will send the data or file. The text
file that is sent to the receiving device should be available in the respective
folder that is assigned for that purpose. Check whether you have received that
file.
A
walkthrough code is available for this kind of application in the MSDN website.
You can download that code and test it. Here we have given the algorithm that
is involved in developing that application to give you a clear idea on how to
create an application for transferring files through the Infrared Communication
port in the smart devices such as Pocket PCs and PDAs.