Chromium Code Reviews| Index: device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm |
| diff --git a/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm b/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm |
| index eeb1ace3beb1996e17dc2abb39103a7ffc787899..ec3afbccb834da73087094055ba33479ef0673d8 100644 |
| --- a/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm |
| +++ b/device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm |
| @@ -23,11 +23,25 @@ bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { |
| } |
| void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( |
| - BluetoothDevice::UUIDList services_uuids) { |
| - discovering_ = true; |
| - pending_ = true; |
| - services_uuids_ = services_uuids; |
| - TryStartDiscovery(); |
| + std::set<BluetoothUUID> services_uuids) { |
|
Jeffrey Yasskin
2016/08/23 21:37:04
Since this doesn't modify the input argument, it s
|
| + // Update Scan UUIDs. |
| + bool should_update_scan = false; |
| + for (const auto& uuid : services_uuids) { |
| + size_t& count = scan_uuids_to_filter_count_[uuid]; |
| + count++; |
| + |
| + if (count == 1) { |
| + should_update_scan = true; |
| + } |
| + } |
| + |
| + // Try to scan only if there are new services or we haven't |
| + // started discovery yet. |
| + if (should_update_scan || !discovering_) { |
|
Jeffrey Yasskin
2016/08/23 21:37:04
If we're not discovering, but there are no service
|
| + pending_ = true; |
| + discovering_ = true; |
| + TryStartDiscovery(); |
| + } |
| } |
| void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { |
| @@ -53,11 +67,13 @@ void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { |
| // Converts the services UUIDs to a CoreBluetooth data structure. |
| NSMutableArray* services = nil; |
| - if (!services_uuids_.empty()) { |
| + if (!scan_uuids_to_filter_count_.empty()) { |
| services = [NSMutableArray array]; |
| - for (auto& service_uuid : services_uuids_) { |
| - NSString* uuidString = |
| - base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); |
| + VLOG(1) << "Scanning for UUIDs: "; |
| + for (const auto& uuid_to_count : scan_uuids_to_filter_count_) { |
| + const std::string& uuid_str = uuid_to_count.first.canonical_value(); |
| + VLOG(1) << uuid_str; |
| + NSString* uuidString = base::SysUTF8ToNSString(uuid_str.c_str()); |
| CBUUID* uuid = [CBUUID UUIDWithString:uuidString]; |
| [services addObject:uuid]; |
| } |
| @@ -76,8 +92,28 @@ void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { |
| pending_ = false; |
| } |
| +void BluetoothLowEnergyDiscoveryManagerMac::RemoveDiscoveryUUIDs( |
| + std::set<BluetoothUUID> uuids_to_remove) { |
| + bool did_remove_service = false; |
| + for (const auto& uuid_to_remove : uuids_to_remove) { |
| + auto it = scan_uuids_to_filter_count_.find(uuid_to_remove); |
| + DCHECK(it != scan_uuids_to_filter_count_.end()); |
| + DCHECK_LT(0u, it->second); |
| + it->second--; |
| + if (it->second == 0) { |
| + scan_uuids_to_filter_count_.erase(it); |
| + did_remove_service = true; |
| + } |
| + } |
| + if (did_remove_service) { |
| + pending_ = true; |
| + TryStartDiscovery(); |
|
Jeffrey Yasskin
2016/08/23 21:37:04
Shouldn't RemoveDiscoveryUUIDs stop discovery when
|
| + } |
| +} |
| + |
| void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { |
| VLOG(1) << "StopDiscovery"; |
| + scan_uuids_to_filter_count_.clear(); |
| if (discovering_ && !pending_) { |
| [central_manager_ stopScan]; |
| } |