Hi
Here is the sample code i promised in connecting PC - Mobile - PC (VB.NET)
1. Start PC Listener in a thread
Try
listener = New BluetoothListener(MyServiceUuid)
listener.Start()
Dim run As Boolean = True
Dim btc As BluetoothClient
Dim sr As StreamReader
While run
btc = listener.AcceptBluetoothClient()
sr = New StreamReader(btc.GetStream())
While Not sr.EndOfStream ' read one line or many lines
message += sr.ReadLine()
End While
' Check if this method is running on a different thread
' than the thread that created the control.if you are using a control to output what you receive. If not then you can justuse the stream directly in the thread
If Me.YourOUTPUTControl.InvokeRequired Then
' It's on a different thread, so use Invoke. where d is your delegate
Dim d As New OUTPUTprocedureCallBack(AddressOf OUTPUTprocedure)
Me.Invoke(d, New Object() {[message]})
Else
' It's on the same thread, no need for Invoke.
Me.YourOUTPUTControl.Text = [message]
End If
End While
sr.Close()
Catch ex As Exception
End Try
2. Send file to Mobile Device using OBEX
' bi is the bluetoothDeviceInfo of the device you want to send to
Dim uri As System.Uri = New Uri("obex://" + bi.DeviceAddress.ToString() + "/test.jpg")
Dim request As ObexWebRequest = New ObexWebRequest(uri)
request.ReadFile("test.jpg")
Dim response As ObexWebResponse = CType(request.GetResponse(), ObexWebResponse)
Cursor.Current = Cursors.Default
' get the response
OUTPUTCONTROL.Text = response.StatusCode.ToString()
response.Close()
3. Receive file on Mobile device
' Setup your listener
listener = New ObexListener(ObexTransport.Bluetooth)
items = New ArrayList()
listener.Start()
' listen (preferably in a thread
Dim olr As ObexListenerRequest
While (listener.IsListening)
Try
Dim olc As ObexListenerContext = listener.GetContext()
olr = olc.Request
Dim filename As String = Uri.UnescapeDataString(olr.RawUrl.TrimStart("/"))
' optional if the device fits parameters to send a file or you can just use the stream data directly from olr.InputStream() and process things in memory
olr.WriteFile(filename)
' keep a record if you want
items.Add(filename)
Catch ex As Exception
MsgBox(ex.Message)
End Try
' remember that if you want to use olr.InputStream() instead of write file it still needs to be in the loop
End While
4. Send message to PC
' need to connect to the service defined earlier with MyServiceUUID
' bi is again the BluetoothDeviceInfo of the PC/Device you want to send message/file/response to
Dim btc As New BluetoothClient()
btc.Connect(bi.DeviceAddress(), MyServiceUUID)
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(btc.GetStream())
sw.WriteLine("Hello World ")
sw.Close()
This is just one set of ways to connect, there are many more with this library as you all know . I hope this helps someone.
Hanson
"Send the lift down for others to come up"