Stack-Events Handling Sample (Compact 2013)
3/26/2014
The BtNotify sample test application illustrates the use of a message queue to handle event notifications received from the Bluetooth stack, and displays event data on the device.
The compiled sample generates a BtNotify.exe.
The sample is located at %_WINCEROOT%*\*Public\Common\Sdk\Samples\Bluetooth\BtNotify\.
Note
This sample application has not been thoroughly tested and is not intended for production use.
Usage
btnotify.exe
Btnotify sets up a message queue to receive events from the stack. The following code sample is an excerpt from this sample application which uses CreateMsgQueue to create a message queue.
HANDLE hMsgQ = CreateMsgQueue(NULL, &mqOptions);
if (! hMsgQ) {
wprintf(L"Error creating message queue.\r\n");
goto exit;
}
After the queue is created, the application calls RequestBluetoothNotifications to subscribe to events that notify about changes in connections, pairing, device, and the core stack. It uses the following event classes:
- BTE_CLASS_CONNECTIONS
- BTE_CLASS_PAIRING
- BTE_CLASS_DEVICE
- BTE_CLASS_STACK
When the stack starts sending notifications to peer Bluetooth device, the application reads the messages in the queue by calling the ReadMsgQueue function. Event data is received in the BTEVENT structure, and BTEVENT.baEventData is parsed to display information about the event.
BOOL fRet = ReadMsgQueue (hMsgQ, &btEvent, sizeof(BTEVENT), &dwBytesRead, 10, &dwFlags);
if (! fRet)
{
wprintf(L"Error - Failed to read message from queue!\r\n");
goto exit;
}
else
{
wprintf(L"----------------------------------------\r\n");
wprintf(L"Got event with id=%d.\r\n", btEvent.dwEventId);
DumpBuff(L"", btEvent.baEventData, sizeof(btEvent.baEventData));
ParseEvent(&btEvent);
}
Remarks
When the specified number of messages is read from the queue, the applications stops receiving notifications from the stack by calling the StopBluetoothNotifications function.