title: Bluetooth with C++ and winsock
body: <p>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. </p>
  
  <p>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++.</p>
  
  <p>I've got some example code (from here <a href="http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4k.html" rel="nofollow">http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4k.html</a>) 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. </p>
  
  <p>I've been looking through the Bluetooth reference page (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa362930%28v=vs.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa362930%28v=vs.85%29.aspx</a>) 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. </p>
  
  <p>I must be missing something, using wrong key words when Googling or something, because I've found nothing about connecting to a Bluetooth device!</p>
  
  <p>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. </p>
  
  <p>Cheers</p>
  
  <p>EDIT:</p>
  
  <p>BitBanks answer pointed me in the right direction. Only thing missing was a WSAStartup request before any socket requests:</p>
  
  <pre><code>WORD wVersionRequested;
  WSADATA wsaData;
  int err;
  
  /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
  wVersionRequested = MAKEWORD(2, 2);
  
  err = WSAStartup(wVersionRequested, &amp;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;
  }
  </code></pre>