private void OnDeviceLost(BluesoleilDeviceInfo deviceInfo)
        {
            lookupDeviceInfo.Remove(deviceInfo.Address);
            foundDevices.Remove(deviceInfo);

            OnDeviceLost(new DeviceInfoEventArgs(deviceInfo));
        }
        private void OnDeviceFound(BluesoleilDeviceInfo deviceInfo)
        {
            lookupDeviceInfo.Add(deviceInfo.Address, deviceInfo);
            foundDevices.Add(deviceInfo);

            OnDeviceFound(new DeviceInfoEventArgs(deviceInfo));
        }
        private void OnDeviceDisconnected(ReportDevice device)
        {
            BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;

            lookupDevice.Remove(deviceInfo.Address);
            connectedDevices.Remove(device);
            OnDeviceDisconnected(new DeviceEventArgs(device));
        }
        private void OnDeviceConnected(ReportDevice device)
        {
            BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;

            OnDeviceLost(deviceInfo);
            lookupDevice.Add(deviceInfo.Address, device);
            connectedDevices.Add(device);
            OnDeviceConnected(new DeviceEventArgs(device));
        }
        public override bool Equals(MsHidDeviceInfo other)
        {
            BluesoleilDeviceInfo bsdiOther = other as BluesoleilDeviceInfo;

            if (bsdiOther == null)
            {
                return(false);
            }
            return(Equals(bsdiOther));
        }
        public IDevice Connect(IDeviceInfo deviceInfo)
        {
            BluesoleilDeviceInfo bluetoothDeviceInfo = (BluesoleilDeviceInfo)deviceInfo;

            Thread.Sleep(100);

            BluetoothConnection connection = BluesoleilService.Instance.ConnectService(bluetoothDeviceInfo.Service);

            ReportDevice device = null;

            foreach (KeyValuePair <string, SafeFileHandle> pair in MsHidDeviceProviderHelper.GetWiiDeviceHandles())
            {
                string         devicePath          = pair.Key;
                SafeFileHandle fileHandle          = pair.Value;
                Stream         communicationStream = new MsHidStream(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)
            {
                bluesoleil.DisconnectService(connection);
                throw new DeviceConnectException("The connected bluetooth device was not found in the HID-list.");
            }

            device.Disconnected += new EventHandler(device_Disconnected);
            lookupConnection.Add(bluetoothDeviceInfo.Address, connection);
            OnDeviceConnected(device);
            return(device);
        }
        void device_Disconnected(object sender, EventArgs e)
        {
            ReportDevice         device     = (ReportDevice)sender;
            BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;
            BluetoothConnection  connection;

            if (lookupConnection.TryGetValue(deviceInfo.Address, out connection))
            {
                lookupConnection.Remove(deviceInfo.Address);
                OnDeviceDisconnected(device);
                try
                {
                    bluesoleil.DisconnectService(connection);
                }
                catch (BluesoleilNonExistingConnectionException)
                {
                }

                MsHidDeviceProviderHelper.SetDevicePathConnected(deviceInfo.DevicePath, false);
            }
        }
 public bool Equals(BluesoleilDeviceInfo other)
 {
     return(this.Address == other.Address);
 }
        protected void Discovering()
        {
            while (discoveringThread == Thread.CurrentThread)
            {
                MsHid.NativeMethods.WSAStartup();

                bluesoleil = BluesoleilService.Instance;
                bluesoleil.Initialize();
                bluesoleil.ConnectionClosed += OnConnectionClosed;

                while (discoveringThread == Thread.CurrentThread)
                {
                    EnsureBluesoleilStarted(bluesoleil);

                    BluetoothDevice[] devices;
                    try
                    {
                        // Scan for bluetooth-devices (like devices).
                        devices = bluesoleil.InquireDevices(pollingTime);
                    }
                    catch (BluesoleilFailException)
                    {
                        // Happens sometimes randomly, but also happens sometimes when the bluetooth-dongle is unplugged.
                        continue;
                    }
                    catch (BluesoleilNotReadyException)
                    {
                        // Happens when bluetooth is stopped or when the bluetooth-dongle is unplugged.
                        Thread.Sleep(NotReadySleepTimeout);
                        continue;
                    }

                    List <BluetoothAddress> notFoundAddresses = new List <BluetoothAddress>(lookupDeviceInfo.Keys);
                    foreach (BluetoothDevice device in devices)
                    {
                        if (!IsWiiDevice(device))
                        {
                            continue;
                        }
                        BluetoothAddress address = new BluetoothAddress(device.Address);
                        if (lookupDeviceInfo.ContainsKey(address))
                        {
                            notFoundAddresses.Remove(address);
                            break;
                        }

                        BluetoothService[] services = null;
                        try
                        {
                            // Scan for bluetooth-devices (like devices).
                            services = bluesoleil.BrowseServices(device);
                            Thread.Sleep(PostBrowseSleepTimeout);
                        }
                        catch (BluesoleilFailException)
                        {
                            // Happens sometimes randomly, but also happens sometimes when the bluetooth-dongle is unplugged.
                            continue;
                        }
                        catch (BluesoleilNotReadyException)
                        {
                            // Happens when bluetooth is stopped or when the bluetooth-dongle is unplugged.
                            continue;
                        }

                        if (services.Length != 3)
                        {
                            continue;
                        }

                        if (!lookupDeviceInfo.ContainsKey(address))
                        {
                            BluesoleilDeviceInfo foundDevice = new BluesoleilDeviceInfo(device, services[1]);
                            OnDeviceFound(foundDevice);
                        }
                    }

                    // Remove the lost devices from the list and notify DeviceLost event.
                    foreach (BluetoothAddress notFoundAddress in notFoundAddresses)
                    {
                        BluesoleilDeviceInfo notFoundDeviceInfo = lookupDeviceInfo[notFoundAddress];
                        OnDeviceLost(notFoundDeviceInfo);
                    }

                    Thread.Sleep(PollSleepTimeout);
                }
                bluesoleil.ConnectionClosed -= OnConnectionClosed;
                bluesoleil.Dispose();
            }
        }