.NET Components for Mobility

Asynchronous Discovery

Last post 06-27-2007 11:24 AM by sackville. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 06-26-2007 2:23 PM

    Asynchronous Discovery

    I've been hunting around the forums and the library, but I can't find any support for or mention of anyone doing asynchronous discovery. Simply put, I want to see all of the discoverable bluetooth devices around me as they come and go. I'd rather not do the old "check every 10 seconds" routine. Can't discovery be handled asynchronously? I'd like to get some sort of callback when a new device is discovered. Ideally there would be a similar callback when a device is "un-discovered" (walks out of range, shuts down, turns off discoverability).

     Anyone doing this?
    Ss.

     

  • 06-26-2007 3:45 PM In reply to

    Re: Asynchronous Discovery

    You could have a thread running (as demonstrated in the chat sample) which attempts discovery once every 5 seconds searching for 10 devices.
    Once the list returns you simply add the items to a dictionary, using the device address as the key.  Obviously, this builds the list of detectable devices continuously, while your main thread continues executing.

    Two methods you could use to discover disappearing devices are to simply use the dictionary as the running list of devices or a seperate thread could attempt connection with each item in the list, removing them when a connection attempt fails.

     Btw, just wondering, but has anyone discovered how to make 32Feet BT API work with the ABE Bluetooth Controller which Microsoft is distributing via Windows Update?  It's driving me nuts and I can't figure out how to remove the ABE drivers!

    matt

  • 06-27-2007 11:03 AM In reply to

    Re: Asynchronous Discovery

    There's nothing in the current library to do this.  In desktop Windows, one uses window messages to detect the arrival/departure/etc of devices, see "Bluetooth and WM_DEVICECHANGE Messages (Windows)" http://msdn2.microsoft.com/en-gb/library/aa362912.aspx  I've a example WinForms app that implements this.  I'll upload it sometime when I get a moment.

    BTW I'm not sure when Windows raises an 'arrival' message.  I've a feeling that a new device is only seen when either it tries to connect to us, we try to connect to it, or one of the two devices does discovery.  However you can check that for yourself.

    I do mean to include that functionality in the library but haven't quite worked out all the details regarding creating a non-visible Form in WinForms and non-UI apps.  Also what kind of callback/event to expose...  Whether this functionality can be exposed in CE too...  And need testing support too.

    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-27-2007 11:24 AM In reply to

    Re: Asynchronous Discovery

    Here's what I ended up doing:

            private const int MAX_DEVICES = 10;
            private const int INQUIRY_LENGTH = 100000000; // 10 seconds = 100,000,000
            private bool discovering;
            private BluetoothClient bc;
    
            public frmDiscoverDevices()
            {
                InitializeComponent();
                bc = new BluetoothClient();
                bc.InquiryLength = new TimeSpan(INQUIRY_LENGTH);
            }
    
            private void btnDiscover_Click(object sender, EventArgs e)
            {
                if (!discovering)
                {
                    discovering = true;
                    Thread t = new Thread(LoopDiscovery);
                    t.Start();
                    btnDiscover.Text = "stop";
                }
                else
                {
                    discovering = false;
                    btnDiscover.Text = "discover";
                }
            }
    
            private void LoopDiscovery()
            {
                BluetoothDeviceInfo[] devInfos;
                while (discovering)
                {
                    devInfos = bc.DiscoverDevices(MAX_DEVICES);
    
                    foreach (BluetoothDeviceInfo devInfo in devInfos)
                    {
                        // Process discovered device
                    }
                }
            }
    
            private void frmDiscoverDevices_Closing(object sender, CancelEventArgs e)
            {
                discovering = false;
                bc.Close();
            }
    

     

    It works out pretty well so far. If there's any way I can help with adding this type of functionality to the library, let me know. It seems like it would go well with the other asynchronous functionality you've already got.

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