Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm

Issue 791763005: Added bluetooth LE support on Mac platform (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
armansito 2015/01/24 17:59:38 Is this file missing the copyright header?
dvh 2015/01/27 00:14:55 Done.
2
3 #include "base/mac/mac_util.h"
4
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
6
7 using namespace device;
8
9 namespace device {
10
11 // This class is a helper to call some protected methods in
12 // BluetoothLowEnergyDiscoverManagerMac.
13 class BluetoothLowEnergyDiscoverManagerMacDelegate {
14 public:
15 BluetoothLowEnergyDiscoverManagerMacDelegate(
16 BluetoothLowEnergyDiscoverManagerMac *manager)
17 : manager_(manager) {}
18
19 virtual ~BluetoothLowEnergyDiscoverManagerMacDelegate() {}
20
21 virtual void DiscoveredPeripheral(CBPeripheral *peripheral,
armansito 2015/01/24 17:59:38 nit: This should read "CBPeripheral* peripheral" (
dvh 2015/01/27 00:14:56 Done.
22 NSDictionary *advertisementData, int rssi) {
23 manager_->DiscoveredPeripheral(peripheral, advertisementData, rssi);
24 }
25
26 virtual void TryStartDiscovery() { manager_->TryStartDiscovery(); }
27
28 private:
29 BluetoothLowEnergyDiscoverManagerMac *manager_;
30 };
31
32 } // namespace device
33
34 // This class will serve as the Objective-C delegate of CBCentralManager.
35 @interface BluetoothLowEnergyDiscoverManagerMacBridge
36 : NSObject<CBCentralManagerDelegate>
37
38 - (id)initWithManager:(BluetoothLowEnergyDiscoverManagerMac *)manager;
39
40 @end
41
42 @implementation BluetoothLowEnergyDiscoverManagerMacBridge {
43 BluetoothLowEnergyDiscoverManagerMac *manager_;
44 BluetoothLowEnergyDiscoverManagerMacDelegate *delegate_;
45 }
46
47 - (id)initWithManager:(BluetoothLowEnergyDiscoverManagerMac *)manager {
48 self = [super init];
armansito 2015/01/24 17:59:38 nit: check if init worked here (i.e. set the membe
dvh 2015/01/27 00:14:56 Done.
49 manager_ = manager;
50 delegate_ = new BluetoothLowEnergyDiscoverManagerMacDelegate(manager_);
51 return self;
52 }
53
54 - (void)dealloc {
55 delete delegate_;
56 [super dealloc];
57 }
58
59 - (void)centralManager:(CBCentralManager *)central
60 didDiscoverPeripheral:(CBPeripheral *)peripheral
61 advertisementData:(NSDictionary *)advertisementData
62 RSSI:(NSNumber *)RSSI {
63 // Notifies the discovery of a device.
64 delegate_->DiscoveredPeripheral(peripheral, advertisementData,
65 [RSSI intValue]);
66 }
67
68 - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
69 // Notifies when the powered state of the central manager changed.
70 delegate_->TryStartDiscovery();
71 }
72
73 @end
74
75 BluetoothLowEnergyDiscoverManagerMac::~BluetoothLowEnergyDiscoverManagerMac() {
76 ClearDevices();
77 }
78
79 bool BluetoothLowEnergyDiscoverManagerMac::IsDiscovering() const {
80 return discovering_;
81 }
82
83 void BluetoothLowEnergyDiscoverManagerMac::StartDiscovery(
84 BluetoothDevice::UUIDList services_uuids) {
85 ClearDevices();
86 discovering_ = true;
87 pending_ = true;
88 services_uuids_ = services_uuids;
89 TryStartDiscovery();
90 }
91
92 void BluetoothLowEnergyDiscoverManagerMac::TryStartDiscovery() {
93 if (!discovering_) {
94 return;
95 }
96 if (!pending_) {
97 return;
98 }
99 // Can only start if the bluetooth power is turned on.
100 if ([manager_ state] != CBCentralManagerStatePoweredOn) {
101 return;
102 }
armansito 2015/01/24 17:59:38 Please add new lines where you can to make the cod
dvh 2015/01/27 00:14:56 Done.
103 // Converts the services UUIDs to a CoreBluetooth data structure.
104 NSMutableArray *services = nil;
105 if (services_uuids_.size() > 0) {
armansito 2015/01/24 17:59:38 nit: if (!services_uuids_.empty()) {
dvh 2015/01/27 00:14:55 Done.
106 services = [NSMutableArray array];
107 for (unsigned int i = 0; i < services_uuids_.size(); i++) {
108 NSString *uuidString = [NSString
109 stringWithUTF8String:services_uuids_[i].canonical_value().c_str()];
110 CBUUID *uuid = [CBUUID UUIDWithString:uuidString];
111 [services addObject:uuid];
112 }
113 }
114 [manager_ scanForPeripheralsWithServices:services options:nil];
115 pending_ = false;
116 }
117
118 void BluetoothLowEnergyDiscoverManagerMac::StopDiscovery() {
119 if (!pending_) {
120 [manager_ stopScan];
armansito 2015/01/24 17:59:38 Don't call this if |discovering_| is true?
dvh 2015/01/27 00:14:55 Done.
121 }
122 discovering_ = false;
123 }
124
125 void BluetoothLowEnergyDiscoverManagerMac::DiscoveredPeripheral(
126 CBPeripheral *peripheral, NSDictionary *advertisementData, int rssi) {
127 BluetoothLowEnergyDeviceMac *device =
armansito 2015/01/24 17:59:38 It seems like this is being unnecessarily created
dvh 2015/01/27 00:14:55 The main goal here is to be able to lookup any exi
armansito 2015/01/29 04:37:18 It seems strange to me to construct a whole object
128 new BluetoothLowEnergyDeviceMac(peripheral, advertisementData, rssi);
129 // Look for existing device.
130 std::map<const std::string, BluetoothLowEnergyDeviceMac *>::iterator iter =
131 devices_.find(device->GetIdentifier());
132 if (iter != devices_.end()) {
armansito 2015/01/24 17:59:38 Do an early return and get rid of the else. I'd ac
dvh 2015/01/27 00:14:55 Done.
133 // A device has an update.
134 delete device;
135 BluetoothLowEnergyDeviceMac *old_device = iter->second;
136 old_device->Update(peripheral, advertisementData, rssi);
137 observer_->DeviceUpdated(old_device);
138 } else {
139 // A device has been added.
140 devices_.insert(devices_.begin(),
141 std::pair<const std::string, BluetoothLowEnergyDeviceMac *>(
142 device->GetIdentifier(), device));
143 observer_->DeviceFound(device);
144 }
145 }
146
147 BluetoothLowEnergyDiscoverManagerMac *
148 BluetoothLowEnergyDiscoverManagerMac::Create(Observer *observer) {
149 return new BluetoothLowEnergyDiscoverManagerMac(observer);
150 }
151
152 BluetoothLowEnergyDiscoverManagerMac::BluetoothLowEnergyDiscoverManagerMac(
153 Observer *observer)
154 : observer_(observer) {
155 bridge_.reset([[BluetoothLowEnergyDiscoverManagerMacBridge alloc]
156 initWithManager:this]);
157 // Since CoreBluetooth is only available on OS X 10.7 or later, we
158 // instantiate CBCentralManager only for OS X >= 10.7.
159 if (base::mac::IsOSLionOrLater()) {
160 manager_.reset(
161 [[CBCentralManager alloc] initWithDelegate:bridge_
armansito 2015/01/24 17:59:38 Will this code compile at all in older versions? W
dvh 2015/01/27 00:14:56 The build system of Chromium will build using a re
armansito 2015/01/29 04:37:18 I don't directly work on Chrome for Mac, so as lon
groby-ooo-7-16 2015/01/30 15:10:32 Unless I'm completely misinformed, we're currently
162 queue:dispatch_get_main_queue()]);
163 }
164 discovering_ = false;
165 }
166
167 void BluetoothLowEnergyDiscoverManagerMac::ClearDevices() {
168 for (std::map<const std::string, BluetoothLowEnergyDeviceMac *>::iterator
169 iter = devices_.begin();
170 iter != devices_.end(); ++iter) {
171 delete iter->second;
172 }
173 devices_.clear();
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698