OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
6 | 6 |
7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/mac/mac_util.h" |
16 #include "base/mac/sdk_forward_declarations.h" | 17 #include "base/mac/sdk_forward_declarations.h" |
17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
18 #include "base/profiler/scoped_tracker.h" | 19 #include "base/profiler/scoped_tracker.h" |
19 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
20 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
21 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
22 #include "base/thread_task_runner_handle.h" | 23 #include "base/thread_task_runner_handle.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
24 #include "device/bluetooth/bluetooth_classic_device_mac.h" | 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
25 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
(...skipping 25 matching lines...) Expand all Loading... |
51 adapter->Init(); | 52 adapter->Init(); |
52 return adapter->weak_ptr_factory_.GetWeakPtr(); | 53 return adapter->weak_ptr_factory_.GetWeakPtr(); |
53 } | 54 } |
54 | 55 |
55 BluetoothAdapterMac::BluetoothAdapterMac() | 56 BluetoothAdapterMac::BluetoothAdapterMac() |
56 : BluetoothAdapter(), | 57 : BluetoothAdapter(), |
57 powered_(false), | 58 powered_(false), |
58 num_discovery_sessions_(0), | 59 num_discovery_sessions_(0), |
59 classic_discovery_manager_( | 60 classic_discovery_manager_( |
60 BluetoothDiscoveryManagerMac::CreateClassic(this)), | 61 BluetoothDiscoveryManagerMac::CreateClassic(this)), |
61 low_energy_discovery_manager_( | |
62 BluetoothLowEnergyDiscoveryManagerMac::Create(this)), | |
63 weak_ptr_factory_(this) { | 62 weak_ptr_factory_(this) { |
| 63 if (IsLowEnergyAvailable()) |
| 64 low_energy_discovery_manager_.reset( |
| 65 BluetoothLowEnergyDiscoveryManagerMac::Create(this)); |
64 DCHECK(classic_discovery_manager_.get()); | 66 DCHECK(classic_discovery_manager_.get()); |
65 } | 67 } |
66 | 68 |
67 BluetoothAdapterMac::~BluetoothAdapterMac() { | 69 BluetoothAdapterMac::~BluetoothAdapterMac() { |
68 } | 70 } |
69 | 71 |
70 std::string BluetoothAdapterMac::GetAddress() const { | 72 std::string BluetoothAdapterMac::GetAddress() const { |
71 return address_; | 73 return address_; |
72 } | 74 } |
73 | 75 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 107 } |
106 | 108 |
107 void BluetoothAdapterMac::SetDiscoverable( | 109 void BluetoothAdapterMac::SetDiscoverable( |
108 bool discoverable, | 110 bool discoverable, |
109 const base::Closure& callback, | 111 const base::Closure& callback, |
110 const ErrorCallback& error_callback) { | 112 const ErrorCallback& error_callback) { |
111 NOTIMPLEMENTED(); | 113 NOTIMPLEMENTED(); |
112 } | 114 } |
113 | 115 |
114 bool BluetoothAdapterMac::IsDiscovering() const { | 116 bool BluetoothAdapterMac::IsDiscovering() const { |
115 return (classic_discovery_manager_->IsDiscovering() || | 117 bool is_discovering = classic_discovery_manager_->IsDiscovering(); |
116 low_energy_discovery_manager_->IsDiscovering()); | 118 if (IsLowEnergyAvailable()) |
| 119 is_discovering = |
| 120 is_discovering || low_energy_discovery_manager_->IsDiscovering(); |
| 121 return is_discovering; |
117 } | 122 } |
118 | 123 |
119 void BluetoothAdapterMac::CreateRfcommService( | 124 void BluetoothAdapterMac::CreateRfcommService( |
120 const BluetoothUUID& uuid, | 125 const BluetoothUUID& uuid, |
121 const ServiceOptions& options, | 126 const ServiceOptions& options, |
122 const CreateServiceCallback& callback, | 127 const CreateServiceCallback& callback, |
123 const CreateServiceErrorCallback& error_callback) { | 128 const CreateServiceErrorCallback& error_callback) { |
124 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); | 129 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); |
125 socket->ListenUsingRfcomm( | 130 socket->ListenUsingRfcomm( |
126 this, uuid, options, base::Bind(callback, socket), error_callback); | 131 this, uuid, options, base::Bind(callback, socket), error_callback); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 173 } |
169 | 174 |
170 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { | 175 void BluetoothAdapterMac::DeviceConnected(IOBluetoothDevice* device) { |
171 // TODO(isherman): Investigate whether this method can be replaced with a call | 176 // TODO(isherman): Investigate whether this method can be replaced with a call |
172 // to +registerForConnectNotifications:selector:. | 177 // to +registerForConnectNotifications:selector:. |
173 DVLOG(1) << "Adapter registered a new connection from device with address: " | 178 DVLOG(1) << "Adapter registered a new connection from device with address: " |
174 << BluetoothClassicDeviceMac::GetDeviceAddress(device); | 179 << BluetoothClassicDeviceMac::GetDeviceAddress(device); |
175 ClassicDeviceAdded(device); | 180 ClassicDeviceAdded(device); |
176 } | 181 } |
177 | 182 |
| 183 // static |
| 184 bool BluetoothAdapterMac::IsLowEnergyAvailable() { |
| 185 return base::mac::IsOSYosemiteOrLater(); |
| 186 } |
| 187 |
178 void BluetoothAdapterMac::RemovePairingDelegateInternal( | 188 void BluetoothAdapterMac::RemovePairingDelegateInternal( |
179 BluetoothDevice::PairingDelegate* pairing_delegate) { | 189 BluetoothDevice::PairingDelegate* pairing_delegate) { |
180 } | 190 } |
181 | 191 |
182 void BluetoothAdapterMac::AddDiscoverySession( | 192 void BluetoothAdapterMac::AddDiscoverySession( |
183 BluetoothDiscoveryFilter* discovery_filter, | 193 BluetoothDiscoveryFilter* discovery_filter, |
184 const base::Closure& callback, | 194 const base::Closure& callback, |
185 const ErrorCallback& error_callback) { | 195 const ErrorCallback& error_callback) { |
186 DVLOG(1) << __func__; | 196 DVLOG(1) << __func__; |
187 if (num_discovery_sessions_ > 0) { | 197 if (num_discovery_sessions_ > 0) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 transport = discovery_filter->GetTransport(); | 249 transport = discovery_filter->GetTransport(); |
240 | 250 |
241 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) { | 251 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) { |
242 if (!classic_discovery_manager_->StopDiscovery()) { | 252 if (!classic_discovery_manager_->StopDiscovery()) { |
243 DVLOG(1) << "Failed to stop classic discovery"; | 253 DVLOG(1) << "Failed to stop classic discovery"; |
244 error_callback.Run(); | 254 error_callback.Run(); |
245 return; | 255 return; |
246 } | 256 } |
247 } | 257 } |
248 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { | 258 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { |
249 low_energy_discovery_manager_->StopDiscovery(); | 259 if (IsLowEnergyAvailable()) |
| 260 low_energy_discovery_manager_->StopDiscovery(); |
250 } | 261 } |
251 | 262 |
252 DVLOG(1) << "Discovery stopped"; | 263 DVLOG(1) << "Discovery stopped"; |
253 num_discovery_sessions_--; | 264 num_discovery_sessions_--; |
254 callback.Run(); | 265 callback.Run(); |
255 } | 266 } |
256 | 267 |
257 void BluetoothAdapterMac::SetDiscoveryFilter( | 268 void BluetoothAdapterMac::SetDiscoveryFilter( |
258 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, | 269 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, |
259 const base::Closure& callback, | 270 const base::Closure& callback, |
(...skipping 16 matching lines...) Expand all Loading... |
276 // TODO(krstnmnlsn): If a classic discovery session is already running then | 287 // TODO(krstnmnlsn): If a classic discovery session is already running then |
277 // we should update its filter. crbug.com/498056 | 288 // we should update its filter. crbug.com/498056 |
278 if (!classic_discovery_manager_->StartDiscovery()) { | 289 if (!classic_discovery_manager_->StartDiscovery()) { |
279 DVLOG(1) << "Failed to add a classic discovery session"; | 290 DVLOG(1) << "Failed to add a classic discovery session"; |
280 return false; | 291 return false; |
281 } | 292 } |
282 } | 293 } |
283 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { | 294 if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { |
284 // Begin a low energy discovery session or update it if one is already | 295 // Begin a low energy discovery session or update it if one is already |
285 // running. | 296 // running. |
286 low_energy_discovery_manager_->StartDiscovery(BluetoothDevice::UUIDList()); | 297 if (IsLowEnergyAvailable()) |
| 298 low_energy_discovery_manager_->StartDiscovery( |
| 299 BluetoothDevice::UUIDList()); |
287 } | 300 } |
288 return true; | 301 return true; |
289 } | 302 } |
290 | 303 |
291 void BluetoothAdapterMac::Init() { | 304 void BluetoothAdapterMac::Init() { |
292 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 305 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
293 PollAdapter(); | 306 PollAdapter(); |
294 } | 307 } |
295 | 308 |
296 void BluetoothAdapterMac::InitForTest( | 309 void BluetoothAdapterMac::InitForTest( |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 DCHECK_EQ(num_removed, 1U); | 431 DCHECK_EQ(num_removed, 1U); |
419 } | 432 } |
420 | 433 |
421 // Add any new paired devices. | 434 // Add any new paired devices. |
422 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 435 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
423 ClassicDeviceAdded(device); | 436 ClassicDeviceAdded(device); |
424 } | 437 } |
425 } | 438 } |
426 | 439 |
427 } // namespace device | 440 } // namespace device |
OLD | NEW |