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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc
diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc
new file mode 100644
index 0000000000000000000000000000000000000000..891d7eb083d7698b1f8f942d9f63258e60035b5a
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc
@@ -0,0 +1,146 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h"
+
+#include <string>
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/logging.h"
+#include "device/bluetooth/bluetooth_adapter_factory.h"
+#include "device/bluetooth/bluetooth_device.h"
+#include "device/bluetooth/bluetooth_discovery_session.h"
+#include "device/bluetooth/bluetooth_uuid.h"
+
+using device::BluetoothAdapter;
+using device::BluetoothDevice;
+
+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.
+
msarda 2015/04/22 08:55:14 Kill empty line.
sacomoto 2015/04/22 09:44:19 Done.
+void DoNothingErrorCallback(BluetoothDevice::ConnectErrorCode error_code) {
+}
+
+BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
+ const std::string& remote_service_uuid)
+ : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)),
+ weak_ptr_factory_(this) {
+}
+
+BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() {
+ 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.
+ adapter_->RemoveObserver(this);
+ adapter_ = NULL;
+ }
+ if (discovery_session_.get()) {
+ StopDiscoverySession();
+ discovery_session_.reset();
+ }
+}
+
+void BluetoothLowEnergyConnectionFinder::Find(
+ const BluetoothDevice::GattConnectionCallback& connection_callback) {
+ 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.
+
+ if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
+ 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
+ return;
+ }
+
+ connection_callback_ = connection_callback;
+
+ device::BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void BluetoothLowEnergyConnectionFinder::Find(
+ 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
+}
+
+void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter,
+ BluetoothDevice* device) {
+ if (device) {
+ DVLOG(1) << "New device found: " << device->GetName();
+ if (HasService(device)) {
+ CreateConnection(device);
+ }
+ }
+}
+
+void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
+ scoped_refptr<BluetoothAdapter> adapter) {
+ DVLOG(1) << "Adapter ready";
+
+ adapter_ = adapter;
+ adapter_->AddObserver(this);
+
+ std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
+ for (auto iter = devices.begin(); iter != devices.end(); iter++) {
+ if (HasService(*iter)) {
+ CreateConnection(*iter);
+ }
+ }
+
+ StartDiscoverySession();
+}
+
+void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
+ scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
+ DVLOG(1) << "Discovery session started";
+ 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.
+}
+
+void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
+ if (!adapter_.get()) {
+ DVLOG(1) << "Adapter not initialized";
+ return;
+ }
+ if (discovery_session_.get() && discovery_session_->IsActive()) {
+ DVLOG(1) << "Discovery session already active";
+ return;
+ }
+ adapter_->StartDiscoverySession(
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::Bind(base::DoNothing));
+}
+
+void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() {
+ DVLOG(1) << "Stopping discovery sesison";
+
+ if (!adapter_.get()) {
+ DVLOG(1) << "Adapter not initialized";
+ return;
+ }
+ if (!discovery_session_.get() || !discovery_session_->IsActive()) {
+ DVLOG(1) << "No Active discovery session";
+ }
+
+ 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.
+ base::Bind(base::DoNothing));
+}
+
+bool BluetoothLowEnergyConnectionFinder::HasService(
+ BluetoothDevice* remote_device) {
+ std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs();
+ for (auto iter = uuids.begin(); iter != uuids.end(); iter++) {
+ if (remote_service_uuid_ == *iter) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void BluetoothLowEnergyConnectionFinder::CreateConnection(
+ device::BluetoothDevice* remote_device) {
+ DVLOG(1) << "SmartLock service found ("
+ << remote_service_uuid_.canonical_value() << ")\n"
+ << "device = " << remote_device->GetAddress()
+ << ", name = " << remote_device->GetName();
+ remote_device->CreateGattConnection(connection_callback_,
+ base::Bind(&DoNothingErrorCallback));
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698