コード例 #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;
 }
コード例 #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;
        }
コード例 #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;
        }
コード例 #4
0
 protected void EnsureBluesoleilStarted(BluesoleilService bluesoleil)
 {
     if (!bluesoleil.IsStarted)
     {
         bluesoleil.StartBluetooth();
         while (discoveringThread == Thread.CurrentThread && !bluesoleil.IsStarted)
         {
             Thread.Sleep(1000);
             bluesoleil.StartBluetooth();
         }
     }
 }
コード例 #5
0
 protected void EnsureBluesoleilStarted(BluesoleilService bluesoleil)
 {
     if (!bluesoleil.IsStarted)
     {
         bluesoleil.StartBluetooth();
         while (discoveringThread == Thread.CurrentThread && !bluesoleil.IsStarted)
         {
             Thread.Sleep(1000);
             bluesoleil.StartBluetooth();
         }
     }
 }
コード例 #6
0
        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();
            }
        }
コード例 #7
0
        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();
            }
        }
コード例 #8
0
 internal BluetoothConnection(BluesoleilService owner, BluetoothService service, int connectionHandle)
 {
     this.owner            = owner;
     this.service          = service;
     this.connectionHandle = connectionHandle;
 }
コード例 #9
0
 internal BluetoothConnection(BluesoleilService owner, BluetoothService service, int connectionHandle)
 {
     this.owner = owner;
     this.service = service;
     this.connectionHandle = connectionHandle;
 }