Improve pairing of RVL-CNT-01-TR

and stop leaking windows handles.
This commit is contained in:
Squall-Leonhart 2026-03-28 23:08:18 +11:00
parent 0ca9121cd3
commit d31ccf28f2

View File

@ -157,6 +157,7 @@ void PairingDialog::WorkerThread()
DWORD result = BluetoothGetRadioInfo(radio, &radioInfo); DWORD result = BluetoothGetRadioInfo(radio, &radioInfo);
if (result != ERROR_SUCCESS) if (result != ERROR_SUCCESS)
{ {
CloseHandle(radio);
UpdateCallback(PairingState::NoBluetoothAvailable); UpdateCallback(PairingState::NoBluetoothAvailable);
return; return;
} }
@ -165,8 +166,8 @@ void PairingDialog::WorkerThread()
{ {
.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS), .dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
.fReturnAuthenticated = FALSE, .fReturnAuthenticated = TRUE,
.fReturnRemembered = FALSE, .fReturnRemembered = TRUE,
.fReturnUnknown = TRUE, .fReturnUnknown = TRUE,
.fReturnConnected = FALSE, .fReturnConnected = FALSE,
@ -184,22 +185,51 @@ void PairingDialog::WorkerThread()
HBLUETOOTH_DEVICE_FIND deviceFind = BluetoothFindFirstDevice(&searchParams, &info); HBLUETOOTH_DEVICE_FIND deviceFind = BluetoothFindFirstDevice(&searchParams, &info);
if (deviceFind == nullptr) if (deviceFind == nullptr)
{ {
CloseHandle(radio);
UpdateCallback(PairingState::SearchFailed); UpdateCallback(PairingState::SearchFailed);
return; return;
} }
while (!m_threadShouldQuit) while (!m_threadShouldQuit)
{ {
if (info.szName == wiimoteName || info.szName == wiiUProControllerName) if (wcscmp(info.szName, wiimoteName.c_str()) == 0 || wcscmp(info.szName, wiiUProControllerName.c_str()) == 0)
{ {
BluetoothFindDeviceClose(deviceFind); BluetoothFindDeviceClose(deviceFind);
if (info.fAuthenticated)
{
DWORD bthResult = BluetoothSetServiceState(radio, &info, &bthHidGuid, BLUETOOTH_SERVICE_ENABLE);
if (bthResult != ERROR_SUCCESS)
{
CloseHandle(radio);
UpdateCallback(PairingState::PairingFailed);
return;
}
CloseHandle(radio);
UpdateCallback(PairingState::Finished);
return;
}
if (info.fRemembered && !info.fAuthenticated)
{
BluetoothRemoveDevice(&info.Address);
Sleep(500);
}
UpdateCallback(PairingState::Pairing); UpdateCallback(PairingState::Pairing);
wchar_t passwd[6] = {radioInfo.address.rgBytes[0], radioInfo.address.rgBytes[1], radioInfo.address.rgBytes[2], radioInfo.address.rgBytes[3], radioInfo.address.rgBytes[4], radioInfo.address.rgBytes[5]}; wchar_t passwd[6] = {radioInfo.address.rgBytes[0], radioInfo.address.rgBytes[1], radioInfo.address.rgBytes[2], radioInfo.address.rgBytes[3], radioInfo.address.rgBytes[4], radioInfo.address.rgBytes[5]};
DWORD bthResult = BluetoothAuthenticateDevice(nullptr, radio, &info, passwd, 6); DWORD bthResult = BluetoothAuthenticateDevice(nullptr, radio, &info, passwd, 6);
if (bthResult != ERROR_SUCCESS) if (bthResult != ERROR_SUCCESS)
{ {
wchar_t passwd2[6] = {info.Address.rgBytes[0], info.Address.rgBytes[1], info.Address.rgBytes[2], info.Address.rgBytes[3], info.Address.rgBytes[4], info.Address.rgBytes[5]};
bthResult = BluetoothAuthenticateDevice(nullptr, radio, &info, passwd2, 6);
}
if (bthResult != ERROR_SUCCESS)
{
CloseHandle(radio);
UpdateCallback(PairingState::PairingFailed); UpdateCallback(PairingState::PairingFailed);
return; return;
} }
@ -207,10 +237,12 @@ void PairingDialog::WorkerThread()
bthResult = BluetoothSetServiceState(radio, &info, &bthHidGuid, BLUETOOTH_SERVICE_ENABLE); bthResult = BluetoothSetServiceState(radio, &info, &bthHidGuid, BLUETOOTH_SERVICE_ENABLE);
if (bthResult != ERROR_SUCCESS) if (bthResult != ERROR_SUCCESS)
{ {
CloseHandle(radio);
UpdateCallback(PairingState::PairingFailed); UpdateCallback(PairingState::PairingFailed);
return; return;
} }
CloseHandle(radio);
UpdateCallback(PairingState::Finished); UpdateCallback(PairingState::Finished);
return; return;
} }
@ -224,6 +256,8 @@ void PairingDialog::WorkerThread()
BluetoothFindDeviceClose(deviceFind); BluetoothFindDeviceClose(deviceFind);
} }
CloseHandle(radio);
} }
#elif defined(HAS_BLUEZ) #elif defined(HAS_BLUEZ)
void PairingDialog::WorkerThread() void PairingDialog::WorkerThread()