| 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/mac_util.h" |
| 17 #include "base/mac/sdk_forward_declarations.h" | 17 #include "base/mac/sdk_forward_declarations.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/profiler/scoped_tracker.h" | 19 #include "base/profiler/scoped_tracker.h" |
| 20 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
| 23 #include "base/thread_task_runner_handle.h" | 23 #include "base/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" | 25 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
| 26 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 27 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" |
| 27 #include "device/bluetooth/bluetooth_socket_mac.h" | 28 #include "device/bluetooth/bluetooth_socket_mac.h" |
| 28 #include "device/bluetooth/bluetooth_uuid.h" | 29 #include "device/bluetooth/bluetooth_uuid.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // The frequency with which to poll the adapter for updates. | 33 // The frequency with which to poll the adapter for updates. |
| 33 const int kPollIntervalMs = 500; | 34 const int kPollIntervalMs = 500; |
| 34 | 35 |
| 35 } // namespace | 36 } // namespace |
| 36 | 37 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 53 return adapter->weak_ptr_factory_.GetWeakPtr(); | 54 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 BluetoothAdapterMac::BluetoothAdapterMac() | 57 BluetoothAdapterMac::BluetoothAdapterMac() |
| 57 : BluetoothAdapter(), | 58 : BluetoothAdapter(), |
| 58 powered_(false), | 59 powered_(false), |
| 59 num_discovery_sessions_(0), | 60 num_discovery_sessions_(0), |
| 60 classic_discovery_manager_( | 61 classic_discovery_manager_( |
| 61 BluetoothDiscoveryManagerMac::CreateClassic(this)), | 62 BluetoothDiscoveryManagerMac::CreateClassic(this)), |
| 62 weak_ptr_factory_(this) { | 63 weak_ptr_factory_(this) { |
| 63 if (IsLowEnergyAvailable()) | 64 if (IsLowEnergyAvailable()) { |
| 64 low_energy_discovery_manager_.reset( | 65 low_energy_discovery_manager_.reset( |
| 65 BluetoothLowEnergyDiscoveryManagerMac::Create(this)); | 66 BluetoothLowEnergyDiscoveryManagerMac::Create(this)); |
| 67 low_energy_central_manager_delegate_.reset( |
| 68 [[BluetoothLowEnergyCentralManagerDelegate alloc] |
| 69 initWithDiscoveryManager:low_energy_discovery_manager_.get() |
| 70 andAdapter:this]); |
| 71 Class aClass = NSClassFromString(@"CBCentralManager"); |
| 72 low_energy_central_manager_.reset([[aClass alloc] |
| 73 initWithDelegate:low_energy_central_manager_delegate_.get() |
| 74 queue:dispatch_get_main_queue()]); |
| 75 low_energy_discovery_manager_->SetCentralManager( |
| 76 low_energy_central_manager_.get()); |
| 77 } |
| 66 DCHECK(classic_discovery_manager_.get()); | 78 DCHECK(classic_discovery_manager_.get()); |
| 67 } | 79 } |
| 68 | 80 |
| 69 BluetoothAdapterMac::~BluetoothAdapterMac() { | 81 BluetoothAdapterMac::~BluetoothAdapterMac() { |
| 70 } | 82 } |
| 71 | 83 |
| 72 std::string BluetoothAdapterMac::GetAddress() const { | 84 std::string BluetoothAdapterMac::GetAddress() const { |
| 73 return address_; | 85 return address_; |
| 74 } | 86 } |
| 75 | 87 |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 return; | 444 return; |
| 433 } | 445 } |
| 434 | 446 |
| 435 // A device has an update. | 447 // A device has an update. |
| 436 static_cast<BluetoothLowEnergyDeviceMac*>(device_reference) | 448 static_cast<BluetoothLowEnergyDeviceMac*>(device_reference) |
| 437 ->Update(peripheral, advertisement_data, rssi); | 449 ->Update(peripheral, advertisement_data, rssi); |
| 438 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 450 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 439 DeviceChanged(this, device_reference)); | 451 DeviceChanged(this, device_reference)); |
| 440 } | 452 } |
| 441 | 453 |
| 454 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
| 455 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} |
| 456 |
| 442 void BluetoothAdapterMac::RemoveTimedOutDevices() { | 457 void BluetoothAdapterMac::RemoveTimedOutDevices() { |
| 443 // Notify observers if any previously seen devices are no longer available, | 458 // Notify observers if any previously seen devices are no longer available, |
| 444 // i.e. if they are no longer paired, connected, nor recently discovered via | 459 // i.e. if they are no longer paired, connected, nor recently discovered via |
| 445 // an inquiry. | 460 // an inquiry. |
| 446 std::set<std::string> removed_devices; | 461 std::set<std::string> removed_devices; |
| 447 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 462 for (DevicesMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 448 BluetoothDevice* device = it->second; | 463 BluetoothDevice* device = it->second; |
| 449 if (device->IsPaired() || device->IsConnected()) | 464 if (device->IsPaired() || device->IsConnected()) |
| 450 continue; | 465 continue; |
| 451 | 466 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 467 } | 482 } |
| 468 } | 483 } |
| 469 | 484 |
| 470 void BluetoothAdapterMac::AddPairedDevices() { | 485 void BluetoothAdapterMac::AddPairedDevices() { |
| 471 // Add any new paired devices. | 486 // Add any new paired devices. |
| 472 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 487 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 473 ClassicDeviceAdded(device); | 488 ClassicDeviceAdded(device); |
| 474 } | 489 } |
| 475 } | 490 } |
| 476 | 491 |
| 492 void BluetoothAdapterMac::SetCentralManagerForTesting( |
| 493 CBCentralManager* central_manager) { |
| 494 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 495 [central_manager performSelector:@selector(setDelegate:) |
| 496 withObject:low_energy_central_manager_delegate_]; |
| 497 low_energy_central_manager_.reset(central_manager); |
| 498 low_energy_discovery_manager_->SetCentralManager( |
| 499 low_energy_central_manager_.get()); |
| 500 } |
| 501 |
| 477 } // namespace device | 502 } // namespace device |
| OLD | NEW |