.NET Components for Mobility

Check if a specific BT client is in range

Last post 07-13-2007 11:32 AM by alanjmcf. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 05-09-2007 11:56 PM

    • JacobS
    • Not Ranked
    • Joined on 05-09-2007
    • Posts 0

    Check if a specific BT client is in range

    Hello.

    Is it possible to check if a specific  BT device is in range of the PC.

    I have the BluetoothDeviceInfo list but I dont know if the devices are in range or not.

    Thanks. 

    Filed under:
  • 05-11-2007 11:07 AM In reply to

    Re: Check if a specific BT client is in range

    That's something I've wondered about myself.  And I suspect that there's no way to know without trying to connect to the device.  For instance, the LastSeen property seems to reports the time that the discovery process run, and the LastUsed property reports the last time there was a connection (since the PC was rebooted?).  And there's no flag 'InRange' in addition to the Remembered, Authenticated, and Connected flags.

    Of course the library can only report what the native protocol stack reports, and it seems that even it tries to connect to see if a device is in range, for instance when I open a device's properties dialog and go to the services page it takes some time to report that the device isn't in range...

    Maybe someone knows better?

    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-14-2007 11:34 AM In reply to

    Re: Check if a specific BT client is in range

    On the desktop you can do a DiscoverDevices and check the LastSeen property matches the time of this latest search. But a much quicker way is to attempt to connect to the device and handle an exception if it fails.

    Peter

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

    Re: Check if a specific BT client is in range

    After some checking Peter and I have found that the native value that we use for the LastSeen property doesn't return what it is documented to return!  It always just returns the time when the discovery was completed(?), we checked both XP SP2 and Vista.  So this property sadly isn't useful to find whether a particular device is in range.

     

    As evidence..  The new SdpBrowserDesktop sample displays information on discovery as follows.  The two devices listed there are not in range, they are in fact both turned off, but it reports LastSeen as a time after the discovery started.

    Discovery process started at 05/14/2007 11:01:46 UTC, 05/14/2007 12:01:46 local

    * FooBar1
    Address: 000999011B99
    Remembered: True, Authenticated: True, Connected: False
    LastSeen: 05/14/2007 11:01:56, LastUsed: 01/01/0001 00:00:00
    CoD: (0x00020114)
     Device:  PdaComputer (0x00000114)
     Service: Network (0x00000010)

    * FooBar2
    Address: 000999686599
    Remembered: True, Authenticated: True, Connected: False
    LastSeen: 05/14/2007 11:01:56, LastUsed: 01/01/0001 00:00:00
    CoD: (0x00720104)
     Device:  DesktopComputer (0x00000104)
     Service: Network, ObjectTransfer, Audio, Telephony (0x00000390)

    As evidence that it is the native method which is wrong, when I go to the Windows Bluetooth Control panel and view the Device Properties dialog for the first of those devices it reports:

    Last connected: 14 May 2007 at 12:07:51

    So that gets it wrong too, presumably using the same value...

    This is not how it is described in MSDN...  At http://msdn2.microsoft.com/en-us/library/aa362924.aspx it says:

    stLastSeen Last time the device was seen, in the form of a SYSTEMTIME structure.
    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-15-2007 9:02 PM In reply to

    • JacobS
    • Not Ranked
    • Joined on 05-09-2007
    • Posts 0

    Re: Check if a specific BT client is in range

    I managed to check by trying to connect to the device and if I manage I know it is in range, my problem is that when the device is being used by a different app I cant connect to it.
    I think I am using the connect method the wrong way.

    BluetoothClient client = new BluetoothClient();
    BluetoothDeviceInfo[] DeviceInfo = client.DiscoverDevices();
    try
    {
      client.Connect(DeviceInfo [1].DeviceAddress, BluetoothService.SerialPort);
      client.Close();
      return IN_RANGE;
    }
    catch
    {
       return NOT_IN_RANGE;
    }

    Any ideas, thanks.
  • 05-16-2007 11:45 AM In reply to

    Re: Check if a specific BT client is in range

    You can check the socket error code (SocketException.SocketErrorCode or on NETCF SocketException.ErrorCode) and use it to find what fault occurred: not in range, or something else e.g. a connection already is active.

    For instance I see error code SocketError.TimedOut (10060) when a device isn't in range, and SocketError.AddressAlreadyInUse (10048) when a there's already connection to the tested service.  So inside your catch, check the value, and return IN_RANGE or NOT_IN_RANGE respectively.  See http://msdn2.microsoft.com/en-us/library/aa362901.aspx for the official list of error reasons on Bluetooth.  Hope that does the job...

    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.
  • 07-11-2007 10:03 AM In reply to

    Re: Check if a specific BT client is in range

    scanning for devices and trying to connect to them is fine...  annoying and if I can get around that I would rather that...

    However I want to scan for mobile phones, I don't really want to connect to them... but for testing to see if they're really there in range I guess I need to try...  the default timeout seems to be 30 seconds...

    Is there a way to set that timeout to be less.. like say a millisecond or something... or is it a mobile phone issue not a bluetooth scanning timeout...??

    Any suggestions would be greatly appreciate..

    Thanks

  • 07-11-2007 11:43 AM In reply to

    Re: Check if a specific BT client is in range

    Well if attempting a connection is the way one checks, then there are various way to shorten the time to wait.  For instance see the code at http://32feet.net/forums/p/377/1251.aspx#1274, i.e. use BeginConnect and WaitOne on its event but also with a timeout.  There should also be a socket level timeout setting for connect, maybe ReceiveTimeout will do, I can't remember.  Or there's always the create new thread to do the connect, and wait on an event it sets -- though the first method above (APM+WaitOne) seems similar to that but better.  So would something along those lines do?

    (There are other methods apart from connect, e.g. BluetoothDeviceInfo.GetServiceRecords, but it seems worse that using connect).

    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.
  • 07-12-2007 8:07 AM In reply to

    Re: Check if a specific BT client is in range

    Might be ok...  I can't seem to find the Beginconnect in the vb side of things...

    The only problem with even a connect or I'm guessing a beginconnect is that I'll be polling for devices every 10 seconds...  My nokia beeps whenever a connection is attempted, I guess to alert me that this is happening so I can confirm the connection on the phone...

    Is there a way to take items off the stack or clear it out rather than trying to connect to the devices...???

     

  • 07-13-2007 11:32 AM In reply to

    Re: Check if a specific BT client is in range

    [edit: Remove all the typos -- must be a Friday afternoon!]

    It's a commonly asked question so we'd like to provide a simple non-instrusive method of checking a peer's availability.  Trying to make a connection is the current best method.  Do you know if the Windows Bluetooth control-panel user-interface etc provides a way to check -- that doesn't cause your phone to beep?

    Also try the GetServiceRecords method on BluetoothDeviceInfo, maybe it's less instrusive?  (Just add that simple call to your code.  Otherwise you could use the SdpBrowserXxxx sample in the current sources to check, the three buttons in the second row each use that method.)

    The BeginConnect method is on BluetoothClient.  It will try a single connect request.  Discovery doesn't make your phone beep I presume.  Its a shame that its LastSeen property is useless (http://www.codeplex.com/32feet/WorkItem/View.aspx?WorkItemId=10280)  If you have the time you could open and call with Microsoft PSS to first report that fault, and secondly ask if there's another better method.  Maybe they would be prompted to first, and as its a bug they will (should!) refund any charge.

    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.
Page 1 of 1 (10 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.