private void device_Disconnected(object sender, EventArgs e) { IDevice device = (IDevice)sender; MsBluetoothDeviceInfo deviceInfo = (MsBluetoothDeviceInfo)device.DeviceInfo; device.Disconnected -= device_Disconnected; connectedDevices.Remove(device); lookupConnectedDevices.Remove(deviceInfo); // The actual disconnecting on bluetooth-level. NativeMethods.BluetoothDeviceInfo bluetoothDeviceInfo = deviceInfo.Device; Guid hidGuid = BluetoothServices.HumanInterfaceDeviceServiceClass_UUID; int result = NativeMethods.BluetoothSetServiceState(IntPtr.Zero, ref bluetoothDeviceInfo, ref hidGuid, 0x0000); if (result != 0) { Debug.WriteLine(string.Format("BluetoothSetServiceState returned 0x{0:x}.", result)); } // Also remove the device from the bluetooth-devices list, so // that doesn't have to be done when connecting again. NativeMethods.RemoveDevice(deviceInfo.Device.address); MsHidDeviceProviderHelper.SetDevicePathConnected(deviceInfo.DevicePath, false); OnDeviceDisconnected(new DeviceEventArgs(device)); }
public override bool Equals(MsHidDeviceInfo other) { MsBluetoothDeviceInfo msbtOther = other as MsBluetoothDeviceInfo; if (msbtOther == null) { return(false); } return(Equals(msbtOther)); }
protected void Discovering() { MsHid.NativeMethods.WSAStartup(); while (discoveringThread == Thread.CurrentThread) { List<BluetoothAddress> notFoundAddresses = new List<BluetoothAddress>(lookupFoundDevices.Keys); foreach (ReportDevice device in lookupConnectedDevices.Values) { notFoundAddresses.Remove(((MsBluetoothDeviceInfo)device.DeviceInfo).Address); } IEnumerable<NativeMethods.BluetoothDeviceInfo> devices; try { devices = NativeMethods.GetDeviceInfos(true, true, true, true, true, 2); } catch (System.ComponentModel.Win32Exception e) { if (e.ErrorCode == -2147467259) { // The dongle was not available, so try again later. Thread.Sleep(1000); continue; } else throw; } foreach (NativeMethods.BluetoothDeviceInfo deviceInfo in devices) { if (!IsWiiDevice(deviceInfo)) continue; if (deviceInfo.connected) continue; if (deviceInfo.remembered) { NativeMethods.RemoveDevice(deviceInfo.address); continue; } BluetoothAddress address = new BluetoothAddress(deviceInfo.address.address); notFoundAddresses.Remove(address); MsBluetoothDeviceInfo bluetoothDeviceInfo; if (lookupFoundDevices.TryGetValue(address, out bluetoothDeviceInfo)) { bluetoothDeviceInfo.Device = deviceInfo; } else { bluetoothDeviceInfo = new MsBluetoothDeviceInfo(address, deviceInfo); FoundDevices.Add(bluetoothDeviceInfo); lookupFoundDevices.Add(address, bluetoothDeviceInfo); OnDeviceFound(new DeviceInfoEventArgs(bluetoothDeviceInfo)); } } // Remove the lost devices from the list and notify DeviceLost event. foreach (BluetoothAddress notFoundAddress in notFoundAddresses) { IDeviceInfo notFoundDeviceInfo = lookupFoundDevices[notFoundAddress]; lookupFoundDevices.Remove(notFoundAddress); foundDevices.Remove(notFoundDeviceInfo); OnDeviceLost(new DeviceInfoEventArgs(notFoundDeviceInfo)); } Thread.Sleep(1000); } }
public bool Equals(MsBluetoothDeviceInfo other) { return(this.Address == other.Address); }
public bool Equals(MsBluetoothDeviceInfo other) { return this.Address == other.Address; }
public IDevice Connect(IDeviceInfo deviceInfo) { int result; MsBluetoothDeviceInfo bluetoothDeviceInfo = deviceInfo as MsBluetoothDeviceInfo; if (bluetoothDeviceInfo == null) { throw new ArgumentException("The specified IDeviceInfo does not belong to this DeviceProvider.", "deviceInfo"); } NativeMethods.BluetoothDeviceInfo bluetoothDevice = bluetoothDeviceInfo.Device; result = NativeMethods.BluetoothUpdateDeviceRecord(ref bluetoothDevice); NativeMethods.HandleError(result); if (bluetoothDevice.connected) { throw new NotImplementedException("The device is already connected."); } if (bluetoothDevice.remembered) { // Remove non-connected devices from MsBluetooth's device list. // This has to be done because: // MsBluetooth can't connect to Hid devices without also pairing to them. // If you think that sounds crazy, you're on the right track. NativeMethods.RemoveDevice(bluetoothDevice.address); } Guid hidGuid = BluetoothServices.HumanInterfaceDeviceServiceClass_UUID; result = NativeMethods.BluetoothSetServiceState(IntPtr.Zero, ref bluetoothDevice, ref hidGuid, 0x0001); NativeMethods.HandleError(result); if (WaitTillConnected(bluetoothDevice.address, TimeSpan.FromSeconds(30))) { Thread.Sleep(2000); ReportDevice device = null; foreach (KeyValuePair <string, SafeFileHandle> pair in MsHidDeviceProviderHelper.GetWiiDeviceHandles()) { string devicePath = pair.Key; SafeFileHandle fileHandle = pair.Value; Stream communicationStream = new MsHidSetOutputReportStream(fileHandle); // determine the device type if (bluetoothDeviceInfo.Name == "Nintendo RVL-WBC-01") { device = new ReportBalanceBoard(deviceInfo, communicationStream); } else if (bluetoothDeviceInfo.Name == "Nintendo RVL-CNT-01") { device = new ReportWiimote(deviceInfo, communicationStream); } else { throw new ArgumentException("The specified deviceInfo with name '" + bluetoothDeviceInfo.Name + "' is not supported.", "deviceInfo"); } if (MsHidDeviceProviderHelper.TryConnect(device, communicationStream, devicePath, fileHandle)) { break; } device = null; } if (device != null) { lookupFoundDevices.Remove(bluetoothDeviceInfo.Address); foundDevices.Remove(bluetoothDeviceInfo); OnDeviceLost(new DeviceInfoEventArgs(bluetoothDeviceInfo)); device.Disconnected += device_Disconnected; ConnectedDevices.Add(device); lookupConnectedDevices.Add(bluetoothDeviceInfo, device); OnDeviceConnected(new DeviceEventArgs(device)); return(device); } else { throw new DeviceConnectException("No working HID device found."); } } else { throw new TimeoutException("Timeout while trying to connect to the bluetooth device."); } }
protected void Discovering() { MsHid.NativeMethods.WSAStartup(); while (discoveringThread == Thread.CurrentThread) { List <BluetoothAddress> notFoundAddresses = new List <BluetoothAddress>(lookupFoundDevices.Keys); foreach (ReportDevice device in lookupConnectedDevices.Values) { notFoundAddresses.Remove(((MsBluetoothDeviceInfo)device.DeviceInfo).Address); } IEnumerable <NativeMethods.BluetoothDeviceInfo> devices; try { devices = NativeMethods.GetDeviceInfos(true, true, true, true, true, 2); } catch (System.ComponentModel.Win32Exception e) { if (e.ErrorCode == -2147467259) { // The dongle was not available, so try again later. Thread.Sleep(1000); continue; } else { throw; } } foreach (NativeMethods.BluetoothDeviceInfo deviceInfo in devices) { if (!IsWiiDevice(deviceInfo)) { continue; } if (deviceInfo.connected) { continue; } if (deviceInfo.remembered) { NativeMethods.RemoveDevice(deviceInfo.address); continue; } BluetoothAddress address = new BluetoothAddress(deviceInfo.address.address); notFoundAddresses.Remove(address); MsBluetoothDeviceInfo bluetoothDeviceInfo; if (lookupFoundDevices.TryGetValue(address, out bluetoothDeviceInfo)) { bluetoothDeviceInfo.Device = deviceInfo; } else { bluetoothDeviceInfo = new MsBluetoothDeviceInfo(address, deviceInfo); FoundDevices.Add(bluetoothDeviceInfo); lookupFoundDevices.Add(address, bluetoothDeviceInfo); OnDeviceFound(new DeviceInfoEventArgs(bluetoothDeviceInfo)); } } // Remove the lost devices from the list and notify DeviceLost event. foreach (BluetoothAddress notFoundAddress in notFoundAddresses) { IDeviceInfo notFoundDeviceInfo = lookupFoundDevices[notFoundAddress]; lookupFoundDevices.Remove(notFoundAddress); foundDevices.Remove(notFoundDeviceInfo); OnDeviceLost(new DeviceInfoEventArgs(notFoundDeviceInfo)); } Thread.Sleep(1000); } }