Bluetoothデバイスの列挙
概要
リファレンス:Bluetooth
WindowsではBluetooth用のAPIが用意されているので、それらを用いれば検出自体は簡単にできる。次のヘッダーとライブラリが必要。
#include <Windows.h> #include <BluetoothAPIs.h> #pragma comment(lib, "Bthprops")
自分が持っているBluetooth機器の列挙
BLUETOOTH_FIND_RADIO_PARAMS param = { 0 };
param.dwSize = sizeof(BLUETOOTH_FIND_RADIO_PARAMS);
HANDLE radio = 0;
HBLUETOOTH_RADIO_FIND find = BluetoothFindFirstRadio(¶m, &radio);
if (find) {
do {
BLUETOOTH_RADIO_INFO info = { 0 };
info.dwSize = sizeof(BLUETOOTH_RADIO_INFO);
BluetoothGetRadioInfo(radio, &info);
wcout << info.szName << endl;
CloseHandle(radio);
} while(BluetoothFindNextRadio(find, &radio));
}
周辺にある機器や、ペア済みの機器の列挙
BLUETOOTH_DEVICE_SEARCH_PARAMSの設定で列挙する条件を設定する。ポイントは次のとおり。
- cTimeoutMultiplierが短いと機器を見つけられない(とりあえず5〜10)
- 機器によっては機器側で「検出の許可」を設定しなければならない
iPod touch 4ではBluetoothをONにすれば検出できるが、Android(Xperia arc)ではBluetooth設定で検出可能にしないと検出できない。また、Windowsも既定では検出を許可されていないないため、機器側から見つけるには許可を設定する必要がある。
BLUETOOTH_DEVICE_SEARCH_PARAMS params = { 0 };
params.fReturnAuthenticated = TRUE;
params.fReturnRemembered = TRUE;
params.fReturnUnknown = TRUE;
params.fReturnConnected = TRUE;
params.fIssueInquiry = TRUE;
params.cTimeoutMultiplier = 5;
params.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
BLUETOOTH_DEVICE_INFO info = {0};
info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
HBLUETOOTH_DEVICE_FIND find = BluetoothFindFirstDevice(¶ms, &info);
if (find) {
do {
wcout << info.szName << endl;
} while(BluetoothFindNextDevice(find, &info));
BluetoothFindDeviceClose(find);
}