接收文本数据从远程安卓蓝牙到Windows xp PC

Recieving text data from remote android bluetooth to Windows xp PC

本文关键字:Windows xp PC 程安卓 文本 数据      更新时间:2023-10-16

我想从远程安卓蓝牙接收文本数据。我写了简单的winApi服务器,但它不响应远程安卓发送。连接很好,并且使用带有蓝牙的控制面板管理器进行了响应(配对)。当发送和接收控制面板管理器工作正常时。但是这一次,我想使用一些c ++ winapi进行测试。这是我到目前为止的代码。它改编自某个代码项目站点:)

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <tchar.h> 
#include <WinSock2.h>
#include <ws2bth.h> 
#include <BluetoothAPIs.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested = 0x202;
WSADATA m_data;
if (0 == WSAStartup(wVersionRequested, &m_data))
{
    SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    const DWORD lastError =  GetLastError();
 if (s == INVALID_SOCKET)
  {
    ostringstream stream;
    stream << lastError;
    string data= stream.str();
    printf("Failed to get bluetooth socket! %sn", data.c_str());
        return 1;
  }
  WSAPROTOCOL_INFO protocolInfo;
  int protocolInfoSize = sizeof(protocolInfo);
  if (0 != getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFO, 
    (char*)&protocolInfo, &protocolInfoSize))
  {
     return 1;
   }
  SOCKADDR_BTH address;
   address.addressFamily = AF_BTH;
  address.btAddr = 0;
  address.serviceClassId = GUID_NULL;
  address.port = BT_PORT_ANY;
  sockaddr *pAddr = (sockaddr*)&address;
  if (0 != bind(s, pAddr, sizeof(SOCKADDR_BTH)))
  {
     ostringstream stream;
     stream << GetLastError();
     string data= stream.str();
     printf("%sn", data.c_str() );
   }
  else
   {
       printf("nBinding Successful....n");
       int length = sizeof(SOCKADDR_BTH) ;
       getsockname(s,(sockaddr*)&address,&length);
       wprintf (L"Local Bluetooth device is %04x%08x nServer channel = %dn", 
    GET_NAP(address.btAddr), GET_SAP(address.btAddr), address.port);
  }
    int size = sizeof(SOCKADDR_BTH);
    if (0 != getsockname(s, pAddr, &size))
    {
        ostringstream stream;
       stream << GetLastError();
      string data= stream.str();
        printf("%sn",  data.c_str());
    }
    if (0 != listen(s, 10))
    {
        ostringstream stream;
    stream << GetLastError();
     string data= stream.str();
        printf("%sn",  data.c_str());
    }
    WSAQUERYSET service;
    memset(&service, 0, sizeof(service));
    service.dwSize = sizeof(service);
   // service.lpszServiceInstanceName = reinterpret_cast<LPWSTR>(_T("Accelerometer Data..."));
    //service.lpszServiceInstanceName = ;
   // service.lpszComment = reinterpret_cast<LPWSTR>(_T("Pushing data to PC"));
    GUID serviceID = OBEXFileTransferServiceClass_UUID;
    service.lpServiceClassId = &serviceID;
    service.dwNumberOfCsAddrs = 1;
    service.dwNameSpace = NS_BTH;
    CSADDR_INFO csAddr;
    memset(&csAddr, 0, sizeof(csAddr));
    csAddr.LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
    csAddr.LocalAddr.lpSockaddr = pAddr;
    csAddr.iSocketType = SOCK_STREAM;
    csAddr.iProtocol = BTHPROTO_RFCOMM;
    service.lpcsaBuffer = &csAddr;
    if (0 != WSASetService(&service, RNRSERVICE_REGISTER, 0))
    {
        printf("Service registration failed....");
        ostringstream stream;
    stream << GetLastError();
    string data= stream.str();
        printf("%dn",  data.c_str());
    }
    else
    {    
        printf("nService registration Successful....n");
    }
    HANDLE hRadio ;
        BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
        HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio);

        if ((hFind != NULL)&& (hRadio !=NULL))
        {
                    if (BluetoothEnableDiscovery(hRadio,TRUE)){
                        printf("BluetoothEnableDiscovery() is working!n");
                    }
                    if (BluetoothEnableIncomingConnections(hRadio,TRUE)){
                        printf("BluetoothEnableIncomingConnections() is working!n");
                    }
                    if (BluetoothIsConnectable(hRadio)){
                        printf("BluetoothIsConnectable() is working!n");
                    }   
        }

    printf("nBefore accept.........");
    SOCKADDR_BTH sab2;
    int ilen = sizeof(sab2);
    SOCKET s2 = accept (s,(sockaddr*)&sab2, &ilen);
    if (s2 == INVALID_SOCKET)
    {
     wprintf (L"Socket bind, error %dn", WSAGetLastError ());
    }
    wprintf (L"nConnection came from %04x%08x to channel %dn",
    GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
    wprintf (L"nAfter Acceptn");
    char buffer[1024] = {0}; 
    memset(buffer, 0, sizeof(buffer));
    int r = recv(s2,(char*)buffer, sizeof(buffer), 0);
    printf("%sn",buffer);
     closesocket(s2);
    if (0 != WSASetService(&service, RNRSERVICE_DELETE, 0))
    {
        ostringstream stream;
       stream << GetLastError();
       string data= stream.str();
        printf("%sn",  data.c_str());
    }
    closesocket(s);
    WSACleanup();
    return 0;
   }
  } 

顺便说一句,我使用微软BT设备。

只是为了确定:你说你在WinXP上...好的,但是带有Microsoft BT堆栈和适当的加密狗的WinXP SP3?还是另一个XP版本和另一个BT堆栈(如Widcomm或BlueSoleil...)?在后一种情况下,BT 套接字 API 将不起作用...

否则,关于代码没什么好说的,除了缺少蓝牙查找无线电关闭...