问题描述
这是本课题的延续:Programming对于蓝牙的WinSock - 解决方案所需
我不坚强的C ++!
的lib /头文件可以在这里找到C:\\ Program Files文件(x86)的\\的Windows套件\\ 8.1 \\包含\\嗯这是这里的Windows SDK的工具包的一部分:的或这里的
我的code:
<$p$p><$c$c>///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MSDN蓝牙和读或写操作:
//参见:https://msdn.microsoft.com/en-us/library/windows/desktop/aa362907(v=vs.85).aspx
//
// MSDN蓝牙连接
//参见:https://msdn.microsoft.com/en-us/library/windows/desktop/aa362901(v=vs.85).aspx
//
// MSDN发送功能
//参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx
//
//错误codeS:
//参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//包括:
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&iostream的GT;#包括LT&;&WinSock2.h GT;
#包括LT&;&bthsdpdef.h GT;
#包括LT&;&bluetoothapis.h GT;#包括LT&;&ws2bth.h GT;// preprocessor指令:编译指示的:
//需要与WS2_32.LIB链接
的#pragma评论(LIB,WS2_32.LIB)
//定义指令的:
#定义DEFAULT_BUFLEN 512DEFINE_GUID(GUID_NULL00000000-0000-0000-0000-000000000000);
INT __cdecl主(INT ARGC,字符** argv的)
{
性病::法院LT&;&LT; ******************* ***** \\ r \\ n;// --------------------------------------------
//当地人:
// --------------------------------------------
INT结果为0;
ULONG iResult = 0;
LPTSTR RemoteEndPointMACAddr =(LPTSTR)38:2D:E8:B9:FA:EB;
// --------------------------------------------
// preP缓冲的:
// --------------------------------------------
INT recvbuflen = DEFAULT_BUFLEN;
烧焦recvbuf [DEFAULT_BUFLEN] =;
字符* sendbuf =AT + COPS \\ R;
// --------------------------------------------
//初始化的WinSock。
// --------------------------------------------
WSADATA WSADATA = {0};
字wVersionRequested = MAKEWORD(2,2);
如果((iResult =调用WSAStartup(wVersionRequested,&放大器;!WSADATA))= 0)
{
}
性病::法院LT&;&LT; WINSOCK:WSADATA'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
// --------------------------------------------
// Winsock的插座。
// --------------------------------------------
SOCKET LocalSocket = INVALID_SOCKET;
// --------------------------------------------
//本地端点SOCKADDR_BTH。
// --------------------------------------------
SOCKADDR_BTH LocalEndpoint;
//服务信道,0或BT_PORT_ANY的号码;
LocalEndpoint.port = 0;
LocalEndpoint.addressFamily = AF_BTH;
LocalEndpoint.btAddr = 0;
LocalEndpoint.serviceClassId = GUID_NULL;
性病::法院LT&;&LT; 本地端点地址:&LT;&LT; LocalEndpoint.btAddr&LT;&LT; \\ r \\ n;
// --------------------------------------------
//远程端点SOCKADDR_BTH。
// --------------------------------------------
SOCKADDR_BTH RemoteEndPoint;
//服务信道,0或BT_PORT_ANY的号码;
RemoteEndPoint.port = 0;
RemoteEndPoint.addressFamily = AF_BTH;
RemoteEndPoint.btAddr = BTH_ADDR(0x382DE8B9FAEB);
RemoteEndPoint.serviceClassId = RFCOMM_PROTOCOL_UUID;
INT BTHAddrLength = sizeof的(RemoteEndPoint);
性病::法院LT&;&LT; 远程端点地址:&LT;&LT; RemoteEndPoint.btAddr&LT;&LT; \\ r \\ n;
// --------------------------------------------
//创建套接字。
// --------------------------------------------
如果((LocalSocket =插座(AF_BTH,SOCK_STREAM,BTHPROTO_RFCOMM))== INVALID_SOCKET)
{
}
性病::法院LT&;&LT; WINSOCK:'插座'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
// --------------------------------------------
//绑定插座。
// --------------------------------------------
如果((iResult =绑定(LocalSocket,(SOCKADDR *)及LocalEndpoint,sizeof的(LocalEndpoint)))== SOCKET_ERROR)
{
}
性病::法院LT&;&LT; WINSOCK:'绑定'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
// --------------------------------------------
//连接插座。
// --------------------------------------------
如果((iResult =连接(LocalSocket,(SOCKADDR *)及RemoteEndPoint,sizeof的(RemoteEndPoint)))== INVALID_SOCKET)
{
}
性病::法院LT&;&LT; WINSOCK:'连接'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
// --------------------------------------------
//发送缓冲区。
// --------------------------------------------
如果((iResult =送(LocalSocket,sendbuf,(INT)strlen的(sendbuf),0))== SOCKET_ERROR)
{
}
性病::法院LT&;&LT; WINSOCK:'送'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
性病::法院LT&;&LT; 发送的字节数:&LT;&LT; sendbuf&LT;&LT; \\ r \\ n;
// --------------------------------------------
//接收,直到对终止。
// --------------------------------------------
如果((iResult =的recv(LocalSocket,recvbuf,recvbuflen,0))&GT; = 1)
{
}
性病::法院LT&;&LT; WINSOCK:recv的'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
性病::法院LT&;&LT; 数据接收:&LT;&LT; recvbuf&LT;&LT; \\ r \\ n;
// --------------------------------------------
//关闭连接。
// --------------------------------------------
如果((iResult =关机(LocalSocket,SD_SEND))== SOCKET_ERROR)
{
}
性病::法院LT&;&LT; WINSOCK:'关机'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
// --------------------------------------------
//关闭套接字。
// --------------------------------------------
如果((iResult =关闭套接字(LocalSocket))== SOCKET_ERROR)
{
}
性病::法院LT&;&LT; WINSOCK:'字关闭'返回code:&LT;&LT;于WSAGetLastError()&所述;&下; \\ r \\ n;
WSACleanup();
性病::法院LT&;&LT; END:&LT;&LT; 应用程序已完成! &LT;&LT; \\ r \\ n;
的std ::的getchar();
返回0;}
就是有人能告诉我在哪里,我错了,我想发送和接收AT命令蓝牙GSM设备。这是一个三星王牌。
我不断收到错误返回。
或:或的
This is a continuation of this Question: Programming for Bluetooth in WinSock - A Solution needed
I am not strong in C++!
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: Windows Software Development Kit (SDK) for Windows 8.1 or here Windows Software Development Kit (SDK) for Windows 10
My 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 <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <WinSock2.h>
#include <bthsdpdef.h>
#include <bluetoothapis.h>
#include <ws2bth.h>
// 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 << "******************************************************\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, &WSAData)) != 0)
{
}
std::cout << "WINSOCK: 'WSAData' Return Code: " << WSAGetLastError() << "\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 << " Local EndPoint Address: " << LocalEndpoint.btAddr << "\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 << " Remote EndPoint Address: " << RemoteEndPoint.btAddr << "\r\n";
//--------------------------------------------
// Create the socket.
//--------------------------------------------
if ((LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM)) == INVALID_SOCKET)
{
}
std::cout << "WINSOCK: 'socket' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Bind the socket.
//--------------------------------------------
if ((iResult = bind(LocalSocket, (SOCKADDR *)&LocalEndpoint, sizeof(LocalEndpoint))) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'bind' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Connect the socket.
//--------------------------------------------
if ((iResult = connect(LocalSocket, (SOCKADDR *)&RemoteEndPoint, sizeof(RemoteEndPoint))) == INVALID_SOCKET)
{
}
std::cout << "WINSOCK: 'connect' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Send the Buffer.
//--------------------------------------------
if ((iResult = send(LocalSocket, sendbuf, (int)strlen(sendbuf), 0)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'send' Return Code: " << WSAGetLastError() << "\r\n";
std::cout << " Bytes Sent: " << sendbuf << "\r\n";
//--------------------------------------------
// Receive until the peer termination.
//--------------------------------------------
if ((iResult = recv(LocalSocket, recvbuf, recvbuflen, 0)) >= 1)
{
}
std::cout << "WINSOCK: 'recv' Return Code: " << WSAGetLastError() << "\r\n";
std::cout << " Data Received: " << recvbuf << "\r\n";
//--------------------------------------------
// Shutdown the connection.
//--------------------------------------------
if ((iResult = shutdown(LocalSocket, SD_SEND)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'shutdown' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Close the Socket.
//--------------------------------------------
if ((iResult = closesocket(LocalSocket)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'closesocket' Return Code: " << WSAGetLastError() << "\r\n";
WSACleanup();
std::cout << "END: " << "Application has completed!" << "\r\n";
std::getchar();
return 0;
}
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.
I keep getting 'ERROR' returned. The command returns "OK" ..., and "ERROR" otherwise - However all AT Commands Fail with 'ERROR'
I have tried several UUID GUID's.
My Output is like so:
******************************************************
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!
At the very least, I hope my questions help others.
Got it working!
For others out there, and there are many from my research, I hope this helps: If it has, please vote up!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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
//
// MSDN Bluetooth and read or write operations:
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa362907(v=vs.85).aspx
//
// Error Codes:
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <WinSock2.h>
#include <bthsdpdef.h>
#include <bluetoothapis.h>
#include <ws2bth.h>
// 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 << "******************************************************\r\n";
//--------------------------------------------
// Locals:
//--------------------------------------------
int result = 0;
ULONG iResult = 0;
LPTSTR RemoteEndPointMACAddress = (LPTSTR)"38:2D:E8:B9:FA:EB";
//--------------------------------------------
// Prep the Buffer's:
//--------------------------------------------
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN] = "";
char *sendbuf = "AT+COPS?\r";
int len = (int)strlen(sendbuf);
//--------------------------------------------
// Initialise WinSock.
//--------------------------------------------
WSADATA WSAData = { 0 };
WORD wVersionRequested = MAKEWORD(2, 2);
if ((iResult = WSAStartup(wVersionRequested, &WSAData)) != 0)
{
}
std::cout << "WINSOCK: 'WSAData' Return Code: " << WSAGetLastError() << "\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 << " Local EndPoint Address: " << LocalEndpoint.btAddr << "\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 = HandsfreeServiceClass_UUID; // RFCOMM_PROTOCOL_UUID
int BTHAddrLength = sizeof(RemoteEndPoint);
std::cout << " Remote EndPoint Address: " << RemoteEndPoint.btAddr << "\r\n";
//--------------------------------------------
// Create the socket.
//--------------------------------------------
if ((LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM)) == INVALID_SOCKET)
{
}
std::cout << "WINSOCK: 'socket' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Bind the socket.
//--------------------------------------------
if ((iResult = bind(LocalSocket, (SOCKADDR *)&LocalEndpoint, sizeof(LocalEndpoint))) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'bind' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Connect the socket.
//--------------------------------------------
if ((iResult = connect(LocalSocket, (SOCKADDR *)&RemoteEndPoint, sizeof(RemoteEndPoint))) == INVALID_SOCKET)
{
}
std::cout << "WINSOCK: 'connect' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Send the Buffer.
//--------------------------------------------
if ((iResult = send(LocalSocket, sendbuf, len, MSG_OOB)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'send' Return Code: " << WSAGetLastError() << "\r\n";
std::cout << " Bytes Sent: " << sendbuf << "\r\n";
//--------------------------------------------
// Receive until the peer termination.
//--------------------------------------------
if ((iResult = recv(LocalSocket, recvbuf, recvbuflen, 0)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'recv' Return Code: " << WSAGetLastError() << "\r\n";
std::cout << " Data Received: " << recvbuf << "\r\n";
//--------------------------------------------
// Shutdown the connection.
//--------------------------------------------
if ((iResult = shutdown(LocalSocket, SD_SEND)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'shutdown' Return Code: " << WSAGetLastError() << "\r\n";
//--------------------------------------------
// Close the Socket.
//--------------------------------------------
if ((iResult = closesocket(LocalSocket)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'closesocket' Return Code: " << WSAGetLastError() << "\r\n";
WSACleanup();
std::cout << "END: " << "Application has completed!" << "\r\n";
std::getchar();
return 0;
}
I had to use the 'HandsfreeServiceClass_UUID' UUID as you can see. I also set the Stream Type to 'MSG_OOB' in the send Method.
My Output:
******************************************************
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:
+COPS: 0,0,"My Provider"
WINSOCK: 'shutdown' Return Code: 0
WINSOCK: 'closesocket' Return Code: 0
END: Application has completed!
EDITIf you want to send SMS Messages, a Server Socket will be needed on the GSM Device: Xamarin Android Connecting with bluetooth deviceor: Android Bluetooth COM port or SENA BTerm Bluetooth Terminal
这篇关于C ++的WinSock蓝牙连接 - AT命令 - 错误接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!