From d16de1c3abadcfaceaae1f53f969a80152e9ed5d Mon Sep 17 00:00:00 2001 From: jfcarroll Date: Mon, 20 Sep 2021 16:42:46 -0700 Subject: [PATCH] Fixed a bug in BluetoothAdapter::SetName, that was causing the loss of the default name. PiperOrigin-RevId: 397871581 --- .../impl/windows/bluetooth_adapter.cc | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/cpp/platform/impl/windows/bluetooth_adapter.cc b/cpp/platform/impl/windows/bluetooth_adapter.cc index 6ae127c4..4402507e 100644 --- a/cpp/platform/impl/windows/bluetooth_adapter.cc +++ b/cpp/platform/impl/windows/bluetooth_adapter.cc @@ -310,21 +310,30 @@ bool BluetoothAdapter::SetName(absl::string_view name) { return false; } - // Sets the data and type of a specified value under a registry key. - // https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetvalueexa - ret = RegSetValueExA( - hKey, // A handle to an open registry key - BLUETOOTH_RADIO_REGISTRY_NAME_KEY, // The name of the value to be set. - 0, // This parameter is reserved and must be zero. - REG_BINARY, // The type of data pointed to by the lpData parameter. - (LPBYTE)std::string(name).c_str(), // The data to be stored. - strlen( - std::string(name).c_str())); // The size of the information pointed - // to by the lpData parameter, in bytes. + if (name != "") { + // Sets the data and type of a specified value under a registry key. + // https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetvalueexa + ret = RegSetValueExA( + hKey, // A handle to an open registry key + BLUETOOTH_RADIO_REGISTRY_NAME_KEY, // The name of the value to be set. + 0, // This parameter is reserved and must be zero. + REG_BINARY, // The type of data pointed to by the lpData parameter. + (LPBYTE)std::string(name).c_str(), // The data to be stored. + strlen(std::string(name) + .c_str())); // The size of the information pointed + // to by the lpData parameter, in bytes. + } else { + // If we are told to set the key to "", we treat this as a reset + // If we delete the key value the OS will default to the system + // name. If we just set it to blank, it will show as blank in all + // system dialogs, this is likely undesireable + ret = RegDeleteValueA(hKey, BLUETOOTH_RADIO_REGISTRY_NAME_KEY); + } if (ret != ERROR_SUCCESS) { NEARBY_LOGS(ERROR) << __func__ - << ": Failed to set registry key. Error code: " << ret; + << ": Failed to set/delete registry key. Error code: " + << ret; return false; }