1 // Copyright 2014 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 DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_
10 #include "base/callback.h"
11 #include "device/bluetooth/bluetooth_export.h"
15 class BluetoothAdapter
;
16 class BluetoothDevice
;
18 // BluetoothGattConnection represents a GATT connection to a Bluetooth device
19 // that has GATT services. Instances are obtained from a BluetoothDevice,
20 // and the connection is kept alive as long as there is at least one
21 // active BluetoothGattConnection object. BluetoothGattConnection objects
22 // automatically update themselves, when the connection is terminated by the
23 // operating system (e.g. due to user action).
24 class DEVICE_BLUETOOTH_EXPORT BluetoothGattConnection
{
26 BluetoothGattConnection(scoped_refptr
<device::BluetoothAdapter
> adapter
,
27 const std::string
& device_address
);
29 // Destructor automatically closes this GATT connection. If this is the last
30 // remaining GATT connection and this results in a call to the OS, that call
31 // may not always succeed. Users can make an explicit call to
32 // BluetoothGattConnection::Close to make sure that they are notified of
33 // a possible error via the callback.
34 virtual ~BluetoothGattConnection();
36 // Returns the Bluetooth address of the device that this connection is open
38 const std::string
& GetDeviceAddress() const;
40 // Returns true if this GATT connection is open.
41 virtual bool IsConnected();
43 // Disconnects this GATT connection. The device may still remain connected due
44 // to other GATT connections. When all BluetoothGattConnection objects are
45 // disconnected the BluetoothDevice object will disconnect GATT.
46 virtual void Disconnect();
49 friend BluetoothDevice
; // For InvalidateConnectionReference.
51 // Sets this object to no longer have a reference maintaining the connection.
52 // Only to be called by BluetoothDevice to avoid reentrant code to
53 // RemoveGattConnection in that destructor after BluetoothDevice subclasses
54 // have already been destroyed.
55 void InvalidateConnectionReference();
57 // The Bluetooth adapter that this connection is associated with. A reference
58 // is held because BluetoothGattConnection keeps the connection alive.
59 scoped_refptr
<BluetoothAdapter
> adapter_
;
61 // Bluetooth address of the underlying device.
62 std::string device_address_
;
63 BluetoothDevice
* device_
= nullptr;
66 bool owns_reference_for_connection_
= false;
68 DISALLOW_COPY_AND_ASSIGN(BluetoothGattConnection
);
73 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_