private static Task <BluetoothAdapter> GetDefaultAsyncImpl() { return(Task.Run(() => { if (s_default == null) { NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS p = new NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS(); p.dwSize = Marshal.SizeOf(p); IntPtr radioHandle; IntPtr findHandle = NativeMethods.BluetoothFindFirstRadio(ref p, out radioHandle); if (findHandle != IntPtr.Zero) { NativeMethods.BLUETOOTH_RADIO_INFO radioInfo = new NativeMethods.BLUETOOTH_RADIO_INFO(); radioInfo.dwSize = Marshal.SizeOf(radioInfo); if (NativeMethods.BluetoothGetRadioInfo(radioHandle, ref radioInfo) == 0) { s_default = new BluetoothAdapter(radioHandle, radioInfo); } NativeMethods.BluetoothFindRadioClose(findHandle); } } return s_default; })); }
internal BluetoothAdapter(IntPtr radioHandle, NativeMethods.BLUETOOTH_RADIO_INFO radioInfo) { _handle = radioHandle; _radioInfo = radioInfo; NativeMethods.SetWindowSubclass(Process.GetCurrentProcess().MainWindowHandle, SubclassProc, 1, IntPtr.Zero); //_messageWindow = new BluetoothMessageWindow(); // register for connection events NativeMethods.DEV_BROADCAST_HANDLE filter = new NativeMethods.DEV_BROADCAST_HANDLE(); filter.dbch_size = Marshal.SizeOf(filter); filter.dbch_handle = radioHandle; filter.dbch_devicetype = NativeMethods.DBT_DEVTYP.HANDLE; filter.dbch_eventguid = NativeMethods.GUID_BLUETOOTH_L2CAP_EVENT; _notifyHandle = NativeMethods.RegisterDeviceNotification(Process.GetCurrentProcess().MainWindowHandle, ref filter, NativeMethods.DEVICE_NOTIFY.WINDOWS_HANDLE); //filter.dbch_eventguid = NativeMethods.GUID_BLUETOOTH_HCI_EVENT; //int notifyHandle = NativeMethods.RegisterDeviceNotification(Process.GetCurrentProcess().MainWindowHandle, ref filter, NativeMethods.DEVICE_NOTIFY.WINDOWS_HANDLE); NativeMethods.PostMessage(Process.GetCurrentProcess().MainWindowHandle, 0x401, IntPtr.Zero, IntPtr.Zero); }
public static BluetoothAdapter GetDefault() { if (s_default == null) { NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS p = new NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS(); p.dwSize = Marshal.SizeOf(p); IntPtr radioHandle; IntPtr findHandle = NativeMethods.BluetoothFindFirstRadio(ref p, out radioHandle); if (findHandle != IntPtr.Zero) { NativeMethods.BLUETOOTH_RADIO_INFO radioInfo = new NativeMethods.BLUETOOTH_RADIO_INFO(); radioInfo.dwSize = Marshal.SizeOf(radioInfo); if (NativeMethods.BluetoothGetRadioInfo(radioHandle, ref radioInfo) == 0) { s_default = new BluetoothAdapter(radioHandle, radioInfo); } NativeMethods.BluetoothFindRadioClose(findHandle); } } return(s_default); }