1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef ASH_BLUETOOTH_DEVICES_OBSERVER_H_
6 #define ASH_BLUETOOTH_DEVICES_OBSERVER_H_
8 #include "ash/ash_export.h"
9 #include "base/callback.h"
10 #include "base/memory/weak_ptr.h"
11 #include "device/bluetooth/bluetooth_adapter.h"
12 #include "ui/events/devices/input_device.h"
16 // This class observes the bluetooth devices and sends out notification when
17 // bluetooth device changes. It's used as a supplementary to the class
18 // ui::InputDeviceEventObserver as InputDeviceEventObserver does not have
19 // knowledge about bluetooth device status thus does not send notifications of
20 // bluetooth device changes.
21 class ASH_EXPORT BluetoothDevicesObserver
22 : public device::BluetoothAdapter::Observer {
24 // Note |device| can be nullptr here if only the bluetooth adapter status
26 using AdapterOrDeviceChangedCallback =
27 base::RepeatingCallback<void(device::BluetoothDevice* device)>;
29 explicit BluetoothDevicesObserver(
30 const AdapterOrDeviceChangedCallback& device_changed_callback);
32 BluetoothDevicesObserver(const BluetoothDevicesObserver&) = delete;
33 BluetoothDevicesObserver& operator=(const BluetoothDevicesObserver&) = delete;
35 ~BluetoothDevicesObserver() override;
37 // device::BluetoothAdapter::Observer:
38 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
39 bool present) override;
40 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
41 bool powered) override;
42 void DeviceChanged(device::BluetoothAdapter* adapter,
43 device::BluetoothDevice* device) override;
45 // Returns true if |input_device| is a connected bluetooth device. Note this
46 // function may not work well if there are more than one identical Bluetooth
47 // devices: the function might return true even if it should return false.
48 // E.g., Connect two idential bluetooth devices (Input device A & input device
49 // B, thus the same vendor id and same product id) to Chrome OS, and then
50 // disconnect device A, calling IsConnectedBluetoothDevice(Input device A)
51 // still returns true although it should return false as Input device B is
52 // still connected. Unfortunately there is no good map from an InputDevice to
53 // a BluetoothDevice, thus we can only guess a match.
54 bool IsConnectedBluetoothDevice(const ui::InputDevice& input_device) const;
57 void InitializeOnAdapterReady(
58 scoped_refptr<device::BluetoothAdapter> adapter);
60 // Reference to the underlying Bluetooth Adapter. Used to listen for bluetooth
61 // device change event.
62 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
64 // Callback function to be called when the bluetooth adapter's status or a
65 // bluetooth device's status changes.
66 AdapterOrDeviceChangedCallback adapter_or_device_changed_callback_;
68 base::WeakPtrFactory<BluetoothDevicesObserver> weak_factory_{this};
73 #endif // ASH_BLUETOOTH_DEVICES_OBSERVER_H_