Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h" | |
| 6 | |
| 7 #include "base/mac/mac_util.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 10 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | |
| 11 | |
| 12 using namespace device; | |
| 13 | |
| 14 namespace device { | |
| 15 | |
| 16 // This class is a helper to call some protected methods in | |
| 17 // BluetoothLowEnergyDiscoveryManagerMac. | |
| 18 class BluetoothLowEnergyDiscoveryManagerMacDelegate { | |
| 19 public: | |
| 20 BluetoothLowEnergyDiscoveryManagerMacDelegate( | |
| 21 BluetoothLowEnergyDiscoveryManagerMac* manager) | |
| 22 : manager_(manager) {} | |
| 23 | |
| 24 virtual ~BluetoothLowEnergyDiscoveryManagerMacDelegate() {} | |
| 25 | |
| 26 virtual void DiscoveredPeripheral(CBPeripheral* peripheral, | |
| 27 NSDictionary* advertisementData, | |
| 28 int rssi) { | |
| 29 manager_->DiscoveredPeripheral(peripheral, advertisementData, rssi); | |
| 30 } | |
| 31 | |
| 32 virtual void TryStartDiscovery() { manager_->TryStartDiscovery(); } | |
| 33 | |
| 34 private: | |
| 35 BluetoothLowEnergyDiscoveryManagerMac* manager_; | |
| 36 }; | |
| 37 | |
| 38 } // namespace device | |
| 39 | |
| 40 // This class will serve as the Objective-C delegate of CBCentralManager. | |
| 41 @interface BluetoothLowEnergyDiscoveryManagerMacBridge | |
| 42 : NSObject<CBCentralManagerDelegate> | |
| 43 | |
| 44 - (id)initWithManager:(BluetoothLowEnergyDiscoveryManagerMac*)manager; | |
| 45 | |
| 46 @end | |
| 47 | |
| 48 @implementation BluetoothLowEnergyDiscoveryManagerMacBridge { | |
| 49 BluetoothLowEnergyDiscoveryManagerMac* manager_; | |
| 50 scoped_ptr<BluetoothLowEnergyDiscoveryManagerMacDelegate> delegate_; | |
| 51 } | |
| 52 | |
| 53 - (id)initWithManager:(BluetoothLowEnergyDiscoveryManagerMac*)manager { | |
| 54 if ((self = [super init])) { | |
| 55 manager_ = manager; | |
| 56 delegate_.reset( | |
| 57 new BluetoothLowEnergyDiscoveryManagerMacDelegate(manager_)); | |
| 58 } | |
| 59 return self; | |
| 60 } | |
| 61 | |
| 62 - (void)dealloc { | |
| 63 [super dealloc]; | |
| 64 } | |
|
Avi (use Gerrit)
2015/01/30 23:18:31
You don't need to override -dealloc if all you're
dvh
2015/01/30 23:57:55
Done.
| |
| 65 | |
| 66 - (void)centralManager:(CBCentralManager*)central | |
| 67 didDiscoverPeripheral:(CBPeripheral*)peripheral | |
| 68 advertisementData:(NSDictionary*)advertisementData | |
| 69 RSSI:(NSNumber*)RSSI { | |
| 70 // Notifies the discovery of a device. | |
| 71 delegate_->DiscoveredPeripheral(peripheral, advertisementData, | |
| 72 [RSSI intValue]); | |
| 73 } | |
| 74 | |
| 75 - (void)centralManagerDidUpdateState:(CBCentralManager*)central { | |
| 76 // Notifies when the powered state of the central manager changed. | |
| 77 delegate_->TryStartDiscovery(); | |
| 78 } | |
| 79 | |
| 80 @end | |
| 81 | |
| 82 BluetoothLowEnergyDiscoveryManagerMac:: | |
| 83 ~BluetoothLowEnergyDiscoveryManagerMac() { | |
| 84 ClearDevices(); | |
| 85 } | |
| 86 | |
| 87 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { | |
| 88 return discovering_; | |
| 89 } | |
| 90 | |
| 91 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( | |
| 92 BluetoothDevice::UUIDList services_uuids) { | |
| 93 ClearDevices(); | |
| 94 discovering_ = true; | |
| 95 pending_ = true; | |
| 96 services_uuids_ = services_uuids; | |
| 97 TryStartDiscovery(); | |
| 98 } | |
| 99 | |
| 100 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { | |
| 101 if (!discovering_) { | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 if (!pending_) { | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 // Can only start if the bluetooth power is turned on. | |
| 110 if ([manager_ state] != CBCentralManagerStatePoweredOn) { | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 // Converts the services UUIDs to a CoreBluetooth data structure. | |
| 115 NSMutableArray* services = nil; | |
| 116 if (!services_uuids_.empty()) { | |
| 117 services = [NSMutableArray array]; | |
| 118 for (auto& service_uuid : services_uuids_) { | |
| 119 NSString* uuidString = | |
| 120 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); | |
| 121 CBUUID* uuid = [CBUUID UUIDWithString:uuidString]; | |
| 122 [services addObject:uuid]; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 [manager_ scanForPeripheralsWithServices:services options:nil]; | |
| 127 pending_ = false; | |
| 128 } | |
| 129 | |
| 130 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { | |
| 131 if (discovering_ && !pending_) { | |
| 132 [manager_ stopScan]; | |
| 133 } | |
| 134 discovering_ = false; | |
| 135 } | |
| 136 | |
| 137 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral( | |
| 138 CBPeripheral* peripheral, | |
| 139 NSDictionary* advertisementData, | |
| 140 int rssi) { | |
| 141 // Look for existing device. | |
| 142 auto iter = devices_.find( | |
| 143 BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(peripheral)); | |
| 144 if (iter == devices_.end()) { | |
| 145 // A device has been added. | |
| 146 BluetoothLowEnergyDeviceMac* device = | |
| 147 new BluetoothLowEnergyDeviceMac(peripheral, advertisementData, rssi); | |
| 148 devices_.insert(devices_.begin(), | |
| 149 std::make_pair(device->GetIdentifier(), device)); | |
| 150 observer_->DeviceFound(device); | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 // A device has an update. | |
| 155 BluetoothLowEnergyDeviceMac* old_device = iter->second; | |
| 156 old_device->Update(peripheral, advertisementData, rssi); | |
| 157 observer_->DeviceUpdated(old_device); | |
| 158 } | |
| 159 | |
| 160 BluetoothLowEnergyDiscoveryManagerMac* | |
| 161 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) { | |
| 162 return new BluetoothLowEnergyDiscoveryManagerMac(observer); | |
| 163 } | |
| 164 | |
| 165 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac( | |
| 166 Observer* observer) | |
| 167 : observer_(observer) { | |
| 168 bridge_.reset([[BluetoothLowEnergyDiscoveryManagerMacBridge alloc] | |
| 169 initWithManager:this]); | |
| 170 // Since CoreBluetooth is only available on OS X 10.7 or later, we | |
| 171 // instantiate CBCentralManager only for OS X >= 10.7. | |
| 172 if (base::mac::IsOSLionOrLater()) { | |
| 173 manager_.reset( | |
| 174 [[CBCentralManager alloc] initWithDelegate:bridge_ | |
| 175 queue:dispatch_get_main_queue()]); | |
| 176 } | |
| 177 discovering_ = false; | |
| 178 } | |
| 179 | |
| 180 void BluetoothLowEnergyDiscoveryManagerMac::ClearDevices() { | |
| 181 STLDeleteValues(&devices_); | |
| 182 } | |
| OLD | NEW |