Ejemplo n.º 1
0
        IEnumerable <BluetoothDeviceInfo> GetPairedDevices()
        {
            BLUETOOTH_DEVICE_SEARCH_PARAMS search = BLUETOOTH_DEVICE_SEARCH_PARAMS.Create();

            search.cTimeoutMultiplier   = 8;
            search.fReturnAuthenticated = true;
            search.fReturnRemembered    = false;
            search.fReturnUnknown       = false;
            search.fReturnConnected     = false;
            search.fIssueInquiry        = false;

            BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();
            IntPtr searchHandle          = NativeMethods.BluetoothFindFirstDevice(ref search, ref device);

            if (searchHandle != IntPtr.Zero)
            {
                yield return(new BluetoothDeviceInfo(device));

                while (NativeMethods.BluetoothFindNextDevice(searchHandle, ref device))
                {
                    yield return(new BluetoothDeviceInfo(device));
                }

                NativeMethods.BluetoothFindDeviceClose(searchHandle);
            }

            yield break;
        }
Ejemplo n.º 2
0
        static void GetPairedDevices()
        {
            BLUETOOTH_DEVICE_SEARCH_PARAMS search = BLUETOOTH_DEVICE_SEARCH_PARAMS.Create();

            search.cTimeoutMultiplier   = 8;
            search.fReturnAuthenticated = true;
            search.fReturnRemembered    = false;
            search.fReturnUnknown       = false;
            search.fReturnConnected     = false;
            search.fIssueInquiry        = false;

            BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();
            IntPtr searchHandle          = NativeMethods.BluetoothFindFirstDevice(ref search, ref device);

            if (searchHandle != IntPtr.Zero)
            {
                Console.WriteLine(ToMacAdress(device.Address) + " " + device.szName);

                while (NativeMethods.BluetoothFindNextDevice(searchHandle, ref device))
                {
                    Console.WriteLine(ToMacAdress(device.Address) + " " + device.szName);
                }

                NativeMethods.BluetoothFindDeviceClose(searchHandle);
            }
        }
Ejemplo n.º 3
0
        public static bool IsStackCompatible()
        {
            BLUETOOTH_DEVICE_INFO device = new BLUETOOTH_DEVICE_INFO();

            device.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO));
            device.szName = "";

            BLUETOOTH_DEVICE_SEARCH_PARAMS searchparams = new BLUETOOTH_DEVICE_SEARCH_PARAMS();

            searchparams.dwSize               = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
            searchparams.fIssueInquiry        = true;
            searchparams.fReturnAuthenticated = true;
            searchparams.fReturnConnected     = true;
            searchparams.fReturnRemembered    = true;
            searchparams.fReturnUnknown       = false;
            searchparams.hRadio               = IntPtr.Zero;
            searchparams.cTimeoutMultiplier   = 1;


            IntPtr handle = BluetoothFindFirstDevice(ref searchparams, ref device);

            if (handle != IntPtr.Zero)
            {
                BluetoothFindDeviceClose(handle);
            }

            return(handle != IntPtr.Zero);
        }
Ejemplo n.º 4
0
        IReadOnlyCollection <BluetoothDeviceInfo> DoDiscoverDevices(int maxDevices)
        {
            List <BluetoothDeviceInfo> devices = new List <BluetoothDeviceInfo>();

            BLUETOOTH_DEVICE_SEARCH_PARAMS search = BLUETOOTH_DEVICE_SEARCH_PARAMS.Create();

            search.fReturnAuthenticated = false;
            search.fReturnRemembered    = true;
            search.fReturnUnknown       = true;
            search.fReturnConnected     = true;
            search.fIssueInquiry        = true;
            search.cTimeoutMultiplier   = Convert.ToByte(inquiryLength.TotalSeconds / 1.28);

            BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();
            IntPtr searchHandle          = NativeMethods.BluetoothFindFirstDevice(ref search, ref device);

            if (searchHandle != IntPtr.Zero)
            {
                NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref device);
                devices.Add(new BluetoothDeviceInfo(device));

                while (NativeMethods.BluetoothFindNextDevice(searchHandle, ref device) && devices.Count < maxDevices)
                {
                    NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref device);
                    devices.Add(new BluetoothDeviceInfo(device));
                }

                NativeMethods.BluetoothFindDeviceClose(searchHandle);
            }

            // get full paired devices list and remove those not recently seen (they were added to the results above regardless)
            search.fReturnAuthenticated = true;
            search.fReturnRemembered    = true;
            search.fReturnUnknown       = false;
            search.fReturnConnected     = false;
            search.fIssueInquiry        = false;

            searchHandle = NativeMethods.BluetoothFindFirstDevice(ref search, ref device);
            if (searchHandle != IntPtr.Zero)
            {
                if (device.LastSeen < DateTime.Now.AddMinutes(-1))
                {
                    devices.Remove(new BluetoothDeviceInfo(device));
                }

                while (NativeMethods.BluetoothFindNextDevice(searchHandle, ref device))
                {
                    if (device.LastSeen < DateTime.Now.AddMinutes(-1))
                    {
                        devices.Remove(new BluetoothDeviceInfo(device));
                    }
                }

                NativeMethods.BluetoothFindDeviceClose(searchHandle);
            }

            return(devices.AsReadOnly());
        }
Ejemplo n.º 5
0
        internal static IEnumerable <BluetoothDeviceInfo> EnumerateDevices(CancellationToken token, Predicate <BluetoothDeviceInfo> match)
        {
            IntPtr hFind = IntPtr.Zero;

            try {
                BLUETOOTH_DEVICE_INFO          btdi = new BLUETOOTH_DEVICE_INFO();
                BLUETOOTH_DEVICE_SEARCH_PARAMS srch = new BLUETOOTH_DEVICE_SEARCH_PARAMS();

                btdi.dwSize = Marshal.SizeOf <BLUETOOTH_DEVICE_INFO>();
                srch.dwSize = Marshal.SizeOf <BLUETOOTH_DEVICE_SEARCH_PARAMS>();

                srch.fReturnAuthenticated = true;
                srch.fReturnRemembered    = true;
                srch.fReturnConnected     = true;
                srch.fReturnUnknown       = true;
                srch.fIssueInquiry        = true;
                srch.cTimeoutMultiplier   = 1;
                srch.hRadio = IntPtr.Zero;
                //srch.hRadio = InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Handle;

                hFind = NativeMethods.BluetoothFindFirstDevice(ref srch, ref btdi);
                do
                {
                    BluetoothDeviceInfo device = new BluetoothDeviceInfo(btdi);
                    if (match?.Invoke(device) ?? true)
                    {
                        yield return(device);
                    }
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }while (NativeMethods.BluetoothFindNextDevice(hFind, ref btdi));
            }
            finally {
                if (hFind != IntPtr.Zero)
                {
                    NativeMethods.BluetoothFindDeviceClose(hFind);
                }
            }
        }
Ejemplo n.º 6
0
        public static bool IsStackCompatible()
        {
            BLUETOOTH_DEVICE_INFO device = new BLUETOOTH_DEVICE_INFO();
            device.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO));
            device.szName = "";

            BLUETOOTH_DEVICE_SEARCH_PARAMS searchparams = new BLUETOOTH_DEVICE_SEARCH_PARAMS();
            searchparams.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
            searchparams.fIssueInquiry = true;
            searchparams.fReturnAuthenticated = true;
            searchparams.fReturnConnected = true;
            searchparams.fReturnRemembered = true;
            searchparams.fReturnUnknown = false;
            searchparams.hRadio = IntPtr.Zero;
            searchparams.cTimeoutMultiplier = 1;

            IntPtr handle = BluetoothFindFirstDevice(ref searchparams, ref device);
            if (handle != IntPtr.Zero)
                BluetoothFindDeviceClose(handle);

            return handle != IntPtr.Zero;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Bluetoothデバイスの一覧を更新します。
        /// </summary>
        private unsafe void refresh_list()
        {
            if (hradio == IntPtr.Zero)
            {
                return;
            }
            var btdi = new BLUETOOTH_DEVICE_INFO();
            var srch = new BLUETOOTH_DEVICE_SEARCH_PARAMS();

            btdi.dwSize = (uint)Marshal.SizeOf(btdi);
            srch.dwSize = (uint)Marshal.SizeOf(srch);

            srch.fReturnAuthenticated = true;
            srch.fReturnRemembered    = true;
            srch.fReturnConnected     = true;
            srch.fReturnUnknown       = true;
            srch.fIssueInquiry        = true;
            srch.cTimeoutMultiplier   = 2;
            srch.hRadio = hradio;

            lock (btdis)
            {
                btdis.Clear();
                var hfind = MyBluetoothFindFirstDevice(ref srch, ref btdi);
                while (hfind != IntPtr.Zero)
                {
                    btdis.Add(btdi);

                    btdi        = new BLUETOOTH_DEVICE_INFO();
                    btdi.dwSize = (uint)Marshal.SizeOf(btdi);

                    if (!MyBluetoothFindNextDevice(hfind, ref btdi))
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 8
0
 public extern static IntPtr MyBluetoothFindFirstDevice(
     ref BLUETOOTH_DEVICE_SEARCH_PARAMS btsp,
     ref BLUETOOTH_DEVICE_INFO btdi
     );
Ejemplo n.º 9
0
 public static extern IntPtr BluetoothFindFirstDevice(ref BLUETOOTH_DEVICE_SEARCH_PARAMS pSearchParams, ref BLUETOOTH_DEVICE_INFO pbtdi);
Ejemplo n.º 10
0
 static extern IntPtr BluetoothFindFirstDevice(ref BLUETOOTH_DEVICE_SEARCH_PARAMS SearchParams, ref BLUETOOTH_DEVICE_INFO DeviceInfo);
Ejemplo n.º 11
0
        private void Connect(bool DisconnectOld)
        {
            //TODO: honour disconnectold parameter
            BLUETOOTH_DEVICE_INFO device = new BLUETOOTH_DEVICE_INFO();

            device.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO));
            device.szName = "";

            BLUETOOTH_DEVICE_SEARCH_PARAMS searchparams = new BLUETOOTH_DEVICE_SEARCH_PARAMS();

            searchparams.dwSize               = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
            searchparams.fIssueInquiry        = true;
            searchparams.fReturnAuthenticated = true;
            searchparams.fReturnConnected     = true;
            searchparams.fReturnRemembered    = true;
            searchparams.fReturnUnknown       = true;
            searchparams.hRadio               = IntPtr.Zero;
            searchparams.cTimeoutMultiplier   = 1;


            bool   connected = false;
            IntPtr handle    = BluetoothFindFirstDevice(ref searchparams, ref device);

            if (handle == IntPtr.Zero)
            {
                int lasterror = Marshal.GetLastWin32Error();
                if (lasterror != 0)
                {
                    LogError("Bluetooth API returned: " + lasterror.ToString());
                }
            }
            else
            {
                while (true)
                {
                    if (Cancel)
                    {
                        break;
                    }

                    if (device.szName.StartsWith("Nintendo RVL"))
                    {
                        if (device.fRemembered)
                        {
                            BluetoothRemoveDevice(ref device.Address);
                        }
                        else
                        {
                            if (BluetoothSetServiceState(IntPtr.Zero, ref device, ref HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE) != 0)
                            {
                                LogError("Failed to connect to wiimote controller");
                            }
                            else
                            {
                                connected = true;
                            }
                        }
                        break;
                    }

                    device.szName = "";
                    if (!BluetoothFindNextDevice(handle, ref device))
                    {
                        break;
                    }
                }
            }
            BluetoothFindDeviceClose(handle);
            if (connected && Connected != null)
            {
                Connected(this, EventArgs.Empty);
            }
            else if (ConnectionFailed != null)
            {
                ConnectionFailed(this, EventArgs.Empty);
            }
        }
Ejemplo n.º 12
0
 internal static extern IntPtr BluetoothFindFirstDevice(
     ref BLUETOOTH_DEVICE_SEARCH_PARAMS pbtsp,
     ref BLUETOOTH_DEVICE_INFO pbtdi);
Ejemplo n.º 13
0
        private void Connect(bool DisconnectOld)
        {
            //TODO: honour disconnectold parameter
            BLUETOOTH_DEVICE_INFO device = new BLUETOOTH_DEVICE_INFO();
            device.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO));
            device.szName = "";

            BLUETOOTH_DEVICE_SEARCH_PARAMS searchparams = new BLUETOOTH_DEVICE_SEARCH_PARAMS();
            searchparams.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
            searchparams.fIssueInquiry = true;
            searchparams.fReturnAuthenticated = true;
            searchparams.fReturnConnected = true;
            searchparams.fReturnRemembered = true;
            searchparams.fReturnUnknown = true;
            searchparams.hRadio = IntPtr.Zero;
            searchparams.cTimeoutMultiplier = 1;

            bool connected = false;
            IntPtr handle = BluetoothFindFirstDevice(ref searchparams, ref device);
            if (handle == IntPtr.Zero)
            {
                int lasterror = Marshal.GetLastWin32Error();
                if (lasterror != 0)
                    LogError("Bluetooth API returned: " + lasterror.ToString());
            }
            else
            {
                while (true)
                {
                    if (Cancel)
                        break;

                    if (device.szName.StartsWith("Nintendo RVL"))
                    {
                        if (device.fRemembered)
                        {
                            BluetoothRemoveDevice(ref device.Address);
                        }
                        else
                        {
                            if (BluetoothSetServiceState(IntPtr.Zero, ref device, ref HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE) != 0)
                                LogError("Failed to connect to wiimote controller");
                            else
                                connected = true;
                        }
                        break;
                    }

                    device.szName = "";
                    if (!BluetoothFindNextDevice(handle, ref device))
                        break;
                }
            }
            BluetoothFindDeviceClose(handle);
            try
                {
            if (connected && Connected != null)
                Connected(this, EventArgs.Empty);

                    else if (ConnectionFailed != null)
                        ConnectionFailed(this, EventArgs.Empty);
                }
                catch(Exception)
                {

                }
        }
Ejemplo n.º 14
0
 static extern IntPtr BluetoothFindFirstDevice(ref BLUETOOTH_DEVICE_SEARCH_PARAMS SearchParams, ref BLUETOOTH_DEVICE_INFO DeviceInfo);