/// <summary>Matches the HID device to the allowed Wiimote types.</summary> private static bool MatchHID(HIDDeviceInfo device) { lock (allowedDeviceNames) { return(device.VendorID == WiimoteConstants.VendorID && allowedProductIDs.Any(p => p == device.ProductID)); } }
private static bool HIDDiscoverLoop(CancellationToken token) { var hids = HIDDeviceInfo.EnumerateDevices(token, MatchHID); foreach (HIDDeviceInfo hid in hids) { if (token.IsCancellationRequested) { return(false); } Wiimote wiimote = null; lock (wiimotes) { wiimote = wiimotes.Find(wm => wm.DevicePath == hid.DevicePath); } if (wiimote == null) { if (autoConnect) { //FIXME: Handle BOTH Bluetooth and DolphinBarMode more gracefully. WiimoteDeviceInfo wiimoteDevice = new WiimoteDeviceInfo(hid, true); // DolphinBarMode); try { Connect(wiimoteDevice); } catch (Exception ex) { RaiseConnectionFailed(wiimoteDevice, ex); } } else if (!RaiseDiscovered(null, hid)) { return(false); } } } token.Sleep(1000); return(true); }
// Called by manager private static bool RaiseDiscovered(BluetoothDeviceInfo bt, HIDDeviceInfo hid) { if (bt?.IsInvalid ?? true) { Debug.WriteLine($"{hid} Discovered"); } else { Debug.WriteLine($"{bt} Discovered"); } WiimoteDeviceInfo device; //FIXME: Quick fix to support both Bluetooth and DolphinBar connections. if (bt?.IsInvalid ?? true) // && DolphinBarMode) { device = new WiimoteDeviceInfo(hid, true); } else { device = new WiimoteDeviceInfo(bt, hid); } WiimoteDiscoveredEventArgs e = new WiimoteDiscoveredEventArgs(device); Discovered?.Invoke(null, e); if (e.AddDevice) { try { Connect(e.Device); } catch (Exception ex) { RaiseConnectionFailed(e.Device, ex); return(true); } } return(e.KeepSearching); }
private static bool BluetoothDiscoverLoop(CancellationToken token) { HashSet <BluetoothAddress> missingDevices = new HashSet <BluetoothAddress>(ConnectedAddresses); var devices = BluetoothDeviceInfo.EnumerateDevices(token, MatchBluetooth); Stopwatch watch = Stopwatch.StartNew(); bool anyPaired = false; foreach (BluetoothDeviceInfo device in devices) { if (token.IsCancellationRequested) { return(false); } Debug.WriteLine($"Took {watch.ElapsedMilliseconds}ms to enumerate bluetooth device"); Wiimote wiimote = null; lock (wiimotes) { wiimote = wiimotes.Find(wm => wm.Address == device.Address); } if (device.Connected) { if (wiimote != null) { // Give Wiimote the updated Bluetooth device wiimote.Device.Bluetooth = device; missingDevices.Remove(device.Address); } else { HIDDeviceInfo hid = HIDDeviceInfo.GetDevice(device.Address); // Drivers must not be installed yet, let's wait a bit if (hid != null) { if (autoConnect) { WiimoteDeviceInfo wiimoteDevice = new WiimoteDeviceInfo(device, hid); try { Connect(wiimoteDevice); } catch (Exception ex) { RaiseConnectionFailed(wiimoteDevice, ex); } } else if (!RaiseDiscovered(device, hid)) { return(false); } } /*else if (device.PairDevice(token)) { * anyPaired = true; * } * else { * Debug.WriteLine("{device} pair failed!"); * }*/ } } else { if (wiimote != null) { lock (wiimotes) { wiimote.Dispose(); wiimotes.Remove(wiimote); RaiseDisconnected(wiimote, DisconnectReason.ConnectionLost); } } else if (device.IsDiscoverable() /*|| !device.Remembered*/) { if (pairOnDiscover) { if (device.PairDevice(token)) { anyPaired = true; } else { Debug.WriteLine("{device} pair failed!"); } } } else if (device.Remembered && unpairOnDisconnect) { device.RemoveDevice(token); } } watch.Restart(); } token.Sleep((anyPaired ? driverInstallDelay : 0) + 1000); return(true); }
public WiimoteDiscoveredEventArgs(HIDDeviceInfo device, bool dolphinBarMode) : this(new WiimoteDeviceInfo(device, dolphinBarMode)) { }