Winsock2 使用蓝牙 OBEX 对象推送配置文件 (OPP) 发送文件

问题描述 投票:0回答:1

我想实现图中“发送文件”的功能 enter image description here

我想实现链接中的第4步在Windows中通过蓝牙发送文件

我在下面设置了引导,

RemoteEndPoint.serviceClassId = OBEXObjectPushServiceClass_UUID;

我想将图像发送到我的智能手机。

那么,我该如何使用套接字

send
api?我现在知道我的发送代码是错误的。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
// 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";
    char *sendbuf = "AT+BTVER?\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(0xD428D57436B0);
    //RemoteEndPoint.serviceClassId = HandsfreeServiceClass_UUID; // RFCOMM_PROTOCOL_UUID
    RemoteEndPoint.serviceClassId = OBEXObjectPushServiceClass_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)
    char *strPath = "D:/OneDrive/016.jpg";
    if ((iResult = send(LocalSocket, strPath, strlen(strPath), 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)) == 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;

}
c++ winsock2 obex
1个回答
0
投票

最终,我找到了解决方案。 OBEX 连接如下:

OBEX 协议是什么样的?

OBEX PUT 命令如下:

const char* str = "82002bc30000000f01001100620074002e007400780074000049001268656c6c6f2c20776f726c64200d0a";
          /* Put | 2B len| HI len | Name|   b   t   .   t   x   t   |HI end|h e l l o ,   w o r l d  \r\n   */

本源码封装的数据分析和发送命令:

https://github.com/zuckschwerdt/openobex

我的测试程序修改如下:

C++ WinSock 蓝牙连接 - AT 命令 - 收到错误

我的测试程序:

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

std::cout << "******************************************************\r\n";

//--------------------------------------------
// Locals:
//--------------------------------------------
int result = 0;
ULONG iResult = 0;

//--------------------------------------------
// Prep the Buffer's:
//--------------------------------------------
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN] = "";
// char *sendbuf = "AT+COPS?\r";
char *sendbuf = "AT+BTVER?\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;
// bt address of my smartphone ;
RemoteEndPoint.btAddr = BTH_ADDR(0xD428D57436B0);
// RemoteEndPoint.serviceClassId = SerialPortServiceClass_UUID;   // COM Protocol
// RemoteEndPoint.serviceClassId = OBEXFileTransferServiceClass_UUID;   // error, connect return 10049
//RemoteEndPoint.serviceClassId = HandsfreeServiceClass_UUID; // RFCOMM_PROTOCOL_UUID
RemoteEndPoint.serviceClassId = OBEXObjectPushServiceClass_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)
char *strPath = "D:/bt.txt";
const char package[] = 
/* Connect | 2B of length| OBEX Ver 1.0| Flag| Max Size               */    
  {0x80,    0x00, 0x07,         0x10, 0x00, 2048>>8, 2048&0xFF};

if ((iResult = send(LocalSocket, package, sizeof(package)/sizeof(char), 0)) == SOCKET_ERROR)
{
}
std::cout << "WINSOCK: 'send' Return Code: " << WSAGetLastError() << "\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";
    return -1;
}
CString strdata = Byte2Ascn((BYTE*)recvbuf, iResult, 16);
std::wcout << "recv data: " << (LPCTSTR) strdata << std::endl;

if(0xA0 == static_cast<unsigned char>(recvbuf[0])){
    const char* str = "82002bc30000000f01001100620074002e007400780074000049001268656c6c6f2c20776f726c64200d0a";
              /* Put | 2B len| HI len | Name|   b   t   .   t   x   t   |HI end|h e l l o ,   w o r l d  \r\n   */    
    int dstlen = strlen((const char*)str)/2;
    BYTE* strdst = (BYTE*)malloc(dstlen * sizeof(BYTE));
    Asc2Byten((void*)str, strdst, dstlen);

    const char package[] = 
    {  
        0x85,0x00,0x08,0x02,0x00,0x01,0x00,0x03
        // 0x82,0x01,0x65,0x01,0x00,0x0F,0x00,0x31,0x00,0x2E,0x00,0x6D,0x00,0x69,0x00,0x64,
        // 0x00,0x00,0xC3,0x00,0x00,0x01,0x39,0x44,0x00,0x12,0x32,0x30,0x30,0x35,0x30,0x32,
        // 0x31,0x34,0x54,0x31,0x39,0x34,0x39,0x33,0x38,0x49,0x01,0x3C,0x4D,0x54,0x68,0x64
        
    };
    if ((iResult = send(LocalSocket, (const char*)strdst, dstlen, 0)) == SOCKET_ERROR)
    {
    }
    std::cout << "WINSOCK: 'send' Return Code: " << WSAGetLastError() << "\r\n";
    delete strdst;

    if ((iResult = recv(LocalSocket, recvbuf, recvbuflen, 0)) == SOCKET_ERROR){
        std::cout << "WINSOCK: 'recv' Return Code: " << WSAGetLastError() << "\r\n";
        return -1;
    }
    strdata = Byte2Ascn((BYTE*)recvbuf, iResult, 16);
    std::wcout << "recv data: " << (LPCTSTR) strdata << std::endl;
}

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

}

© www.soinside.com 2019 - 2024. All rights reserved.