| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_low_energy_discovery_manager_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h" |
| 6 | 6 |
| 7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #include "base/mac/sdk_forward_declarations.h" | 8 #include "base/mac/sdk_forward_declarations.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "device/bluetooth/bluetooth_adapter_mac.h" | 11 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | 12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| 13 | 13 |
| 14 using device::BluetoothLowEnergyDeviceMac; | |
| 15 using device::BluetoothLowEnergyDiscoveryManagerMac; | |
| 16 using device::BluetoothLowEnergyDiscoveryManagerMacDelegate; | |
| 17 | |
| 18 namespace device { | 14 namespace device { |
| 19 | 15 |
| 20 // This class is a helper to call some protected methods in | |
| 21 // BluetoothLowEnergyDiscoveryManagerMac. | |
| 22 class BluetoothLowEnergyDiscoveryManagerMacDelegate { | |
| 23 public: | |
| 24 BluetoothLowEnergyDiscoveryManagerMacDelegate( | |
| 25 BluetoothLowEnergyDiscoveryManagerMac* manager) | |
| 26 : manager_(manager) {} | |
| 27 | |
| 28 virtual ~BluetoothLowEnergyDiscoveryManagerMacDelegate() {} | |
| 29 | |
| 30 virtual void DiscoveredPeripheral(CBPeripheral* peripheral, | |
| 31 NSDictionary* advertisementData, | |
| 32 int rssi) { | |
| 33 manager_->DiscoveredPeripheral(peripheral, advertisementData, rssi); | |
| 34 } | |
| 35 | |
| 36 virtual void TryStartDiscovery() { manager_->TryStartDiscovery(); } | |
| 37 | |
| 38 private: | |
| 39 BluetoothLowEnergyDiscoveryManagerMac* manager_; | |
| 40 }; | |
| 41 | |
| 42 } // namespace device | |
| 43 | |
| 44 // This class will serve as the Objective-C delegate of CBCentralManager. | |
| 45 @interface BluetoothLowEnergyDiscoveryManagerMacBridge | |
| 46 : NSObject<CBCentralManagerDelegate> { | |
| 47 BluetoothLowEnergyDiscoveryManagerMac* manager_; | |
| 48 scoped_ptr<BluetoothLowEnergyDiscoveryManagerMacDelegate> delegate_; | |
| 49 } | |
| 50 | |
| 51 - (id)initWithManager:(BluetoothLowEnergyDiscoveryManagerMac*)manager; | |
| 52 | |
| 53 @end | |
| 54 | |
| 55 @implementation BluetoothLowEnergyDiscoveryManagerMacBridge | |
| 56 | |
| 57 - (id)initWithManager:(BluetoothLowEnergyDiscoveryManagerMac*)manager { | |
| 58 if ((self = [super init])) { | |
| 59 manager_ = manager; | |
| 60 delegate_.reset( | |
| 61 new BluetoothLowEnergyDiscoveryManagerMacDelegate(manager_)); | |
| 62 } | |
| 63 return self; | |
| 64 } | |
| 65 | |
| 66 - (void)centralManager:(CBCentralManager*)central | |
| 67 didDiscoverPeripheral:(CBPeripheral*)peripheral | |
| 68 advertisementData:(NSDictionary*)advertisementData | |
| 69 RSSI:(NSNumber*)RSSI { | |
| 70 // Notifies the discovery of a device. | |
| 71 delegate_->DiscoveredPeripheral(peripheral, advertisementData, | |
| 72 [RSSI intValue]); | |
| 73 } | |
| 74 | |
| 75 - (void)centralManagerDidUpdateState:(CBCentralManager*)central { | |
| 76 // Notifies when the powered state of the central manager changed. | |
| 77 delegate_->TryStartDiscovery(); | |
| 78 } | |
| 79 | |
| 80 @end | |
| 81 | |
| 82 BluetoothLowEnergyDiscoveryManagerMac:: | 16 BluetoothLowEnergyDiscoveryManagerMac:: |
| 83 ~BluetoothLowEnergyDiscoveryManagerMac() { | 17 ~BluetoothLowEnergyDiscoveryManagerMac() { |
| 84 } | 18 } |
| 85 | 19 |
| 86 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { | 20 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { |
| 87 return discovering_; | 21 return discovering_; |
| 88 } | 22 } |
| 89 | 23 |
| 90 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( | 24 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( |
| 91 BluetoothDevice::UUIDList services_uuids) { | 25 BluetoothDevice::UUIDList services_uuids) { |
| 92 discovering_ = true; | 26 discovering_ = true; |
| 93 pending_ = true; | 27 pending_ = true; |
| 94 services_uuids_ = services_uuids; | 28 services_uuids_ = services_uuids; |
| 95 TryStartDiscovery(); | 29 TryStartDiscovery(); |
| 96 } | 30 } |
| 97 | 31 |
| 98 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { | 32 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { |
| 99 if (!discovering_) { | 33 if (!discovering_) { |
| 100 return; | 34 return; |
| 101 } | 35 } |
| 102 | 36 |
| 103 if (!pending_) { | 37 if (!pending_) { |
| 104 return; | 38 return; |
| 105 } | 39 } |
| 106 | 40 |
| 107 // Can only start if the bluetooth power is turned on. | 41 // Can only start if the bluetooth power is turned on. |
| 108 if ([manager_ state] != CBCentralManagerStatePoweredOn) { | 42 if (!central_manager_ || |
| 43 [central_manager_ state] != CBCentralManagerStatePoweredOn) { |
| 109 return; | 44 return; |
| 110 } | 45 } |
| 111 | 46 |
| 112 // Converts the services UUIDs to a CoreBluetooth data structure. | 47 // Converts the services UUIDs to a CoreBluetooth data structure. |
| 113 NSMutableArray* services = nil; | 48 NSMutableArray* services = nil; |
| 114 if (!services_uuids_.empty()) { | 49 if (!services_uuids_.empty()) { |
| 115 services = [NSMutableArray array]; | 50 services = [NSMutableArray array]; |
| 116 for (auto& service_uuid : services_uuids_) { | 51 for (auto& service_uuid : services_uuids_) { |
| 117 NSString* uuidString = | 52 NSString* uuidString = |
| 118 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); | 53 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); |
| 119 Class aClass = NSClassFromString(@"CBUUID"); | 54 Class aClass = NSClassFromString(@"CBUUID"); |
| 120 CBUUID* uuid = [aClass UUIDWithString:uuidString]; | 55 CBUUID* uuid = [aClass UUIDWithString:uuidString]; |
| 121 [services addObject:uuid]; | 56 [services addObject:uuid]; |
| 122 } | 57 } |
| 123 }; | 58 }; |
| 124 | 59 |
| 125 [manager_ scanForPeripheralsWithServices:services options:nil]; | 60 [central_manager_ scanForPeripheralsWithServices:services options:nil]; |
| 126 pending_ = false; | 61 pending_ = false; |
| 127 } | 62 } |
| 128 | 63 |
| 129 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { | 64 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { |
| 130 if (discovering_ && !pending_) { | 65 if (discovering_ && !pending_) { |
| 131 [manager_ stopScan]; | 66 [central_manager_ stopScan]; |
| 132 } | 67 } |
| 133 discovering_ = false; | 68 discovering_ = false; |
| 134 } | 69 } |
| 135 | 70 |
| 71 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager( |
| 72 CBCentralManager* central_manager) { |
| 73 central_manager_ = central_manager; |
| 74 } |
| 75 |
| 136 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral( | 76 void BluetoothLowEnergyDiscoveryManagerMac::DiscoveredPeripheral( |
| 137 CBPeripheral* peripheral, | 77 CBPeripheral* peripheral, |
| 138 NSDictionary* advertisementData, | 78 NSDictionary* advertisementData, |
| 139 int rssi) { | 79 int rssi) { |
| 140 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi); | 80 observer_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi); |
| 141 } | 81 } |
| 142 | 82 |
| 143 BluetoothLowEnergyDiscoveryManagerMac* | 83 BluetoothLowEnergyDiscoveryManagerMac* |
| 144 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) { | 84 BluetoothLowEnergyDiscoveryManagerMac::Create(Observer* observer) { |
| 145 return new BluetoothLowEnergyDiscoveryManagerMac(observer); | 85 return new BluetoothLowEnergyDiscoveryManagerMac(observer); |
| 146 } | 86 } |
| 147 | 87 |
| 148 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac( | 88 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac( |
| 149 Observer* observer) | 89 Observer* observer) |
| 150 : observer_(observer) { | 90 : observer_(observer) { |
| 151 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 91 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 152 bridge_.reset([[BluetoothLowEnergyDiscoveryManagerMacBridge alloc] | |
| 153 initWithManager:this]); | |
| 154 Class aClass = NSClassFromString(@"CBCentralManager"); | |
| 155 manager_.reset([[aClass alloc] initWithDelegate:bridge_ | |
| 156 queue:dispatch_get_main_queue()]); | |
| 157 discovering_ = false; | 92 discovering_ = false; |
| 158 } | 93 } |
| 159 | 94 |
| 160 void BluetoothLowEnergyDiscoveryManagerMac::SetManagerForTesting( | 95 } // namespace device |
| 161 CBCentralManager* manager) { | |
| 162 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | |
| 163 [manager performSelector:@selector(setDelegate:) withObject:bridge_]; | |
| 164 manager_.reset(manager); | |
| 165 } | |
| OLD | NEW |