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

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: Reverting build file. 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 {
21
22 void DoNothingErrorCallback(BluetoothDevice::ConnectErrorCode error_code) {
23 }
24
25 } // namespace
26
27 namespace proximity_auth {
28
29 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
30 const std::string& remote_service_uuid)
31 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)),
32 weak_ptr_factory_(this) {
33 }
34
35 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() {
36 if (discovery_session_.get()) {
37 StopDiscoverySession();
38 }
39 if (adapter_) {
40 adapter_->RemoveObserver(this);
41 adapter_ = NULL;
42 }
43 }
44
45 void BluetoothLowEnergyConnectionFinder::Find(
46 const BluetoothDevice::GattConnectionCallback& connection_callback) {
47 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
48 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting.";
49 return;
50 }
51 DVLOG(1) << "Finding connection";
52
53 connection_callback_ = connection_callback;
54
55 device::BluetoothAdapterFactory::GetAdapter(
56 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized,
57 weak_ptr_factory_.GetWeakPtr()));
58 }
59
60 void BluetoothLowEnergyConnectionFinder::Find(
61 const ConnectionCallback& connection_callback) {
msarda 2015/04/22 15:58:13 This method does not do what the user expects. Ple
sacomoto 2015/04/24 14:04:56 Done.
62 }
63
64 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter,
65 BluetoothDevice* device) {
66 if (device) {
67 DVLOG(1) << "New device found: " << device->GetName();
68 if (HasService(device)) {
69 CreateConnection(device);
70 }
71 }
72 }
73
74 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
75 scoped_refptr<BluetoothAdapter> adapter) {
76 DVLOG(1) << "Adapter ready";
77
78 adapter_ = adapter;
79 adapter_->AddObserver(this);
80
81 std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
82 for (auto iter = devices.begin(); iter != devices.end(); iter++) {
msarda 2015/04/22 15:58:12 I think we should have a single method to handle n
sacomoto 2015/04/24 14:04:56 Done. I think the first option is better, becaus
83 if (HasService(*iter)) {
84 CreateConnection(*iter);
85 }
86 }
87
88 StartDiscoverySession();
89 }
90
91 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
92 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
93 DVLOG(1) << "Discovery session started";
94 discovery_session_ = discovery_session.Pass();
95 }
96
97 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
msarda 2015/04/22 15:58:12 I expect this method should not be called when ada
msarda 2015/04/22 15:58:13 How can we be sure that this method is not called
sacomoto 2015/04/24 14:04:56 Done.
sacomoto 2015/04/24 14:04:56 This method does exactly what the method "Bluetoot
98 if (!adapter_.get()) {
99 DVLOG(1) << "Adapter not initialized";
100 return;
101 }
102 if (discovery_session_.get() && discovery_session_->IsActive()) {
103 DVLOG(1) << "Discovery session already active";
104 return;
105 }
106 adapter_->StartDiscoverySession(
107 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
108 weak_ptr_factory_.GetWeakPtr()),
109 base::Bind(base::DoNothing));
msarda 2015/04/22 15:58:13 We need error handling. At least let's log the err
sacomoto 2015/04/24 14:04:56 Done.
110 }
111
112 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() {
113 DVLOG(1) << "Stopping discovery sesison";
114
115 if (!adapter_.get()) {
116 DVLOG(1) << "Adapter not initialized";
117 return;
118 }
119 if (!discovery_session_.get() || !discovery_session_->IsActive()) {
120 DVLOG(1) << "No Active discovery session";
121 }
122
123 discovery_session_->Stop(base::Bind(base::DoNothing),
msarda 2015/04/22 15:58:13 I think it is wrong to reset the discovery_session
sacomoto 2015/04/24 14:04:56 Done.
124 base::Bind(base::DoNothing));
125 discovery_session_.reset();
126 }
127
128 bool BluetoothLowEnergyConnectionFinder::HasService(
129 BluetoothDevice* remote_device) {
130 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs();
131 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) {
132 if (remote_service_uuid_ == *iter) {
133 return true;
134 }
135 }
136 return false;
137 }
138
139 void BluetoothLowEnergyConnectionFinder::CreateConnection(
140 device::BluetoothDevice* remote_device) {
141 DVLOG(1) << "SmartLock service found ("
142 << remote_service_uuid_.canonical_value() << ")\n"
143 << "device = " << remote_device->GetAddress()
144 << ", name = " << remote_device->GetName();
145 remote_device->CreateGattConnection(connection_callback_,
146 base::Bind(&DoNothingErrorCallback));
msarda 2015/04/22 15:58:13 Ditto: Same here about error handling.
sacomoto 2015/04/24 14:04:56 Done.
147 }
148
149 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698