PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Bluetooth: Registering and searching a Service

PsychoChris · 12 Apr 2007, 16:03 UTC
Hello folks,

i don't manage to register a SDP service record with WSASetService() in order to publish the RFCOMM channel on which my server is listening correctly or i fail in executing a search for the registered record.
Did anybody manage this? If yes, do you have any example code on this? Or any hints on how to generate a SDP record for publishing and searching a service? All documents i found on this lack in explanation or don't work at all.
Any hint or information would be helpful. If anybody did this i could also post some code to show what i tried.

Kind regards,
Chris
This is pretty much verbatim from a sample (can't find the source offhand).

Code:

int CreateBluetooth(const GUID *pGUID, char *sServiceName) {
m_sock=socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (m_sock==INVALID_SOCKET)
return 1;

SOCKADDR_BTH SockAddrBthLocal = {0};
SockAddrBthLocal.addressFamily = AF_BTH;
SockAddrBthLocal.port = BT_PORT_ANY;

if (bind(m_sock, (SOCKADDR *)&SockAddrBthLocal;, sizeof(SOCKADDR_BTH))== SOCKET_ERROR)
return 2;

int iAddrLen = sizeof(SOCKADDR_BTH);
if (getsockname(m_sock, (struct sockaddr *)&SockAddrBthLocal;, &iAddrLen;) == SOCKET_ERROR)
return 3;

LPCSADDR_INFO lpCSAddrInfo = (LPCSADDR_INFO) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CSADDR_INFO));
lpCSAddrInfo[0].LocalAddr.iSockaddrLength = sizeof( SOCKADDR_BTH );
lpCSAddrInfo[0].LocalAddr.lpSockaddr = (LPSOCKADDR)&SockAddrBthLocal;
lpCSAddrInfo[0].RemoteAddr.iSockaddrLength = sizeof( SOCKADDR_BTH );
lpCSAddrInfo[0].RemoteAddr.lpSockaddr = (LPSOCKADDR)&SockAddrBthLocal;
lpCSAddrInfo[0].iSocketType = SOCK_STREAM;
lpCSAddrInfo[0].iProtocol = BTHPROTO_RFCOMM;

//
// If we got an address, go ahead and advertise it.
//
WSAQUERYSET wsaQuerySet = {0};
ZeroMemory(&wsaQuerySet;, sizeof(WSAQUERYSET));
wsaQuerySet.dwSize = sizeof(WSAQUERYSET);
wsaQuerySet.lpServiceClassId = (LPGUID) pGUID;

wsaQuerySet.lpszServiceInstanceName = sServiceName;
wsaQuerySet.lpszComment = "";
wsaQuerySet.dwNameSpace = NS_BTH;
wsaQuerySet.dwNumberOfCsAddrs = 1; // Must be 1.
wsaQuerySet.lpcsaBuffer = lpCSAddrInfo; // Req'd.

//
// As long as we use a blocking accept(), we will have a race
// between advertising the service and actually being ready to
// accept connections. If we use non-blocking accept, advertise
// the service after accept has been called.
//
if (WSASetService(&wsaQuerySet;, RNRSERVICE_REGISTER, 0)==SOCKET_ERROR)
{
return 4;
}

//
// listen() call indicates winsock2 to listen on a given socket for any incoming connection.
//
if (listen(m_sock, SOMAXCONN)==SOCKET_ERROR)
{
return 5;
}

SOCKET sClient=accept(m_sock, NULL, NULL);

return 0;
}


Dan East
PsychoChris · 13 Apr 2007, 21:49 UTC
Hello Dan,

thank you for the code snippet. Unfortunately WSASetService() fails with the error code 10022 which means that an invalid argument has been provided. I tried the snippet on a PocketPC 2003 SE (phone edition) device.
Some things that are not clear for me:

    btAddr and serviceClassId of SockAddrBthLocal after calling getsockname() are zero. Should it be like that?

    LocalAddr and RemoteAddr of lpCSAddrInfo are the same. Does this make sense?

    Can the GUID be freely choosen as long as it is unique?


Regards,
Chris[/list]
No, those shouldn't be zero. The code I posted runs properly on XP with a D-Link bluetooth USB dongle, installed with the MS BT stack (not the drivers that shipped with the device). I've not created a host / service under CE, just XP.

One thing that comes to mind is to make sure you're linking to Winsock 2 (ws2.lib) instead of Winsock 1 (winsock.lib). I know on the desktop you have to use Winsock 2 for bluetooth, so I assume that also applies to Windows CE.

Dan East
Make sure you are initializing with the proper winsock version too:
Code:

WSADATA WSAData = {0};
if (WSAStartup(MAKEWORD(2, 2), &WSAData;)) {
//Fail
}


Dan East
PsychoChris · 14 Apr 2007, 19:54 UTC
Hi Dan,

the initialization succeeds. I tested the code under Windows XP and was surprised: it worked. But on Pocket PC devices WSASetService() fails. So i guess this has to be done differently on Pocket PCs. I'm also confused that the SOCKADDR_BTH structure is filled on Windows XP by calling getsockname() with a GUID and an address. Both values are set to zero on a Pocket PC device after a successfull call of getsockname(). Only the channel is set.
My main problem is that i somehow need to publish the channel according to MSDN as a service so that connecting devices can retrieve this information and use it for establishing a connection. The only way i manage a bluetooth connection on Pocket PCs is by knowing the channel i have to use for establishing the connection. MSDN mentions a SDP record i have to register in order to publish the channel. But examples are quite rare and everything else than detailed. Even after managing to register this vague documented SDP record successfully, or lets say at least without any errors given back by any function, i still cannot connect to the device without knowing the channel.
This problem now annoys me now more than a week... sigh.

Kind regards,
Chris
PsychoChris · 16 Apr 2007, 15:10 UTC
Hello folks,

i finally made some progress and managed to publish a custom bluetooth service record (SDP record) containing the RFCOMM channel needed for connecting to the server. The next problem i'm facing is that a client needs to query the SDP database on the server by using WSALookupServiceBegin() and WSALookupServiceNext(). Well, using both functions for querying bluetooth devices in range is no big deal. But how can i use them to query for my custom service? Again MSDN raises more questions than giving answers on how to do that. They mention a BTHNS_RESTRICTIONBLOB structure which should be used for this but all my experiments on getting any information of any service fail although the functions succeed. Somehow i need to find my service by searching for its GUID and then retrieve all data belonging to the service. But how? Anybody any ideas?

Kind regards,
Chris
hhq188 · 21 Apr 2008, 08:44 UTC — Thank you for your help
Dan East wrote:This is pretty much verbatim from a sample (can't find the source offhand).

Code:

int CreateBluetooth(const GUID *pGUID, char *sServiceName) {
m_sock=socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (m_sock==INVALID_SOCKET)
return 1;

SOCKADDR_BTH SockAddrBthLocal = {0};
SockAddrBthLocal.addressFamily = AF_BTH;
SockAddrBthLocal.port = BT_PORT_ANY;

if (bind(m_sock, (SOCKADDR *)&SockAddrBthLocal;, sizeof(SOCKADDR_BTH))== SOCKET_ERROR)
return 2;

int iAddrLen = sizeof(SOCKADDR_BTH);
if (getsockname(m_sock, (struct sockaddr *)&SockAddrBthLocal;, &iAddrLen;) == SOCKET_ERROR)
return 3;

LPCSADDR_INFO lpCSAddrInfo = (LPCSADDR_INFO) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CSADDR_INFO));
lpCSAddrInfo[0].LocalAddr.iSockaddrLength = sizeof( SOCKADDR_BTH );
lpCSAddrInfo[0].LocalAddr.lpSockaddr = (LPSOCKADDR)&SockAddrBthLocal;
lpCSAddrInfo[0].RemoteAddr.iSockaddrLength = sizeof( SOCKADDR_BTH );
lpCSAddrInfo[0].RemoteAddr.lpSockaddr = (LPSOCKADDR)&SockAddrBthLocal;
lpCSAddrInfo[0].iSocketType = SOCK_STREAM;
lpCSAddrInfo[0].iProtocol = BTHPROTO_RFCOMM;

//
// If we got an address, go ahead and advertise it.
//
WSAQUERYSET wsaQuerySet = {0};
ZeroMemory(&wsaQuerySet;, sizeof(WSAQUERYSET));
wsaQuerySet.dwSize = sizeof(WSAQUERYSET);
wsaQuerySet.lpServiceClassId = (LPGUID) pGUID;

wsaQuerySet.lpszServiceInstanceName = sServiceName;
wsaQuerySet.lpszComment = "";
wsaQuerySet.dwNameSpace = NS_BTH;
wsaQuerySet.dwNumberOfCsAddrs = 1; // Must be 1.
wsaQuerySet.lpcsaBuffer = lpCSAddrInfo; // Req'd.

//
// As long as we use a blocking accept(), we will have a race
// between advertising the service and actually being ready to
// accept connections. If we use non-blocking accept, advertise
// the service after accept has been called.
//
if (WSASetService(&wsaQuerySet;, RNRSERVICE_REGISTER, 0)==SOCKET_ERROR)
{
return 4;
}

//
// listen() call indicates winsock2 to listen on a given socket for any incoming connection.
//
if (listen(m_sock, SOMAXCONN)==SOCKET_ERROR)
{
return 5;
}

SOCKET sClient=accept(m_sock, NULL, NULL);

return 0;
}


Dan East



I just used your code to replace the part
struct init to implement WSASetservice that provided by ppc 5.0 sdk bthchat sample
It did work,and now the socket between pc and ppc
had been created,
Thanks a lot!!
Hi..
Thank you for posting this code and I used also this code. It did worked proper and and now the socket between pc and ppc
had been created.