Bjenkins_X_123
Member
+11|6713|London, UK
I've hit a roadwall on a college project. I downloaded a piece of 'Hello World' VB code that supposedly enables Bluetooth chat, and I'm trying to modify it to integrate file transfers to be able to happen as well. I managed to work out the OpenFileDialog section, but I'm not sure how to make  the file selected send over Bluetooth. Can anyone help me please? VB is, quite apparently, not my strong point, and I'm truly stumped. I'll pay back in the only way I can (with plenty of karma), along with my eternal gratitude.

Currently running Visual Studio 2008.

-----

Imports System.Runtime.InteropServices
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

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

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    'Friend WithEvents InputPanel1 As Microsoft.WindowsCE.Forms.InputPanel
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents txtMessage As System.Windows.Forms.TextBox
    Friend WithEvents txtMessageLog As System.Windows.Forms.TextBox
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.txtMessage = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.txtMessageLog = New System.Windows.Forms.TextBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.Add(Me.MenuItem1)
        '
        'MenuItem1
        '
        Me.MenuItem1.MenuItems.Add(Me.MenuItem2)
        Me.MenuItem1.MenuItems.Add(Me.MenuItem3)
        Me.MenuItem1.Text = "Settings"
        '
        'MenuItem2
        '
        Me.MenuItem2.Text = "Connect"
        '
        'MenuItem3
        '
        Me.MenuItem3.Text = "Disconnect"
        '
        'txtMessage
        '
        Me.txtMessage.Location = New System.Drawing.Point(8, 8)
        Me.txtMessage.Name = "txtMessage"
        Me.txtMessage.Size = New System.Drawing.Size(224, 21)
        Me.txtMessage.TabIndex = 2
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(160, 164)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 20)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Send"
        '
        'txtMessageLog
        '
        Me.txtMessageLog.Location = New System.Drawing.Point(8, 190)
        Me.txtMessageLog.Multiline = True
        Me.txtMessageLog.Name = "txtMessageLog"
        Me.txtMessageLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtMessageLog.Size = New System.Drawing.Size(224, 66)
        Me.txtMessageLog.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(8, 51)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(224, 21)
        Me.TextBox1.TabIndex = 3
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(8, 87)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(72, 20)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Browse"
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.FileName = "OpenFileDialog1"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(159, 87)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(72, 20)
        Me.Button3.TabIndex = 5
        Me.Button3.Text = "Send File"
        '
        'Form1
        '
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
        Me.ClientSize = New System.Drawing.Size(240, 268)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.txtMessageLog)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.txtMessage)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Chat"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim infileHandler As Long
    Dim outfileHandler As Long
    Dim buffer() As Byte
    Dim lBytes As Long
    Dim temp As String
    Dim numReadWrite As Integer
    Dim t1 As System.Threading.Thread
    Dim stopThread As Boolean = False

    Public Sub connect()
        '---port number for Bluetooth connection
        Dim inPort As Short = 7
        Dim outPort As Short = 8

        '---Opens the port for Bluetooth
        infileHandler = CreateFile("COM" & inPort & ":", _
                                    &HC0000000, 0, 0, 3, 0, 0)
        Application.DoEvents()
        outfileHandler = CreateFile("COM" & outPort & ":", _
                                    &HC0000000, 0, 0, 3, 0, 0)
        Application.DoEvents()

        '---invoke the thread to receive incoming messages
        stopThread = False
        t1 = New Threading.Thread(AddressOf receiveLoop)
        t1.Start()
    End Sub

    Public Sub disconnect()
        stopThread = True
        CloseHandle(infileHandler)
        CloseHandle(outfileHandler)
    End Sub


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

    End Sub
    Public Function stringToByteArray(ByVal str As String) As Byte()
        '---e.g. "abcdefg" to {a,b,c,d,e,f,g}
        Dim s As Char()
        s = str.ToCharArray
        Dim b(s.Length - 1) As Byte
        Dim i As Integer
        For i = 0 To s.Length - 1
            b(i) = Convert.ToByte(s(i))
        Next
        Return b
    End Function
    Function byteArrayToString(ByVal b() As Byte) As String
        '---e.g. {a,b,c,d,e,f,g} to "abcdefg"
        Dim str As String
        Dim enc As System.Text.ASCIIEncoding
        enc = New System.Text.ASCIIEncoding
        str = enc.GetString(b, 0, b.Length())
        Return str
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function CreateFile(ByVal lpFileName As String, _
                                       ByVal dwDesiredAccess As Integer, _
                                       ByVal dwShareMode As Integer, _
                                       ByVal lpSecurityAttributes As Integer, _
                                       ByVal dwCreationDisposition As Integer, _
                                       ByVal dwFlagsAndAttributes As Integer, _
                                       ByVal hTemplateFile As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
   Private Shared Function ReadFile(ByVal hFile As Integer, _
                                    ByVal Buffer() As Byte, _
                                    ByVal nNumberOfBytesToRead As Integer, _
                                    ByRef lpNumberOfBytesRead As Integer, _
                                    ByRef lpOverlapped As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function WriteFile(ByVal hFile As Integer, _
                                      ByVal Buffer() As Byte, _
                                      ByVal nNumberOfBytesToWrite As Integer, _
                                      ByRef lpNumberOfBytesWritten As Integer, _
                                      ByVal lpOverlapped As Integer) As Boolean
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
    End Function

    Public Function send(ByVal message As String) As Integer
        '---send the message through the serial port
        Dim value As String = message & vbCrLf
        Dim retCode As Integer = WriteFile(outfileHandler, _
                                           stringToByteArray(value), _
                                           value.Length(), _
                                           numReadWrite, _
                                           0)
        txtMessageLog.Text += value
        Return retCode
    End Function

    Public Sub receiveLoop()
        '---receive the message through the serial port
        Dim inbuff(300) As Byte
        Dim retCode As Integer = ReadFile(infileHandler, _
                                          inbuff, _
                                          inbuff.Length, _
                                          numReadWrite, _
                                          0)
        Application.DoEvents()
        While True
            If retCode = 0 Or stopThread Then
                'MsgBox("Error reading message.")
                Exit While
            Else
                Dim updateDelegate As New  _
                    myDelegate(AddressOf updateMessageLog)

                updateDelegate.Invoke(byteArrayToString(inbuff))
                ReDim inbuff(300)
                retCode = ReadFile(infileHandler, _
                                   inbuff, _
                                   inbuff.Length, _
                                   numReadWrite, _
                                   0)
                Application.DoEvents()
            End If
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button1.Click
        If send(txtMessage.Text) = 0 Then
            MsgBox("Error sending message.")
        End If
    End Sub

    Public Delegate Sub myDelegate(ByVal str As String)

    Public Sub updateMessageLog(ByVal str As String)
        If str.Length > 0 Then
            txtMessageLog.Text += "-->" & str
        End If
    End Sub

    Private Sub MenuItem1_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles MenuItem1.Click

    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        connect()
        MenuItem2.Enabled = False
        MenuItem3.Enabled = True
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        disconnect()
        MenuItem2.Enabled = True
        MenuItem3.Enabled = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        OpenFileDialog1.ShowDialog()
        TextBox1.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        lBytes = 0

        '

    End Sub

End Class
Harmor
Error_Name_Not_Found
+605|6766|San Diego, CA, USA
I hope to God your school is not teaching you VB and that this is a project in your Comparative Language class.
jsnipy
...
+3,276|6740|...

wow old school com objects in .net

pm me with somewhere i can dl ur project ill help you
jsnipy
...
+3,276|6740|...

Assuming your regular text send worked this would be how you send your file. I tested it in the Pocket PC 2003 SE Emulator.

edit: As a side tip use string.Concat()  rather than "string" + "string" + "string". This can save memory and heap size. Normally not a big deal, but on a mobile OS it can make a real difference.

Code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '
        Dim fs As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)

        Dim len As Long = fs.Length

        Dim fileBytes(len) As Byte

        ' To do it proper you want to check size and read in chunks
        fs.Read(fileBytes, 0, System.Int32.Parse(len.ToString()))

        Dim result As Integer = WriteFile(outfileHandler, fileBytes, len, numReadWrite, 0)

        'If result = some success value Then
        '    indicate success
        txtMessageLog.Text = String.Concat(txtMessageLog.Text, TextBox1.Text + "sent") 
        'Else 
        '    indicate fail
        'End If
    End Sub
I feel bad they are using VB for this (or anything lol)
C# would have been

Last edited by jsnipy (2009-06-05 17:05:57)

Bjenkins_X_123
Member
+11|6713|London, UK

jsnipy wrote:

Assuming your regular text send worked this would be how you send your file. I tested it in the Pocket PC 2003 SE Emulator.

edit: As a side tip use string.Concat()  rather than "string" + "string" + "string". This can save memory and heap size. Normally not a big deal, but on a mobile OS it can make a real difference.

Code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '
        Dim fs As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)

        Dim len As Long = fs.Length

        Dim fileBytes(len) As Byte

        ' To do it proper you want to check size and read in chunks
        fs.Read(fileBytes, 0, System.Int32.Parse(len.ToString()))

        Dim result As Integer = WriteFile(outfileHandler, fileBytes, len, numReadWrite, 0)

        'If result = some success value Then
        '    indicate success
        txtMessageLog.Text = String.Concat(txtMessageLog.Text, TextBox1.Text + "sent") 
        'Else 
        '    indicate fail
        'End If
    End Sub
I feel bad they are using VB for this (or anything lol)
C# would have been
I may just fall in love with you and have your babies, thank you so much for your time on this

+1337 karma to you

Board footer

Privacy Policy - © 2024 Jeff Minard