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