简体   繁体   中英

Recieving text data from remote android bluetooth to Windows xp PC

I want to recieve text data from remote android bluetooth. I had written simple winApi server, but it doesnt respond to remote android sending. 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. Here's my code so far. It was adapted from somewhere codeproject site :)

#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..

Just to be sure: you said you're on WinXP... OK, but WinXP SP3 with Microsoft BT stack and appropriate dongle? or another XP version and another BT stack (like Widcomm or BlueSoleil...)? In the later case, BT Socket API won't work...

Otherwise, not much to say about the code, except a missing BluetoothFindRadioClose...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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