CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2004
    Posts
    26

    Bluetooth connection fails

    Why does the connection going down on the following code???:

    int main(int argc, char* argv[])
    {

    WSADATA wsaData;

    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );

    if ( iResult != NO_ERROR )
    {
    printf("Error at WSAStartup()\n");
    exit(1);
    }

    SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

    if (s == INVALID_SOCKET)
    {
    printf(GetLastErrorMessage(GetLastError()));
    exit(1);
    }

    SOCKADDR_BTH bthSockAddr;
    bthSockAddr.addressFamily=AF_BTH;
    bthSockAddr.btAddr=(BTH_ADDR)0x000e6d8e8a98;
    bthSockAddr.port=BT_PORT_ANY;
    bthSockAddr.serviceClassId=SerialPortServiceClass_UUID;

    if(0!=connect(s,(sockaddr*)&bthSockAddr,sizeof(bthSockAddr)))
    printf(GetLastErrorMessage(GetLastError()));
    else
    {
    printf("Connected\n");
    getchar();
    }

    if(0!=closesocket(s))
    printf("failed to close socket");


    WSACleanup();

    return 0;

    }

  2. #2
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: Bluetooth connection fails

    at what stage does the connection go down? i mean.. you do close the socket so it should go down at the end of your code...

  3. #3
    Join Date
    Dec 2004
    Posts
    26

    Re: Bluetooth connection fails

    Well, I meant _getch() inestead of getchar(), sorry. The thing is that I want the connection opened till I press any key in the keyboard.But...
    the connection goes down before I press any key. The secuence of what happens is something like this: (connection from PC to Nokia 6600)
    Run programm on computer, then I accept connection on the phone. everything seems to be right (there is a small item that appears on the screen of the mobile phone when a Bluetooth connection is stablished), because that item is there. Then it dissapears, 2 or 3 seconds later appears again and then definitely dissapears.I also cpntrol the status of the connection looking in the bluetoothmanager of microsoft, and there I see the same that I see on my phone(connected-not connected-connected-not connected and not connect again).
    I have started thinking that probably something goes wrong in the mobile (doesn´t accept the connection or something like that, but I can´t exactly figure out what's going on).
    Any help will be well recibed.

  4. #4
    Join Date
    Nov 2004
    Posts
    39

    Red face Re: Bluetooth connection fails

    Where can i get the Ws2bth.h header file?

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Bluetooth connection fails

    I think it is part of the current platform SDK. See "About Bluetooth" at http://msdn.microsoft.com/library/en..._bluetooth.asp

    Remember to "#include Ws2bth.h" after the #include to Winsock2.h to use Bluetooth sockets.

    Mike

  6. #6
    Join Date
    Feb 2011
    Posts
    2

    Re: Bluetooth connection fails

    Hi,

    I would like to ask during the connect(s,(sockaddr*)&bthSockAddr,sizeof(bthSockAddr),
    the handphone will ask for key in the pass code, and the computer shud oso send a same passcode, so how I can set this passcode???

    Best Regard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured