Connecting Android Device to Winsock via Bluetooth

Connecting Android Device to Winsock via Bluetooth

I am writing this tutorial in order to help others who find themselves in a similar position to my own. I recently had the task of connecting an android 4.3 device to a windows machine relying entirely on winsock. The more I researched the issue, the more I realized that there simply aren’t many good resources when it comes to this very specific problem I was attempting to solve. As a result, I am going to attempt to outline here the best explanation I can as to how I understand solving this problem. In addition, I will omit things as I see fit (usually with reference to an outside source). Others may or may not find this post helpful (It’s entirely possible this post will end up one more among many others which fail to convey the idea).

Android 4.3 – The following was tested using Android 4.4.4, and 4.3.1

The android side of this project is actually fairly simple. You can find great documentation, and example code on the official website which make this part of the project very doable.

As a result of this, I suggest you take a look and work out the basics of Android bluetooth.

To summarize, however, you want to do the following:

  1. Modify application permissions to allow “android.permission.BLUETOOTH” or “android.permission.BLUETOOTH_ADMIN” as per your needs
  2. Acquire ‘BluetoothAdapter’
  3. Pair with a device
  4. Secure a ‘BluetoothSocket’ connection with the paired device

There are, however, a few tips I would like to add to make this process easier.

Tip 1: Fetch SDP Records – If you android app is having trouble discovering service records on the target device, be sure to call “fetchUuidWithSdp()” on your targetted ‘BluetoothDevice’. I have found this step necessary on at least 3 android devices, and yet it is rarely mentioned on the tutorial page.

Tip 2: COMMON UUID – If you find that your two devices just simply aren’t communicating then be sure to check that your android application UUID string matches your desktop application’s GUID. This is crucial.

Winsock 2.2 – Server Side

In order to create a bluetooth server utilizing winsock we begin by initializing our winsocket context. “MAKEWORD(2,2)” simple instructs winsock to utilize version 2.2 of the library, as opposed to version 1.0.

WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);

//Bluetooth Socket code goes here


The next couple of steps are almost identical to how winsock is typically used. We will begin by creating our listener socket, which will be used later to wait on incoming connections. The listener socket MUST be created with the following parameters supplied.


After this will we want to create our bluetooth socket address, and fill it with the relevant information. Since our desktop application will behave as a server, we will fill in the following information. If you want your desktop to behave as a client, I suggest you look here.

//Socket address information
addr.addressFamily = AF_BTH;
addr.btAddr = 0;
addr.serviceClassId = GUID_NULL;
addr.port = BT_PORT_ANY; //Can be 0 or BT_PORT_ANY

Next we will bind our socket, and set it to a listening status.

bind(listenSocket, (SOCKADDR*)&addr, sizeof(SOCKADDR_BTH));

listen(listenSocket, SOMAXCONN);

Now that the socket has been bound, we can acquire more in-depth information which will be used a little bit later. This step may be unnecessary for some situations, but it is likely safest to do it anyways.

int size = sizeof(SOCKADDR_BTH);
getsockname(listenSocket, (SOCKADDR*)&addr, &size);

After all of this has been finished, we can begin what I consider to be the most complicating step involved in this process. In order for bluetooth devices spanning various platforms to communicate with one another it is necessary for them to share a common understanding of “the connection”. Bluetooth accomplishes this primarily with “Service Discovery Protocol (SDP) Records”. This is the bluetooth device’s way of telling another bluetooth device “Hey! I’m can do these things!” or “I can make these connections!”. The most important thing to know is that both your android application, and winsock application both have to have the same 128-bit GUID in their SDP records.

const GUID mServiceGuid= {0x00001101, 0x0000, 0x1000, 0x8000, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; // Can be anything

//Set WSA service
WSAQUERYSET wsaQuery = {0};
wsaQuery.dwSize = sizeof(WSAQUERYSET);
wsaQuery.lpszServiceInstanceName = L"BT Service"; // Can be anything
wsaQuery.lpszComment = L"bt"; //Can be anything
wsaQuery.lpServiceClassId = &mServiceGuid;
wsaQuery.dwNameSpace = NS_BTH;
wsaQuery.dwNumberOfCsAddrs = 1;

Above I used the well known GUID for a bluetooth serial board, but you can use any 128-bit hex value you want. Preferably pick something that doesn’t overlap with existing devices.

The end is in our sights at this point. Now we need to fill in our ‘CSADDR_INFO’ structure in order to bridge the local connection with our service record. Notice here that our address info structure refers back to our “SOCKADDR_BTH” we used earlier during the binding stage.

CSADDR_INFO addrInfo = {0};
addrInfo.LocalAddr.lpSockaddr = (SOCKADDR*)&addr;
addrInfo.LocalAddr.iSockaddrLength = size;
addrInfo.iSocketType = SOCK_STREAM;
addrInfo.iProtocol = BTHPROTO_RFCOMM;

Now that we have all of this finished, we can initialize our service, and set our socket to listen mode.

wsaQuery.lpcsaBuffer = &addrInfo; //Link our CSADDR_INFO with the Query set
WSASetService(&wsaQuery, RNRSERVICE_REGISTER, 0);

SOCKET acceptSocket = accept(listenSocket, NULL, 0);

At this point your program will lock up, and wait for a connection to be secured before resuming. This concludes all the critical steps when it comes to setting up your bluetooth server.

There are a few more functions that you may need to/want to play around with depending on your specific bluetooth adapter. In order to modify local bluetooth adapter settings programmatically you can make use of the functions listed here.

In order to find your local bluetooth device (also known as a ‘radio’) you can make use of the following code:


//Locate bluetooth radio
HBLUETOOTH_RADIO_FIND btFind = BluetoothFindFirstRadio(&btFindParams, &mRadio);

This will locate the first possible bluetooth device on your system. If there are multiple bluetooth devices which you need to iterate over, you can use the following code.

while(BluetoothFindNextRadio(&btFind, &mRadio)) {}

You can also alter some convenience settings on your local device. For example,

BluetoothEnableDiscovery(mRadio, true);
BluetoothEnableIncomingConnections(mRadio, true);

The use of each of these functions is pretty self-explanatory.


Bluetooth Version of Winsock Functions

Bluetooth Functions

You can find my source code here.

Note: If you have any questions or constructive criticism I would be happy to hear it.

Comments are closed.