Tim Song | 51ffb660 | 2018-08-02 05:57:35 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
| 5 | #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_ADVERTISEMENT_MANAGER_MAC_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_ADVERTISEMENT_MANAGER_MAC_H_ |
| 7 | |
| 8 | #include "base/mac/sdk_forward_declarations.h" |
| 9 | #include "base/macros.h" |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "device/bluetooth/bluetooth_advertisement_mac.h" |
| 13 | |
| 14 | namespace device { |
| 15 | |
| 16 | // Class used by BluetoothAdapterMac to manage LE advertisements. |
| 17 | // Currently, only a single concurrent BLE advertisement is supported. Future |
| 18 | // work will add support for multiple concurrent and rotating advertisements. |
| 19 | class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyAdvertisementManagerMac { |
| 20 | public: |
| 21 | BluetoothLowEnergyAdvertisementManagerMac(); |
| 22 | ~BluetoothLowEnergyAdvertisementManagerMac(); |
| 23 | |
| 24 | // Initializes the advertisement manager. |
| 25 | void Init(scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 26 | CBPeripheralManager* peripheral_manager); |
| 27 | |
| 28 | // Registers a new BLE advertisement. |
| 29 | void RegisterAdvertisement( |
| 30 | std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data, |
| 31 | const BluetoothAdapter::CreateAdvertisementCallback& callback, |
| 32 | const BluetoothAdapter::AdvertisementErrorCallback& error_callback); |
| 33 | |
| 34 | // Unregisters an existing BLE advertisement. |
| 35 | void UnregisterAdvertisement( |
| 36 | BluetoothAdvertisementMac* advertisement, |
| 37 | const BluetoothAdvertisementMac::SuccessCallback& success_callback, |
| 38 | const BluetoothAdvertisementMac::ErrorCallback& error_callback); |
| 39 | |
| 40 | // Called when the peripheral manager state changes. |
| 41 | void OnPeripheralManagerStateChanged(); |
| 42 | |
| 43 | // Called when advertisement starts with success or failure. |
| 44 | void DidStartAdvertising(NSError* error); |
| 45 | |
| 46 | private: |
| 47 | void StartAdvertising(); |
| 48 | |
| 49 | CBPeripheralManagerState GetPeripheralManagerState(); |
| 50 | |
| 51 | scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 52 | |
| 53 | CBPeripheralManager* peripheral_manager_; |
| 54 | |
| 55 | scoped_refptr<BluetoothAdvertisementMac> active_advertisement_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyAdvertisementManagerMac); |
| 58 | }; |
| 59 | |
| 60 | } // namespace device |
| 61 | |
| 62 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_ADVERTISEMENT_MANAGER_MAC_H_ |