I know there are a lot of tutorials on how to implement a winsock bluetooth socket, but they are all very confusing. One says it's not possible in Code:Blocks
, others say you have to download another full Windows API of about 300MB again and others are in different languages like c# or c++ or only for Linux.
I have installed Code:Blocks
and I'm relatively new to it. I already included the ws2_32.lib
and now I want to create a bluetooth socket but code:blocks tells me file not found
. I'm working on windows and have to add windows stuff again and again. Please can anyone tell me how I get a WinSock Bluetooth Socket on Windows (NOT LINUX please) to work? I use this code:
ps: i also tried #define WIN32_LEAN_AND_MEAN
without 1
pps: i found someone saying you cant use VC compiled libs with MinGW but how can i use bluetooth sockets then?
#define WIN32_LEAN_AND_MEAN 1 // Don't include Winsock v1
#pragma comment(lib, "ws2_32.lib")
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2bth.h>
#include <BluetoothAPIs.h>
#define SDP_RECORD_SIZE 0x0000004f
#define SDP_CHANNEL_OFFSET 32
struct
{
BTHNS_SETBLOB b;
unsigned char uca[SDP_RECORD_SIZE];
} bigBlob;
BOOL BT_SetService()
{
ULONG recordHandle = 0;
ULONG ulSdpVersion = BTH_SDP_VERSION;
BLOB blob;
WSAQUERYSET Service;
// Bluetooth device profile sample
BYTE rgbSdpRecord[] = {
0x35, 0x4d, 0x09, 0x00, 0x01, 0x35, 0x11, 0x1c,
0x29, 0xf9, 0xc0, 0xfd, 0xbb, 0x6e, 0x47, 0x97,
0x9f, 0xa9, 0x3e, 0xc9, 0xa8, 0x54, 0x29, 0x0c,
0x09, 0x00, 0x04, 0x35, 0x0c, 0x35, 0x03, 0x19,
0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08,
0x1a, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65,
0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, 0x00, 0x09,
0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11,
0x05, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25,
0x06, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c
};
bigBlob.b.pRecordHandle = (HANDLE *)&recordHandle;
bigBlob.b.pSdpVersion = &ulSdpVersion;
// bigBlob.b.fSecurity = 0;
// bigBlob.b.fOptions = 0;
bigBlob.b.ulRecordLength = SDP_RECORD_SIZE;
memcpy (bigBlob.b.pRecord, rgbSdpRecord, SDP_RECORD_SIZE);
blob.cbSize = sizeof(BTHNS_SETBLOB) + SDP_RECORD_SIZE - 1;
blob.pBlobData = (PBYTE) &bigBlob;
memset (&Service, 0, sizeof(Service));
Service.dwSize = sizeof(Service);
Service.lpBlob = &blob;
Service.dwNameSpace = NS_BTH;
if (WSASetService(&Service,RNRSERVICE_REGISTER,0) == SOCKET_ERROR)
{
printf("WSASetService() failed with error code %d\n", WSAGetLastError ());
return FALSE;
}
else
{
printf("WSASetService() is working!\n");
return TRUE;
}
}
int main(int argc, char **argv)
{
WSADATA wsd;
BOOL RetVal;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Unable to load Winsock! Error code is %d\n", WSAGetLastError());
return 1;
}
else
printf("WSAStartup() is OK, Winsock lib loaded!\n");
RetVal = BT_SetService();
printf("The return value is: %d\n", RetVal);
if(WSACleanup() == 0)
printf("WSACleanup() is OK!\n");
else
printf("WSACleanup() failed miserably with error code %d\n", WSAGetLastError());
return 0;
}
User contributions licensed under CC BY-SA 3.0