Hi All,
I've posted this here since there doesn't seem to be an embedded development section.
I am trying to code up a simple Bluetooth device discovery application - all it does it try to list the devices currently available. I have followed sample code from a book (Prog. MS WinCe.NET by D. Boling), and have checked the SDK to see if I'm doing anything obviously stupid (which I don't seem to be!?). The destination device is a HP iPAQ h4150, which has BT built in and runs PPC2003.
Anyway, the problem is that the program accepts the WSALookupServiceBegin, but fails with error code 10050 (WSAENETDOWN) when WSALookupServiceNext is called. Any help on this would be greatly appreciated, the code I'm using is included below, the commented out part is optional according to the SDK.
Thanks.
I've posted this here since there doesn't seem to be an embedded development section.
I am trying to code up a simple Bluetooth device discovery application - all it does it try to list the devices currently available. I have followed sample code from a book (Prog. MS WinCe.NET by D. Boling), and have checked the SDK to see if I'm doing anything obviously stupid (which I don't seem to be!?). The destination device is a HP iPAQ h4150, which has BT built in and runs PPC2003.
Anyway, the problem is that the program accepts the WSALookupServiceBegin, but fails with error code 10050 (WSAENETDOWN) when WSALookupServiceNext is called. Any help on this would be greatly appreciated, the code I'm using is included below, the commented out part is optional according to the SDK.
Thanks.
Code:
// ws2.lib library linked
#include <windows.h>
#include <winsock2.h>
#include <bthapi.h>
#include <bt_api.h>
#define BUFFERSIZE 16*1024
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
void doBluetooth(HWND);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hWnd, hwndChild;
MSG msg;
RECT rect;
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL,
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("BT_TEST");
if (RegisterClass (&wc) == 0) return -1;
hWnd = CreateWindowEx(WS_EX_NODRAG,
TEXT("BT_TEST"),
TEXT("Bluetooth test"),
WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!IsWindow (hWnd)) return -1;
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
GetClientRect(hWnd, &rect);
hwndChild = CreateWindow (TEXT("listbox"),
TEXT("blah"),
WS_VISIBLE | WS_VSCROLL,
rect.left,
rect.top,
rect.right-rect.left,
rect.bottom-rect.top,
hWnd,
NULL,
hInstance,
NULL);
if (!IsWindow (hwndChild)) return 0;
ShowWindow (hwndChild, nCmdShow);
UpdateWindow (hwndChild);
doBluetooth(hwndChild);
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_DESTROY:
PostQuitMessage (0);
break;
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
void doBluetooth(HWND hwndListbox){
WSADATA wsaData;
DWORD flags, len;
TCHAR log[256];
HANDLE handle;
int i, retCode, nMax = 10;
WSAQUERYSET QuerySet;
char * buffer = new char[BUFFERSIZE];
WSAQUERYSET *pQueryResult = (WSAQUERYSET *)buffer;
SOCKADDR_BTH *pbta;
if(!buffer){
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("ERROR - Cannot Allocate Buffer"));
return;
}
if ((retCode = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0){
wsprintf(log, TEXT("ERROR - Cannot Start Sockets: %d"), retCode);
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)log);
return;
} else {
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("Winsock Started OK"));
}
// as per Programming Windows CE.NET by Douglas Boling
// according to the SDK doc's this BLOB is optional, so
// I tried removing it, but still no joy!
//
//BTHNS_INQUIRYBLOB inqblob;
//memset(&inqblob, 0, sizeof(inqblob));
//inqblob.LAP = BT_ADDR_GIAC;
//inqblob.length = 6;
//inqblob.num_responses = nMax;
//
//BLOB blob;
//blob.cbSize = sizeof(BTHNS_INQUIRYBLOB);
//blob.pBlobData = (PBYTE)&inqblob;
//
//memset(&QuerySet, 0, sizeof(QuerySet));
//QuerySet.dwSize = sizeof(WSAQUERYSET);
//QuerySet.dwNameSpace = NS_BTH;
//QuerySet.lpBlob = &blob;
memset(&QuerySet, 0, sizeof(QuerySet));
QuerySet.dwSize = sizeof(WSAQUERYSET);
QuerySet.dwNameSpace = NS_BTH;
retCode = WSALookupServiceBegin(&QuerySet, LUP_CONTAINERS, &handle);
if(retCode != 0){
// Unfortunately FormatMessage won't work with WSAGetLastError in WinCE
wsprintf(log, TEXT("ERROR - Failed Service Begin: %d"),WSAGetLastError());
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)log);
return;
}
for(i = 0; i < nMax; i++){
len = BUFFERSIZE;
flags = LUP_RETURN_NAME | LUP_RETURN_ADDR;
retCode = WSALookupServiceNext(handle, flags, &len, pQueryResult);
if(retCode == SOCKET_ERROR){
retCode = WSAGetLastError();
if(retCode == WSA_E_NO_MORE){
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("No More Devices"));
}else{
// Unfortunately FormatMessage won't work on WSAGetLastError in WinCE
wsprintf(log, TEXT("ERROR - Failed next lookup: %d"), retCode);
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)log);
}
break;
}else{
pbta = (SOCKADDR_BTH *)pQueryResult->lpcsaBuffer->RemoteAddr.lpSockaddr;
wsprintf(log, TEXT("Device: %s %d"), pQueryResult->lpszServiceInstanceName, pbta->btAddr);
SendMessage(hwndListbox, LB_ADDSTRING, NULL, (LPARAM)log);
}
}
WSALookupServiceEnd(handle);
delete [] buffer;
}