.NET Components for Mobility

Serial Port device discovery

Last post 05-23-2007 6:28 PM by dariosalvi. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 05-11-2007 12:48 PM

    Serial Port device discovery

    Hi,


    I am trying to implement the following application:
    I need to search for all the devices that support the serial port emulation and, if needed, I want to pair with a known pin.
    I am using many functionalities of the library but I can't obtain it. Can anyone help me to choose the rigth instructions?

    Here is an example:

    BluetoothClient client = new BluetoothClient();

    BluetoothDeviceInfo[] devices = client.DiscoverDevices();

    if (!device.Authenticated)
    {
        client.SetPin(device.DeviceAddress, pin);
    }
    foreach (Guid service in device.InstalledServices)
    {
        if (service.CompareTo(InTheHand.Net.Bluetooth.BluetoothService.SerialPort) == 0)
        {
            //ok
        }
    }

    But it doesn't work

    The SetPin gives me an exception (always) and the InstalledServices is always empty.

    I am using a Qtek S100 with Windows mobile 2003.

    Any suggestion ?
  • 05-11-2007 2:06 PM In reply to

    Re: Serial Port device discovery

    Hi, I repsonded to your question on codeplex (http://www.codeplex.com/32feet/Thread/View.aspx?ThreadId=10055), but let's continue here.  My response there was:

    Hi

    Well I'm not too knowledgable about the use of SetPin etc, so apart from asking what exception you receive, I can't offer any advice now.

    On the search for services, well the InstalledDevices property (on desktop, calls the native method BluetoothEnumerateInstalledServices which) apparently returns the services already configured for use. Those are the ones that appear, and are checked, in the "Services" tab of the device property sheet in the Bluetooth Control panel... I presume the behaviour is similar on CE.

    So instead you should use BluetoothDeviceInfo.GetServiceRecords passing in BluetoothService.SerialPort. That should do the job!

    For 100% surety that any records returned are truly for the SPP, one should really check the content of the record. The SDP search by UUID looks anywhere in the record for the UUID, but we want to be sure that it is in the ServiceClassIdList attribute's element. I'd not worry about that initially unless you see it occuring as a problem. If you use the development code (from here) and compile it yourself the you can of course Dump the record to see its content, and also access its attributes directly to see if it the correct type.
    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.
  • 05-11-2007 2:21 PM In reply to

    Re: Serial Port device discovery

    My problem is setting the PIN without the user being promped to do.

    I have tried the InstalledServices but it gives the Serial service ONLY after the device has been connected (so after it has been paired).

     

    Finally I resolved the issue using this code:

                        InTheHand.Net.BluetoothEndPoint ep = new InTheHand.Net.BluetoothEndPoint(device.DeviceAddress, InTheHand.Net.Bluetooth.BluetoothService.SerialPort);

                        if (!device.Authenticated)
                        {
                            InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(device.DeviceAddress, pin);
                        }
                        client.Connect(ep);

                        client.Close();

    which seems to work (I am still testing it).

    Now I have the inverse problem: I want my application to listen on the BT and wait for a serial connection request.
    On the request (which might be with security) I want the PIN to be passed by the application (without prompting to the user), and I want the serial connection to be established.

    I am looking at BluetoothSerialPort and BluetoothListener classes but I don't see the way to get the device info of the client which is trying to connect to the application.

     

    Any suggestion here ?

    Thanks. 

  • 05-14-2007 11:23 AM In reply to

    Re: Serial Port device discovery

    If you use SetPin the pin will be stored, if the other device request a PIN it will be sent automatically and you will not get prompted. To listen for incoming connections use BluetoothListener. Check out the Server property which gives access to the listening Socket - you can then access the remote EndPoint to determine what device has connected.

    Peter

    Peter Foot
    Microsoft Device Application Development MVP
    www.peterfoot.net | www.inthehand.com
  • 05-15-2007 4:35 PM In reply to

    Re: Serial Port device discovery

    dariosalvi:

    I am looking at BluetoothSerialPort and BluetoothListener classes but I don't see the way to get the device info of the client which is trying to connect to the application.

     

    I'm trying to solve the same problem, identifying the device trying to connect. If one doesn't know when they device connects, would asynchronous callbacks be the way to go?

    I've started my listener and added a callback to it.

    btListener.BeginAcceptBluetoothClient(new AsyncCallback(incomingConnection), btListener); 

    In my incomingConnections method Ive tried the server property of the listener, but can't make any good use out of it. What am i looking for in the remote EndPoint? The address family?

     

     

  • 05-16-2007 2:45 PM In reply to

    Re: Serial Port device discovery

    I have trued to use the listener but: I can't get the client's address !
    The Server propoerty has a RemoteEndPoint, but I don't know how to get the Bluetooth address from it.
    Moreover, I need a blocking call that waits for the connection in order to know when the request has been performed:

    The AcceptBluetoothClient returns AFTER the pairing has been done (and doesn't give me the control for setting the pin.
    The BeginAcceptBluetoothClient maybe would work, but how can I get the client address?
     

     
    A possible (and working) solution can be the following:

                this.keepListening = true;

                Thread t = new Thread(new ThreadStart(listening));
                t.Start();

                Thread.Sleep(wait_time * 1000);

                this.keepListening = false;
                listener.Stop();

     

    with a thread:

            private void listening()
            {
                while (this.keepListening)
                {
                    BluetoothAddress incoming = BluetoothSecurity.GetPinRequest();
                    if (incoming != null)
                    {
                            BluetoothSecurity.SetPin(incoming, this.pin);
                    }
                }


    But I don't like this solution (it should be blocking).


  • 05-23-2007 10:26 AM In reply to

    Re: Serial Port device discovery

    For BluetoothClient.Pin/SetPin on CE we use socket option SO_BTH_SET_PIN, to associate the PIN value on the socket.  At http://msdn2.microsoft.com/en-us/library/aa915851.aspx MSDN notes that it can also be used on a listening socket, where if no Bluetooth address is used, "the PIN becomes an access code for all incoming connections."  So that sounds really what you want.

    You can investigate whether this works.  With the sources downloaded from http://www.codeplex.com/32feet copy the Pin and SetPin members from BluetoothClient to BluetoothListener, make any necessary wee changes (e.g. comment out the "if (clientSocket.Connected)" branch), and see if that does what it should!  Let us know if it works and we can made that change in the library.

    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.
  • 05-23-2007 12:49 PM In reply to

    Re: Serial Port device discovery

    I have added the following code to the Listener class:

            public void SetPin(string pin)
            {
                byte[ link = new byte[32];

                //copy PIN
                if (pin != null & pin.Length > 0)
                {
                    if (pin.Length > 16)
                    {
                        throw new ArgumentOutOfRangeException("PIN must be between 1 and 16 characters");
                    }
                    //copy pin bytes
                    byte[ pinbytes = System.Text.Encoding.ASCII.GetBytes(pin);
                    Buffer.BlockCopy(pinbytes, 0, link, 16, pin.Length);
                    BitConverter.GetBytes(pin.Length).CopyTo(link, 0);
                }

                this.serverSocket.SetSocketOption(BluetoothSocketOptionLevel.RFComm, BluetoothSocketOptionName.SetPin, link);
            }
     

    But when I simply declare an instance of the listener class it gives me this exception:

    "Type ElementSequence and ElementAlternative need an list of ServiceElement."

     which seems to be generated by ServiceElement class.

     
    Any suggestion ?

     

    Dario
     

    Filed under:
  • 05-23-2007 2:37 PM In reply to

    Re: Serial Port device discovery

    Ahh, ok, well I probably broke something in the last commit.  I don't have a CE device so I can't do very thorough testing for that platform...  I'll have a look at that as soon as I get some time.  Are you using the CF1 version BTW?  I presume so, as the lack of Generics may be the cause.  If your device has NETCFv2 use that instead and see if it works there.

    If its urgent, download the previous changeset (22358) the one before I changed BluetoothListener to use the SDP classes.

    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.
  • 05-23-2007 4:51 PM In reply to

    Re: Serial Port device discovery

    I've committed a fix (changeset 22841).  That should solve it...
    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.
  • 05-23-2007 6:28 PM In reply to

    Re: Serial Port device discovery

    Thanks,

     

    Yes, I'm using CF1, all the application works with CF1 so I can't change framework right now.

    I will try using the previuos version, and let you know.

     

    Dario 

  • 05-23-2007 6:28 PM In reply to

    Re: Serial Port device discovery

    OK,

    thank you. I'll tell you if it works !

     

    Dario 

Page 1 of 1 (12 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.