1 // Copyright 2020 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 CHROME_BROWSER_NEARBY_SHARING_BLUETOOTH_ADVERTISING_INTERVAL_CLIENT_H_
6 #define CHROME_BROWSER_NEARBY_SHARING_BLUETOOTH_ADVERTISING_INTERVAL_CLIENT_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "device/bluetooth/bluetooth_advertisement.h"
14 
15 namespace device {
16 class BluetoothAdapter;
17 }  // namespace device
18 
19 // BluetoothAdvertisingIntervalClient is responsible for setting the Bluetooth
20 // advertising interval to a lower value when we start advertising for Nearby
21 // Share, and then restores the interval to the system default when advertising
22 // stops.
23 class BluetoothAdvertisingIntervalClient {
24  public:
25   BluetoothAdvertisingIntervalClient(
26       scoped_refptr<device::BluetoothAdapter> adapter);
27   ~BluetoothAdvertisingIntervalClient();
28 
29   // Sets the advertising interval to a lowered value to allow for faster
30   // connections.
31   void ReduceInterval();
32   // Restores the advertising interval to the system default.
33   void RestoreDefaultInterval();
34 
35  private:
36   void OnSetIntervalForAdvertisingError(
37       device::BluetoothAdvertisement::ErrorCode code);
38   void OnRestoreDefaultIntervalError(
39       device::BluetoothAdvertisement::ErrorCode code);
40 
41   scoped_refptr<device::BluetoothAdapter> adapter_;
42   base::WeakPtrFactory<BluetoothAdvertisingIntervalClient> weak_ptr_factory_{
43       this};
44 };
45 
46 #endif  // CHROME_BROWSER_NEARBY_SHARING_BLUETOOTH_ADVERTISING_INTERVAL_CLIENT_H_
47