VB.NET - To Send an IrDA Transfer



Sends a text file using infrared transmission to another device.

'Assembly: System.Windows.Forms.dll
'Assembly: System.Net.IrDA.dll

'Namespace: Microsoft.VisualBasic
'Namespace: System
'Namespace: System.IO
'Namespace: System.Net
'Namespace: System.Net.Sockets
'Namespace: System.Windows.Forms


    Private Sub sendIrDAData(ByVal irServiceName As String, ByVal dataFile As String)

        Dim irClient As New IrDAClient()
        Dim irDevices() As IrDADeviceInfo
        Dim buffersize As Integer = 256

        ' Create a collection of devices to discover.
        irDevices = irClient.DiscoverDevices(2)

        ' Connect to the device
        Dim irEndP As New IrDAEndPoint(irDevices(0).DeviceID, irServiceName)
        Dim irListen As New IrDAListener(irEndP)
        irListen.Start()
        irClient = irListen.AcceptIrDAClient()

        ' Open a Pocket Word file to send and get its stream.
        Dim fs As Stream
        fs = New FileStream(dataFile, FileMode.Open)

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

        Dim length As Byte() = BitConverter.GetBytes(fs.Length)
        baseStream.Write(length, 0, length.Length)

        ' Create buffer for reading the file.
        Dim buffer(buffersize) As Byte
        Dim fileLength As Integer = CInt(fs.Length)

        ' 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
        fs.Close()
        baseStream.Close()
        irClient.Close()

    End Sub

Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.