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:
Using the IrDAClient to Access the Device IrDA PortThe 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 ClientWhen 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:
Connecting as a Client: Sample CodeThe 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 ServerTo establish an IrDA connection as a server device, follow these steps:
Establishing an IrDA Connection as a Server: Sample CodeThis 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 IrDAClientOnce 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 :
Reading Data from an IrDAClient : Sample CodeThe 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 IrDAClientOnce 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:
Writing Data to an IrDAClient : Sample CodeThe 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: IrDAChatThe 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() . |