Export (0) Print
Expand All

IrDAClient Class

Provides connection services for an infrared connection.

Namespace: System.Net.Sockets
Assembly: System.Net.IrDA (in system.net.irda.dll)

'Declaration
Public Class IrDAClient
'Usage
Dim instance As IrDAClient

public class IrDAClient
public class IrDAClient

Provides services for an infrared end point that includes making connections, obtaining data streams and discovering devices.

Because of protocol differences, you cannot interactively use the infrared Beam features on a Pocket PC with a .NET Compact Framework infrared application. Protocol differences also exist with the built-in infrared features on a laptop or desktop computer.

Infrared connections are made by specifying a service name, which can be any value provided the participating devices refer the same name.

TopicLocation
How to: Make an Infrared File Transfer.NET Compact Framework
How to: Make an Infrared File Transfer.NET Compact Framework
How to: Make an Infrared File Transfer.NET Compact Framework

This code example demonstrates how to send and receive files between devices using infrared communications. You need two Pocket PCs, one as the device to send the file and the other to receive it. To run this sample, do the following:

  1. Build this example.

  2. Create a text file named send.txt in the My Documents folder of the device that will send the file.

  3. Align the infrared ports of the devices.

  4. Start the application, IrDADemo.exe, on both devices.

  5. On both devices, click the Discover button for a list of infrared devices within range.

  6. On the device that will receive the file, select the device that will send the file, and then click the Receive button. This button must be clicked before clicking Send on the sending device.

  7. On the device that will send the file, click the Send button.

  8. The receiving device will receive the file, which will be saved to a file named receive.txt in the My Documents folder.

Imports System
Imports System.Windows.Forms
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private ListBox1 As System.Windows.Forms.ListBox
    Private StatusBar1 As System.Windows.Forms.StatusBar
    Private WithEvents Send As System.Windows.Forms.Button
    Private WithEvents Receive As System.Windows.Forms.Button
    Private WithEvents Discover As System.Windows.Forms.Button

    Dim irListen As IrDAListener
    Dim irClient As IrDAClient
    Dim irEndP As IrDAEndPoint
    Dim irDevices() As IrDADeviceInfo

    Private fileSend As String
    Private fileReceive As String
    Private irServiceName As String

    Private buffersize As Integer

    Public Sub New()
        InitializeComponent()
    End Sub 

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Display Pocket PC OK button for closing.
        Me.MinimizeBox = False
    End Sub

    Protected Overrides Sub Dispose(disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub 

    Private Sub InitializeComponent()
        Me.Send = New System.Windows.Forms.Button
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Receive = New System.Windows.Forms.Button
        Me.StatusBar1 = New System.Windows.Forms.StatusBar
        Me.Discover = New System.Windows.Forms.Button
        '
        ' Send
        '
        Me.Send.Location = New System.Drawing.Point(16, 120)
        Me.Send.Size = New System.Drawing.Size(80, 20)
        Me.Send.Text = "Send File"
        '
        ' ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16, 48)
        Me.ListBox1.Size = New System.Drawing.Size(200, 58)
        '
        ' Receive
        '
        Me.Receive.Location = New System.Drawing.Point(136, 120)
        Me.Receive.Size = New System.Drawing.Size(80, 20)
        Me.Receive.Text = "Receive File"
        '
        ' StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0, 248)
        Me.StatusBar1.Size = New System.Drawing.Size(240, 22)
        '
        ' Discover
        '
        Me.Discover.Location = New System.Drawing.Point(16, 16)
        Me.Discover.Size = New System.Drawing.Size(112, 20)
        Me.Discover.Text = "Discover Devies"
        '
        ' Form1
        '
        Me.Controls.Add(Discover)
        Me.Controls.Add(StatusBar1)
        Me.Controls.Add(Receive)
        Me.Controls.Add(ListBox1)
        Me.Controls.Add(Send)
        Me.Text = "IrDA Demo"
    End Sub 

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    ' Discover and list the available infrared devices.
    ' Infrared ports must be aligned.
    Private Sub Discover_Click(sender As Object, e As System.EventArgs) _
        Handles Discover.Click

        ' Create a collection of a maximum of three devices.
        irDevices = irClient.DiscoverDevices(2)

        ' Show a message if no devices are found.
        If irDevices.Length = 0 Then
            MsgBox("No remote infrared devices found!")
            Return
        End If

        ' Enumerate the IrDADeviceInfo
        ' array and list device information
        ' for each device in the list box.
        Dim device As String
        Dim ID As Integer
        ListBox1.Items.Clear()
        For Each irDevice As IrDADeviceInfo In  irDevices
            ID = BitConverter.ToInt32(irDevice.DeviceID, 0)
            device = ID.ToString() _
                & " " & irDevice.DeviceName _
                & " " & irDevice.CharacterSet _
                & " " & irDevice.Hints
            ListBox1.Items.Add(device)
        Next irDevice

        ListBox1.SelectedIndex = 0
        If irDevices.Length > 0 Then
            StatusBar1.Text = irDevices.Length.ToString() & " remote device(s)"
        End If 

        ' Enable the Send and Receive buttons.
        Send.Enabled = True
        Receive.Enabled = True
    End Sub 

    Private Sub IrDAConfig()

        irClient = New IrDAClient()

        ' Files for sending and receiving.
        fileSend = ".\My Documents\send.txt"
        fileReceive = ".\My Documents\receive.txt"

        ' Specify a name for the IrDA Service.
        ' It can be any name, provided all 
        ' participating devices use the same name.
        irServiceName = "IrDAFtp"

        ' Set maximum buffer size for file transfers.
        buffersize = 256

        ' Disable Send and Receive buttons until devices are discovered.
        Send.Enabled = False
        Receive.Enabled = False

    End Sub

    ' Sends a file.
    Private Sub Send_Click(sender As Object, e As System.EventArgs) _
        Handles Send.Click

        ' Open the file to send and get its stream.
        Dim fs As Stream
        Try
            fs = New FileStream(fileSend, FileMode.Open)
        Catch exFile As Exception
            MsgBox("Cannot open " & exFile.ToString())
            Return
        End Try

        ' Create an IrDA client with a service name that must
        ' match the service name of the receiving IrDA client.
        Try
            irClient = New IrDAClient(irServiceName)
        Catch exS As SocketException
            MsgBox("Create socket error: " & exS.Message & _
                " - Did you click Receive on the other device?")
            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(Fix(fs.Length))
        baseStream.Write(length, 0, length.Length)

        ' Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte

        ' Display the number of bytes being sent.
        Dim fileLength As Integer = CInt(fs.Length)
        StatusBar1.Text = "Sending " & fileLength & " bytes"

        ' Read the file stream into the base stream.
        While fileLength > 0
            Dim numRead As Integer = fs.Read(buffer, 0, buffer.Length)
            baseStream.Write(buffer, 0, numRead)
            fileLength -= numRead
        End While

        fs.Close()
        baseStream.Close()
        irClient.Close()
        StatusBar1.Text = "File sent"
    End Sub 

    ' Receives a file.
    Private Sub Receive_Click(sender As Object, e As System.EventArgs) _
        Handles Receive.Click

        ' Create a stream for writing the file.
        Dim writeStream As Stream
        Try
            writeStream = New FileStream(fileReceive, FileMode.OpenOrCreate)
        Catch 
            MsgBox("Couldn't open " & fileReceive & " for writing")
            Return
        End Try

        ' Create a connection, with the IrDAEndPoint class,
        ' for the selected device in the list box.
        ' Start listening for incoming requests from
        ' that device with an IrDAListener object.
        Try
            Dim i As Integer = ListBox1.SelectedIndex
            irEndP = New IrDAEndPoint(irDevices(i).DeviceID, irServiceName)
            irListen = New IrDAListener(irEndP)
            irListen.Start()
        Catch exSoc As SocketException
            MsgBox("Couldn't listen on service " & irServiceName & ": " _
                & exSoc.ErrorCode)
        End Try

        ' Show listening for selected device.
        StatusBar1.Text = "Listening for " & ListBox1.SelectedItem.ToString()

        ' Create a client connection for the
        ' service detected by the listener.
        Dim irClient As IrDAClient
        Try
            irClient = irListen.AcceptIrDAClient()
        Catch exp As SocketException
            MsgBox("Couldn't accept socket " & exp.ErrorCode)
            Return
        End Try

        ' Show whether a transfer from
        ' the remote device is pending.
        If irListen.Pending() = True Then
            StatusBar1.Text = "Pending from " & irClient.RemoteMachineName
        Else
            StatusBar1.Text = "Not pending from " & irClient.RemoteMachineName
        End If

        ' Get the underlying stream of the client.
        Dim baseStream As Stream = irClient.GetStream()

        Dim numToRead As Integer

        'Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte

        ' Read the stream of data, which contains
        ' the data from the remote device, until
        ' there are no more bytes to read.
        numToRead = 4
        While numToRead > 0
            Dim numRead As Integer = baseStream.Read(buffer, 0, numToRead)
            numToRead -= numRead
        End While

        ' Get the size of the buffer to show
        ' the number of bytes to write to the file.
        numToRead = BitConverter.ToInt32(buffer, 0)
        StatusBar1.Text = "Going to write " & numToRead & " bytes"

        ' Write the stream to the file until
        ' there are no more bytes to read.
        While numToRead > 0
            Dim numRead As Integer = baseStream.Read(buffer, 0, buffer.Length)
            numToRead -= numRead
            writeStream.Write(buffer, 0, numRead)
        End While

        ' Show that the file was received.
        StatusBar1.Text = "File received"

        baseStream.Close()
        writeStream.Close()
        irListen.Stop()
        irClient.Close()
    End Sub 
End Class

System.Object
   System.Net.EndPoint
    System.Net.Sockets.IrDAClient

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft