C++ WinSock Bluetooth Connection - AT Command - Error Received
<p>This is a continuation of this Question: <a href="https://stackoverflow.com/questions/37020491/programming-for-bluetooth-in-winsock-a-solution-needed">https://stackoverflow.com/questions/37020491/programming-for-bluetooth-in-winsock-a-solution-needed</a></p>

<p>I am not strong in C++!</p>

<p>The Lib/Header files can be found here C:\Program Files (x86)\Windows Kits\8.1\Include\um which is part of the Windows SDK Kit here: <a href="https://developer.microsoft.com/en-us/windows/downloads/windows-8-1-sdk" rel="nofollow noreferrer">Windows Software Development Kit (SDK) for Windows 8.1</a> or here <a href="https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk" rel="nofollow noreferrer">Windows Software Development Kit (SDK) for Windows 10</a></p>

<p>My Code:</p>

<pre><code>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
// MSDN Bluetooth and read or write operations:
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa362907(v=vs.85).aspx
// 
// MSDN Bluetooth and Connect
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa362901(v=vs.85).aspx
//
// MSDN Send Function
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx
// 
// Error Codes: 
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
// 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes:
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;iostream&gt;

#include &lt;WinSock2.h&gt;
#include &lt;bthsdpdef.h&gt;
#include &lt;bluetoothapis.h&gt;

#include &lt;ws2bth.h&gt;

// Preprocessor Directive: Pragma's:
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")


// define Directive's:
#define DEFAULT_BUFLEN 512

DEFINE_GUID(GUID_NULL, "00000000-0000-0000-0000-000000000000");


int __cdecl main(int argc, char **argv)
{


std::cout &lt;&lt; "******************************************************\r\n";

//--------------------------------------------
// Locals:
//--------------------------------------------
int result = 0;
ULONG iResult = 0;
LPTSTR RemoteEndPointMACAddr = (LPTSTR)"38:2D:E8:B9:FA:EB";


//--------------------------------------------
// Prep the Buffer's:
//--------------------------------------------
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN] = "";
char *sendbuf = "AT+COPS?\r";


//--------------------------------------------
// Initialise WinSock.
//--------------------------------------------
WSADATA WSAData = { 0 };
WORD wVersionRequested = MAKEWORD(2, 2);
if ((iResult = WSAStartup(wVersionRequested, &amp;WSAData)) != 0)
{
}
std::cout &lt;&lt; "WINSOCK: 'WSAData' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


//--------------------------------------------
// The WinSock Socket.
//--------------------------------------------
SOCKET LocalSocket = INVALID_SOCKET;


//--------------------------------------------
// Local End Point SOCKADDR_BTH.
//--------------------------------------------
SOCKADDR_BTH LocalEndpoint;
// number of service channel, 0 or BT_PORT_ANY;
LocalEndpoint.port = 0;
LocalEndpoint.addressFamily = AF_BTH;
LocalEndpoint.btAddr = 0;
LocalEndpoint.serviceClassId = GUID_NULL;
std::cout &lt;&lt; "  Local EndPoint Address: " &lt;&lt; LocalEndpoint.btAddr &lt;&lt; "\r\n";


//--------------------------------------------
// Remote End Point SOCKADDR_BTH.
//--------------------------------------------
SOCKADDR_BTH RemoteEndPoint;
// number of service channel, 0 or BT_PORT_ANY;
RemoteEndPoint.port = 0;
RemoteEndPoint.addressFamily = AF_BTH;
RemoteEndPoint.btAddr = BTH_ADDR(0x382DE8B9FAEB);
RemoteEndPoint.serviceClassId = RFCOMM_PROTOCOL_UUID;
int BTHAddrLength = sizeof(RemoteEndPoint);
std::cout &lt;&lt; "  Remote EndPoint Address: " &lt;&lt; RemoteEndPoint.btAddr &lt;&lt; "\r\n";


//--------------------------------------------
// Create the socket.
//--------------------------------------------
if ((LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM)) == INVALID_SOCKET)
{
}
std::cout &lt;&lt; "WINSOCK: 'socket' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


//--------------------------------------------
// Bind the socket.
//--------------------------------------------
if ((iResult = bind(LocalSocket, (SOCKADDR *)&amp;LocalEndpoint, sizeof(LocalEndpoint))) == SOCKET_ERROR)
{
}
std::cout &lt;&lt; "WINSOCK: 'bind' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


//--------------------------------------------
// Connect the socket.
//--------------------------------------------
if ((iResult = connect(LocalSocket, (SOCKADDR *)&amp;RemoteEndPoint, sizeof(RemoteEndPoint))) == INVALID_SOCKET)
{
}
std::cout &lt;&lt; "WINSOCK: 'connect' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


//--------------------------------------------
// Send the Buffer.
//--------------------------------------------
if ((iResult = send(LocalSocket, sendbuf, (int)strlen(sendbuf), 0)) == SOCKET_ERROR)
{
}
std::cout &lt;&lt; "WINSOCK: 'send' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";
std::cout &lt;&lt; "  Bytes Sent: " &lt;&lt; sendbuf &lt;&lt; "\r\n";


//--------------------------------------------
// Receive until the peer termination.
//--------------------------------------------
if ((iResult = recv(LocalSocket, recvbuf, recvbuflen, 0)) &gt;= 1)
{
}
std::cout &lt;&lt; "WINSOCK: 'recv' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";
std::cout &lt;&lt; "  Data Received: " &lt;&lt; recvbuf &lt;&lt; "\r\n";


//--------------------------------------------
// Shutdown the connection.
//--------------------------------------------
if ((iResult = shutdown(LocalSocket, SD_SEND)) == SOCKET_ERROR)
{
}
std::cout &lt;&lt; "WINSOCK: 'shutdown' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


//--------------------------------------------
// Close the Socket.
//--------------------------------------------
if ((iResult = closesocket(LocalSocket)) == SOCKET_ERROR)
{
}
std::cout &lt;&lt; "WINSOCK: 'closesocket' Return Code: " &lt;&lt; WSAGetLastError() &lt;&lt; "\r\n";


WSACleanup();


std::cout &lt;&lt; "END: " &lt;&lt; "Application has completed!" &lt;&lt; "\r\n";


std::getchar();


return 0;

}
</code></pre>

<p>Is someone able to tell me where I am going wrong, I am trying to send and receive AT Commands to the Bluetooth GSM Device. It’s a Samsung Ace.</p>

<p>I keep getting 'ERROR' returned. <a href="http://www.lammertbies.nl/comm/info/hayes-at-commands.html" rel="nofollow noreferrer">The command returns "OK" ..., and "ERROR" otherwise</a> - However all AT Commands Fail with 'ERROR'</p>

<p>I have tried several UUID GUID's.</p>

<p>My Output is like so:</p>

<pre><code>******************************************************
WINSOCK: 'WSAData' Return Code: 0
        Local EndPoint Address: 0
        Remote EndPoint Address: 61769829186283
WINSOCK: 'socket' Return Code: 0
WINSOCK: 'bind' Return Code: 0
WINSOCK: 'connect' Return Code: 0
WINSOCK: 'send' Return Code: 0
        Bytes Sent: AT+COPS?
WINSOCK: 'recv' Return Code: 0
        Data Received:
ERROR

WINSOCK: 'shutdown' Return Code: 0
WINSOCK: 'closesocket' Return Code: 0
END: Application has completed!
</code></pre>

<p>At the very least, I hope my questions help others.</p>