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

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

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

Powered by Google App Engine
This is Rietveld 408576698