/// <summary>Disconnects the Wiimote.</summary> /// <param name="wiimote">The Wiimote to disconnect.</param> /// <param name="removeDevice">Should the device be removed. If null, the default /// action will be performed.</param> public static void Disconnect(Wiimote wiimote, bool?removeDevice = null) { lock (wiimotes) { if (!wiimotes.Contains(wiimote)) { throw new ArgumentException($"{wiimote} is not connected!", nameof(wiimote)); } wiimote.Dispose(); wiimotes.Remove(wiimote); RaiseDisconnected(wiimote, DisconnectReason.User, removeDevice); } }
/// <summary>Disconnects the Wiimote with the specified device path.</summary> /// <param name="devicePath">The HID device path of the Wiimote.</param> /// <param name="removeDevice">Should the device be removed. If null, the default /// action will be performed.</param> public static void Disconnect(string devicePath, bool?removeDevice = null) { lock (wiimotes) { Wiimote wiimote = wiimotes.Find(wm => wm.DevicePath == devicePath); if (wiimote == null) { throw new ArgumentException($"Wiimote ({devicePath}) is not connected!", nameof(devicePath)); } wiimote.Dispose(); wiimotes.Remove(wiimote); RaiseDisconnected(wiimote, DisconnectReason.User, removeDevice); } }
/// <summary>Disconnects the Wiimote with the specified address.</summary> /// <param name="address">The address of the Wiimote.</param> /// <param name="removeDevice">Should the device be removed. If null, the default /// action will be performed.</param> public static void Disconnect(BluetoothAddress address, bool?removeDevice = null) { lock (wiimotes) { Wiimote wiimote = wiimotes.Find(wm => wm.Address == address); if (wiimote == null) { throw new ArgumentException($"Wiimote {{{address}}} is not connected!", nameof(address)); } wiimote.Dispose(); wiimotes.Remove(wiimote); RaiseDisconnected(wiimote, DisconnectReason.User, removeDevice); } }
private void teardownWiimoteConnection(Wiimote pDevice) { if (pDevice != null) { pDeviceMutex.WaitOne(); pDevice.WiimoteChanged -= this.wiimoteChangedEventHandler; pDevice.WiimoteExtensionChanged -= this.wiimoteExtensionChangedEventHandler; int wiimoteid; if (pWiimoteMap.Keys.Contains(pDevice.HIDDevicePath)) { wiimoteid = this.pWiimoteMap[pDevice.HIDDevicePath].Status.ID; this.pWiimoteMap[pDevice.HIDDevicePath].Teardown(); this.pWiimoteMap.Remove(pDevice.HIDDevicePath); } else { wiimoteid = this.pWiimoteMap.Count + 1; } pDeviceMutex.ReleaseMutex(); pDevice.SetReportType(InputReport.Status, false); pDevice.SetRumble(false); pDevice.SetLEDs(true, true, true, true); // Close the connection and dispose of the device. pDevice.Disconnect(); pDevice.Dispose(); OnDisconnect(wiimoteid, this.pWiimoteMap.Count); } }
/// <summary> /// Start the IR Server plugin. /// </summary> public override void Start() { LoadSettings(); _wiimote = new Wiimote(); try { _wiimote.WiimoteChanged += WiimoteChanged; _wiimote.WiimoteExtensionChanged += WiimoteExtensionChanged; _wiimote.Connect(); _wiimote.SetReportType(InputReport.IRAccel, true); _wiimote.SetLEDs(_led1, _led2, _led3, _led4); _wiimote.SetRumble(false); } catch { throw; } finally { _wiimote.Dispose(); _wiimote = null; } }
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); }