Example #1
0
        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);
        }
Example #2
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));
            }
        }