Upstream changes for NetworkQualityProvider
[chromium-blink-merge.git] / device / bluetooth / bluetooth_low_energy_discovery_manager_mac.mm
blobceab7924a1246b5fbb9442b4077c07d386ff474a
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/sdk_forward_declarations.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "device/bluetooth/bluetooth_adapter_mac.h"
12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
14 namespace device {
16 BluetoothLowEnergyDiscoveryManagerMac::
17     ~BluetoothLowEnergyDiscoveryManagerMac() {
20 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const {
21   return discovering_;
24 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery(
25     BluetoothDevice::UUIDList services_uuids) {
26   discovering_ = true;
27   pending_ = true;
28   services_uuids_ = services_uuids;
29   TryStartDiscovery();
32 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() {
33   if (!discovering_) {
34     return;
35   }
37   if (!pending_) {
38     return;
39   }
41   // Can only start if the bluetooth power is turned on.
42   if (!central_manager_ ||
43       [central_manager_ state] != CBCentralManagerStatePoweredOn) {
44     return;
45   }
47   // Converts the services UUIDs to a CoreBluetooth data structure.
48   NSMutableArray* services = nil;
49   if (!services_uuids_.empty()) {
50     services = [NSMutableArray array];
51     for (auto& service_uuid : services_uuids_) {
52       NSString* uuidString =
53           base::SysUTF8ToNSString(service_uuid.canonical_value().c_str());
54       Class aClass = NSClassFromString(@"CBUUID");
55       CBUUID* uuid = [aClass UUIDWithString:uuidString];
56       [services addObject:uuid];
57     }
58   };
60   [central_manager_ scanForPeripheralsWithServices:services options:nil];
61   pending_ = false;
64 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
65   if (discovering_ && !pending_) {
66     [central_manager_ stopScan];
67   }
68   discovering_ = false;
71 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager(
72     CBCentralManager* central_manager) {
73   central_manager_ = central_manager;
76 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral(
77     CBPeripheral* peripheral,
78     NSDictionary* advertisementData,
79     int rssi) {
80   observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi);
83 BluetoothLowEnergyDiscoveryManagerMac*
84 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) {
85   return new BluetoothLowEnergyDiscoveryManagerMac(observer);
88 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
89     Observer* observer)
90     : observer_(observer) {
91   DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
92   discovering_ = false;
95 }  // namespace device