2016-05-08 19 views
1

を受信これは、この質問の続きです:C++のWinSock Bluetooth接続 - コマンドAT - エラー

私はC++で強力ではありませんよ!

のLib /ヘッダファイルは、ここではCを見つけることができます:\プログラムファイル(x86の)\ Windowsのキット\ 8.1 \含めるここでのWindows SDKキットの一部であるUM \:Windows Software Development Kit (SDK) for Windows 8.1またはここWindows Software Development Kit (SDK) for Windows 10

マイコード:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// 
// 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; 

} 

私が間違っている場所を教えてくれる人がいますか?ATコマンドをBluetooth GSMデバイスに送信しようとしています。それは三星エースだ。

「ERROR」が返され続けます。 The command returns "OK" ..., and "ERROR" otherwise - すべてのATコマンドが 'ERROR'で失敗する

私はいくつかのUUID GUIDを試しました。

私の出力はそうのようなものです:非常に少なくとも

****************************************************** 
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! 

が、私は私の質問は、他人を助ける願っています。

答えて

0

他の人には、私の研究から多くのものがありますが、これが助けてくれることを願っています:もしあれば、投票してください!

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// 
// 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; 

} 

「HandsfreeServiceClass_UUID」UUIDを使用する必要がありました。また、sendメソッドでStream Typeを 'MSG_OOB'に設定しました。

マイ出力:あなたはSMSメッセージを送信する場合は は、サーバソケットがGSMデバイス上で必要とされるであろう

****************************************************** 
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! 

EDIT:Xamarin Android Connecting with bluetooth device か:Android Bluetooth COM portまたはSENA BTerm Bluetooth Terminal

関連する問題