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


/********************************************************************
created: 2010/01/29
file base: wBlue
file ext: c
author: TODY_GUO
Revision: 1.0.0
purpose: Searching Bluetooth Devices.... this just for MS BT
Driver
*********************************************************************/
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <bthdef.h>
#include <BluetoothAPIs.h>
#include <bthdef.h>

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

int search_bluetooth_device()
{
BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(btfrp) };
BLUETOOTH_DEVICE_SEARCH_PARAMS btdp = { sizeof(btdp) };
BLUETOOTH_DEVICE_INFO_STRUCT Devinfo;
BLUETOOTH_RADIO_INFO RadioInfo;
HANDLE hRadio;

char blueT_Name[256]={'\0'};
HBLUETOOTH_RADIO_FIND hFind_R ;
HBLUETOOTH_DEVICE_FIND hFind_D;
int blueCount = 0;


printf("Searching Local Bluetooth(s)...");

hFind_R = BluetoothFindFirstRadio(&btfrp,&hRadio);
if (NULL != hFind_R)
{
RadioInfo.dwSize = sizeof(RadioInfo);
do
{
printf(" Found!\n");
if( ERROR_SUCCESS == BluetoothGetRadioInfo(hRadio, &RadioInfo))
{
wcstombs(blueT_Name,RadioInfo.szName,sizeof(blueT_Name));
printf("-----------------------------------------\n");
printf("Bluetooth Name\t\tMAC Address\n");
printf("-----------------------------------------\n");
printf("%-15S\t\t%02x:%02x:%02x:%02x:%02x:%02x",RadioInfo.szName,
RadioInfo.address.rgBytes[5],
RadioInfo.address.rgBytes[4],
RadioInfo.address.rgBytes[3],
RadioInfo.address.rgBytes[2],
RadioInfo.address.rgBytes[1],
RadioInfo.address.rgBytes[0]);
}
else
{
printf("Local Get Info Fail.");
return 1;
}

// Find near Device...
memset(&btdp,0,sizeof(btdp));
btdp.hRadio = hRadio;
btdp.fReturnAuthenticated = TRUE;
btdp.fReturnRemembered = TRUE;
btdp.fIssueInquiry = TRUE;
btdp.fReturnUnknown = TRUE;
btdp.fReturnConnected = FALSE;
btdp.dwSize = sizeof(btdp);
btdp.cTimeoutMultiplier = 10;

Devinfo.dwSize = sizeof(Devinfo);
hFind_D = BluetoothFindFirstDevice(&btdp,&Devinfo);
if ( hFind_D != NULL)
{
do
{
if ( ERROR_SUCCESS == BluetoothGetDeviceInfo(hRadio,&Devinfo))
{
printf("\n%-15S\t\t%02x:%02x:%02x:%02x:%02x:%02x",Devinfo.szName,
Devinfo.Address.rgBytes[5],
Devinfo.Address.rgBytes[4],
Devinfo.Address.rgBytes[3],
Devinfo.Address.rgBytes[2],
Devinfo.Address.rgBytes[1],
Devinfo.Address.rgBytes[0]);
blueCount++;
}
else
{
printf("Remote Get Info fail.");
return 1;
}
} while(BluetoothFindNextDevice(hFind_D,&Devinfo));
BluetoothFindDeviceClose(hFind_D);
}
else
{
printf("\nRemote Bluetooth Not Found!\n");
return 1;
}
} while(BluetoothFindNextRadio(hFind_R, &hRadio));
BluetoothFindRadioClose(hFind_R); // Close Local Function
}
else
{
printf(" not Found!\n");
return 1;
}

printf("\nFound %d devices\n", blueCount);

if (blueCount >= 3){
return 0;
}else{
return 1;
}
}

int main(int argc, char* argv[])
{
return search_bluetooth_device();
}