Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth with C++ and winsock

I'm finding it very hard to get information on Bluetooth communication in C++. Specifically I want to avoid using any third party libraries and I simply want to connect to a device already paired with my computer.

The device has already had its passcode entered and is available in the 'Show Bluetooth Devices' under my devices and printers. I'm using Windows 7 and visual studio 2013 professional for development in C++.

I've got some example code (from here http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4k.html) which displays information on my Bluetooth radio and then displays device information and it seems to work well. Although it's printing out every Bluetooth device already paired with the computer, not ones which are within range, but that may be me misinterpreting what the code is suppose to do.

I've been looking through the Bluetooth reference page (http://msdn.microsoft.com/en-us/library/windows/desktop/aa362930%28v=vs.85%29.aspx) and all the functions are just to do with setting the Bluetooth radio availability and other things like that; no sign of connecting to a found device at all.

I must be missing something, using wrong key words when Googling or something, because I've found nothing about connecting to a Bluetooth device!

If anyone has any suggestions, code, or links that would be great! I can connect to my device using the serial functionality (very easily) but I have to manually enter the COM port it's registered on, which isn't very user friendly. I want to scan and select, or enter a Bluetooth device name, and connect that way.

Cheers

EDIT:

BitBanks answer pointed me in the right direction. Only thing missing was a WSAStartup request before any socket requests:

WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
    /* Tell the user that we could not find a usable */
    /* Winsock DLL.                                  */
    printf("WSAStartup failed with error: %d\n", err);
    return 1;
}
like image 543
ritchie888 Avatar asked Nov 19 '13 19:11

ritchie888


People also ask

Does Bluetooth use sockets?

To establish a Bluetooth connection, a program must first create a socket that will serve as the endpoint of the connection. Sockets are used for all types of network programming, so the first thing to do is specify what kind of socket it's going to be.

What is Winsock C++?

Winsock is a programming interface and the supporting program that handles input/output requests for Internet applications in a Windows operating system. It's called Winsock because it's an adaptation for Windows of the Berkeley UNIX sockets interface.

What is socket in Bluetooth?

The most common type of Bluetooth socket is RFCOMM, which is the type supported by the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth. It is also known as the Serial Port Profile (SPP). To create a BluetoothSocket for connecting to a known device, use BluetoothDevice.

What are Bluetooth devices?

Bluetooth technology allows devices to communicate with each other without cables or wires. Bluetooth relies on short-range radio frequency, and any device that incorporates the technology can communicate as long as it is within the required distance.


1 Answers

If you have a bluetooth address from discovery or the paired-devices list, you can connect to it like this (error checking needs to be added):

#include <winsock2.h>
#include <ws2bth.h>
SOCKADDR_BTH sockAddr;
SOCKET btSocket;
int error;

   btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
   memset (&sockAddr, 0, sizeof(sockAddr));
   sockAddr.addressFamily = AF_BTH;
   sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
   sockAddr.port = BT_PORT_ANY;
   sockAddr.btAddr = <your bluetooth address>
   error = connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));

Some devices advertise the SerialPortServiceClass_UUID instead of the RFCOMM_PROTOCOL_UUID. You also may need to retry the connection several times. Certain poorly implemented bluetooth devices (cough PowerA Moga cough) require multiple tries to connect.

This may not be the official way to do it, but I get the 6-byte BT address of the device I'm interested in from the paired list like this:

unsigned char *p;
ULONGLONG ullAddr;

   p = (unsigned char *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr; // point to addr
   memcpy(&ullAddr, &p[2], 8); // copy Bluetooth address of device we found
like image 117
BitBank Avatar answered Sep 20 '22 17:09

BitBank