Exemplo n.º 1
0
 internal BluetoothDevice(BluesoleilService owner, NativeMethods.BLUETOOTH_DEVICE_INFO deviceInfo)
 {
     this.owner = owner;
     this.deviceInfo = deviceInfo;
     int zeroIndex = Array.IndexOf<byte>(deviceInfo.szName, 0);
     this.name = Encoding.ASCII.GetString(deviceInfo.szName, 0, zeroIndex);
     address = deviceInfo.address;
 }
Exemplo n.º 2
0
        internal BluetoothDevice(BluesoleilService owner, NativeMethods.BLUETOOTH_DEVICE_INFO deviceInfo)
        {
            this.owner      = owner;
            this.deviceInfo = deviceInfo;
            int zeroIndex = Array.IndexOf <byte>(deviceInfo.szName, 0);

            this.name = Encoding.ASCII.GetString(deviceInfo.szName, 0, zeroIndex);
            address   = deviceInfo.address;
        }
Exemplo n.º 3
0
        internal BluetoothService(BluesoleilService owner, BluetoothDevice device, NativeMethods.GENERAL_SERVICE_INFO serviceInfo)
        {
            this.owner       = owner;
            this.serviceInfo = serviceInfo;
            int zeroIndex = Array.IndexOf <byte>(serviceInfo.szServiceName, 0);

            this.name   = Encoding.ASCII.GetString(serviceInfo.szServiceName, 0, zeroIndex);
            this.device = device;
        }
 protected void EnsureBluesoleilStarted(BluesoleilService bluesoleil)
 {
     if (!bluesoleil.IsStarted)
     {
         bluesoleil.StartBluetooth();
         while (discoveringThread == Thread.CurrentThread && !bluesoleil.IsStarted)
         {
             Thread.Sleep(1000);
             bluesoleil.StartBluetooth();
         }
     }
 }
 protected void EnsureBluesoleilStarted(BluesoleilService bluesoleil)
 {
     if (!bluesoleil.IsStarted)
     {
         bluesoleil.StartBluetooth();
         while (discoveringThread == Thread.CurrentThread && !bluesoleil.IsStarted)
         {
             Thread.Sleep(1000);
             bluesoleil.StartBluetooth();
         }
     }
 }
        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();
            }
        }
        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();
            }
        }
Exemplo n.º 8
0
 internal BluetoothConnection(BluesoleilService owner, BluetoothService service, int connectionHandle)
 {
     this.owner            = owner;
     this.service          = service;
     this.connectionHandle = connectionHandle;
 }
 internal BluetoothConnection(BluesoleilService owner, BluetoothService service, int connectionHandle)
 {
     this.owner = owner;
     this.service = service;
     this.connectionHandle = connectionHandle;
 }