Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedded Visual C++ Bluetooth Discovery Problems 1

Status
Not open for further replies.

greeneka

Programmer
Aug 11, 2003
17
IE
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.

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;	
}
 
I do not know you SDK, but aren't you missing a call to WSAInitialize. And I think you should first try to do some simple amplications that are just trying to connect to the device. I think there is a problem with IPs etc.

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Hi IonelBurtan,

I don't think there is a WSAInitialize function, perhaps you were thinking of WSAStartup?

This was the simplest app I could think of - AFAIK i need to obtain the address of the device before I can connect to the device, and to find the address I need to be able to see the device, and to see the device I need to be able to enumerate the currently available devices, which is what I'm trying to do here, if you catch my drift;)

IP shouldn't effect discovering Bluetooth devices, I'm not trying to create a connection, just list available devices, which shouldn't have anything to do with IP addresses!

Anyway, thanks for the reply!
 
Sorry, I have missed your WSAStartup call. I think I understand what are you tring to do now. The IP uncopatibility cannot be excluded though.

I thins the functions are doing this the "detection" by sending unicast packets(BROADCAST). I am thinking of the 3rd number from the IP of the devices. Maybe they have to be in the same subnet. I am working in Embedded VC++ 3.0 but with PocketPC terminals under Windows CE.

These are just ideas, nothing to die for :)

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
I have just met the same problem. That's why I'd like to know if you finally managed and how.

Thanking you in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top