//---------------------------------------------------------- // --- BLUETOOTH CODE --- //---------------------------------------------------------- // a c# wrapper for the C++ Windows API call // RequestBluetoothNotifications // // creates a queue where Bluetooth notifications can go, and // starts a thread that will wait for messages to appear on // that queue public void RegisterForBluetoothNotifications() { /* define the queue we will create for Bluetooth */ /* notification messages */ MSGQUEUEOPTIONS mqo = new MSGQUEUEOPTIONS(); mqo.dwSize = Marshal.SizeOf(mqo); // allocate message buffers on demand and free after reading mqo.dwFlags = 0;// MSGQUEUE_NOPRECOMMIT; // number of messages to allow to build up on queue mqo.dwMaxMessages = 10; // max size of a message mqo.cbMaxMessage = SIZEOF_BTEVENT; // we will only be getting messages from the queue mqo.bReadAccess = true; /* create the message queue and store handle */ msgQueueHandle = CreateMsgQueue(IntPtr.Zero, ref mqo); utils.ddump("msgQueue created: handle=" + msgQueueHandle.ToInt32().ToString("x")); /* register for BTE_CONNECTION and BTE_DISCONNECTION events */ notificationHandle = RequestBluetoothNotifications( BTE_CLASS.ALL, //BTE_CLASS.CONNECTIONS | BTE_CLASS.DEVICE | BTE_CLASS.PAIRING | BTE_CLASS.STACK, msgQueueHandle); utils.ddump("BT notification requested: " + notificationHandle.ToInt32().ToString("x")); /* create and start a background thread which will wait for */ /* (block on) messages from this Queue */ Thread t = new Thread(new ThreadStart(BluetoothEventThread)); t.IsBackground = true; t.Start(); }
public static extern IntPtr CreateMsgQueue([MarshalAs(UnmanagedType.LPWStr)] string lpszName, MSGQUEUEOPTIONS lpOptions);
public static extern IntPtr CreateMsgQueue([MarshalAs(UnmanagedType.LPWStr)]string lpszName, MSGQUEUEOPTIONS lpOptions);
static extern IntPtr CreateMsgQueue(IntPtr hString, ref MSGQUEUEOPTIONS pOptions);
static extern int CreateMsgQueue(string szName, ref MSGQUEUEOPTIONS pOptions);
void MsgQueueThread() { //only BTE_DISCONNECTION and BTE_CONNECTION change this state! addLog("thread about to start"); int hMsgQueue = 0; // IntPtr hBTevent = IntPtr.Zero; // allocate space to store the received messages byte[] msgBuffer = new byte[160];// Marshal.AllocHGlobal(160);// ITE_MESSAGE_SIZE); //MESSAGE _msg; WM_EVT_DATA _msg; try { //create msgQueueOptions MSGQUEUEOPTIONS msgQueueOptions = new MSGQUEUEOPTIONS(); msgQueueOptions.dwSize = (DWORD)Marshal.SizeOf(msgQueueOptions); msgQueueOptions.dwFlags = 0;// MSGQUEUE_NOPRECOMMIT; msgQueueOptions.dwMaxMessages = 10; msgQueueOptions.cbMaxMessage = MESSAGE_SIZE;// (DWORD)Marshal.SizeOf(ite_msg); msgQueueOptions.bReadAccess = ACCESS_READONLY; hMsgQueue = CreateMsgQueue(MESSAGE_QUEUE_NAME, ref msgQueueOptions); addLog("CreateMsgQueue=" + Marshal.GetLastWin32Error().ToString()); //6 = InvalidHandle if (hMsgQueue == 0) { addLog("Create MsgQueue failed"); throw new Exception("Create MsgQueue failed"); } Wait_Object waitRes = 0; //create a msg queue while (bRunThread) { // initialise values returned by ReadMsgQueue int bytesRead = 0; int msgProperties = 0; //block until message waitRes = (Wait_Object)WaitForSingleObject(hMsgQueue, 5000); if ((int)waitRes == -1) { int iErr = Marshal.GetLastWin32Error(); addLog("error in WaitForSingleObject=" + iErr.ToString()); //6 = InvalidHandle Thread.Sleep(1000); } switch (waitRes) { case Wait_Object.WAIT_OBJECT_0: //signaled //check event type and fire event //ReadMsgQueue entry //_msg = new MESSAGE(160); _msg = new WM_EVT_DATA(); bool success = ReadMsgQueue(hMsgQueue, // the open message queue _msg.buffer,// msgBuffer, // buffer to store msg _msg.size, // size of the buffer out bytesRead, // bytes stored in buffer -1, // wait forever out msgProperties); if (success) { // marshal the data read from the queue into a structure //ite_msg = (ITE_MESSAGE)Marshal.PtrToStructure(msgBuffer, typeof(ITE_MESSAGE)); addLog("msgqueue read: " + _msg.ToString());//Encoding.Unicode.GetString(ite_msg.msg, 0, bytesRead)); } else { addLog("ReadMsgQueue error: " + Marshal.GetLastWin32Error().ToString()); continue; //start a new while cirlce } //addLog("message received: " + ite_msg.ToString()); break; case Wait_Object.WAIT_ABANDONED: //wait has abandoned addLog("msg queue thread: WAIT_ABANDONED"); break; case Wait_Object.WAIT_TIMEOUT: //timed out addLog("msg queue thread: WAIT_TIMEOUT"); break; }//WaitRes }//while bRunThread } catch (ThreadAbortException ex) { addLog("msg queue thread ThreadAbortException: " + ex.Message + "\r\n" + ex.StackTrace); } catch (Exception ex) { addLog("msg queue thread exception: " + ex.Message + "\r\n" + ex.StackTrace); } finally { //Marshal.FreeHGlobal(msgBuffer); CloseMsgQueue(hMsgQueue); } addLog("msgqueue thread ended"); }
void MsgQueueThread() { //only BTE_DISCONNECTION and BTE_CONNECTION change this state! addLog("thread about to start"); int hMsgQueue = 0; // IntPtr hBTevent = IntPtr.Zero; // allocate space to store the received messages byte[] msgBuffer = new byte[160];// Marshal.AllocHGlobal(160);// ITE_MESSAGE_SIZE); //MESSAGE _msg; WM_EVT_DATA _msg; try { //create msgQueueOptions MSGQUEUEOPTIONS msgQueueOptions = new MSGQUEUEOPTIONS(); msgQueueOptions.dwSize = (DWORD)Marshal.SizeOf(msgQueueOptions); msgQueueOptions.dwFlags = 0; // MSGQUEUE_NOPRECOMMIT; msgQueueOptions.dwMaxMessages = 10; msgQueueOptions.cbMaxMessage = MESSAGE_SIZE; // (DWORD)Marshal.SizeOf(ite_msg); msgQueueOptions.bReadAccess = ACCESS_READONLY; hMsgQueue = CreateMsgQueue(MESSAGE_QUEUE_NAME, ref msgQueueOptions); addLog("CreateMsgQueue=" + Marshal.GetLastWin32Error().ToString()); //6 = InvalidHandle if (hMsgQueue == 0) { addLog("Create MsgQueue failed"); throw new Exception("Create MsgQueue failed"); } Wait_Object waitRes = 0; //create a msg queue while (bRunThread) { // initialise values returned by ReadMsgQueue int bytesRead = 0; int msgProperties = 0; //block until message waitRes = (Wait_Object)WaitForSingleObject(hMsgQueue, 5000); if ((int)waitRes == -1) { int iErr = Marshal.GetLastWin32Error(); addLog("error in WaitForSingleObject=" + iErr.ToString()); //6 = InvalidHandle Thread.Sleep(1000); } switch (waitRes) { case Wait_Object.WAIT_OBJECT_0: //signaled //check event type and fire event //ReadMsgQueue entry //_msg = new MESSAGE(160); _msg = new WM_EVT_DATA(); bool success = ReadMsgQueue(hMsgQueue, // the open message queue _msg.buffer, // msgBuffer, // buffer to store msg _msg.size, // size of the buffer out bytesRead, // bytes stored in buffer -1, // wait forever out msgProperties); if (success) { // marshal the data read from the queue into a structure //ite_msg = (ITE_MESSAGE)Marshal.PtrToStructure(msgBuffer, typeof(ITE_MESSAGE)); addLog("msgqueue read: " + _msg.ToString()); //Encoding.Unicode.GetString(ite_msg.msg, 0, bytesRead)); } else { addLog("ReadMsgQueue error: " + Marshal.GetLastWin32Error().ToString()); continue; //start a new while cirlce } //addLog("message received: " + ite_msg.ToString()); break; case Wait_Object.WAIT_ABANDONED: //wait has abandoned addLog("msg queue thread: WAIT_ABANDONED"); break; case Wait_Object.WAIT_TIMEOUT: //timed out addLog("msg queue thread: WAIT_TIMEOUT"); break; } //WaitRes } //while bRunThread } catch (ThreadAbortException ex) { addLog("msg queue thread ThreadAbortException: " + ex.Message + "\r\n" + ex.StackTrace); } catch (Exception ex) { addLog("msg queue thread exception: " + ex.Message + "\r\n" + ex.StackTrace); } finally { //Marshal.FreeHGlobal(msgBuffer); CloseMsgQueue(hMsgQueue); } addLog("msgqueue thread ended"); }
static public extern IntPtr CreateMsgQueue(string lpszName, ref MSGQUEUEOPTIONS lpOptions);
internal static extern IntPtr CreateMsgQueue(string lpszName, ref MSGQUEUEOPTIONS lpOptions);
//---------------------------------------------------------- // --- BLUETOOTH CODE --- //---------------------------------------------------------- // a c# wrapper for the C++ Windows API call // RequestBluetoothNotifications // // creates a queue where Bluetooth notifications can go, and // starts a thread that will wait for messages to appear on // that queue public void RegisterForBluetoothNotifications() { /* define the queue we will create for Bluetooth */ /* notification messages */ MSGQUEUEOPTIONS mqo = new MSGQUEUEOPTIONS(); mqo.dwSize = Marshal.SizeOf(mqo); // allocate message buffers on demand and free after reading mqo.dwFlags = 0;// MSGQUEUE_NOPRECOMMIT; // number of messages to allow to build up on queue mqo.dwMaxMessages = 10; // max size of a message mqo.cbMaxMessage = SIZEOF_BTEVENT; // we will only be getting messages from the queue mqo.bReadAccess = true; /* create the message queue and store handle */ msgQueueHandle = CreateMsgQueue(IntPtr.Zero, ref mqo); utils.ddump("msgQueue created: handle="+msgQueueHandle.ToInt32().ToString("x")); /* register for BTE_CONNECTION and BTE_DISCONNECTION events */ notificationHandle = RequestBluetoothNotifications( BTE_CLASS.ALL, //BTE_CLASS.CONNECTIONS | BTE_CLASS.DEVICE | BTE_CLASS.PAIRING | BTE_CLASS.STACK, msgQueueHandle); utils.ddump("BT notification requested: "+notificationHandle.ToInt32().ToString("x")); /* create and start a background thread which will wait for */ /* (block on) messages from this Queue */ Thread t = new Thread(new ThreadStart(BluetoothEventThread)); t.IsBackground = true; t.Start(); }