Communicating through the Device IrDA Port


Many Pocket PC and other Windows CE devices include a built-in infrared data transmission port. The .NET Compact Framework includes classes to program against the IrDA port.

IrDA communication occurs between two computers, a client and a server. The server computer offers a connection to any client that comes within range of its infrared port. The connection that a server offers is identified by a device name and device ID.

Client computers may be in range of many computers offering IrDA connections. It is assumed that each computer in range that offers a connection has a unique device ID and device name. The client enumerates through the available connections, chooses one, and communicates with the desired remote computer. Thus, infrared communication occurs with the passing of these events:

  1. A device offers one or more services to all other devices within range of its IrDA port. The device is identified by a device name and device ID. The offered services are identified by name.

  2. A client device that wishes to open a communication enumerates through all possible devices in range of the client device.

  3. The client chooses one of the available devices and connects to one of the services offered by the chosen device.

Using the IrDAClient to Access the Device IrDA Port

The central object for IrDA connectivity on the .NET Compact Framework is the IrDAClient . With the help of several support classes discussed in the sidebar, the IrDAClient can act as a server or a client. That is, the IrDAClient can be used to look for available connections or to offer connections to other devices.

The IrDAClient and related IrDA classes reside in the library named System.Net.IrDA.dll . You must add a reference to this library in your project before you can use these classes. To add the library, use the pull-down menu to view the Solution Explorer: View, Solution Explorer. Find the icon labeled References in the Solution Explorer and right-click it. From the context menu, select Add Reference. In the new dialog that opens, double-click the entry labeled System.Net.IrDA and then click OK.

Once a connection is made with a remote party, the IrDAClient offers the GetStream() method, which exposes a Stream instance with which programs can read and write data.

Connecting to an IrDA Port as a Client

When connecting as an IrDA client, it is presumed that as the developer, you know the name of the device with which you want to connect. The program must iterate through all of the available devices and choose one with the desired service. Specifically, follow these steps:

  1. Create a new instance of an IrDAClient .

  2. Retrieve the list of available devices offering a connection by calling IrDAClient.DiscoverDevices . Pass in the maximum number of devices to look for. The DiscoverDevices method returns an array of IrDADeviceInfo objects.

  3. Scan each IrDADeviceInfo in the array to find out whether one of the available devices is the one the application should connect with.

  4. If a desired device is found, then connect to it by calling IrDAClient.Connect() . Pass in the name of the service to connect to.

  5. Use the IrDAClient to communicate with the remote party.

Connecting as a Client: Sample Code

The following sample code is derived from the IrDAChat sample application. The code enumerates all of the devices available and tries to connect to one that offers a service named IRDA_CHAT_SERVER. It is this connection that has a chat server at the remote end waiting for someone to connect and start chatting. The following code shows the user each connection it finds using a MessageBox:

 
 C# m_IrDAClient = new IrDAClient(); bool l_foundAnyDevice = false; int MAX_DEVICES = 5; // Find out who's out there to connect with... IrDADeviceInfo[] l_DevsAvailable =         m_IrDAClient.DiscoverDevices(MAX_DEVICES); // Show a MessageBox telling user every device we see out there foreach (IrDADeviceInfo l_devInfo in l_DevsAvailable) {     l_foundAnyDevice = true;     MessageBox.Show(l_devInfo.DeviceName, "Discovered IrDA device");     // Now try to connect to the devices, hoping it offers a service     // named "IRDA_CHAT_SERVER"    try    {        // Assume that first device is offering a service that we        // want        IrDAEndPoint chatEndPoint = new IrDAEndPoint(                l_DevsAvailable[0].DeviceID, "IRDA_CHAT_SERVER");        m_IrDAClient.Connect(chatEndPoint);        MessageBox.Show("Connected to chat server!", "Ready to chat");        m_Connected = true;        break;     }     catch (SocketException exc)   {    } } // m_IrdaClient can now be read from or written to. VB m_IrDAClient = New System.Net.Sockets.IrDAClient Dim l_foundAnyDevice As Boolean Dim MAX_DEVICES as Integer l_foundAnyDevice = False ' Find out who's out there to connect with... Dim l_DevsAvailable() As System.Net.Sockets.IrDADeviceInfo l_DevsAvailable = m_IrDAClient.DiscoverDevices(MAX_DEVICES) ' Show a MessageBox telling user every device we see out there Dim i As Integer i = 0 While ((i < l_DevsAvailable.Length) And (m_Connected = False))     Dim l_devInfo As System.Net.Sockets.IrDADeviceInfo     l_devInfo = l_DevsAvailable(i)     l_foundAnyDevice = True     MessageBox.Show(l_devInfo.DeviceName, "Discovered IrDA device")     ' Now try to connect to the devices, hoping it offers a service     ' named "IRDA_CHAT_SERVER"     Try        ' Assume that first device is offering a service that we want        Dim chatEndPoint As System.Net.IrDAEndPoint        chatEndPoint = New                System.Net.IrDAEndPoint(l_DevsAvailable(0).DeviceID,                "IRDA_CHAT_SERVER")         m_IrDAClient.Connect(chatEndPoint)         MessageBox.Show("Connected to chat server!", "Ready to chat")         m_Connected = True      Catch exc As System.Net.Sockets.SocketException End Try ' m_IrDAClient can now be read from and written to 

Establishing an IrDA Connection as a Server

To establish an IrDA connection as a server device, follow these steps:

  1. Create an instance of an IrDAListener , passing the name of the device into the constructor.

  2. Call Start() on the IrDAListener .

  3. Call IrDAListener.AcceptIrDAClient() to receive an instance of an IrDAClient when someone connects.

  4. Use the IrDAClient to communicate with the remote party.

Establishing an IrDA Connection as a Server: Sample Code

This sample code is derived from the IrDAChat sample application. It demonstrates how to use an IrDAListener to offer a connection called IRDA_CHAT_SERVER to other devices and then wait for someone to connect.

 
 C# IrDAListener l_IrDAListener = new IrDAListener("IRDA_CHAT_SERVER"); // Listen for anyone who wants to connect l_IrDAListener.Start(); // And now pull the first queued connection request out as an // IrDAClient m_IrDAClient = l_IrDAListener.AcceptIrDAClient(); MessageBox.Show("Accepted a connection", "Ready to chat"); VB Dim l_IrDAListener As System.Net.Sockets.IrDAListener l_IrDAListener = New         System.Net.Sockets.IrDAListener("IRDA_CHAT_SERVER") ' Listen for anyone who wants to connect l_IrDAListener.Start() ' And now pull the first queued connection request out as ' an IrDAClient m_IrDAClient = l_IrDAListener.AcceptIrDAClient() MessageBox.Show("Accepted a connection", "Ready to chat") 

Reading Data from an IrDAClient

Once an IrDAClient is connected to a remote party, reading data is achieved the same way whether connected as a server or as a client, as follows :

  1. Create a StreamReader by passing in the Stream associated with the IrDAClient into the StreamReader constructor.

  2. Read data from the StreamReader .

Reading Data from an IrDAClient : Sample Code

The following example code is derived from the IrDAChat sample application. This code expects a single line at a time to come from the remote party.

 
 C# l_StreamReader = new StreamReader(this.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII); // Read a line of text and paint it into a GUI this.lbInText.Items.Add(l_StreamReader.ReadLine()); l_StreamReader.Close(); VB l_StreamReader = New         System.IO.StreamReader(Me.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII) Me.lbInText.Items.Add(l_StreamReader.ReadLine()) l_StreamReader.Close() 

Writing Data to an IrDAClient

Once an IrDAClient is connected to a remote party, writing data is achieved the same way whether connected as a server or as a client, as follows:

  1. Create a StreamWriter by passing in the Stream associated with the IrDAClient into the StreamWriter constructor.

  2. Write data to the StreamWriter .

Writing Data to an IrDAClient : Sample Code

The following example code is derived from the IrDAChat sample application. This code writes a single line of text, which it acquires from the user interface, to the stream acquired from the IrDAClient .

 
 C# // Grab a reference to the stream in the m_IrDAClient and send the // text to it. StreamWriter l_StreamWriter = new StreamWriter(this.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII); l_StreamWriter.WriteLine(this.txtSendText.Text); l_StreamWriter.Close(); VB ' Grab a reference to the stream in the m_IrDAClient ' and send the text to it. Dim l_StreamWriter As System.IO.StreamWriter l_StreamWriter = New System.IO.StreamWriter(Me.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII) l_StreamWriter.WriteLine(Me.txtSendText.Text) l_StreamWriter.Close() 

Sample Application: IrDAChat

The IrDAChat sample application is available in the directories SampleApplications\Chapter5\ IrDAChat_CSharp and SampleApplications\Chapter5\IrDAChat_VB . This sample application pulls together all of the ideas presented in this "Communicating through the Device IrDA Port" section by implementing a simple chat program. The application uses an IrDAClient object to connect with a remote party as either the client or the server. It transmits and receives data by accessing the stream associated with the IrDAClient by calling IrDAClient.GetStream() .



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net