OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h " | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/logging.h" | |
12 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
13 #include "device/bluetooth/bluetooth_device.h" | |
14 #include "device/bluetooth/bluetooth_discovery_session.h" | |
15 #include "device/bluetooth/bluetooth_uuid.h" | |
16 | |
17 using device::BluetoothAdapter; | |
18 using device::BluetoothDevice; | |
19 | |
20 namespace proximity_auth { | |
msarda
2015/04/22 08:55:14
This function is internal. Use an anonymous namesp
sacomoto
2015/04/22 09:44:19
Done.
| |
21 | |
msarda
2015/04/22 08:55:14
Kill empty line.
sacomoto
2015/04/22 09:44:19
Done.
| |
22 void DoNothingErrorCallback(BluetoothDevice::ConnectErrorCode error_code) { | |
23 } | |
24 | |
25 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( | |
26 const std::string& remote_service_uuid) | |
27 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | |
28 weak_ptr_factory_(this) { | |
29 } | |
30 | |
31 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { | |
32 if (adapter_.get()) { | |
msarda
2015/04/22 08:55:14
You are starting discovery after the adapter is se
sacomoto
2015/04/22 09:44:19
Done.
sacomoto
2015/04/22 09:44:19
Done.
| |
33 adapter_->RemoveObserver(this); | |
34 adapter_ = NULL; | |
35 } | |
36 if (discovery_session_.get()) { | |
37 StopDiscoverySession(); | |
38 discovery_session_.reset(); | |
39 } | |
40 } | |
41 | |
42 void BluetoothLowEnergyConnectionFinder::Find( | |
43 const BluetoothDevice::GattConnectionCallback& connection_callback) { | |
44 DVLOG(1) << "Finding connection"; | |
msarda
2015/04/22 08:55:14
Move this below the IsBluetoothAvailable line.
sacomoto
2015/04/22 09:44:19
Done.
| |
45 | |
46 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | |
47 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; | |
msarda
2015/04/22 08:55:14
LOG(WARNING)
sacomoto
2015/04/22 09:44:19
I'm being consistent with the current proximity/bl
| |
48 return; | |
49 } | |
50 | |
51 connection_callback_ = connection_callback; | |
52 | |
53 device::BluetoothAdapterFactory::GetAdapter( | |
54 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, | |
55 weak_ptr_factory_.GetWeakPtr())); | |
56 } | |
57 | |
58 void BluetoothLowEnergyConnectionFinder::Find( | |
59 const ConnectionCallback& connection_callback) { | |
msarda
2015/04/22 08:55:14
I would expect this would be the method that we sh
sacomoto
2015/04/22 09:44:19
I agree. The problem is that the current callback
| |
60 } | |
61 | |
62 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, | |
63 BluetoothDevice* device) { | |
64 if (device) { | |
65 DVLOG(1) << "New device found: " << device->GetName(); | |
66 if (HasService(device)) { | |
67 CreateConnection(device); | |
68 } | |
69 } | |
70 } | |
71 | |
72 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( | |
73 scoped_refptr<BluetoothAdapter> adapter) { | |
74 DVLOG(1) << "Adapter ready"; | |
75 | |
76 adapter_ = adapter; | |
77 adapter_->AddObserver(this); | |
78 | |
79 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | |
80 for (auto iter = devices.begin(); iter != devices.end(); iter++) { | |
81 if (HasService(*iter)) { | |
82 CreateConnection(*iter); | |
83 } | |
84 } | |
85 | |
86 StartDiscoverySession(); | |
87 } | |
88 | |
89 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( | |
90 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | |
91 DVLOG(1) << "Discovery session started"; | |
92 discovery_session_.reset(discovery_session.release()); | |
msarda
2015/04/22 08:55:14
discovery_session_ = discovery_session.Pass();
sacomoto
2015/04/22 09:44:19
Done.
| |
93 } | |
94 | |
95 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { | |
96 if (!adapter_.get()) { | |
97 DVLOG(1) << "Adapter not initialized"; | |
98 return; | |
99 } | |
100 if (discovery_session_.get() && discovery_session_->IsActive()) { | |
101 DVLOG(1) << "Discovery session already active"; | |
102 return; | |
103 } | |
104 adapter_->StartDiscoverySession( | |
105 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, | |
106 weak_ptr_factory_.GetWeakPtr()), | |
107 base::Bind(base::DoNothing)); | |
108 } | |
109 | |
110 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { | |
111 DVLOG(1) << "Stopping discovery sesison"; | |
112 | |
113 if (!adapter_.get()) { | |
114 DVLOG(1) << "Adapter not initialized"; | |
115 return; | |
116 } | |
117 if (!discovery_session_.get() || !discovery_session_->IsActive()) { | |
118 DVLOG(1) << "No Active discovery session"; | |
119 } | |
120 | |
121 discovery_session_->Stop(base::Bind(base::DoNothing), | |
msarda
2015/04/22 08:55:14
Should you reset the discovery_session_ object whe
sacomoto
2015/04/22 09:44:19
Done.
| |
122 base::Bind(base::DoNothing)); | |
123 } | |
124 | |
125 bool BluetoothLowEnergyConnectionFinder::HasService( | |
126 BluetoothDevice* remote_device) { | |
127 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); | |
128 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) { | |
129 if (remote_service_uuid_ == *iter) { | |
130 return true; | |
131 } | |
132 } | |
133 return false; | |
134 } | |
135 | |
136 void BluetoothLowEnergyConnectionFinder::CreateConnection( | |
137 device::BluetoothDevice* remote_device) { | |
138 DVLOG(1) << "SmartLock service found (" | |
139 << remote_service_uuid_.canonical_value() << ")\n" | |
140 << "device = " << remote_device->GetAddress() | |
141 << ", name = " << remote_device->GetName(); | |
142 remote_device->CreateGattConnection(connection_callback_, | |
143 base::Bind(&DoNothingErrorCallback)); | |
144 } | |
145 | |
146 } // namespace proximity_auth | |
OLD | NEW |