Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Bluetooth programming with c++


BluePain's Avatar
Member
0 0

Hey, I was wondering if I could get some help with a project. I am trying to make program that find the bluetooth devices in range and show them to me. But with no luck. I need some serious help with this. If some one could give me apush in the right direction or a code sampel or anything would I be more then happy:

Some codes I have been thro:

#include <iostream> #include <windows.h> #include <bthsdpdef.h> #include <BluetoothAPIs.h>

#pragma comment ( lib, "Irprops.lib")

using namespace std;

// for og finne blåtannenheten min og enheter void min_bluetooth() { DWORD BluetoothAuthenticateDevice( HWND hwndParent, HANDLE hRadio, BLUETOOTH_DEVICE_INFO* pbtdi, PWCHAR pszPasskey, ULONG ulPasskeyLength );

}

int main () { //velkommen cout << "Start programmet…" <<endl; Sleep(1000);

//Blåtann starter
min_bluetooth();

}

// blue.cpp : Defines the entry point for the console application. //

#include <winsock2.h> #include <Ws2bth.h> #include <BluetoothAPIs.h>

#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")

#pragma comment(lib, "irprops.lib")

TCHAR *GetLastErrorMessage(DWORD last_error) { static TCHAR errmsg[512];

if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, last_error, 0, errmsg, 511, NULL)) { /* if we fail, call ourself to find out why and return that error */ return (GetLastErrorMessage(GetLastError()));
}

return errmsg; }

BOOL __stdcall callback( ULONG uAttribId, LPBYTE pValueStream, ULONG cbStreamSize, LPVOID pvParam) { // printf("Callback %d\n", uAttribId);

SDP_ELEMENT_DATA element;

if (ERROR_SUCCESS != BluetoothSdpGetElementData(pValueStream, cbStreamSize, &element)) { printf("%s\n", GetLastErrorMessage(GetLastError())); }

return true; }

int main(int argc, char* argv[]) { 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(&quot;Failed to get bluetooth socket! %s&#92;n&quot;, 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);
  }

  
  WSAQUERYSET querySet;

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

  querySet.dwSize = sizeof(querySet);

  querySet.dwNameSpace = NS_BTH;

  HANDLE hLookup;
  DWORD flags = LUP_RETURN_NAME | LUP_CONTAINERS | LUP_RETURN_ADDR | LUP_FLUSHCACHE | LUP_RETURN_TYPE | LUP_RETURN_BLOB | LUP_RES_SERVICE;

  int result = WSALookupServiceBegin(&querySet, flags, &hLookup);

  if (0 == result)
  {
     while (0 == result)
     {
        BYTE buffer[1000];

        DWORD bufferLength = sizeof(buffer);

        WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer;

        result = WSALookupServiceNext(hLookup, flags, &bufferLength, pResults);

        if (result != 0)
        {
           printf(&quot;%s&#92;n&quot;, GetLastErrorMessage(GetLastError()));
           printf(&quot;%d&#92;n&quot;, bufferLength);
        }
        else
        {
           printf(&quot;%s&#92;n&quot;, pResults-&gt;lpszServiceInstanceName);

           CSADDR_INFO *pCSAddr = (CSADDR_INFO *)pResults-&gt;lpcsaBuffer;

           BTH_DEVICE_INFO *pDeviceInfo = (BTH_DEVICE_INFO*)pResults-&gt;lpBlob;

           WSAQUERYSET querySet2;

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

           querySet2.dwSize = sizeof(querySet2);

           GUID protocol = L2CAP_PROTOCOL_UUID;

           querySet2.lpServiceClassId = &protocol;

           querySet2.dwNameSpace = NS_BTH;

           char addressAsString[1000];

           DWORD addressSize = sizeof(addressAsString);

           addressSize = sizeof(addressAsString);

           if (0 == WSAAddressToString(pCSAddr-&gt;LocalAddr.lpSockaddr, pCSAddr-&gt;LocalAddr.iSockaddrLength, &protocolInfo, addressAsString, &addressSize))
           {  
              printf(&quot;local address: %s&#92;n&quot;, addressAsString);
           }

           addressSize = sizeof(addressAsString);

           if (0 == WSAAddressToString(pCSAddr-&gt;RemoteAddr.lpSockaddr, pCSAddr-&gt;RemoteAddr.iSockaddrLength, &protocolInfo, addressAsString, &addressSize))
           {
              printf(&quot;device address: %s&#92;n&quot;, addressAsString);
           }




           querySet2.lpszContext = addressAsString;

           HANDLE hLookup2;
           DWORD flags = LUP_FLUSHCACHE |LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_ADDR | LUP_RETURN_BLOB | LUP_RETURN_COMMENT;

           int result = WSALookupServiceBegin(&querySet2, flags, &hLookup2);

           if (0 == result)
           {
              while (0 == result)
              {
                 BYTE buffer[2000];

                 DWORD bufferLength = sizeof(buffer);

                 WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer;

                 result = WSALookupServiceNext(hLookup2, flags, &bufferLength, pResults);

                 if (result != 0)
                 {
                    printf(&quot;%s&#92;n&quot;, GetLastErrorMessage(GetLastError()));
                    printf(&quot;%d&#92;n&quot;, bufferLength);
                 }
                 else
                 {
                    printf(&quot;%s&#92;n&quot;, pResults-&gt;lpszServiceInstanceName);
                    //printf(&quot;%s&#92;n&quot;, pResults-&gt;lpszComment);

                    CSADDR_INFO *pCSAddr = (CSADDR_INFO *)pResults-&gt;lpcsaBuffer;

                    addressSize = sizeof(addressAsString);

                    if (0 == WSAAddressToString(pCSAddr-&gt;LocalAddr.lpSockaddr, pCSAddr-&gt;LocalAddr.iSockaddrLength, &protocolInfo, addressAsString, &addressSize))
                    {  
                       printf(&quot;local address: %s&#92;n&quot;, addressAsString);
                    }

                    addressSize = sizeof(addressAsString);

                    if (0 == WSAAddressToString(pCSAddr-&gt;RemoteAddr.lpSockaddr, pCSAddr-&gt;RemoteAddr.iSockaddrLength, &protocolInfo, addressAsString, &addressSize))
                    {
                       printf(&quot;device address: %s&#92;n&quot;, addressAsString);
                    }

                    if (pResults-&gt;lpBlob)
                    {
                       const BLOB *pBlob = (BLOB*)pResults-&gt;lpBlob;

                       if (!BluetoothSdpEnumAttributes(pBlob-&gt;pBlobData, pBlob-&gt;cbSize, callback, 0))
                       {
                          printf(&quot;BluetoothSdpEnumAttributes - %s&#92;n&quot;, GetLastErrorMessage(GetLastError()));
                       }

                    }

                 }
              }
     
              result = WSALookupServiceEnd(hLookup2);

           }
           else
           {
              printf(&quot;%s&#92;n&quot;, GetLastErrorMessage(GetLastError()));
           }

        }
     }
     
     result = WSALookupServiceEnd(hLookup);
  }
  else
  {
     printf(&quot;%s&#92;n&quot;, GetLastErrorMessage(GetLastError()));
  }

  WSACleanup();

}

HANDLE hRadio; BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(btfrp) };

HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio( &btfrp, &hRadio );

if ( NULL != hFind ) { printf("hFind\n");

  do
  {
     //
     //  TODO: Do something with the radio handle.
     //

	 BLUETOOTH_RADIO_INFO radioInfo;

	 radioInfo.dwSize = sizeof(radioInfo);

	 if (ERROR_SUCCESS == BluetoothGetRadioInfo(hRadio, &radioInfo))
	 {
		wprintf(L&quot;Raido: %s&#92;n&quot;, radioInfo.szName);
	 }

	 BLUETOOTH_DEVICE_INFO_STRUCT deviceInfo;

	 deviceInfo.dwSize = sizeof(deviceInfo);

	 BLUETOOTH_DEVICE_SEARCH_PARAMS deviceSearchParams;

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

	 deviceSearchParams.dwSize = sizeof(deviceSearchParams);

	 //deviceSearchParams.fReturnAuthenticated = true;
	 deviceSearchParams.fReturnRemembered = true;
	 //deviceSearchParams.fReturnUnknown = true;
	 //deviceSearchParams.fReturnConnected = true;

	 deviceSearchParams.hRadio = hRadio;

	 HANDLE hDeviceFind = BluetoothFindFirstDevice(&deviceSearchParams, &deviceInfo);

	 if (NULL != hDeviceFind)
	 {
		 do
		 {
			 wprintf(L&quot;Device: %s&#92;n&quot;, deviceInfo.szName);

			 //BluetoothDisplayDeviceProperties(0, &deviceInfo);
		 }
		 while(BluetoothFindNextDevice(hDeviceFind, &deviceInfo));

		 BluetoothFindDeviceClose(hDeviceFind);
	 }


	 if (BluetoothGetDeviceInfo(hRadio, &deviceInfo))
	 {
		wprintf(L&quot;+ Device: %s&#92;n&quot;, deviceInfo.szName);

		// BluetoothUpdateDeviceRecord - change name

		// BluetoothRemoveDevice

	 }

	 GUID guidServices[10];

	 DWORD numServices = sizeof(guidServices);

	 DWORD result = BluetoothEnumerateInstalledServices(hRadio, &deviceInfo, &numServices, guidServices);

	          CloseHandle( hRadio );
  } while( BluetoothFindNextRadio( hFind, &hRadio ) );

  BluetoothFindRadioClose( hFind );

}

return 0;

}


#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;BluetoothAPIs.h&gt;
#include &lt;BT_Finder.h&gt;

#pragma comment ( lib, &quot;wsock32.lib&quot;);
#pragma comment ( lib, &quot;Ws2.lib&quot;);

using namespace std;

int main ( int argc, char *argv[] )

{
	// Bare en liten velkomshilsen
	cout &lt;&lt; &quot;starter programmet...&quot; &lt;&lt;endl;
	Sleep(1000);

	// Her starter blåtann
	
	
	CBT_Finder::CBT_Finder(void)
{
}

CBT_Finder::~CBT_Finder(void)
{
}

void CBT_Finder::QueryDevices()
{
BLUETOOTH_FIND_RADIO_PARAMS RadioParam;
RadioParam.dwSize = sizeof(BLUETOOTH_FIND_RADIO_PARAMS);

BLUETOOTH_FIND_RADIO_PARAMS* FindParam;

HANDLE* hRadio;
returnParam = BluetoothFindFirstRadio(&RadioParam, hRadio);


BOOL MoreDevices;
do
{
 Radios.push_back(hRadio);


MoreDevices = BluetoothFindNextRadio(returnParam,hRadio);

returnParam
}while (MoreDevices == TRUE);

BluetoothFindRadioClose(FindParam);

for(std::list &lt;HANDLE*&gt;::iterator i = Radios.begin(); i != Radios.end(); i++ )
{
 BLUETOOTH_DEVICE_INFO RadioData;
 BluetoothGetDeviceInfo(*i,&RadioData);
 printf(&quot;%s&quot;,RadioData-&gt;szName);
}




}

#pragma once #include "windows.h" #include "BluetoothAPIs.h" #include <list>

class CBT_Finder { public: CBT_Finder(void); ~CBT_Finder(void); void QueryDevices(); std::list <HANDLE*> Radios;

};

Some links:

links: http://msdn2.microsoft.com/en-gb/library/aa362942.aspx http://msdn2.microsoft.com/en-gb/library/aa362941.aspx http://msdn2.microsoft.com/en-gb/library/aa362770.aspx http://msdn2.microsoft.com/en-gb/library/aa362941.aspx http://msdn2.microsoft.com/en-gb/library/ms741679.aspx http://msdn2.microsoft.com/en-gb/library/aa362913.aspx http://msdn2.microsoft.com/en-gb/library/aa362914.aspx http://msdn2.microsoft.com/en-gb/library/aa362916.aspx http://msdn2.microsoft.com/en-gb/library/aa362915.aspx http://msdn2.microsoft.com/en-gb/library/aa362900.aspx http://msdn2.microsoft.com/en-gb/library/aa362920.aspx http://msdn2.microsoft.com/en-gb/library/aa363059.aspx http://msdn2.microsoft.com/en-us/library/aa363057.aspx http://msdn2.microsoft.com/en-us/library/aa362899.aspx http://support.microsoft.com/default.aspx?kbid=841803 http://www.lenholgate.com/archives/000102.html http://msdn2.microsoft.com/en-gb/library/aa362930.aspx http://msdn2.microsoft.com/en-gb/library/aa362924.aspx

I have been trying to make it with sockets and Api. But with no luck thanks for all help.