Exemple #1
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);
            }
        }
        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;
        }
        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());
        }
        public Win32BluetoothAuthentication(BluetoothAddress address, string pin)
        {
            Address = address;
            _pin    = pin;
            BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();

            device.Address = address;
            Win32Native.BluetoothGetDeviceInfo(IntPtr.Zero, ref device);
            _callback = new BluetoothAuthenticationCallbackEx(Callback);

            int result = Win32Native.BluetoothRegisterForAuthenticationEx(ref device, out _handle, _callback, IntPtr.Zero);
        }
        string GetRemoteMachineName()
        {
            if (GetConnected())
            {
                var remote = _socket.RemoteEndPoint as BluetoothEndPoint;
                var info   = BLUETOOTH_DEVICE_INFO.Create();
                info.Address = remote.Address;
                NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref info);
                return(info.szName);
            }

            return(string.Empty);
        }
 /// <summary>
 /// Initializes an instance of the BluetoothDeviceInfo class for the device with the given address.
 /// </summary>
 /// <param name="address">The BluetoothAddress.</param>
 public BluetoothDeviceInfo(BluetoothAddress address)
 {
     _info         = BLUETOOTH_DEVICE_INFO.Create();
     _info.Address = address;
     NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref _info);
 }
 /// <summary>
 /// Initializes an instance of the BluetoothDeviceInfo class for the device with the given address.
 /// </summary>
 /// <param name="address">The BluetoothAddress.</param>
 public BluetoothDeviceInfo(BluetoothAddress address)
 {
     _info         = BLUETOOTH_DEVICE_INFO.Create();
     _info.Address = address;
     DoRefresh();
 }
Exemple #8
0
        private void DoDevTypHandle(ref Message m, ref String text)
        {
#if DEBUG
            WindowsBluetoothDeviceInfo dev = null;
#endif
            DEV_BROADCAST_HANDLE hdrHandle = (DEV_BROADCAST_HANDLE)m.GetLParam(typeof(DEV_BROADCAST_HANDLE));
            var pData = Utils.Pointers.Add(m.LParam, _OffsetOfData);
            if (BluetoothDeviceNotificationEvent.BthPortDeviceInterface == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BTHPORT_DEVICE_INTERFACE";
            }
            else if (BluetoothDeviceNotificationEvent.RadioInRange == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_RADIO_IN_RANGE";
                BTH_RADIO_IN_RANGE inRange
                      = (BTH_RADIO_IN_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_IN_RANGE));
                text += String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                      " 0x{0:X12}", inRange.deviceInfo.address);
                text += String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                      " is ({0}) 0x{0:X}", inRange.deviceInfo.flags);
                text += String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                      " was ({0}) 0x{0:X}", inRange.previousDeviceFlags);
                var bdi0 = BLUETOOTH_DEVICE_INFO.Create(inRange.deviceInfo);
                var e    = BluetoothWin32RadioInRangeEventArgs.Create(
                    inRange.previousDeviceFlags,
                    inRange.deviceInfo.flags, bdi0);
#if DEBUG
                dev = new WindowsBluetoothDeviceInfo(bdi0);
                Debug.WriteLine("InRange: " + dev.DeviceAddress);
#endif
                RaiseInRange(e);
            }
            else if (BluetoothDeviceNotificationEvent.RadioOutOfRange == hdrHandle.dbch_eventguid)
            {
                BTH_RADIO_OUT_OF_RANGE outOfRange
                      = (BTH_RADIO_OUT_OF_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_OUT_OF_RANGE));
                text += "GUID_BLUETOOTH_RADIO_OUT_OF_RANGE";
                text += String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                      " 0x{0:X12}", outOfRange.deviceAddress);
                var e = BluetoothWin32RadioOutOfRangeEventArgs.Create(
                    outOfRange.deviceAddress);
                Debug.WriteLine("OutOfRange: " + outOfRange.deviceAddress);
                RaiseOutOfRange(e);
            }
            else if (BluetoothDeviceNotificationEvent.PinRequest == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_PIN_REQUEST";
                // "This message should be ignored by the application.
                //  If the application must receive PIN requests, the
                //  BluetoothRegisterForAuthentication function should be used."
            }
            else if (BluetoothDeviceNotificationEvent.L2capEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_L2CAP_EVENT";
                // struct BTH_L2CAP_EVENT_INFO {
                //   BTH_ADDR bthAddress; USHORT psm; UCHAR connected; UCHAR initiated; }
#if DEBUG
                var l2capE = Marshal_PtrToStructure <BTH_L2CAP_EVENT_INFO>(pData);
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                              "L2CAP_EVENT: addr: {0:X}, psm: {1}, conn: {2}, init'd: {3}",
                                              l2capE.bthAddress, l2capE.psm, l2capE.connected, l2capE.initiated));
#endif
            }
            else if (BluetoothDeviceNotificationEvent.HciEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_HCI_EVENT";
                // struct BTH_HCI_EVENT_INFO {
                //   BTH_ADDR bthAddress; UCHAR connectionType; UCHAR connected; }
#if DEBUG
                var hciE = Marshal_PtrToStructure <BTH_HCI_EVENT_INFO>(pData);
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                              "HCI_EVENT: addr: {0:X}, type: {1}, conn: {2}",
                                              hciE.bthAddress, hciE.connectionType, hciE.connected));
#endif
            }
            // -- New somewhere after WinXP.
            else if (BluetoothDeviceNotificationEvent.AuthenticationRequestEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_AUTHENTICATION_REQUEST";
                // Same content as BluetoothRegisterForAuthenticationEx
            }
            else if (BluetoothDeviceNotificationEvent.KeyPressEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_KEYPRESS_EVENT";
                // struct BTH_HCI_KEYPRESS_INFO {
                //   BTH_ADDR  BTH_ADDR; UCHAR   NotificationType; // HCI_KEYPRESS_XXX value }
            }
            else if (BluetoothDeviceNotificationEvent.HciVendorEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_HCI_VENDOR_EVENT";
            }
            // -- Unknown
            else
            {
                text += "Unknown event: " + hdrHandle.dbch_eventguid;
            }
            Debug.WriteLine("Text: " + text);
        }
Exemple #9
0
        private void DoDevTypHandle(ref WndProcClient.WindowMessage m, ref String text)
        {
            DEV_BROADCAST_HANDLE hdrHandle = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(m.lParam, typeof(DEV_BROADCAST_HANDLE));
            var pData = PointerUtils.Add(m.lParam, _OffsetOfData);

            if (BluetoothDeviceNotificationEvent.BthPortDeviceInterface == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BTHPORT_DEVICE_INTERFACE";
            }
            else if (BluetoothDeviceNotificationEvent.RadioInRange == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_RADIO_IN_RANGE";
                BTH_RADIO_IN_RANGE inRange
                      = (BTH_RADIO_IN_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_IN_RANGE));
                text += String.Format(" 0x{0:X12}", inRange.deviceInfo.address);
                text += String.Format(" is ({0}) 0x{0:X}", inRange.deviceInfo.flags);
                text += String.Format(" was ({0}) 0x{0:X}", inRange.previousDeviceFlags);
                var bdi0 = BLUETOOTH_DEVICE_INFO.Create(inRange.deviceInfo);
                var e    = BluetoothWin32RadioInRangeEventArgs.Create(
                    inRange.previousDeviceFlags,
                    inRange.deviceInfo.flags, bdi0);
                DeviceInRange?.Invoke(this, e);
            }
            else if (BluetoothDeviceNotificationEvent.RadioOutOfRange == hdrHandle.dbch_eventguid)
            {
                BTH_RADIO_OUT_OF_RANGE outOfRange
                      = (BTH_RADIO_OUT_OF_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_OUT_OF_RANGE));
                text += "GUID_BLUETOOTH_RADIO_OUT_OF_RANGE";
                text += String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                      " 0x{0:X12}", outOfRange.deviceAddress);
                var e = BluetoothWin32RadioOutOfRangeEventArgs.Create(
                    outOfRange.deviceAddress);

                DeviceOutOfRange?.Invoke(this, e);
            }
            else if (BluetoothDeviceNotificationEvent.PinRequest == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_PIN_REQUEST";
            }
            else if (BluetoothDeviceNotificationEvent.L2capEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_L2CAP_EVENT";
            }
            else if (BluetoothDeviceNotificationEvent.HciEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_HCI_EVENT";
            }
            else if (BluetoothDeviceNotificationEvent.AuthenticationRequestEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_AUTHENTICATION_REQUEST";
            }
            else if (BluetoothDeviceNotificationEvent.KeyPressEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_KEYPRESS_EVENT";
            }
            else if (BluetoothDeviceNotificationEvent.HciVendorEvent == hdrHandle.dbch_eventguid)
            {
                text += "GUID_BLUETOOTH_HCI_VENDOR_EVENT";
            }
            else
            {
                text += "Unknown event: " + hdrHandle.dbch_eventguid;
            }

            Log.Verbose("Interop.Win32: Device changed: " + text);
        }