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 #include <stddef.h> | 9 #include <stddef.h> |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 namespace { | 34 namespace { |
35 | 35 |
36 // The frequency with which to poll the adapter for updates. | 36 // The frequency with which to poll the adapter for updates. |
37 const int kPollIntervalMs = 500; | 37 const int kPollIntervalMs = 500; |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 namespace device { | 41 namespace device { |
42 | 42 |
43 // static | 43 // static |
44 const NSTimeInterval BluetoothAdapterMac::kDiscoveryTimeoutSec = | |
45 180; // 3 minutes | |
46 | |
47 // static | |
48 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | 44 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( |
49 const InitCallback& init_callback) { | 45 const InitCallback& init_callback) { |
50 return BluetoothAdapterMac::CreateAdapter(); | 46 return BluetoothAdapterMac::CreateAdapter(); |
51 } | 47 } |
52 | 48 |
53 // static | 49 // static |
54 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapter() { | 50 base::WeakPtr<BluetoothAdapterMac> BluetoothAdapterMac::CreateAdapter() { |
55 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); | 51 BluetoothAdapterMac* adapter = new BluetoothAdapterMac(); |
56 adapter->Init(); | 52 adapter->Init(); |
57 return adapter->weak_ptr_factory_.GetWeakPtr(); | 53 return adapter->weak_ptr_factory_.GetWeakPtr(); |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 VLOG(2) << "LowEnergyDeviceUpdated"; | 516 VLOG(2) << "LowEnergyDeviceUpdated"; |
521 device_mac->Update(advertisement_data, rssi); | 517 device_mac->Update(advertisement_data, rssi); |
522 // TODO(scheib): Call DeviceChanged only if UUIDs change. crbug.com/547106 | 518 // TODO(scheib): Call DeviceChanged only if UUIDs change. crbug.com/547106 |
523 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 519 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
524 DeviceChanged(this, device_mac)); | 520 DeviceChanged(this, device_mac)); |
525 } | 521 } |
526 | 522 |
527 // TODO(krstnmnlsn): Implement. crbug.com/511025 | 523 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
528 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} | 524 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} |
529 | 525 |
530 void BluetoothAdapterMac::RemoveTimedOutDevices() { | |
531 // Notify observers if any previously seen devices are no longer available, | |
532 // i.e. if they are no longer paired, connected, nor recently discovered via | |
533 // an inquiry. | |
534 std::set<std::string> removed_devices; | |
535 for (DevicesMap::const_iterator it = devices_.begin(); it != devices_.end(); | |
536 ++it) { | |
537 BluetoothDevice* device = it->second; | |
538 if (device->IsPaired() || device->IsConnected()) | |
539 continue; | |
540 | |
541 NSDate* last_update_time = | |
542 static_cast<BluetoothDeviceMac*>(device)->GetLastUpdateTime(); | |
543 if (last_update_time && | |
544 -[last_update_time timeIntervalSinceNow] < kDiscoveryTimeoutSec) | |
545 continue; | |
546 | |
547 FOR_EACH_OBSERVER( | |
548 BluetoothAdapter::Observer, observers_, DeviceRemoved(this, device)); | |
549 removed_devices.insert(it->first); | |
550 // The device will be erased from the map in the loop immediately below. | |
551 } | |
552 for (const std::string& device_address : removed_devices) { | |
553 size_t num_removed = devices_.erase(device_address); | |
554 DCHECK_EQ(num_removed, 1U); | |
555 } | |
556 } | |
557 | |
558 void BluetoothAdapterMac::AddPairedDevices() { | 526 void BluetoothAdapterMac::AddPairedDevices() { |
559 // Add any new paired devices. | 527 // Add any new paired devices. |
560 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 528 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
561 ClassicDeviceAdded(device); | 529 ClassicDeviceAdded(device); |
562 } | 530 } |
563 } | 531 } |
564 | 532 |
565 void BluetoothAdapterMac::CreateGattConnection( | 533 void BluetoothAdapterMac::CreateGattConnection( |
566 BluetoothLowEnergyDeviceMac* device_mac) { | 534 BluetoothLowEnergyDeviceMac* device_mac) { |
567 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ | 535 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 std::string device_address = | 591 std::string device_address = |
624 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 592 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
625 DevicesMap::const_iterator iter = devices_.find(device_address); | 593 DevicesMap::const_iterator iter = devices_.find(device_address); |
626 if (iter == devices_.end()) { | 594 if (iter == devices_.end()) { |
627 return nil; | 595 return nil; |
628 } | 596 } |
629 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 597 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
630 } | 598 } |
631 | 599 |
632 } // namespace device | 600 } // namespace device |
OLD | NEW |