Roll src/third_party/WebKit 7bf0c31:94af568 (svn 202004:202005)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_low_energy_central_manager_delegate.mm
blob598e4b97bcf70a177ce756c6a909519117f2a7e4
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.
5 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h"
7 #include "device/bluetooth/bluetooth_adapter_mac.h"
8 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
10 namespace device {
12 // This class exists to bridge between the Objective-C CBCentralManagerDelegate
13 // class and our BluetoothLowEnergyDiscoveryManagerMac and BluetoothAdapterMac
14 // classes.
15 class BluetoothLowEnergyCentralManagerBridge {
16  public:
17   BluetoothLowEnergyCentralManagerBridge(
18       BluetoothLowEnergyDiscoveryManagerMac* discovery_manager,
19       BluetoothAdapterMac* adapter)
20       : discovery_manager_(discovery_manager), adapter_(adapter) {}
22   virtual ~BluetoothLowEnergyCentralManagerBridge() {}
24   virtual void DiscoveredPeripheral(CBPeripheral* peripheral,
25                                     NSDictionary* advertisementData,
26                                     int rssi) {
27     discovery_manager_->DiscoveredPeripheral(peripheral, advertisementData,
28                                              rssi);
29   }
31   virtual void UpdatedState() {
32     discovery_manager_->TryStartDiscovery();
33     adapter_->LowEnergyCentralManagerUpdatedState();
34   }
36  private:
37   BluetoothLowEnergyDiscoveryManagerMac* discovery_manager_;
38   BluetoothAdapterMac* adapter_;
41 }  // namespace device
43 @implementation BluetoothLowEnergyCentralManagerDelegate
45 - (id)initWithDiscoveryManager:
46           (device::BluetoothLowEnergyDiscoveryManagerMac*)discovery_manager
47                     andAdapter:(device::BluetoothAdapterMac*)adapter {
48   if ((self = [super init])) {
49     bridge_.reset(new device::BluetoothLowEnergyCentralManagerBridge(
50         discovery_manager, adapter));
51   }
52   return self;
55 - (void)centralManager:(CBCentralManager*)central
56  didDiscoverPeripheral:(CBPeripheral*)peripheral
57      advertisementData:(NSDictionary*)advertisementData
58                   RSSI:(NSNumber*)RSSI {
59   // Notifies the discovery of a device.
60   bridge_->DiscoveredPeripheral(peripheral, advertisementData, [RSSI intValue]);
63 - (void)centralManagerDidUpdateState:(CBCentralManager*)central {
64   // Notifies when the powered state of the central manager changed.
65   bridge_->UpdatedState();
68 @end