private int SubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, uint uIdSubclass, IntPtr dwRefData) { switch (uMsg) { case NativeMethods.WM_DEVICECHANGE: if (lParam != IntPtr.Zero) { NativeMethods.DEV_BROADCAST_HANDLE dbh = Marshal.PtrToStructure <NativeMethods.DEV_BROADCAST_HANDLE>(lParam); if (dbh.dbch_eventguid == NativeMethods.GUID_BLUETOOTH_HCI_EVENT) { // BTH_HCI_EVENT_INFO IntPtr bthhci = IntPtr.Add(lParam, 40); NativeMethods.BTH_HCI_EVENT_INFO ei = Marshal.PtrToStructure <NativeMethods.BTH_HCI_EVENT_INFO>(bthhci); Debug.WriteLine(ei.bthAddress + (ei.connected > 0 ? " connected" : " disconnected")); ConnectionStatusChanged?.Invoke(null, ei.bthAddress); } else if (dbh.dbch_eventguid == NativeMethods.GUID_BLUETOOTH_L2CAP_EVENT) { // BTH_L2CAP_EVENT_INFO IntPtr bthl2cap = IntPtr.Add(lParam, 40); NativeMethods.BTH_L2CAP_EVENT_INFO ei = Marshal.PtrToStructure <NativeMethods.BTH_L2CAP_EVENT_INFO>(bthl2cap); Debug.WriteLine(ei.bthAddress + (ei.connected > 0 ? " connected" : " disconnected")); ConnectionStatusChanged?.Invoke(null, ei.bthAddress); } } return(0); } return(0); }
private int WindowProc(IntPtr hwnd, uint uMsg, IntPtr wParam, IntPtr lParam) { Debug.WriteLine(uMsg); switch (uMsg) { case NativeMethods.WM_DEVICECHANGE: if (lParam != IntPtr.Zero) { NativeMethods.DEV_BROADCAST_HANDLE dbh = Marshal.PtrToStructure <NativeMethods.DEV_BROADCAST_HANDLE>(lParam); if (dbh.dbch_eventguid == NativeMethods.GUID_BLUETOOTH_HCI_EVENT) { //BTH_HCI_EVENT_INFO IntPtr bthhci = IntPtr.Add(lParam, 40); NativeMethods.BTH_HCI_EVENT_INFO ei = Marshal.PtrToStructure <NativeMethods.BTH_HCI_EVENT_INFO>(bthhci); Debug.WriteLine(ei.bthAddress + (ei.connected > 0 ? " connected" : " disconnected")); ConnectionStateChanged?.Invoke(null, ei.bthAddress); } else if (dbh.dbch_eventguid == NativeMethods.GUID_BLUETOOTH_L2CAP_EVENT) { //BTH_L2CAP_EVENT_INFO IntPtr bthl2cap = IntPtr.Add(lParam, 40); NativeMethods.BTH_L2CAP_EVENT_INFO ei = Marshal.PtrToStructure <NativeMethods.BTH_L2CAP_EVENT_INFO>(bthl2cap); Debug.WriteLine(ei.bthAddress + (ei.connected > 0 ? " connected" : " disconnected")); ConnectionStateChanged?.Invoke(null, ei.bthAddress); } } return(0); case 0x81: return(-1); case 1: case 0x83: return(0); } if (_prevWndProc != IntPtr.Zero) { return(NativeMethods.CallWindowProc(_prevWndProc, hwnd, uMsg, wParam, lParam)); } else { return(NativeMethods.DefWindowProc(hwnd, uMsg, wParam, lParam)); } }
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); }