static void Main(string[] args) { try { displayBluetoothRadio(); _bluetoothEvents = BluetoothWin32Events.GetInstance(); _bluetoothEvents.InRange += onInRange; _bluetoothEvents.OutOfRange += onOutOfRange; using (BluetoothClient client = new BluetoothClient()) { BluetoothComponent component = new BluetoothComponent(client); component.DiscoverDevicesProgress += onDiscoverDevicesProgress; component.DiscoverDevicesComplete += onDiscoverDevicesComplete; component.DiscoverDevicesAsync(255, true, false, false, false, null); //BluetoothDeviceInfo[] peers = client.DiscoverDevices(); //device = peers.ToList().Where(p => p.DeviceName == DEVICE_NAME).FirstOrDefault(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadKey(); } }
public static bool initialise() { try { localRadio = BluetoothRadio.PrimaryRadio; if (null == localRadio) { lastError = "There is no Bluetooth hardware, or it uses unsupported software."; return(false); } localRadioInfo = new BluetoothDeviceInfo(localRadio.LocalAddress); if (null == localRadioInfo) { lastError = "Bluetooth hardware exists, but failed to attach."; return(false); } localRadio.Mode = RadioMode.Discoverable; setServices(); if (bluetoothServiceItems.Count > 0) { service = bluetoothServiceItems[0].name; } setDevices(); if (bluetoothDeviceInfos.Count > 0) { device = bluetoothDeviceInfos[0].DeviceName; } BluetoothWin32Events.GetInstance().InRange += new EventHandler <BluetoothWin32RadioInRangeEventArgs>(inRangeEvent); BluetoothWin32Events.GetInstance().OutOfRange += new EventHandler <BluetoothWin32RadioOutOfRangeEventArgs>(outOfRangeEvent); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return(false); } return(true); }
private void Watch() { if (eventService. GetEvents(). Where(m => m.EventType == Event.Types.EventType.BluetoothEvent).Any()) { if (!isWatching && BluetoothRadio.IsSupported) { isWatching = true; bluetoothWin32Events = new BluetoothWin32Events(); bluetoothWin32Events.InRange += BluetoothWin32Events_InRange; } } else { if (isWatching) { isWatching = false; bluetoothWin32Events.InRange -= BluetoothWin32Events_InRange; bluetoothWin32Events = null; } } }
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); }
public MainWindow() { if (Settings.Default.DarkMode2 == DarkMode.Unset) { Settings.Default.DarkMode2 = (DarkMode)Convert.ToInt32(Settings.Default.DarkMode); Settings.Default.Save(); } DarkModeHelper.Update(); _mainPage = new MainPage(this); _systemPage = new SystemPage(this); _selfTestPage = new SelfTestPage(this); _factoryResetPage = new FactoryResetPage(this); _findMyGearPage = new FindMyGearPage(this); _touchpadPage = new TouchpadPage(this); _customActionPage = new CustomActionPage(this); _ambientSoundPage = new AmbientSoundPage(this); _equalizerPage = new EqualizerPage(this); _connectionLostPage = new ConnectionLostPage(this); _deviceSelectPage = new DeviceSelectPage(this); _settingPage = new SettingPage(this); _updatePage = new UpdatePage(this); _advancedPage = new AdvancedPage(this); _unsupportedFeaturePage = new UnsupportedFeaturePage(this); _popupSettingPage = new PopupSettingPage(this); InitializeComponent(); _tbi = new TaskbarIcon(); Stream iconStream = Application.GetResourceStream(new Uri("pack://*****:*****@"CRITICAL: Unknown Win32 Bluetooth service error"); Console.WriteLine(e); } }