Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
May I ask, how to activate Bluetooth SPP service in WindowsCE devices? Thank you very much! By the way, I tried the following code does not work.
C++
HANDLE ActivateSPP(WCHAR* sz, BT_ADDR btAddr)
{
	int index = 0;
	PORTEMUPortParams pp;
	memset(&pp, 0, sizeof(pp));
	unsigned char nChannel = 0xfe;
	pp.channel = nChannel & 0xff;
	if (wcsicmp(sz, L"client") == 0)
	{
		pp.device = btAddr;
		pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB;
	}
	if (wcsicmp(sz, L"server") == 0)
	{
		pp.flocal = TRUE;
	}
	HANDLE hDevice = NULL;
	for (index = 1; index <= 9; index++)
	{
		hDevice = RegisterDevice(L"COM", index, L"btd.dll", (DWORD)&pp);
		if (hDevice != NULL)
		{
			break;
		}
	}
	WCHAR szComPort[30];
	wsprintf(szComPort, L"COM%d:", index);
	HANDLE hCommPort = CreateFile(szComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (hCommPort == INVALID_HANDLE_VALUE)
	{
		DeregisterDevice(hDevice);
		return INVALID_HANDLE_VALUE;
	}
	return hDevice;
}
Posted
Updated 15-Apr-11 22:31pm
v2
Comments
Richard MacCutchan 16-Apr-11 4:32am    
"Does not work" tells us nothing that will help to diagnose your problem; please be specific about what you expect to happen and what does happen.
xiaboyang 17-Apr-11 5:52am    
Forgive me, my English is very bad, so did not clear. It is in Chinese:这段代码不起作用,SPP服务没有能够正常开启.
Richard MacCutchan 17-Apr-11 7:01am    
Your message says "This code does not work, SPP service is not able to open normally". This still does not help, maybe you should post your question in the Chinese forum: http://www.codeproject.com/Forums/1580230/General-Chinese-Topics.aspx.
xiaboyang 17-Apr-11 10:09am    
Thank you for your reply.
xiaboyang 17-Apr-11 10:15am    
I used "Windows Mobile 5.0 Pocket PC SDK\Samples\CPP\Win32\Bthchat" Sample Code,bluetooth spp service still does not start.

//SDP record generated by bthnscreate.exe
BYTE rgbSdpRecord[] = {
0x35, 0x4d, 0x09, 0x00, 0x01, 0x35, 0x11, 0x1c,
0x29, 0xf9, 0xc0, 0xfd, 0xbb, 0x6e, 0x47, 0x97,
0x9f, 0xa9, 0x3e, 0xc9, 0xa8, 0x54, 0x29, 0x0c,
0x09, 0x00, 0x04, 0x35, 0x0c, 0x35, 0x03, 0x19,
0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08,
0x1a, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65,
0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, 0x00, 0x09,
0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11,
0x05, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25,
0x06, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c
};

//SDP record size constant returned by bthnscreate.exe
#define SDP_RECORD_SIZE 0x0000004f
#define SDP_CHANNEL_OFFSET 40

int BthUtils::RegisterService(BYTE *rgbSdpRecord, int cSdpRecord, int iChannelOffset, UCHAR channel)
{
ULONG recordHandle = 0;

struct bigBlob
{
BTHNS_SETBLOB b;

}*pBigBlob;

pBigBlob = (bigBlob *)malloc(sizeof(struct bigBlob)+cSdpRecord);
ULONG ulSdpVersion = BTH_SDP_VERSION;
pBigBlob->b.pRecordHandle = &recordHandle;
pBigBlob->b.pSdpVersion = &ulSdpVersion;
pBigBlob->b.fSecurity = 0;
pBigBlob->b.fOptions = 0;
pBigBlob->b.ulRecordLength = cSdpRecord;

memcpy (pBigBlob->b.pRecord, rgbSdpRecord, cSdpRecord);
pBigBlob->b.pRecord[iChannelOffset] = (unsigned char)channel;
BLOB blob;
blob.cbSize = sizeof(BTHNS_SETBLOB) + cSdpRecord - 1;
blob.pBlobData = (PBYTE) pBigBlob;

WSAQUERYSET Service;
memset (&Service, 0, sizeof(Service));
Service.dwSize = sizeof(Service);
Service.lpBlob = &blob;
Service.dwNameSpace = NS_BTH;
if (WSASetService(&Service,RNRSERVICE_REGISTER,0) == SOCKET_ERROR)
{
free(pBigBlob);
return WSAGetLastError();
}
else
{
free(pBigBlob);
return 0;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900