Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Make an Infrared File Transfer 

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.

    ' Align the infrared ports of the devices.
    ' Click the Receive button first, then click Send.
    Private Sub SendButton_Click(sender As Object, e As System.EventArgs) _
        Handles SendButton.Click
    
        Dim irClient As New IrDAClient()
        Dim irServiceName As String = "IrDATest"
        Dim irDevices() As IrDADeviceInfo
        Dim buffersize As Integer = 256
    
        ' Create a collection of devices to discover.
        irDevices = irClient.DiscoverDevices(2)
    
        ' Show the name of the first device found.
        If irDevices.Length = 0 Then
            MsgBox("No remote infrared devices found.")
            Return
        End If
    
        Try
            Dim irEndP As New IrDAEndPoint(irDevices(0).DeviceID, _
                irServiceName)
            Dim irListen As New IrDAListener(irEndP)
            irListen.Start()
            irClient = irListen.AcceptIrDAClient()
            MsgBox("Connected!")
    
        Catch exSoc As SocketException
             MsgBox("Couldn't listen on service " & irServiceName & ": " _
                & exSoc.ErrorCode)
        End Try
    
        ' Open a file to send and get its stream.
        Dim fs As Stream
        Try
            fs = New FileStream(".\My Documents\send.txt", FileMode.Open)
        Catch exFile As Exception
            MsgBox("Cannot open " & exFile.ToString())
            Return
        End Try
    
        ' Get the underlying stream of the client.
        Dim baseStream As Stream = irClient.GetStream()
    
        ' Get the size of the file to send
        ' and write its size to the stream.
        Dim length As Byte() = BitConverter.GetBytes(fs.Length)
        baseStream.Write(length, 0, length.Length)
     
        ' Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte
    
        Dim fileLength As Integer = CInt(fs.Length)
    
        Try
            ' Read the file stream into the base stream.
            While fileLength > 0
                Dim numRead As Int64 = fs.Read(buffer, 0, buffer.Length)
                baseStream.Write(buffer, 0, numRead)
                fileLength -= numRead
            End While
            MsgBox("File sent")
        Catch exSend As Exception
            MsgBox(exSend.Message)
        End Try
    
        fs.Close()
        baseStream.Close()
        irClient.Close()
    End Sub
    
    
  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.

    ' Align the infrared ports of the devices.
    ' Click the Receive button first, then click Send.
    Private Sub ReceiveButton_Click(sender As Object, e As System.EventArgs) _
        Handles ReceiveButton.Click
    
        Dim irDevices() As IrDADeviceInfo
        Dim irClient As New IrDAClient()
        Dim irServiceName As String = "IrDATest"
    
        Dim buffersize As Integer = 256
        
        ' Create a collection for discovering up to
        ' three devices, although only one is needed.
        irDevices = irClient.DiscoverDevices(2)
          
        ' Cancel if no devices are found.
        If irDevices.Length = 0 Then
            MsgBox("No remote infrared devices found.")
            Return
        End If
    
        ' Connect to the first IrDA device
        Dim irEndP As New IrDAEndPoint(irDevices(0).DeviceID, irServiceName)
        irClient.Connect(irEndP)
        
        ' Create a stream for writing a Pocket Word file.
        Dim writeStream As Stream
        Try
            writeStream = New FileStream(".\My Documents\receive.txt", _
                FileMode.OpenOrCreate)
        Catch
            MsgBox("Cannot open file for writing.")
            Return
        End Try
    
        ' Get the underlying stream of the client.
        Dim baseStream As Stream = irClient.GetStream()
    
        ' Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte
        
        Dim numToRead, numRead As Int64
    
        ' Read the file into a stream 8 bytes at a time.
        ' Because the stream does not support seek operations,
        ' its length cannot be determined.
        numToRead = 8
    
        Try    
            While numToRead > 0
                numRead = baseStream.Read(buffer, 0, numToRead)
                numToRead -= numRead
            End While
        Catch exReadIn As Exception
            MsgBox("Read in: " & exReadIn.Message)
        End Try
     
        ' Get the size of the buffer to show
        ' the number of bytes to write to the file.
        numToRead = BitConverter.ToInt64(buffer, 0)
    
        Try
            ' Write the stream to the file until
            ' there are no more bytes to read.
            While numToRead > 0
                numRead = baseStream.Read(buffer, 0, buffer.Length)
                numToRead -= numRead
                writeStream.Write(buffer, 0, numRead)
            End While
            writeStream.Close()
            MsgBox("File received.")
        Catch exWriteOut As Exception
            MsgBox("Write out: " & exWriteOut.Message)
        End Try
    
         baseStream.Close()
         irClient.Close()
    End Sub
    
    

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.

Compiling the Code

This example requires references to the following namespaces:

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.