示例#1
0
        private bool TryConnect(MsHidDeviceInfo hidDeviceInfo, out ReportWiimote wiimote)
        {
            Stream hidStream;

            if (UseSetOutputReport)
            {
                hidStream = new MsHidSetOutputReportStream(hidDeviceInfo.DevicePath);
            }
            else
            {
                hidStream = new MsHidStream(hidDeviceInfo.DevicePath);
            }

            try
            {
                wiimote = new ReportWiimote(hidDeviceInfo, hidStream);
                wiimote.Initialize();
            }
            catch (TimeoutException)
            {
                hidStream.Dispose();
                wiimote = null;
                return(false);
            }
            catch (Exception)
            {
                hidStream.Dispose();
                throw;
            }
            return(true);
        }
示例#2
0
        void device_Disconnected(object sender, EventArgs e)
        {
            ReportWiimote   wiimote    = (ReportWiimote)sender;
            MsHidDeviceInfo deviceInfo = (MsHidDeviceInfo)wiimote.DeviceInfo;

            wiimote.Disconnected -= device_Disconnected;
            ConnectedDevices.Remove(wiimote);
            MsHidDeviceProviderHelper.SetDevicePathConnected(deviceInfo.DevicePath, false);
            OnDeviceDisconnected(new DeviceEventArgs(wiimote));
        }
        public IDevice Connect(IDeviceInfo deviceInfo)
        {
            BluezDeviceInfo info = deviceInfo as BluezDeviceInfo;

            if (info == null)
            {
                throw new ArgumentException("The specified IDeviceInfo does not belong to this DeviceProvider.", "deviceInfo");
            }

            Stream     bluezStream = new BluezStream(info.Address);
            IWiiDevice device      = null;

            // determine the device type
            if (info.Name == "Nintendo RVL-WBC-01")
            {
                device = new ReportBalanceBoard(deviceInfo, bluezStream);
            }
            else if (info.Name == "Nintendo RVL-CNT-01")
            {
                device = new ReportWiimote(deviceInfo, bluezStream);
            }
            else
            {
                throw new ArgumentException("The specified deviceInfo with name '" + info.Name + "' is not supported.", "deviceInfo");
            }

            try
            {
                device.Initialize();
            }
            catch (Exception e)
            {
                throw new DeviceConnectException("Failed to connect to device", e);
            }

            _FoundDevices.Remove(info.Address);
            _LostDevices.Add(info.Address, info);
            if (DeviceLost != null)
            {
                DeviceLost(this, new DeviceInfoEventArgs(info));
            }

            _ConnectedDevices.Add(device);
            device.Disconnected += DeviceDisconnectedHandler;

            if (DeviceConnected != null)
            {
                DeviceConnected(this, new DeviceEventArgs(device));
            }
            return(device);
        }
        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);
        }
        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;
        }
        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.");
            }
        }
        private bool TryConnect(MsHidDeviceInfo hidDeviceInfo, out ReportWiimote wiimote)
        {
            Stream hidStream;
            if (UseSetOutputReport)
                hidStream = new MsHidSetOutputReportStream(hidDeviceInfo.DevicePath);
            else
                hidStream = new MsHidStream(hidDeviceInfo.DevicePath);

            try
            {
                wiimote = new ReportWiimote(hidDeviceInfo, hidStream);
                wiimote.Initialize();
            }
            catch (TimeoutException)
            {
                hidStream.Dispose();
                wiimote = null;
                return false;
            }
            catch (Exception)
            {
                hidStream.Dispose();
                throw;
            }
            return true;
        }
        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.");
            }
        }