Bluetooth 関連の API は BluetoothAPIs.h
ヘッダにも詳しく使い方や説明が書かれているため、ヘッダ見た方が
わかりやすいようです。(XP SP2 以降)
//Bluetooth インターフェース検索 #include#include #include // 必要 #include BLUETOOTH_FIND_RADIO_PARAMS param; memset( ¶m, 0, sizeof(BLUETOOTH_FIND_RADIO_PARAMS) ); param.dwSize= sizeof(BLUETOOTH_FIND_RADIO_PARAMS); HANDLE hradio= NULL; HBLUETOOTH_RADIO_FIND hfind= NULL; if( hfind= BluetoothFindFirstRadio( ¶m, &hradio ) ){ do{ BLUETOOTH_RADIO_INFO rinfo; memset( &rinfo, 0, sizeof(BLUETOOTH_RADIO_INFO) ); rinfo.dwSize= sizeof(BLUETOOTH_RADIO_INFO); BluetoothGetRadioInfo( hradio, &rinfo ); // rinfo.szName Local Hostname // rinfo.address Local Address ~ CloseHandle( hradio ); }while( BluetoothFindNextRadio( hfind, &hradio ) ); BluetoothFindRadioClose( hfind ); }
これで hRadio や Local の Address が得られます。
ただ Device 関連の API で hRadio が必要な部分はほぼ全部 NULL を
渡すことができるため、あまり必要ないかもしれません。
NULL だと全部の Bluetooth interface が対象。
//Device の検索
BLUETOOTH_DEVICE_SEARCH_PARAMS searchparam;
memset( &searchparam, 0, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) );
searchparam.dwSize= sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
searchparam.fReturnAuthenticated= TRUE;
searchparam.fReturnRemembered= TRUE;
searchparam.fReturnConnected= TRUE;
searchparam.fReturnUnknown= TRUE;
searchparam.fIssueInquiry= FALSE;
searchparam.cTimeoutMultiplier= 0;
searchparam.hRadio= NULL; // all
HBLUETOOTH_DEVICE_FIND hfind= NULL;
BLUETOOTH_DEVICE_INFO devinfo;
memset( &devinfo, 0, sizeof(BLUETOOTH_DEVICE_INFO) );
devinfo.dwSize= sizeof(BLUETOOTH_DEVICE_INFO);
if( hfind= BluetoothFindFirstDevice( &searchparam, &devinfo ) ){
do{
// devinfo.szName デバイス名
// devinfo.Address アドレス
// devinfo.fConnected
// devinfo.fRemembered
// devinfo.fAuthenticated
}while( BluetoothFindNextDevice( hfind, &devinfo ) );
BluetoothFindDeviceClose( hfind );
}
これで Wiiリモコン/バランスWiiボード 等も列挙できます。
新しいデバイスの検出も行う場合は searchparam.fIssueInquiry= TRUE、
searchparam.cTimeoutMultiplier= 10 くらいに。
パスキーは
wchar_t* passkey= ~;
unsigned long passlen= wcslen(passkey)*sizeof(wchar_t);
BluetoothAuthenticateDevice( NULL, NULL, &devinfo, passkey, passlen );
または
BluetoothRegisterForAuthentication() → Callback →
BluetoothSendAuthenticationResponse()
BluetoothUnregisterAuthentication()
の手順で行うようです。Dialog を表示させて手入力も可能。
サービス一覧(enable のものだけ)
BluetoothEnumerateInstalledServices( NULL, &devinfo, &numofbuf, _guidbuf );
サービスの切り替え
BluetoothSetServiceState( hradio, &devinfo, &_guid,
BLUETOOTH_SERVICE_ENABLE or BLUETOOTH_SERVICE_DISABLE );
BluetoothEnumerateInstalledServices() は Enable のものだけ列挙する
ので、EnumerateInstalledServices で取得した GUID に対して
SetServiceState( ~ENABLE ) すると必ずエラーになります。
既に ENABLE なため。DISABLE には使える。
pass key 無しで Device を追加登録する方法がまだわかりません。
参考ページ
・MSDN BluetoothFindFirstRadio Function
・VS2008 \Microsoft SDKs\Windows\v6.0A\Include\BluetoothAPIs.h