dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Handle bluetooth pairing code

  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Handle bluetooth pairing code

    Hi,

    I'm using windows 7 pc, and trying to write a Bluetooth server app.

    My requirement is to pair my Mobile (Nokia -N8) with my server app, and then exchange data using a service class.

    ISSUE:

    The issue I'm facing is, on running server on my PC, the server waits for connection request from phone.

    But phone is asking me to enter a pass code, But i don't know how to handle this passcode issue at server side. and establish the connection.

    The connection always fails with a message that no response from server.



    Here is my server code, which I'm running on my win 7 PC.

    // Bluetooth.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <WinSock2.h>
    #include <ws2bth.h>
    #include <bthsdpdef.h>
    #include <BluetoothAPIs.h>

    #pragma comment(lib, "Ws2_32.lib")


    int _tmain(int argc, _TCHAR* argv[])
    {
    //----------------------
    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR)
    {
    wprintf(L"WSAStartup failed with error: %ld\n", iResult);
    return 1;
    }

    //----------------------
    // Create a SOCKET for listening for
    // incoming connection requests.
    SOCKET ListenSocket;
    ListenSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    if (ListenSocket == INVALID_SOCKET) {
    wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port for the socket that is being bound.
    SOCKADDR_BTH blthsocket;
    memset(&blthsocket,0,sizeof(SOCKADDR_BTH));

    blthsocket.addressFamily = AF_BTH;
    blthsocket.btAddr = 0;
    blthsocket.serviceClassId = GUID_NULL;
    blthsocket.port = BT_PORT_ANY ;

    wprintf(L"============ Before Bind =============\n");
    wprintf(L"blthsocket.btAddr = %d \n",blthsocket.btAddr);
    wprintf(L"blthsocket.port = %d \n",blthsocket.port);


    if (bind(ListenSocket,(SOCKADDR *) & blthsocket, sizeof (blthsocket)) == SOCKET_ERROR)
    {
    wprintf(L"bind failed with error: %ld\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return 1;
    }

    wprintf(L"Binding is complete\n");
    wprintf(L"\n\n============ After Bind =============\n");
    int len = sizeof(blthsocket);
    if (0 == getsockname (ListenSocket, (sockaddr*)&blthsocket, &len))
    {
    wprintf (L"Local Bluetooth device is %04x%08x \nServer channel = %d\n",
    GET_NAP(blthsocket.btAddr), GET_SAP(blthsocket.btAddr), blthsocket.port);
    }

    //----------------------------------------------------------------------------
    wprintf(L"Waiting for client to connect...\n");
    listen (ListenSocket, 5);
    for ( ; ; ) {
    SOCKADDR_BTH sab2;
    int ilen = sizeof(sab2);
    SOCKET s2 = accept (ListenSocket,(sockaddr*)&sab2, &ilen);
    if (s2 == INVALID_SOCKET) {
    wprintf (L"Socket bind, error %d\n", WSAGetLastError ());
    break;
    }
    wprintf (L"Connection came from %04x%08x to channel %d\n",
    GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
    wprintf (L"Hello");
    SpinConnectionThreadsOnSocket (s2);
    }
    closesocket (ListenSocket);
    WSACleanup();
    return 0;
    }



    Kindly help resolving this issue, Thanks in advance.

    Regards,

    Vijay.

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

    Re: Handle bluetooth pairing code

    Please post your client code from the phone's side.

    If your answer is something like "I didn't write any code for the phone", then you're probably doing this wrong. Look instead into Bluetooth "device discovery", and write code on the PC that looks for Bluetooth devices.

    Some sample Bluetooth device-discovery code was posted by Len Holgate at his blog: http://www.lenholgate.com/archives/000102.html . Maybe it could help, but please note that the code is several years old by now, and that there might be more recent and more efficient samples of good code.

    Mike

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

    Re: Handle bluetooth pairing code

    Incidentally, why did you post this in "C++ and WinAPI"? It seems to have no relation to WinAPI, and probably would have been better in the "Network Programming" forum.

  4. #4
    Join Date
    Feb 2009
    Posts
    24

    Re: Handle bluetooth pairing code

    Thanks Mike,
    Yes, I haven't written any client code on the phone.
    But , I'm trying to just pair devices, so i thought it doesn't need any client code on phone.
    ok, I'll check it further.

    Kindly let me know if you come across more information on this.

    Regards,
    Vijay.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)