diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index d93d88ac..c761bdd2 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -366,6 +366,8 @@ bool BluetoothClassicMedium::StopScanning() { winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added( DeviceWatcher sender, DeviceInformation deviceInfo) { + EnterCriticalSection(&critical_section_); + NEARBY_LOGS(INFO) << "Device added " << winrt::to_string(deviceInfo.Id()); if (IsWatcherStarted()) { // Represents a Bluetooth device. // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothdevice?view=winrt-20348 @@ -378,35 +380,33 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added( // Add to our internal list if necessary if (it != discovered_devices_by_id_.end()) { - // We're already tracking this one + // We're already tracking this one NEARBY_LOGS(INFO) << + // "DeviceWatcher_Added entered critical section."; + + LeaveCriticalSection(&critical_section_); + return winrt::fire_and_forget(); } // Create a bluetooth device out of this id - winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromIdAsync( - deviceInfo.Id()) - .Completed([this, deviceInfo]( - winrt::Windows::Foundation::IAsyncOperation< - winrt::Windows::Devices::Bluetooth::BluetoothDevice> - bluetoothDevice, - winrt::Windows::Foundation::AsyncStatus status) { - EnterCriticalSection(&critical_section_); + auto bluetoothDevice = + winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromIdAsync( + deviceInfo.Id()) + .get(); - auto bluetoothDeviceP = - absl::WrapUnique(new BluetoothDevice(bluetoothDevice.get())); + auto bluetoothDeviceP = + absl::WrapUnique(new BluetoothDevice(bluetoothDevice)); - discovered_devices_by_id_[deviceInfo.Id()] = - std::move(bluetoothDeviceP); + discovered_devices_by_id_[deviceInfo.Id()] = std::move(bluetoothDeviceP); - if (discovery_callback_.device_discovered_cb != nullptr) { - discovery_callback_.device_discovered_cb( - *discovered_devices_by_id_[deviceInfo.Id()]); - } - - LeaveCriticalSection(&critical_section_); - }); + if (discovery_callback_.device_discovered_cb != nullptr) { + discovery_callback_.device_discovered_cb( + *discovered_devices_by_id_[deviceInfo.Id()]); + } } + LeaveCriticalSection(&critical_section_); + return winrt::fire_and_forget(); } @@ -414,6 +414,11 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated( DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { EnterCriticalSection(&critical_section_); + NEARBY_LOGS(INFO) + << "Device updated " + << discovered_devices_by_id_[deviceInfoUpdate.Id()]->GetName() << " (" + << winrt::to_string(deviceInfoUpdate.Id()) << ")"; + if (!IsWatcherStarted()) { // Spurious call, watcher has stopped or wasn't started LeaveCriticalSection(&critical_section_); @@ -442,8 +447,13 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated( winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Removed( DeviceWatcher sender, DeviceInformationUpdate deviceInfo) { EnterCriticalSection(&critical_section_); + NEARBY_LOGS(INFO) << "Device removed " + << discovered_devices_by_id_[deviceInfo.Id()]->GetName() + << " (" << winrt::to_string(deviceInfo.Id()) << ")"; if (!IsWatcherStarted()) { + LeaveCriticalSection(&critical_section_); + return winrt::fire_and_forget(); }