Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc

Issue 1094273003: Implementing a BLE connection finder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Error handling and nits. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
21
22 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
23 const std::string& remote_service_uuid)
24 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)),
25 weak_ptr_factory_(this) {
26 }
27
28 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() {
29 if (discovery_session_.get()) {
30 StopDiscoverySession();
31 }
32 if (adapter_) {
33 adapter_->RemoveObserver(this);
34 adapter_ = NULL;
35 }
36 }
37
38 void BluetoothLowEnergyConnectionFinder::Find(
39 const BluetoothDevice::GattConnectionCallback& connection_callback) {
40 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
41 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting.";
42 return;
43 }
44 VLOG(1) << "Finding connection";
45
46 connection_callback_ = connection_callback;
47
48 device::BluetoothAdapterFactory::GetAdapter(
49 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized,
50 weak_ptr_factory_.GetWeakPtr()));
51 }
52
53 void BluetoothLowEnergyConnectionFinder::Find(
54 const ConnectionCallback& connection_callback) {
55 NOTREACHED();
56 }
57
58 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter,
59 BluetoothDevice* device) {
60 if (device) {
61 VLOG(1) << "New device found: " << device->GetName();
62 HandleDeviceAdded(device);
63 }
64 }
65
66 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
67 scoped_refptr<BluetoothAdapter> adapter) {
68 VLOG(1) << "Adapter ready";
69
70 adapter_ = adapter;
71 adapter_->AddObserver(this);
72
73 std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
74 for (auto iter = devices.begin(); iter != devices.end(); iter++) {
75 HandleDeviceAdded((*iter));
76 }
77
78 StartDiscoverySession();
79 }
80
81 void BluetoothLowEnergyConnectionFinder::HandleDeviceAdded(
82 BluetoothDevice* remote_device) {
83 if (HasService(remote_device)) {
84 CreateConnection(remote_device);
msarda 2015/04/24 14:12:16 This seems to potentially create multiple concurre
sacomoto 2015/04/24 14:56:22 You are right. Modified.
85 }
86 }
87
88 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
89 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
90 VLOG(1) << "Discovery session started";
91 discovery_session_ = discovery_session.Pass();
92 }
93
94 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() {
95 VLOG(1) << "Error starting discovery session";
96 }
97
98 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
99 DCHECK(adapter_);
100 if (discovery_session_.get() && discovery_session_->IsActive()) {
101 VLOG(1) << "Discovery session already active";
102 return;
103 }
104 adapter_->StartDiscoverySession(
105 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
106 weak_ptr_factory_.GetWeakPtr()),
107 base::Bind(
108 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError,
109 weak_ptr_factory_.GetWeakPtr()));
110 }
111
112 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() {
113 VLOG(1) << "Discovery session stopped";
114 discovery_session_.reset();
115 }
116
117 void BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError() {
118 VLOG(1) << "Error stopping discovery session";
119 }
120
121 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() {
122 VLOG(1) << "Stopping discovery sesison";
123
124 if (!adapter_.get()) {
125 VLOG(1) << "Adapter not initialized";
126 return;
127 }
128 if (!discovery_session_.get() || !discovery_session_->IsActive()) {
129 VLOG(1) << "No Active discovery session";
130 }
131
132 discovery_session_->Stop(
133 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped,
134 weak_ptr_factory_.GetWeakPtr()),
135 base::Bind(
136 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError,
137 weak_ptr_factory_.GetWeakPtr()));
138 }
139
140 bool BluetoothLowEnergyConnectionFinder::HasService(
141 BluetoothDevice* remote_device) {
142 if (remote_device) {
msarda 2015/04/24 14:12:16 Can this actually be nullptr? If not, then I think
sacomoto 2015/04/24 14:56:22 I think it can. When the observer interface method
143 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs();
144 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) {
145 if (remote_service_uuid_ == *iter) {
146 return true;
147 }
148 }
149 }
150 return false;
151 }
152
153 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError(
154 BluetoothDevice::ConnectErrorCode error_code) {
155 VLOG(1) << "Error creating connection";
156 }
157
158 void BluetoothLowEnergyConnectionFinder::CreateConnection(
159 device::BluetoothDevice* remote_device) {
160 VLOG(1) << "SmartLock service found ("
161 << remote_service_uuid_.canonical_value() << ")\n"
162 << "device = " << remote_device->GetAddress()
163 << ", name = " << remote_device->GetName();
164 remote_device->CreateGattConnection(
165 connection_callback_,
166 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError,
167 weak_ptr_factory_.GetWeakPtr()));
168 }
169
170 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698