a little help in bluetooth

J

jabslim

excuse me, im a newbie programmer and i need a code on how to to be able to
send files from a mobile phone to a pc through bluetooth in vb.net

can anyone post a code here on how to detect a bluetooth device and how to
send files to the pc in vb.net?

i would really really appreciate your help!!
 
J

JDS

excuse me, im a newbie programmer and i need a code on how to to be able to
send files from a mobile phone to a pc through bluetooth in vb.net

can anyone post a code here on how to detect a bluetooth device and how to
send files to the pc in vb.net?

i would really really appreciate your help!!

One way to do it is to assign a com port to the PC Bluetooth
connection and then use System.IO.SerialPort. You will then also need
a program on the PC to receive the file.

HTH
 
J

jabslim via DotNetMonster.com

One way to do it is to assign a com port to the PC Bluetooth
connection and then use System.IO.SerialPort. You will then also need
a program on the PC to receive the file.

HTH



thanks for the info!! if it's convenient for you, can you please post a
sample on how to really use the codes for it? because i haven't the slightest
idea on how to really control the dongle(bluetooth) using codes in vb.net. i
have searched and read other forums about bluetooth, but i just cant
understand how it is being controlled to receive files.

what kind of program for the pc do you recommend for the dongle?
 
J

JDS

thanks for the info!! if it's convenient for you, can you please post a
sample on how to really use the codes for it? because i haven't the slightest
idea on how to really control the dongle(bluetooth) using codes in vb.net. i
have searched and read other forums about bluetooth, but i just cant
understand how it is being controlled to receive files.

what kind of program for the pc do you recommend for the dongle?

Try the following. It is only a test piece of code and does not have
any file IO but you should be able to adapt it easily. It has a simple
form with a combo box to display the available com ports (you will
have to assign a com port to the Bluetooth connection manually
beforehand) and a text box that displays received data (well the first
6 characters of each received line in this case).

For testing you could use Hyperterminal on the PC and the functions on
the Transfer menu ("Capture text..." and "Send text file..."). Again
you will have to assign a com port to the Bluetooth connection
manually beforehand.

Imports System.IO.Ports

Public Class frmMain
#Region "Variables"
Dim WithEvents mserialPort As New SerialPort
Dim mstrRxData As String
#End Region

#Region "Form"
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim strPorts() As String
strPorts = SerialPort.GetPortNames
For Each port As String In strPorts
Me.cbComPort.Items.Add(port)
Next
If Me.cbComPort.Items.Count = 0 Then
MsgBox("No com ports found")
Else
Me.cbComPort.SelectedIndex = 0
End If
Catch ex As Exception
MsgBox("Form load error: " & ex.Message)
End Try
End Sub
#End Region

#Region "Data"
Private Sub DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
mserialPort.DataReceived
Try
Me.tbRxData.Invoke(New myDelegate(AddressOf ProcessData),
New Object() {})
Catch ex As Exception
MsgBox("DataReceived() error: " & ex.Message)
End Try
End Sub

Public Delegate Sub myDelegate()
Public Sub ProcessData()
Try
Dim rxData As String = ""
Static strBuffer As String

rxData = mserialPort.ReadExisting
strBuffer &= rxData
If strBuffer.EndsWith(vbCrLf) Then
Me.tbRxData.Text = Mid(strBuffer, 1, 6)
strBuffer = ""
End If
Catch ex As Exception
MsgBox("ProcessData() error: " & ex.Message)
End Try
End Sub
#End Region

#Region "Menus"
Private Sub mnuConnect_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuConnect.Click
Try
With mserialPort
If Not .IsOpen Then
.PortName = Me.cbComPort.Text
.Open()
Me.tbRxData.Text = "Connected"
Me.mnuConnect.Text = "Disconnect"
Me.mnuExit.Enabled = False
Else
.Close()
Me.tbRxData.Text = "Disconnected"
Me.mnuConnect.Text = "Connect"
Me.mnuExit.Enabled = True
End If
End With
Catch ex As InvalidOperationException
MsgBox("mnuConnect error: port already open")
Catch ex As ArgumentOutOfRangeException
MsgBox("mnuConnect error: invalid port setting")
Catch ex As ArgumentException
MsgBox("mnuConnect error: invalid port name or file type")
Catch ex As IO.IOException
MsgBox("mnuConnect error: invalid port state or port
setting")
Catch ex As UnauthorizedAccessException
MsgBox("mnuConnect error: access denied to specified
port")
Catch ex As Exception
MsgBox("mnuConnect error: " & ex.Message)
End Try
End Sub

Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuExit.Click
Application.Exit()
End Sub
#End Region
End Class
 
J

jabslim via DotNetMonster.com

thanks a lot pal!! ill try to understand and learn how the code works!!

if i have further questions, ill just post it here again!!

once again, i really thank you!! you're really helpful!!
 
K

kimiraikkonen

Try the following. It is only a test piece of code and does not have
any file IO but you should be able to adapt it easily. It has a simple
form with a combo box to display the available com ports (you will
have to assign a com port to the Bluetooth connection manually
beforehand) and a text box that displays received data (well the first
6 characters of each received line in this case).

For testing you could use Hyperterminal on the PC and the functions on
the Transfer menu ("Capture text..." and "Send text file..."). Again
you will have to assign a com port to the Bluetooth connection
manually beforehand.

Imports System.IO.Ports

Public Class frmMain
#Region "Variables"
Dim WithEvents mserialPort As New SerialPort
Dim mstrRxData As String
#End Region

#Region "Form"
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim strPorts() As String
strPorts = SerialPort.GetPortNames
For Each port As String In strPorts
Me.cbComPort.Items.Add(port)
Next
If Me.cbComPort.Items.Count = 0 Then
MsgBox("No com ports found")
Else
Me.cbComPort.SelectedIndex = 0
End If
Catch ex As Exception
MsgBox("Form load error: " & ex.Message)
End Try
End Sub
#End Region

#Region "Data"
Private Sub DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
mserialPort.DataReceived
Try
Me.tbRxData.Invoke(New myDelegate(AddressOf ProcessData),
New Object() {})
Catch ex As Exception
MsgBox("DataReceived() error: " & ex.Message)
End Try
End Sub

Public Delegate Sub myDelegate()
Public Sub ProcessData()
Try
Dim rxData As String = ""
Static strBuffer As String

rxData = mserialPort.ReadExisting
strBuffer &= rxData
If strBuffer.EndsWith(vbCrLf) Then
Me.tbRxData.Text = Mid(strBuffer, 1, 6)
strBuffer = ""
End If
Catch ex As Exception
MsgBox("ProcessData() error: " & ex.Message)
End Try
End Sub
#End Region

#Region "Menus"
Private Sub mnuConnect_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuConnect.Click
Try
With mserialPort
If Not .IsOpen Then
.PortName = Me.cbComPort.Text
.Open()
Me.tbRxData.Text = "Connected"
Me.mnuConnect.Text = "Disconnect"
Me.mnuExit.Enabled = False
Else
.Close()
Me.tbRxData.Text = "Disconnected"
Me.mnuConnect.Text = "Connect"
Me.mnuExit.Enabled = True
End If
End With
Catch ex As InvalidOperationException
MsgBox("mnuConnect error: port already open")
Catch ex As ArgumentOutOfRangeException
MsgBox("mnuConnect error: invalid port setting")
Catch ex As ArgumentException
MsgBox("mnuConnect error: invalid port name or file type")
Catch ex As IO.IOException
MsgBox("mnuConnect error: invalid port state or port
setting")
Catch ex As UnauthorizedAccessException
MsgBox("mnuConnect error: access denied to specified
port")
Catch ex As Exception
MsgBox("mnuConnect error: " & ex.Message)
End Try
End Sub

Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuExit.Click
Application.Exit()
End Sub
#End Region
End Class

Does it work for USB Bluetooth dongles?
 
J

JDS

Does it work for USB Bluetooth dongles?- Hide quoted text -

- Show quoted text -

I have used it with a USB Bluetooth dongle on the PC communicating
with a Windows Mobile device. It is not that sophisticated as it
requires the user to assign a com port to the Bluetooth interface on
both the PC and the mobile device - but it works! I am sure there will
be ways of creating a more user-friendly interface by handling the
Bluetooth directly but it is bound to require more programming.

One note on assigning ports. The terminology uses "outgoing" and
"incoming" which is a little confusing as it does not relate to the
direction of the data. It refers to which device initiates the
connection; i.e. if the PC is listening for the connection and the
mobile device initiates, then the PC port will be "incoming" and the
mobile device "outgoing" even if all the data is transmitted only from
the PC to the mobile device.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top