[go: nahoru, domu]

blob: c6f28d3ae7a793e6fccb56645025fe9c9b46b7e2 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2018 The Chromium Authors
Xiaoqian Dai45fbc042018-09-25 21:00:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/bluetooth_devices_observer.h"
6
Avi Drissman4de6dab2023-01-06 23:17:357#include "base/functional/bind.h"
Xiaoqian Dai45fbc042018-09-25 21:00:558#include "device/bluetooth/bluetooth_adapter_factory.h"
9
10namespace ash {
11
12BluetoothDevicesObserver::BluetoothDevicesObserver(
Xiaoqian Daif6310842018-11-15 05:07:1213 const AdapterOrDeviceChangedCallback& device_changed_callback)
Jeremy Roman47d432e2019-08-20 14:24:0014 : adapter_or_device_changed_callback_(device_changed_callback) {
Xiaoqian Daif6310842018-11-15 05:07:1215 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) {
Reilly Grant865804a62020-04-21 18:13:1816 device::BluetoothAdapterFactory::Get()->GetAdapter(
Reilly Grantd833b302019-01-08 01:39:0217 base::BindOnce(&BluetoothDevicesObserver::InitializeOnAdapterReady,
18 weak_factory_.GetWeakPtr()));
Xiaoqian Daif6310842018-11-15 05:07:1219 } else {
20 adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
21 }
Xiaoqian Dai45fbc042018-09-25 21:00:5522}
23
24BluetoothDevicesObserver::~BluetoothDevicesObserver() {
25 if (bluetooth_adapter_)
26 bluetooth_adapter_->RemoveObserver(this);
27}
28
Xiaoqian Daif6310842018-11-15 05:07:1229void BluetoothDevicesObserver::AdapterPresentChanged(
30 device::BluetoothAdapter* adapter,
31 bool present) {
32 adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
33}
34
35void BluetoothDevicesObserver::AdapterPoweredChanged(
36 device::BluetoothAdapter* adapter,
37 bool powered) {
38 adapter_or_device_changed_callback_.Run(/*device=*/nullptr);
39}
40
Xiaoqian Dai45fbc042018-09-25 21:00:5541void BluetoothDevicesObserver::DeviceChanged(device::BluetoothAdapter* adapter,
42 device::BluetoothDevice* device) {
Xiaoqian Daif6310842018-11-15 05:07:1243 adapter_or_device_changed_callback_.Run(device);
Xiaoqian Dai45fbc042018-09-25 21:00:5544}
45
46void BluetoothDevicesObserver::InitializeOnAdapterReady(
47 scoped_refptr<device::BluetoothAdapter> adapter) {
48 bluetooth_adapter_ = std::move(adapter);
49 bluetooth_adapter_->AddObserver(this);
50}
51
52bool BluetoothDevicesObserver::IsConnectedBluetoothDevice(
53 const ui::InputDevice& input_device) const {
David Padlipskya0f3c4c2023-05-22 23:57:3554 return GetConnectedBluetoothDevice(input_device) != nullptr;
55}
56
57device::BluetoothDevice* BluetoothDevicesObserver::GetConnectedBluetoothDevice(
58 const ui::InputDevice& input_device) const {
Xiaoqian Daif6310842018-11-15 05:07:1259 if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPresent() ||
60 !bluetooth_adapter_->IsInitialized() ||
61 !bluetooth_adapter_->IsPowered()) {
David Padlipskya0f3c4c2023-05-22 23:57:3562 return nullptr;
Xiaoqian Daif6310842018-11-15 05:07:1263 }
Xiaoqian Dai45fbc042018-09-25 21:00:5564
65 // Since there is no map from an InputDevice to a BluetoothDevice. We just
66 // comparing their vendor id and product id to guess a match.
67 for (auto* device : bluetooth_adapter_->GetDevices()) {
David Padlipskya0f3c4c2023-05-22 23:57:3568 if (!device->IsConnected()) {
Xiaoqian Dai45fbc042018-09-25 21:00:5569 continue;
David Padlipskya0f3c4c2023-05-22 23:57:3570 }
Xiaoqian Dai45fbc042018-09-25 21:00:5571
72 if (device->GetVendorID() == input_device.vendor_id &&
73 device->GetProductID() == input_device.product_id) {
David Padlipskya0f3c4c2023-05-22 23:57:3574 return device;
Xiaoqian Dai45fbc042018-09-25 21:00:5575 }
76 }
77
David Padlipskya0f3c4c2023-05-22 23:57:3578 return nullptr;
Xiaoqian Dai45fbc042018-09-25 21:00:5579}
80
81} // namespace ash