80 00 1A 00 0F D7

| 164 Comments

My dabbling with Bluetooth continues. Using WSASetService it's pretty easy to get your device to advertise a standard Bluetooth service on one of its sockets. Simply create your socket with the Bluetooth address family, bind and listen as normal and then call WSASetService to start advertising a service on this socket to other devices within range. Now call accept and wait...

I chose the File Transfer Service as my hello world service. Get one of your other devices to discover your services and the new service should appear. Connect and your accept will return a connected socket. Issue a recv and you get the bytes that form the title to this piece. That's the start of an OBEX Connect packet...

164 Comments

Hi Hi... I am now working on Bluetooth programming on winsock also... But I find that I always fail in WSASetService, can you send me your program for teaching me? Thanks a lot for your kind help. Yours, KK

Rough and ready code, but it worked for me.

WORD wVersionRequested = 0x202;
WSADATA m_data;

if (0 == ::WSAStartup(wVersionRequested, &m_data))
{
SOCKET s = ::socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

const DWORD lastError = ::GetLastError();

if (s == INVALID_SOCKET)
{
printf("Failed to get bluetooth socket! %s\n", GetLastErrorMessage(lastError));
exit(1);
}

WSAPROTOCOL_INFO protocolInfo;

int protocolInfoSize = sizeof(protocolInfo);

if (0 != getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFO, (char*)&protocolInfo, &protocolInfoSize))
{
exit(1);
}

SOCKADDR_BTH address;

address.addressFamily = AF_BTH;
address.btAddr = 0;
address.serviceClassId = GUID_NULL;
address.port = BT_PORT_ANY;

sockaddr *pAddr = (sockaddr*)&address;

if (0 != bind(s, pAddr, sizeof(SOCKADDR_BTH)))
{
printf("%s\n", GetLastErrorMessage(GetLastError()));
}

int size = sizeof(SOCKADDR_BTH);

if (0 != getsockname(s, pAddr, &size))
{
printf("%s\n", GetLastErrorMessage(GetLastError()));
}

if (0 != listen(s, 10))
{
printf("%s\n", GetLastErrorMessage(GetLastError()));
}

WSAQUERYSET service;

memset(&service, 0, sizeof(service));

service.dwSize = sizeof(service);

service.lpszServiceInstanceName = "My Service";
service.lpszComment = "My comment";

GUID serviceID = OBEXFileTransferServiceClass_UUID;

service.lpServiceClassId = &serviceID;

service.dwNumberOfCsAddrs = 1;
service.dwNameSpace = NS_BTH;

CSADDR_INFO csAddr;

memset(&csAddr, 0, sizeof(csAddr));

csAddr.LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
csAddr.LocalAddr.lpSockaddr = pAddr;

csAddr.iSocketType = SOCK_STREAM;
csAddr.iProtocol = BTHPROTO_RFCOMM;

service.lpcsaBuffer = &csAddr;

if (0 != WSASetService(&service, RNRSERVICE_REGISTER, 0))
{
printf("%s\n", GetLastErrorMessage(GetLastError()));
}

SOCKET s1 = accept(s, 0, 0);

unsigned char buffer[2000];

memset(buffer, 0, sizeof(buffer));

int r = recv(s1,(char*)buffer, sizeof(buffer), 0);

Interesting how the socket function works for you.

I get a 10047 (Protocol not supported by address family error) when I use the socket command.

You need windows XP SP 1 and native support for your bluetooth device for this stuff to work.

I have XP SP1.

Will, see the comments on the Bluetooth Sockets entry. In summary:

"As far as I know it will only work if XP recognises your bluetooth device and doesnt need additional drivers. I have a TDK dongle that needs its own drivers and doesnt integrate with XP's wireless link control panel applet and that doesnt work with the code above. I have built in bluetooth on the Sony Vaio and that integrates with XP's control panel applet and works with the code above. "

what did you find in the windows catalog?
the link does not work for me.
did you find a list of winxp-natively supported bluetooth-hardware there?

Hi,
I am trying to send a file from PC to nokia3650.I am getting Invalid_socket error when I create a socket using..
SOCKET s = ::socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
I have windows xp with service pack1,I am using VC++.My bluetooth adapter is D-Link(DBT-120). What else do I need?.

You need a bluetooth stack/driver that supports Windows XP SP1's bluetooth integration.

Where Can I get it or download it?.

I think you need to speak to the supplier of your hardware.

ok..I'll. Thank you very much.

I have downloaded the latest driver for windows XP from Dlink site. It didn't help. My laptop could identify the cell phone but not able to exchage messages programmatically. I am planning to test with some other BT adapter . Can anyone suggest me a good one?.
Thanks,
Swarna

If you take a look in the Windows XP driver you find a list of bluetooth hardware, natively supported by XP:

[ALPS.NT.5.1]
ALPS Integrated Bluetooth Device= BthUsbDriverInstallXP, USB\Vid_044e&Pid_3005
Alps Bluetooth USB Adapter= BthUsbDriverInstallXP, USB\Vid_044e&Pid_3006

[BrainBoxes.NT.5.1]
Brain Boxes USB Bluetooth Adapter BL-554= BthUsbDriverInstallXP, USB\Vid_05d1&Pid_0003

[Cambridge.NT.5.1]
Cambridge Silicon Radio Ltd. Bluetooth Adapter=BthUsbDriverInstallXP, USB\Vid_0a12&Pid_0001

[FIC.NT.5.1]
FIC Bluetooth Wireless Adapter= BthUsbDriverInstallXP, USB\Vid_05b1&Pid_1389

[GVC.NT.5.1]
GVC Bluetooth Wireless Adapter= BthUsbDriverInstallXP, USB\Vid_0525&Pid_a220

[IBM.NT.5.1]
Bluetooth UltraPort Module from IBM= BthUsbDriverInstallXP, USB\Vid_04BF&Pid_0317
IBM Integrated Bluetooth= BthUsbDriverInstallXP, USB\Vid_04BF&Pid_0318
IBM Integrated Bluetooth II= BthUsbDriverInstallXP, USB\Vid_1668&Pid_0441

[Microsoft.NT.5.1]
Microsoft Wireless Transceiver for Bluetooth= BthUsbDriverInstallXP, USB\Vid_045e&Pid_007e

[SiliconWave.NT.5.1]
Silicon Wave Bluetooth Wireless Adapter= BthUsbDriverInstallXP, USB\Vid_0c10&Pid_0000

[Sony.NT.5.1]
USB Bluetooth Device= BthUsbDriverInstallXP, USB\Vid_044E&Pid_3002
USB Bluetooth Device= BthUsbDriverInstallXP, USB\Vid_044E&Pid_3003
Sony Bluetooth USB Adapter= BthUsbDriverInstallXP, USB\Vid_044E&Pid_3004

[TDK.NT.5.1]
TDK Bluetooth USB Adapter= BthUsbDriverInstallXP, USB\Vid_04BF&Pid_0319

[Toshiba.NT.5.1]
TOSHIBA Integrated Bluetooth= BthUsbDriverInstallXP, USB\Vid_0930&Pid_0502&Rev_1350
TOSHIBA Integrated Bluetooth 2= BthUsbDriverInstallXP, USB\Vid_0930&Pid_0505

I've got it running with the BL-554 from brainboxes. But i had to adapt the USB Adress previously....

Just found this:

http://www.tdksystems.com/support/faqs2/index.html?http://www.metafaq.com/faq/tdksys/bluetooth

It seems that you can purchase the windows xp bluetooth stack for the TDK USB device. I've no idea why it's something you have to purchase rather than them just making it available as a download.... Does anyone know if the stack is available elsewhere as a download rather than a purchase? (more interested in getting it faster than saving money...)

I didnt notice that the url I posted in the last comment was some useless framebased crap. This is what you need to get the drivers from TDK.

http://www.tdkshop.com/products.aspx?catid=5

Purchased and installed the TDK drivers and they work fine.

Note that only the USB Bluetooth devices shipped with the Microsoft logo (Designed for XP) will actually work under Windows XP SP1 Bluetooth drivers.
To check, the TDK Systems USB vendor ID (VID) is 0x4BF and the product ID (PID) for TDK Systems USB products which work with the Microsoft drivers are 0x319.
I hope this may help.

Hi, Len, what's the best way to connect to a device that does not have a standard service/profile? Can we just get its radio address and then connect to it using "connect" socket function? Also, in this situation, are we writing a client application instead of server application? For bluetooth client application, it appears that we need to use both bind and connect functions, am I right?

Thanks.

Rockie,

To be honest, I dont know. I've only just got to a point where I had two machines which supported the bluetooth winsock stuff and I havent had a chance to play with them together yet.

I would imagine that you can just use sockets like you'd expect to for other transports...

Hi,

I get a compilation error, "Ws2bth.h: No such file or directory" when I compile the sample code in Microsoft Visual studio .Net ( vc7) 2002.
I searched under vc7\PlatformSDK\include but I could not find this file. I compiled in WinXP Sp1.

Do I need any patches or anything? How do I get this file.

Thanks in advance

I have the Feb 2003 platform sdk installed and Ws2bth.h is in the D:\Program Files\Microsoft SDK\include directory.

Hi Len, do you have the code for the client which corresponds to the example above or did you not get that far? It Would be great if you could make that available.

Phil

Phil

No, not at the moment.

Hi! All,

I was reading this Bluetooth stuff, very impressive, and like it in C++ and specially on Microsoft Platform.

I have a query, is it possible to use that code in C#? if so then any hint to start with?

Take Care,

Mudassir
San Jose, CA

Mudassir

I don't think you can get down to the low level winsock code in C# directly. You'd have to use pInvoke or whatever. The approach I'd take is to take care of the messy stuff in C++ and either wrap that in COM and use COM interop to get at it from C# or wrap it in managed C++ and access it directly...

Thanks,

Seems to me i have to make my hand dirty in C++ , never used that lingo before.

Oh one more query, I have Platform SDK, and Windows 2000 Server can we develop this Bluetooh Server kind of thing on 2000 Platform? Because as I read the artilce of , it clearly indicate that you must have XP? Any idea if its okay to use this on 2000 Server , using some other mean

Thanks again,

Mudassir,

San Jose, CA

Only XP supports winsock bluetooth access, as far as I know.

Hi! Len,

Just installed the XP & SP1 on my old PIII and with VS.NET after having an awesome weekend on Santa Cruz.

Any idea which DLL i have to call using PInvoke to get access to the Bluetooth Socket? WinSock2? or Win32?

ciao

Mudassir

San Jose, CA

I'd guess at ws2_32.dll

Hi Len! i'm developing a bluetooth project and i have a problem. I need to comunicate a Pocket Pc 2003 with a server to exchange some files. The problem is that, in the server software (that i'm developing with Visual Studio.NET 2003) i can't open a socket connection. It compiles correctly and executes normal, but when it opens a socket connection it breaks. I don't know what happends.
Do you know what can be happening or what i'm doing wrong? Another question is, can i comunicate 30 Pocket PC's with the server? (Not simultaniusly). How?

Thanks for the response!

This is the code i'm using:

WORD wVersion=0x202;
WSADATA m_data;

if(0==::WSAStartup(wVersion,&m_data))
{
SOCKET s=socket(AF_BTH,SOCK_STREAM,BTHPROTO_RFCOMM);
if(s==INVALID_SOCKET)
{
MessageBox((CString)"No se ha podido crear el socket de ConexiĆ³n",(CString)"Socket Error",MB_OK|MB_ICONERROR);
}
else
{
SOCKADDR_BTH name;
memset(&name,0,sizeof(name));
name.addressFamily=AF_BTH;
name.btAddr=0;
name.serviceClassId=GUID_NULL;
name.port=0;
sockaddr *pAddr=(sockaddr*)&name;
if(0!=bind(s,pAddr,sizeof(SOCKADDR_BTH)))
{
MessageBox((CString)"Bind Error",(CString)"Socket Error",MB_OK|MB_ICONERROR);
closesocket(s);
exit(1);
}
listen(s,SOMAXCONN);

for(; ;)
{
SOCKADDR_BTH sab2;
int ilen=sizeof(sab2);

SOCKET s2=accept(s,(SOCKADDR*)&sab2,&ilen);

if(s2==INVALID_SOCKET)
{
MessageBox((CString)"No se ha podido crear el socket de RecepciĆ³n",(CString)"Socket Error",MB_OK|MB_ICONERROR);
}
else
{
MessageBox((CString)"OK");
}
}
closesocket(s);
}
}

Javi, it would help if you told me how it breaks... Have you read all the comments on the bluetooth articles here? Are you SURE you have a device that is supported natively by the Windows XP SP1 Bluetooth support? Does my sample code here run OK?

Hello again!

Now i know that the error i get is this one:

WSAEAFNOSUPPORT (10047)
Translation: Address family not supported by protocol family.
Description: An address that is incompatible with the requested protocol was used. All sockets are created with an associated address family (that is, AF_INET for Internet protocols) and a generic protocol type (that is, SOCK_STREAM). This error is returned if an incorrect protocol is explicitly requested in the socket (Wsapiref_2qr6.asp) call or if an address of the wrong family is used for a socket, for example, in sendto (Wsapiref_4sqa.asp).

Anyone knows how to resolve or debug it?

thanks in advance!

Javi,

Take a look at the sample code here: http://www.lenholgate.com/archives/000102.html this code works if you have the right hardware.

Len

Hi Len!

Thanks in advance for all your help!

When I execute your code i get the following error:
"Error on init the application. Can't find irprops.cpl"

Do you or Someone knows what happends here?

irprops.cpl is part of the Windows XP SP1 Bluetooth support. If you dont have compatible hardware and you dont have it working natively with windows xp then this code wont work.

Hi everybody!

I'm developing a bluetooth compatible application and i found the same errors like yours.
But it's difficult to me to understand why need windows XP native recognizing of our dongle Bluetooth device.

Someone can explain me why? I search in all MSDN and i haven't found any page where says that developers only can program with native bluetooth devices. Has it an explanation?

thanks in advance!!

If your device has native Windows XP SP 1 support then it has the correct drivers to work with Winsock. If it doesn't, then you need to contact the manufacturer of your device and find out who supplies the SDK for the drivers.

Hi Len!

I have installed Windows XP SP1 and now it recognizes my Bluetooth device natively (an Conceptronic CBT100u with CSR radio xip) as CSR Bluetooth Device, but yet i get the 10047 Error when i try to create a socket.

Am I forgetting something?

P.D.

I also instaled Q323183_WXP_SP2_X86_ESN that makes possible the recognition of my device.

thanks in advance.

Are you running MY sample code and getting this error?

Hi Len

Yes.I'm doing a copy-paste from the web to the Visual Studio.Net Project. It compiles but breaks when has to create the socket and return Error code 10047. I have 2 different Bluetooth devices and it breaks with all them.

Strange. Sorry but all I can say is that 'it works for me'. Read all the comments on all of the bluetooth postings; perhaps someone has had similar problems?

I want to ask you something.

When you say "Native recognition" you mean that, when you insert the USB dongle at first time, windows ask you for drivers, at this moment it's not necessary for me to insert any cd or indicate to windows any path to locate those drivers. Windows automatically searches for the drivers and find one who is " CSR USB Bluetooth Device" and finish the installation.
The device runs correctly.

it's that what you understands for "Native Recognition"? or is any other thing?

Possibly. What you MUST have (as far as I know) is a control panel applet named "Wireless Link" which has a "Bluetooth" tab for configuring your hardware. You shouldn't need to have ANY other drivers loaded and if you've previously loaded drivers you should remove them. If you dont have the control panel applet then the winsock code doesnt work.

Ok

I haven't this control panel applet.
All i have is a Bluetooth option at the Control Panel (the default Windows XP control Panel in My Pc). Nothing like Wireless Link.

What this means? That i haven't installed correctly Windows XP SP1?

I've no idea. Have you uninstalled all the drivers that came with your device?

Yes. I've sended a mail to Conceptronic (the manufacturer) and tell me that my device was compatible with Windows XP SP2 but this pack is still in a Beta version. They tell me that all the disponible drivers are in the web and i've donwloaded and installed all them.

Leave a comment