Connecting only to paired devices in BLE connection finder.
[chromium-blink-merge.git] / components / proximity_auth / ble / bluetooth_low_energy_connection_finder.h
blob973fe01e6abf5081429294af96712e9eaa81f69f
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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H
8 #include <set>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "components/proximity_auth/connection.h"
17 #include "components/proximity_auth/connection_finder.h"
18 #include "components/proximity_auth/connection_observer.h"
19 #include "device/bluetooth/bluetooth_adapter.h"
20 #include "device/bluetooth/bluetooth_device.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_gatt_connection.h"
24 namespace proximity_auth {
26 // This ConnectionFinder implementation is specialized in finding a Bluetooth
27 // Low Energy remote device.
28 class BluetoothLowEnergyConnectionFinder
29 : public ConnectionFinder,
30 public ConnectionObserver,
31 public device::BluetoothAdapter::Observer {
32 public:
33 BluetoothLowEnergyConnectionFinder(
34 const std::string& remote_service_uuid,
35 const std::string& to_peripheral_char_uuid,
36 const std::string& from_peripheral_char_uuid,
37 int max_number_of_tries);
38 ~BluetoothLowEnergyConnectionFinder() override;
40 // Finds a connection to the remote device. Only the first one is functional.
41 void Find(const ConnectionCallback& connection_callback) override;
43 // Closes the connection and forgets the device.
44 void CloseGattConnection(
45 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
47 // proximity_auth::ConnectionObserver:
48 void OnConnectionStatusChanged(Connection* connection,
49 Connection::Status old_status,
50 Connection::Status new_status) override;
52 // device::BluetoothAdapter::Observer:
53 void DeviceAdded(device::BluetoothAdapter* adapter,
54 device::BluetoothDevice* device) override;
55 void DeviceChanged(device::BluetoothAdapter* adapter,
56 device::BluetoothDevice* device) override;
57 void DeviceRemoved(device::BluetoothAdapter* adapter,
58 device::BluetoothDevice* device) override;
60 protected:
61 // Creates a proximity_auth::Connection based on |gatt_connection|. Exposed
62 // for testing.
63 virtual scoped_ptr<Connection> CreateConnection(
64 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
66 // Sets |delay_after_gatt_connection_| for testing.
67 void SetDelayForTesting(base::TimeDelta delay);
69 private:
70 // Callback to be called when the Bluetooth adapter is initialized.
71 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
73 // Checks if |remote_device| contains |remote_service_uuid| and creates a
74 // connection in that case.
75 void HandleDeviceUpdated(device::BluetoothDevice* remote_device);
77 // Callback called when a new discovery session is started.
78 void OnDiscoverySessionStarted(
79 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
81 // Callback called when there is an error starting a new discovery session.
82 void OnStartDiscoverySessionError();
84 // Starts a discovery session for |adapter_|.
85 void StartDiscoverySession();
87 // Callback called when |discovery_session_| is stopped.
88 void OnDiscoverySessionStopped();
90 // Callback called when there is an error stopping |discovery_session_|.
91 void OnStopDiscoverySessionError();
93 // Stops the discovery session given by |discovery_session_|.
94 void StopDiscoverySession();
96 // Checks if a service with |service_uuid| is offered by |remote_device|.
97 bool HasService(device::BluetoothDevice* remote_device);
99 // Callback called when there is an error creating the connection.
100 void OnCreateGattConnectionError(
101 std::string device_address,
102 device::BluetoothDevice::ConnectErrorCode error_code);
104 // Callback called when a GATT connection is created.
105 void OnGattConnectionCreated(
106 scoped_ptr<device::BluetoothGattConnection> gatt_connection);
108 // Creates a GATT connection with |remote_device|, |connection_callback_| will
109 // be called once the connection is established.
110 void CreateGattConnection(device::BluetoothDevice* remote_device);
112 // Creates a BluetoothLowEnergyconnection object and adds the necessary
113 // observers.
114 void CompleteConnection();
116 // The uuid of the service it looks for to establish a GattConnection.
117 device::BluetoothUUID remote_service_uuid_;
119 // Characteristic used to send data to the remote device.
120 device::BluetoothUUID to_peripheral_char_uuid_;
122 // Characteristic used to receive data from the remote device.
123 device::BluetoothUUID from_peripheral_char_uuid_;
125 // The Bluetooth adapter over which the Bluetooth connection will be made.
126 scoped_refptr<device::BluetoothAdapter> adapter_;
128 // The discovery session associated to this object.
129 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
131 // True if a connection was established to a remote device that has the
132 // service |remote_service_uuid|.
133 bool connected_;
135 // The GATT connection with |remote_device|.
136 scoped_ptr<device::BluetoothGattConnection> gatt_connection_;
138 // The connection with |remote_device|.
139 scoped_ptr<Connection> connection_;
141 // Callback called when the connection is established.
142 // device::BluetoothDevice::GattConnectionCallback connection_callback_;
143 ConnectionCallback connection_callback_;
145 // The set of devices this connection finder has tried to connect to.
146 std::set<device::BluetoothDevice*> pending_connections_;
148 // BluetoothLowEnergyConnection parameter.
149 int max_number_of_tries_;
151 // Necessary delay after a GATT connection is created and before any
152 // read/write request is sent to the characteristics.
153 base::TimeDelta delay_after_gatt_connection_;
155 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_;
157 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder);
160 } // namespace proximity_auth
162 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_CONNECTION_FINDER_H