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.