Bluetooth communication using Winsock
Hi,
Presently I’m working on Win CE for Pocket PC and I’m trying to do some enhancements to an existing Win CE application.
The application has originally support for USB and Acitve Sync and other connectiviry mediums and right now I’m working on theBluetooth connectivity mechanism.
The win CE application should connect to a host application that resides on a PC (windows XP). On Win CE side I’m trying to useMicrosoft stack and on the PC side I’m using widcomm stack.
I’m trying winsock over Bluetooth but I’m not sure if my approach is correct as it is new to me.J
These are the steps I’m doing..
Creating the server socket(winsock)
socket (AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM);
creating the socket address structure
SOCKADDR_BTH address ;
memset (&address, 0, sizeof(address));
address.addressFamily = AF_BT;
address.port = channel & 0xff; //here channel is set to zero as suggested in Microsoft site.
binding the socket to the address.
iRes=bind(m_ThisSocket,(sockaddr*)&address,sizeof(address));
then listen on the socket created
listen(m_ThisSocket,5);
Then accept the incoming client connection.
SOCKADDR_BTH sa2;
int size = sizeof(sa2);
m_ClientSocket = accept (m_ThisSocket, (SOCKADDR *)&sa2, &size);After this point can we assume that the connection is established? Please let me know if I’m missing any step . The problem I face is the client (windows PC side) socket is not able to connect to the server socket.
I have also been trying to use virtual COM port over Bluetooth and not been able to connect the device with the PC.
Any inputs will be of great help to me.
[4588 byte] By [
ibeanz] at [2008-2-15]
You are missing a step - you must register an SDP record to advertise your service to other Bluetooth devices. This allows your client to interrogate the correct channel to use for your service. See this page for details on the WSASetService function and it's usage:-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50lrfWSASetServiceBluetooth.aspThis should be done after you call bind. Use getsockname to get the local socketaddress details, then use this to construct an SDP record which can be published with WSASetService, I've not seen an example for this published within the SDK, but there is an example within "Programming Microsoft Windows CE.NET Third Edition" by Douglas Boling. The technique is to start with a dummy SDP record, then insert into it the guid of your service, and the channel number. I've only done this with C# (www.peterfoot.net/Bluetoothv14.aspx) so don't have C++ example code to share, you may be able to duplicate the process by following my C# code.
Peter