Example #1
0
        public virtual IBluetoothDeviceInfo[] DiscoverDevices(int maxDevices,
                                                              bool authenticated, bool remembered, bool unknown, bool discoverableOnly,
                                                              InTheHand.Net.Sockets.BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState)
        {
#if !NETCF
            //   In the MSFT+Win32 API there's no simple way to get only the
            // in-range-discoverable devices, and thus it can't provide
            // 'discoverableOnly' nor live discovery.  So, we register for the
            // native event whilst we run a normal discovery.  We use the
            // native event to raise our live event and to gather the devices
            // to be included in the discoverableOnly result.
            //   Note the event seems NOT be raised on WinXP in this case.
            //   The event (on Win7 at least) raises lots of apparently duplicate
            // events, so we need to do some filtering for both results.
            var realLiveDiscoHandler = liveDiscoHandler;
            //if (realLiveDiscoHandler != null) Debug.Assert(discoverableOnly || unknown, "You want live events from 'remembered'?!?");
            bool keepDiscoOnly = false;
            if (discoverableOnly)
            {
                keepDiscoOnly    = true;
                unknown          = authenticated = remembered = true;
                discoverableOnly = false;
            }
            BluetoothWin32Events events;
            EventHandler <BluetoothWin32RadioInRangeEventArgs> radioInRangeHandler;
            List <IBluetoothDeviceInfo> seenDevices;
            if (keepDiscoOnly || realLiveDiscoHandler != null)
            {
                events              = BluetoothWin32Events.GetInstance();
                seenDevices         = new List <IBluetoothDeviceInfo>();
                radioInRangeHandler = delegate(object sender, BluetoothWin32RadioInRangeEventArgs e) {
                    var newdevice = e.DeviceWindows;
                    Debug.WriteLine("Radio in range: " + newdevice.DeviceAddress);
                    bool unique = BluetoothDeviceInfo.AddUniqueDevice(seenDevices, newdevice);
                    if (unique && realLiveDiscoHandler != null)
                    {
                        Debug.WriteLine("Live IS unique.");
                        realLiveDiscoHandler(newdevice, liveDiscoState);
                    }
                    else     //COVERAGE
                    {
                        Debug.WriteLine("Live is NOT unique ("
                                        + unique + "," + realLiveDiscoHandler != null + ").");
                    }
                };
                events.InRange += radioInRangeHandler;
            }
            else
            {
                events = null;
                radioInRangeHandler = null;
                seenDevices         = null;
            }
            // Don't use the WM live-disco functionality.
            liveDiscoHandler = null;
#endif
            IBluetoothDeviceInfo[] result;
            try {
                result = DoDiscoverDevices(maxDevices,
                                           authenticated, remembered, unknown, discoverableOnly,
                                           liveDiscoHandler, liveDiscoState);
            } finally {
#if !NETCF
                if (events != null && radioInRangeHandler != null)
                {
                    events.InRange -= radioInRangeHandler;
                }
#endif
            }
#if !NETCF
            if (keepDiscoOnly)
            {
                Debug.Assert(seenDevices != null, "Should have created list 'seenDevices' above!");
                Debug.WriteLine("Disco result: " + result.Length + ", liveDevice: " + seenDevices.Count);
                result = BluetoothDeviceInfo.Intersect(result, seenDevices);
                Debug.WriteLine("Result result: " + result.Length);
            }
#endif
            return(result);
        }