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

时间:2015-04-20 12:29:58

标签: android c++ winapi bluetooth

我想从远程android蓝牙接收文本数据。我写过简单的winApi服务器,但它不响应远程android发送。连接很好,并使用控制面板管理器和我的蓝牙做出响应(配对)。通过控制面板管理器发送和接收工作正常。但是这一次,我想用一些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! %s\n", 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("%s\n", 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 = %d\n", 
    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("%s\n",  data.c_str());
    }
    if (0 != listen(s, 10))
    {
        ostringstream stream;
    stream << GetLastError();
     string data= stream.str();
        printf("%s\n",  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("%d\n",  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 %d\n", WSAGetLastError ());
    }
    wprintf (L"\nConnection came from %04x%08x to channel %d\n",
    GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
    wprintf (L"\nAfter Accept\n");

    char buffer[1024] = {0}; 
    memset(buffer, 0, sizeof(buffer));
    int r = recv(s2,(char*)buffer, sizeof(buffer), 0);
    printf("%s\n",buffer);

     closesocket(s2);
    if (0 != WSASetService(&service, RNRSERVICE_DELETE, 0))
    {
        ostringstream stream;
       stream << GetLastError();
       string data= stream.str();
        printf("%s\n",  data.c_str());
    }
    closesocket(s);
    WSACleanup();
    return 0;
   }
  } 

我使用微软BT设备btw ..

1 个答案:

答案 0 :(得分:0)

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

否则,关于代码的说法不多,除了缺少BluetoothFindRadioClose ......