.NET Components for Mobility

Send file to device using custom service

Last post 07-17-2009 12:59 PM by createSoftware. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 06-15-2009 6:48 PM

    Send file to device using custom service

    Hi

     I am new to the 32feet.Net Library but i am an experienced .NET programmer.

     I am trying to write some code to send a file to a client program on a WM6 device using a custom service. I followed the example in the DeviceApplicationBT5 example but i can not connect to the service i specified.

     I am running windows XP 64 with bluetooth dongle and visual studio 2008.

    I also noticed that even when i try to connect to the other listed services nothing happens - is it possible that the OS is picking up the request ?

     Do i need to specify a service record in detail ? i would be most grateful for a sample code line to defince a service and start it.

     Thanks

    Hanson

    Hanson

    "Send the lift down for others to come up"
    Filed under: , , ,
  • 06-18-2009 9:03 AM In reply to

    Re: Send file to device using custom service

    I don't know "DeviceApplicationBT5" where's it from?

    What release are you using?  To use the library on Win32 x64 you need at least version 2.3 [1] (and for BluetoothListener, 2.4 [2]).

    There are simple code samples in the User Guide (in the release or at http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html).  See also the sample programs in the release.

    BluetoothListener will automatically add a service record suitable for most situations.  It also can be supplied with a custom service record if you require.

     

    [1] http://32feet.codeplex.com/WorkItem/View.aspx?WorkItemId=14698

    [2] http://32feet.codeplex.com/WorkItem/View.aspx?WorkItemId=22501

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 06-30-2009 9:16 AM In reply to

    Re: Send file to device using custom service

     Hi Alan,

     

    Sorry for the delayed reply - i was on a short break. 

     

    Thank you for your quick reply - shortly after i posted that i realised that BluetoothListener adds a service recod so i will try to use that. 


    DeviceApplicationBT5 is one of 6 example applications in the IntheHand code sample archive. It shows how to access a service.

     I will read up a bit more on the custom service method and i'll get back to you if i need to perhaps with a more specific example

     

    Thanks

     

    Hanson

    Hanson

    "Send the lift down for others to come up"
  • 06-30-2009 3:18 PM In reply to

    Re: Send file to device using custom service

    I guess that sample must be in a very old release because I know nothing of it.

    I'm somewhat confused when your are "*Send* file to" and are looking at ServiceRecords with BluetoothListener.  Are you creating both the server and the client code?

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 06-30-2009 7:38 PM In reply to

    Re: Send file to device using custom service

     Hi

     

    Yes I am building the client and the server. I figured out the problem - i needed to use a thread to control the reception of messages/files and i hadn't implemented that part properly so  i wasn't seeing any response or even an error - this was my first time with bluetooth:-)

     Anyway thanks for your help i may post back a bit of my solution - someone may need the help too

    Hanson

    "Send the lift down for others to come up"
  • 07-17-2009 12:59 PM In reply to

    Re: Send file to device using custom service

     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"

    Hanson

    "Send the lift down for others to come up"
Page 1 of 1 (6 items)
Copyright © 2001-2010 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.