The trouble is that while Windows provides an API for bluetooth communication (the bluetooth equivalent of TCP/IP) it doesn't appear to offer APIs for the higher level protocols (the bluetooth equivalents of HTTP, FTP, etc) needed to transfer files, etc. However, your phone most likely uses the OBEX protocol (a protocol is called a service in bluetooth parlance) to transfer files and there is an open source implementation called Open Obex that may be able to be ported to Windows.
You may be better off parsing the result of the transfer program than trying to code your own.
Anyway, the first step is to enumerate devices. This code, from the platform SDK bluetooth sample, finds all the devices in range. You will need MSVC and a recent platform SDK to compile it. Compile it (it's C++) and if everything is working it should list your device.
Code:
#include <stdio.h>
#include <winsock2.h>
#include <ws2bth.h>
#pragma comment(lib, "ws2_32.lib")
#define CXN_MAX_INQUIRY_RETRY 3
#define CXN_DELAY_NEXT_INQUIRY 15
//
// This function demonstrates device inquiry, with optional LUP flags.
//
ULONG ListBluetoothDevices(IN const char * pszRemoteName, OUT BTH_ADDR * pRemoteBtAddr)
{
INT iResult = 0, iRetryCount = 0;
BOOL bContinueLookup = FALSE, bRemoteDeviceFound = FALSE;
ULONG ulFlags = 0, ulPQSSize = sizeof(WSAQUERYSET);
HANDLE hLookup = 0;
PWSAQUERYSET pWSAQuerySet = NULL;
if ( ( NULL == pszRemoteName ) || ( NULL == pRemoteBtAddr ) )
{
goto CleanupAndExit;
}
if ( NULL == ( pWSAQuerySet = (PWSAQUERYSET) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulPQSSize) ) )
{
printf("!ERROR! | Unable to allocate memory for WSAQUERYSET\n");
goto CleanupAndExit;
}
//
// Search for the device with the correct name
//
for (iRetryCount = 0; !bRemoteDeviceFound && (iRetryCount < CXN_MAX_INQUIRY_RETRY); iRetryCount++)
{
//
// WSALookupService is used for both service search and device inquiry
// LUP_CONTAINERS is the flag which signals that we're doing a device inquiry.
//
ulFlags = LUP_CONTAINERS;
//
// Friendly device name (if available) will be returned in lpszServiceInstanceName
//
ulFlags |= LUP_RETURN_NAME;
//
// BTH_ADDR will be returned in lpcsaBuffer member of WSAQUERYSET
//
ulFlags |= LUP_RETURN_ADDR;
if ( 0 == iRetryCount )
{
printf("*INFO* | Inquiring device from cache...\n");
}
else
{
//
// Flush the device cache for all inquiries, except for the first inquiry
//
// By setting LUP_FLUSHCACHE flag, we're asking the lookup service to do
// a fresh lookup instead of pulling the information from device cache.
//
ulFlags |= LUP_FLUSHCACHE;
//
// Pause for some time before all the inquiries after the first inquiry
//
//
// BUGBUG why sleep. Try to get rid of this
//
// Remote Name requests will arrive after device inquiry has
// completed. Without a window to receive IN_RANGE notifications,
// we don't have a direct mechanism to determine when remote
// name requests have completed.
//
printf("*INFO* | Unable to find device. Waiting for %d seconds before re-inquiry...\n", CXN_DELAY_NEXT_INQUIRY);
Sleep(CXN_DELAY_NEXT_INQUIRY * 1000);
printf("*INFO* | Inquiring device ...\n");
}
//
// Start the lookup service
//
iResult = 0;
hLookup = 0;
bContinueLookup = FALSE;
ZeroMemory(pWSAQuerySet, ulPQSSize);
pWSAQuerySet->dwNameSpace = NS_BTH;
pWSAQuerySet->dwSize = sizeof(WSAQUERYSET);
iResult = WSALookupServiceBegin(pWSAQuerySet, ulFlags, &hLookup);
if ( (NO_ERROR == iResult) && (NULL != hLookup) )
{
bContinueLookup = TRUE;
}
else if ( 0 < iRetryCount )
{
printf("=CRITICAL= | WSALookupServiceBegin() failed with error code %d, WSALastError = %d\n", iResult, WSAGetLastError());
goto CleanupAndExit;
}
while ( bContinueLookup )
{
//
// Get information about next bluetooth device
//
// Note you may pass the same WSAQUERYSET from LookupBegin
// as long as you don't need to modify any of the pointer
// members of the structure, etc.
//
// ZeroMemory(pWSAQuerySet, ulPQSSize);
// pWSAQuerySet->dwNameSpace = NS_BTH;
// pWSAQuerySet->dwSize = sizeof(WSAQUERYSET);
if ( NO_ERROR == WSALookupServiceNext(hLookup, ulFlags, &ulPQSSize, pWSAQuerySet) )
{
printf("Found Device: %s\n", pWSAQuerySet->lpszServiceInstanceName);
//
// Since we're a non-unicode application, the remote
// name in lpszServiceInstanceName will have been converted
// from CP_UTF8 to CP_ACP, this may cause the name match
// to fail unexpectedly. If the app is to handle this,
// the app needs to be unicode.
//
if ( ( pWSAQuerySet->lpszServiceInstanceName != NULL ) && ( 0 == _stricmp(pWSAQuerySet->lpszServiceInstanceName, pszRemoteName) ) )
{
//
// Found a remote bluetooth device with matching name.
// Get the address of the device and exit the lookup.
//
CopyMemory(pRemoteBtAddr,
&((PSOCKADDR_BTH) pWSAQuerySet->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr,
sizeof(*pRemoteBtAddr));
bRemoteDeviceFound = TRUE;
bContinueLookup = FALSE;
}
}
else
{
if ( WSA_E_NO_MORE == ( iResult = WSAGetLastError() ) ) //No more data
{
//
// No more devices found. Exit the lookup.
//
bContinueLookup = FALSE;
}
else if ( WSAEFAULT == iResult )
{
//
// The buffer for QUERYSET was insufficient.
// In such case 3rd parameter "ulPQSSize" of function "WSALookupServiceNext()" receives
// the required size. So we can use this parameter to reallocate memory for QUERYSET.
//
HeapFree(GetProcessHeap(), 0, pWSAQuerySet);
pWSAQuerySet = NULL;
if ( NULL == ( pWSAQuerySet = (PWSAQUERYSET) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulPQSSize) ) )
{
printf("!ERROR! | Unable to allocate memory for WSAQERYSET\n");
bContinueLookup = FALSE;
}
}
else
{
printf("=CRITICAL= | WSALookupServiceNext() failed with error code %d\n", iResult);
bContinueLookup = FALSE;
}
}
}
//
// End the lookup service
//
WSALookupServiceEnd(hLookup);
}
CleanupAndExit:
if ( NULL != pWSAQuerySet )
{
HeapFree(GetProcessHeap(), 0, pWSAQuerySet);
pWSAQuerySet = NULL;
}
if ( bRemoteDeviceFound )
{
return(0);
}
else
{
return(1);
}
}
int main(void)
{
WSADATA WSAData = {0};
WSAStartup(MAKEWORD(2, 2), &WSAData);
BTH_ADDR btAddr;
ListBluetoothDevices("Test", &btAddr);
getchar();
return 0;
}