简体   繁体   English

从远程Android蓝牙到Windows XP PC接收文本数据

[英]Recieving text data from remote android bluetooth to Windows xp PC

I want to recieve text data from remote android bluetooth. 我想从远程Android蓝牙接收文本数据。 I had written simple winApi server, but it doesnt respond to remote android sending. 我写了简单的winApi服务器,但是它不响应远程android发送。 The connection was fine and did responds( paired) using control panel manager with my bluetooth. 连接很好,并且确实使用控制面板管理器和我的蓝牙进行了响应(配对)。 When sending and recieving over control panel manager was working fine. 在发送和接收控制面板时,经理工作正常。 But This time, I want to test using some c++ winapi. 但是这次,我想使用一些c ++ winapi进行测试。 Here's my code so far. 到目前为止,这是我的代码。 It was adapted from somewhere codeproject site :) 它是从某个地方的projectproject网站改编的:)

#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;
   }
  } 

I use microsoft BT device btw.. 我使用Microsoft BT设备顺便说一句..

Just to be sure: you said you're on WinXP... OK, but WinXP SP3 with Microsoft BT stack and appropriate dongle? 只是要确保:您说过您使用的是WinXP ...好,但是带有Microsoft BT堆栈和适当的加密狗的WinXP SP3吗? or another XP version and another BT stack (like Widcomm or BlueSoleil...)? 或另一个XP版本和另一个BT堆栈(例如Widcomm或BlueSoleil ...)? In the later case, BT Socket API won't work... 在后一种情况下,BT Socket API将无法工作...

Otherwise, not much to say about the code, except a missing BluetoothFindRadioClose... 否则,除了缺少BluetoothFindRadioClose以外,关于代码的内容就不多说了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM