diff --git a/internal/platform/implementation/windows/bluetooth_adapter.cc b/internal/platform/implementation/windows/bluetooth_adapter.cc index 0c2675e3..7f9d143c 100644 --- a/internal/platform/implementation/windows/bluetooth_adapter.cc +++ b/internal/platform/implementation/windows/bluetooth_adapter.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2020-2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -32,7 +32,9 @@ #include #include +#include #include +#include #include #include "absl/strings/str_format.h" @@ -342,14 +344,15 @@ std::string BluetoothAdapter::GetName() const { return *device_name_; } - char *_instance_id = GetGenericBluetoothAdapterInstanceID(); - if (_instance_id == nullptr) { + std::optional adapter_instance_id = + GetGenericBluetoothAdapterInstanceID(); + if (!adapter_instance_id.has_value()) { NEARBY_LOGS(ERROR) << __func__ << ": Failed to get Generic Bluetooth Adapter InstanceID"; return std::string(); } - std::string instance_id(_instance_id); + std::string instance_id = *adapter_instance_id; // Change radio module local name in registry HKEY hKey; @@ -435,13 +438,16 @@ bool BluetoothAdapter::SetName(absl::string_view name, bool persist) { return true; } - std::string instance_id(GetGenericBluetoothAdapterInstanceID()); + std::optional adapter_instance_id = + GetGenericBluetoothAdapterInstanceID(); - if (instance_id.empty()) { + if (!adapter_instance_id.has_value()) { NEARBY_LOGS(ERROR) << __func__ << ": Failed to get Generic Bluetooth Adapter InstanceID"; return false; } + + std::string instance_id = *adapter_instance_id; // defined in usbiodef.h const GUID guid = GUID_DEVINTERFACE_USB_DEVICE; @@ -697,7 +703,8 @@ void BluetoothAdapter::find_and_replace(char *source, const char *strFind, memcpy(source, s.c_str(), s.size()); } -char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const { +std::optional +BluetoothAdapter::GetGenericBluetoothAdapterInstanceID() const { unsigned i; CONFIGRET r; HDEVINFO hDevInfo; @@ -715,7 +722,7 @@ char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const { if (hDevInfo == INVALID_HANDLE_VALUE) { NEARBY_LOGS(ERROR) << __func__ << ": Could not find BluetoothDevice on this machine"; - return NULL; + return std::nullopt; } // Get first Generic Bluetooth Adapter InstanceID @@ -741,14 +748,15 @@ char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const { // computer's USB ports. // https://docs.microsoft.com/en-us/windows-hardware/drivers/bluetooth/bluetooth-host-radio-support if (strncmp("USB", deviceInstanceID, 3) == 0) { - return deviceInstanceID; + SetupDiDestroyDeviceInfoList(hDevInfo); + return std::string(deviceInstanceID); } } NEARBY_LOGS(ERROR) << __func__ << ": Failed to get the generic bluetooth adapter id"; - - return NULL; + SetupDiDestroyDeviceInfoList(hDevInfo); + return std::nullopt; } // Returns BT MAC address assigned to this adapter. diff --git a/internal/platform/implementation/windows/bluetooth_adapter.h b/internal/platform/implementation/windows/bluetooth_adapter.h index 9d5fbc0f..d1e6a001 100644 --- a/internal/platform/implementation/windows/bluetooth_adapter.h +++ b/internal/platform/implementation/windows/bluetooth_adapter.h @@ -109,7 +109,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { std::string registry_bluetooth_adapter_name_; IRadio windows_bluetooth_radio_; - char *GetGenericBluetoothAdapterInstanceID() const; + std::optional GetGenericBluetoothAdapterInstanceID() const; void find_and_replace(char *source, const char *strFind, const char *strReplace) const; ScanMode scan_mode_ = ScanMode::kNone;