Enhance on Bluetooth discovery check

PiperOrigin-RevId: 691947459
This commit is contained in:
Guogang Li
2024-10-31 15:05:16 -07:00
committed by Copybara-Service
parent 7f7dee54c0
commit f391af8e47
7 changed files with 45 additions and 19 deletions
+3 -3
View File
@@ -99,6 +99,7 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
[this](api::BluetoothDevice& device) {
VLOG(1) << "BT .device_discovered_cb for " << device.GetName();
MutexLock lock(&mutex_);
if (!discovery_enabled_) return;
auto pair = devices_.emplace(
&device, std::make_unique<DeviceDiscoveryInfo>());
auto& context = *pair.first->second;
@@ -111,26 +112,26 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
context.device = BluetoothDevice(&device);
LOG(INFO) << "Adding device=" << context.device.GetMacAddress()
<< ",impl=" << &device;
if (!discovery_enabled_) return;
discovery_callback_.device_discovered_cb(context.device);
},
.device_name_changed_cb =
[this](api::BluetoothDevice& device) {
VLOG(1) << "BT .device_name_changed_cb for " << device.GetName();
MutexLock lock(&mutex_);
if (!discovery_enabled_) return;
// If the device is not already in devices_, we should not be able
// to change its name.
if (devices_.find(&device) == devices_.end()) return;
auto& context = *devices_[&device];
LOG(INFO) << "Renaming device=" << context.device.GetMacAddress()
<< ",impl=" << &device;
if (!discovery_enabled_) return;
discovery_callback_.device_name_changed_cb(context.device);
},
.device_lost_cb =
[this](api::BluetoothDevice& device) {
VLOG(1) << "BT .device_lost_cb for " << device.GetMacAddress();
MutexLock lock(&mutex_);
if (!discovery_enabled_) return;
auto item = devices_.extract(&device);
if (!item) {
LOG(WARNING) << "Removing unknown device: "
@@ -140,7 +141,6 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) {
auto& context = *item.mapped();
LOG(INFO) << "Removing device=" << context.device.GetMacAddress()
<< ",impl=" << &device;
if (!discovery_enabled_) return;
discovery_callback_.device_lost_cb(context.device);
},
});
@@ -191,6 +191,7 @@ bool BluetoothClassicMedium::StopDiscovery() {
if (IsWatcherStarted()) {
result = StopScanning();
discovery_callback_ = {};
}
return result;
@@ -659,8 +660,10 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated(
LOG(INFO) << "Updated device name:"
<< mac_address_to_bluetooth_device_map_[mac_address]->GetName();
discovery_callback_.device_name_changed_cb(
*mac_address_to_bluetooth_device_map_[mac_address]);
if (discovery_callback_.device_name_changed_cb != nullptr) {
discovery_callback_.device_name_changed_cb(
*mac_address_to_bluetooth_device_map_[mac_address]);
}
}
}