Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
Listing 2.4. Dobieranie w pary urządzeń Bluetooth z wykorzystaniem
wspólnego kodu
#include <iostream>
#pragma option push -a1
#include <winsock2>
#include "Ws2bth.h"
#include "BluetoothAPIs.h"
#pragma option pop
#pragma comment(lib, "Bthprops.lib")
using namespace std;
BLUETOOTH_FIND_RADIO_PARAMS pbtfrp = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};
BLUETOOTH_RADIO_INFO pRadioInfo = {
sizeof(BLUETOOTH_RADIO_INFO), 0,
};
BLUETOOTH_DEVICE_SEARCH_PARAMS pbtsp = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
TRUE, TRUE, TRUE, TRUE, TRUE, 5 /*6.14 sek*/, NULL
};
BLUETOOTH_DEVICE_INFO pbtdi = {
sizeof(BLUETOOTH_DEVICE_INFO), 0,
};
HANDLE phRadio = NULL;
HBLUETOOTH_RADIO_FIND bthRadioFind = NULL;
HBLUETOOTH_DEVICE_FIND hbthDeviceFind = NULL;
//----------------------------------------------------
void showError()
66
Bluetooth. Praktyczne programowanie
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), 0, (LPTSTR)
&lpMsgBuf, 0, NULL );
fprintf(stderr, "\n%s\n", lpMsgBuf);
free(lpMsgBuf);
}
//----------------------------------------------------
BOOL __cdecl pfnCallback(LPVOID pvParam,
PBLUETOOTH_DEVICE_INFO pDevice)
{
HANDLE hRadio = (HANDLE)pvParam;
WCHAR temp[15] = {0};
LPWSTR pszPasskey = temp;
for (int j = 0; j < 8; j+=2) {
((PUCHAR)pszPasskey)[j] = '1';
}
if (ERROR_SUCCESS ==
BluetoothSendAuthenticationResponse(hRadio,
pDevice, pszPasskey)){
wprintf(L"\nPIN (hasło) wysłane(y) przez "\
" urządzenie: %20s\n", pszPasskey);
system("PAUSE");
return TRUE;
}
else {
showError();
return FALSE;
}
}
//----------------------------------------------------
int main() {
bthRadioFind = BluetoothFindFirstRadio(&pbtfrp,
&phRadio);
int radioNumber = 0;
do {
radioNumber++;
Detekcja urządzeń. Część I
67
BluetoothGetRadioInfo(phRadio, &pRadioInfo);
wprintf(L"Radio %d:\n", radioNumber);
wprintf(L"\tRadio Name: %s\n",
pRadioInfo.szName);
wprintf(L"\tAddress:
%02x:%02x:%02x:%02x:%02x:%02x\n",
pRadioInfo.address.rgBytes[5],
pRadioInfo.address.rgBytes[4],
pRadioInfo.address.rgBytes[3],
pRadioInfo.address.rgBytes[2],
pRadioInfo.address.rgBytes[1],
pRadioInfo.address.rgBytes[0]);
wprintf(L"\tSubversion: 0x%08x\n",
pRadioInfo.lmpSubversion);
wprintf(L"\tClass: 0x%08x\n",
pRadioInfo.ulClassofDevice);
wprintf(L"\tManufacturer: 0x%04x\n",
pRadioInfo.manufacturer);
pbtsp.hRadio = phRadio;
memset(&pbtdi, 0, sizeof(BLUETOOTH_DEVICE_INFO));
pbtdi.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
hbthDeviceFind = BluetoothFindFirstDevice(&pbtsp,
&pbtdi);
int deviceNumber = 0;
do {
deviceNumber++;
wprintf(L"\t\Device %d:\n", deviceNumber);
wprintf(L"\t\tName: %s\n", pbtdi.szName);
wprintf(L"\t\tAddress:
%02x:%02x:%02x:%02x:%02x:%02x\n",
pbtdi.Address.rgBytes[5],
pbtdi.Address.rgBytes[4],
pbtdi.Address.rgBytes[3],
pbtdi.Address.rgBytes[2],
pbtdi.Address.rgBytes[1],
pbtdi.Address.rgBytes[0]);
wprintf(L"\t\tClass: 0x%08x\n",
pbtdi.ulClassofDevice);
wprintf(L"\t\tConnected: %s\n",
68
Bluetooth. Praktyczne programowanie
pbtdi.fConnected ? L"true" \
: L"false");
wprintf(L"\t\tAuthenticated: %s\n",
pbtdi.fAuthenticated ? \
L"true" : L"false");
wprintf(L"\t\tRemembered: %s\n",
pbtdi.fRemembered ? L"true"\
: L"false");
HBLUETOOTH_AUTHENTICATION_REGISTRATION
phRegHandle;
if (ERROR_SUCCESS !=
BluetoothRegisterForAuthentication(&pbtdi,
&phRegHandle, pfnCallback, phRadio)) {
//showError();
}
LPWSTR pszPasskey;
for (int j = 0; j < 8; j+=2) {
((PUCHAR)pszPasskey)[j] = '1';
}
ULONG ulPasskeyLength = 4;
wprintf(L"\n\t\t\Podaj PIN lub hasło w
urządzeniu: %10s\n", pszPasskey);
DWORD result =
BluetoothAuthenticateDevice(NULL,
phRadio, &pbtdi, pszPasskey,
ulPasskeyLength);
if (result == ERROR_NO_MORE_ITEMS) {
printf("\n\t\t%s\n", "Urządzenie zostało
wcześniej zidentyfikowane\n");
}
else if(result != ERROR_SUCCESS) {
showError();
}
} while(BluetoothFindNextDevice(hbthDeviceFind,
&pbtdi));
BluetoothFindDeviceClose(hbthDeviceFind);