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 #include #include #include #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_SA
-
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 #include #include #include #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_SA
Windows has several bluetooth APIs that you can use.
BluetoothAuthenticateDevice
is the API to send authentication code to the bluetooth device.«_Superman_» _I love work. It gives me something to do between weekends.