Reduce the frequency of redundant Bluetooth Classic operations

PiperOrigin-RevId: 458057650
This commit is contained in:
aaronyujiaze
2022-06-29 14:05:41 -07:00
committed by Copybara-Service
parent 9edad3a803
commit ff9a986595
2 changed files with 21 additions and 0 deletions
@@ -47,6 +47,8 @@ namespace location {
namespace nearby {
namespace windows {
constexpr uint8_t kAndroidDiscoverableBluetoothNameMaxLength = 37; // bytes
BluetoothAdapter::BluetoothAdapter() : windows_bluetooth_adapter_(nullptr) {
windows_bluetooth_adapter_ =
winrt::Windows::Devices::Bluetooth::BluetoothAdapter::GetDefaultAsync()
@@ -188,6 +190,22 @@ bool BluetoothAdapter::SetName(absl::string_view name) {
return false;
}
if (name.size() > kAndroidDiscoverableBluetoothNameMaxLength * sizeof(char)) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to set name for bluetooth adapter because "
"Android cannot discover Windows bluetooth device "
"name that exceeded the 37 bytes limit (11 "
"characters in EndpointInfo).";
return false;
}
if (registry_bluetooth_adapter_name_ == name) {
NEARBY_LOGS(INFO)
<< __func__
<< ": Tried to set name for bluetooth adapter to the same name again.";
return true;
}
std::string instance_id(GetGenericBluetoothAdapterInstanceID());
if (instance_id.empty()) {
@@ -401,6 +419,8 @@ bool BluetoothAdapter::SetName(absl::string_view name) {
return false;
}
registry_bluetooth_adapter_name_ = std::string(name);
return true;
}
@@ -97,6 +97,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
private:
void process_error();
WindowsBluetoothAdapter windows_bluetooth_adapter_;
std::string registry_bluetooth_adapter_name_;
IRadio windows_bluetooth_radio_;
char *GetGenericBluetoothAdapterInstanceID(void) const;