• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 965
  • Last Modified:

Win32 Bluetooth chat kind of code needed.

Hi ,

I am new to win32 programming and I need to build a bluetooth file transfer (not unsing OBEX) application. I appreciate if some one send my a sample bluetooth code in VC++.

Thanks
0
JPGaur
Asked:
JPGaur
  • 5
  • 3
5 Solutions
 
btframeworkCommented:
First at all you should know that each bluetooth drivers have its own API. Take a look at thi product: http://www.btframework.com
0
 
JPGaurAuthor Commented:
Thanks btframework,

The WCL library can be good, but I need to do all the development using microsoft stack and SDK.
0
 
btframeworkCommented:
Aha. Than it is easy :) Take a look on this blog: http://www.lenholgate.com/archives/000102.html it is exlained there very well.
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
JPGaurAuthor Commented:
Thanks btframework,

This code does not working on desktop, any way I need some code in win32 sdk (not MFC CSocket) for windows mobile device/smartphone.
0
 
btframeworkCommented:
MS Bluetooth SDK for WinMobile IS BASED on WindowsSocket. There is no other common API. The code provided on that page will work on WinMobile without changes. If you want more detailed explaination (there are lots of thing you should to know and it is very hard to explaing it here) you may contact me directly by MSN(mike@btframework.com) or ICQ(371012020).

Here is peace of code for discovering BT devices on WinMobile (the code is from working PPC app (www.bluexpand.com)).
DWORD dwDevBufSize = 1024 * 30;
	LPWSAQUERYSET lpResults = (LPWSAQUERYSET)LocalAlloc(LPTR, dwDevBufSize);
	if (lpResults)
	{
		DWORD dwFlags = LUP_CONTAINERS;
		WSAQUERYSET QuerySet;
		ZeroMemory(&QuerySet, sizeof(WSAQUERYSET));
		QuerySet.dwSize = sizeof(WSAQUERYSET);
		QuerySet.dwNameSpace = NS_BTH;
		HANDLE hLookup = NULL;
		
		DWORD dwRes = WSALookupServiceBegin(&QuerySet, dwFlags, &hLookup);
		if (!dwRes)
		{
			dwFlags = LUP_RETURN_NAME | LUP_RETURN_ADDR;
			while ((!dwRes) && m_hWnd)
			{
				DWORD dwResultsSize = dwDevBufSize;
				ZeroMemory(lpResults, dwResultsSize);
 
				dwRes = WSALookupServiceNext(hLookup, dwFlags, &dwResultsSize, lpResults);
				if (!dwRes)
				{
					LPCSADDR_INFO lpCSAddr = (LPCSADDR_INFO)lpResults->lpcsaBuffer;
					if (lpCSAddr)
					{
						PSOCKADDR_BTH lpSockAddrBth =
							(PSOCKADDR_BTH)lpCSAddr->RemoteAddr.lpSockaddr;
						if (lpSockAddrBth)
						{
							CString sServName = L"";
							/*if (BtSettings.m_bCheckForModel)
							{
								DWORD dwServBufSize = 1024 * 30;
								LPWSAQUERYSET lpServResults = (LPWSAQUERYSET)LocalAlloc(LPTR,
									dwServBufSize);
								if (lpServResults)
								{
									GUID Service = L2CAP_PROTOCOL_UUID;
									WSAQUERYSET ServicesQuerySet;
									ZeroMemory(&ServicesQuerySet, sizeof(WSAQUERYSET));
									ServicesQuerySet.dwSize = sizeof(WSAQUERYSET);
									ServicesQuerySet.dwNameSpace = NS_BTH;
									ServicesQuerySet.lpServiceClassId = &Service;
									ServicesQuerySet.lpcsaBuffer = lpResults->lpcsaBuffer;
									HANDLE hServLookup = NULL;
									
									DWORD dwServRes = WSALookupServiceBegin(&ServicesQuerySet,
										0, &hServLookup);
									if (!dwServRes)
									{
										while ((!dwRes) && m_hWnd)
										{
											DWORD dwServResultsSize = dwServBufSize;
											ZeroMemory(lpServResults, dwServResultsSize);
											dwServRes = WSALookupServiceNext(hServLookup, 0,
												&dwServResultsSize, lpServResults);
											if (!dwRes)
											{
												// Decode here
											}
										}
										
										WSALookupServiceEnd(hServLookup);
									}
								}
							}*/
							
							CBtDevice* pDevice = new CBtDevice(lpSockAddrBth->btAddr,
								lpResults->lpszServiceInstanceName, sServName);
							pDevices->Add(pDevice);
						}
					}
				}
			}
			
			WSALookupServiceEnd(hLookup);
		}
		
		LocalFree((HLOCAL)lpResults);
	}

Open in new window

0
 
btframeworkCommented:
This code connects t remote device (i do repeat, the code is from working app and has some things which are not related to BT, but I think you can understand it :) )
SOCKADDR_BTH SockAddr;
	DWORD dwSockAddrSize = sizeof(SOCKADDR_BTH);
	ZeroMemory(&SockAddr, dwSockAddrSize);
	SockAddr.addressFamily = AF_BTH;
	SockAddr.btAddr = m_BtAddr;
	SockAddr.serviceClassId = OBEXObjectPushServiceClass_UUID;
 
	BtManager.Lock();
	if (!StartTimer())
	{
		SendMessage(m_hWnd, WM_BT_SEND_COMPLETE, (WPARAM)resIgnored, 0);
		BtManager.Unlock();
		return;
	}
	if (connect(m_hSocket, (PSOCKADDR)&SockAddr, dwSockAddrSize) == SOCKET_ERROR)
	{
		BtManager.Unlock();
		int iRes = WSAGetLastError();
		if ((iRes == WSAEACCES) || (iRes == WSAEADDRINUSE) || (iRes == WSAETIMEDOUT) ||
			(iRes == WSAEADDRNOTAVAIL))
			SendMessage(m_hWnd, WM_BT_SEND_COMPLETE, (WPARAM)resIgnored, 0);
		else
			// WSAECONNABORTED and others not decoded
			SendMessage(m_hWnd, WM_BT_SEND_COMPLETE, (WPARAM)resRejected, 0);
		return;
	}
	BtManager.Unlock();

Open in new window

0
 
JPGaurAuthor Commented:
Thanks again btframework:

But my problem is slightly different I got an example in windows mobile 6 SDK BTChat but it seems to works only with itself (need same application installed on the other device) while I need to make it communicate to other device like symbian S60 etc, I was able to change the service Id in this example and set it to generic spp service ID, then the example application was able to send the message to the other device(symbian) , but It can't able to receive the return message from the other symbian device.

So I am done with device discovery, service discovery, connect to other device (any device like symbian/ a desktop which has bluesolie stack) and send a message to that device.
The problem is the BTChat(\Program Files\Windows Mobile 6 SDK\Samples\PocketPC\CPP\win32\Bluetooth\BthChat) application is not able to receive an incoming message from an device other then a winmobile device running same application.
0
 
btframeworkCommented:
Of course! Because you MUST connect to specified service whcih device can understand. If you would like to send a file to device then you must use OBEX (FTP/OPP or Nokia specified). There is NO way to send message without known the target service. And, of course, there is NO way to send file to target device without OBEX if there is no app running on target device which can understand your own protocol. As I wrote above there are too many things you should to know before work with bluetooth. Good start point is www.bluetooth.org - read the dcs and specs. Or, as I wrote, contact me directly. I'll try to explaing some things.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now