1 // Copyright (c) 2012 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_DEVICE_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/string16.h"
17 class BluetoothServiceRecord
;
18 class BluetoothSocket
;
20 struct BluetoothOutOfBandPairingData
;
22 // BluetoothDevice represents a remote Bluetooth device, both its properties and
23 // capabilities as discovered by a local adapter and actions that may be
24 // performed on the remove device such as pairing, connection and disconnection.
26 // The class is instantiated and managed by the BluetoothAdapter class
27 // and pointers should only be obtained from that class and not cached,
28 // instead use the GetAddress() method as a unique key for a device.
30 // Since the lifecycle of BluetoothDevice instances is managed by
31 // BluetoothAdapter, that class rather than this provides observer methods
32 // for devices coming and going, as well as properties being updated.
33 class BluetoothDevice
{
35 // Possible values that may be returned by GetDeviceType(), representing
36 // different types of bluetooth device that we support or are aware of
37 // decoded from the bluetooth class information.
52 DEVICE_KEYBOARD_MOUSE_COMBO
55 // Possible errors passed back to an error callback function in case of a
56 // failed call to Connect().
57 enum ConnectErrorCode
{
65 ERROR_UNSUPPORTED_DEVICE
68 // Interface for observing changes from bluetooth devices.
71 virtual ~Observer() {}
73 // TODO(keybuk): add observers for pairing and connection.
76 // Interface for negotiating pairing of bluetooth devices.
77 class PairingDelegate
{
79 virtual ~PairingDelegate() {}
81 // This method will be called when the Bluetooth daemon requires a
82 // PIN Code for authentication of the device |device|, the delegate should
83 // obtain the code from the user and call SetPinCode() on the device to
84 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
87 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
88 // for which there is no automatic pairing or special handling.
89 virtual void RequestPinCode(BluetoothDevice
* device
) = 0;
91 // This method will be called when the Bluetooth daemon requires a
92 // Passkey for authentication of the device |device|, the delegate should
93 // obtain the passkey from the user (a numeric in the range 0-999999) and
94 // call SetPasskey() on the device to provide it, or RejectPairing() or
95 // CancelPairing() to reject or cancel the request.
97 // Passkeys are generally required for Bluetooth 2.1 and later devices
98 // which cannot provide input or display on their own, and don't accept
99 // passkey-less pairing.
100 virtual void RequestPasskey(BluetoothDevice
* device
) = 0;
102 // This method will be called when the Bluetooth daemon requires that the
103 // user enter the PIN code |pincode| into the device |device| so that it
104 // may be authenticated. The DismissDisplayOrConfirm() method
105 // will be called to dismiss the display once pairing is complete or
108 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
109 // |pincode| will always be a six-digit numeric in the range 000000-999999
110 // for compatibilty with later specifications.
111 virtual void DisplayPinCode(BluetoothDevice
* device
,
112 const std::string
& pincode
) = 0;
114 // This method will be called when the Bluetooth daemon requires that the
115 // user enter the Passkey |passkey| into the device |device| so that it
116 // may be authenticated. The DismissDisplayOrConfirm() method will be
117 // called to dismiss the display once pairing is complete or cancelled.
119 // This is used for Bluetooth 2.1 and later devices that support input
120 // but not display, such as keyboards. The Passkey is a numeric in the
121 // range 0-999999 and should be always presented zero-padded to six
123 virtual void DisplayPasskey(BluetoothDevice
* device
,
126 // This method will be called when the Bluetooth daemon requires that the
127 // user confirm that the Passkey |passkey| is displayed on the screen
128 // of the device |device| so that it may be authenticated. The delegate
129 // should display to the user and ask for confirmation, then call
130 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
131 // to reject or CancelPairing() on the device to cancel authentication
132 // for any other reason.
134 // This is used for Bluetooth 2.1 and later devices that support display,
135 // such as other computers or phones. The Passkey is a numeric in the
136 // range 0-999999 and should be always present zero-padded to six
138 virtual void ConfirmPasskey(BluetoothDevice
* device
,
141 // This method will be called when any previous DisplayPinCode(),
142 // DisplayPasskey() or ConfirmPasskey() request should be concluded
143 // and removed from the user.
144 virtual void DismissDisplayOrConfirm() = 0;
147 // Returns true if uuid is in a a valid canonical format
148 // (see utils::CanonicalUuid).
149 static bool IsUUIDValid(const std::string
& uuid
);
151 virtual ~BluetoothDevice();
153 // Returns the Bluetooth of address the device. This should be used as
154 // a unique key to identify the device and copied where needed.
155 virtual std::string
GetAddress() const = 0;
157 // Returns the name of the device suitable for displaying, this may
158 // be a synthesied string containing the address and localized type name
159 // if the device has no obtained name.
160 virtual string16
GetName() const;
162 // Returns the type of the device, limited to those we support or are
163 // aware of, by decoding the bluetooth class information. The returned
164 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
165 // DEVICE_PERIPHERAL.
166 DeviceType
GetDeviceType() const;
168 // Indicates whether the device is paired with the adapter.
169 virtual bool IsPaired() const = 0;
171 // Indicates whether the device is currently connected to the adapter.
172 virtual bool IsConnected() const = 0;
174 // Indicates whether the paired device accepts connections initiated from the
175 // adapter. This value is undefined for unpaired devices.
176 virtual bool IsConnectable() const = 0;
178 // Indicates whether there is a call to Connect() ongoing. For this attribute,
179 // we consider a call is ongoing if none of the callbacks passed to Connect()
180 // were called after the corresponding call to Connect().
181 virtual bool IsConnecting() const = 0;
183 // Returns the services (as UUID strings) that this device provides.
184 typedef std::vector
<std::string
> ServiceList
;
185 virtual ServiceList
GetServices() const = 0;
187 // The ErrorCallback is used for methods that can fail in which case it
188 // is called, in the success case the callback is simply not called.
189 typedef base::Callback
<void()> ErrorCallback
;
191 // The ConnectErrorCallback is used for methods that can fail with an error,
192 // passed back as an error code argument to this callback.
193 // In the success case this callback is not called.
194 typedef base::Callback
<void(enum ConnectErrorCode
)> ConnectErrorCallback
;
196 // Returns the services (as BluetoothServiceRecord objects) that this device
198 typedef ScopedVector
<BluetoothServiceRecord
> ServiceRecordList
;
199 typedef base::Callback
<void(const ServiceRecordList
&)> ServiceRecordsCallback
;
200 virtual void GetServiceRecords(const ServiceRecordsCallback
& callback
,
201 const ErrorCallback
& error_callback
) = 0;
203 // Indicates whether this device provides the given service.
204 virtual bool ProvidesServiceWithUUID(const std::string
& uuid
) const;
206 // The ProvidesServiceCallback is used by ProvidesServiceWithName to indicate
207 // whether or not a matching service was found.
208 typedef base::Callback
<void(bool)> ProvidesServiceCallback
;
210 // Indicates whether this device provides the given service.
211 virtual void ProvidesServiceWithName(
212 const std::string
& name
,
213 const ProvidesServiceCallback
& callback
) = 0;
215 // Indicates whether the device is currently pairing and expecting a
216 // PIN Code to be returned.
217 virtual bool ExpectingPinCode() const = 0;
219 // Indicates whether the device is currently pairing and expecting a
220 // Passkey to be returned.
221 virtual bool ExpectingPasskey() const = 0;
223 // Indicates whether the device is currently pairing and expecting
224 // confirmation of a displayed passkey.
225 virtual bool ExpectingConfirmation() const = 0;
227 // SocketCallback is used by ConnectToService to return a BluetoothSocket to
228 // the caller, or NULL if there was an error. The socket will remain open
229 // until the last reference to the returned BluetoothSocket is released.
230 typedef base::Callback
<void(scoped_refptr
<BluetoothSocket
>)>
233 // Initiates a connection to the device, pairing first if necessary.
235 // Method calls will be made on the supplied object |pairing_delegate|
236 // to indicate what display, and in response should make method calls
237 // back to the device object. Not all devices require user responses
238 // during pairing, so it is normal for |pairing_delegate| to receive no
239 // calls. To explicitly force a low-security connection without bonding,
240 // pass NULL, though this is ignored if the device is already paired.
242 // If the request fails, |error_callback| will be called; otherwise,
243 // |callback| is called when the request is complete.
244 // After calling Connect, CancelPairing should be called to cancel the pairing
245 // process and release |pairing_delegate_| if user cancels the pairing and
246 // closes the pairing UI.
247 virtual void Connect(PairingDelegate
* pairing_delegate
,
248 const base::Closure
& callback
,
249 const ConnectErrorCallback
& error_callback
) = 0;
251 // Sends the PIN code |pincode| to the remote device during pairing.
253 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
254 // for which there is no automatic pairing or special handling.
255 virtual void SetPinCode(const std::string
& pincode
) = 0;
257 // Sends the Passkey |passkey| to the remote device during pairing.
259 // Passkeys are generally required for Bluetooth 2.1 and later devices
260 // which cannot provide input or display on their own, and don't accept
261 // passkey-less pairing, and are a numeric in the range 0-999999.
262 virtual void SetPasskey(uint32 passkey
) = 0;
264 // Confirms to the remote device during pairing that a passkey provided by
265 // the ConfirmPasskey() delegate call is displayed on both devices.
266 virtual void ConfirmPairing() = 0;
268 // Rejects a pairing or connection request from a remote device.
269 virtual void RejectPairing() = 0;
271 // Cancels a pairing or connection attempt to a remote device or release
272 // |pairing_deleage_| and |agent_|.
273 virtual void CancelPairing() = 0;
275 // Disconnects the device, terminating the low-level ACL connection
276 // and any application connections using it. Link keys and other pairing
277 // information are not discarded, and the device object is not deleted.
278 // If the request fails, |error_callback| will be called; otherwise,
279 // |callback| is called when the request is complete.
280 virtual void Disconnect(const base::Closure
& callback
,
281 const ErrorCallback
& error_callback
) = 0;
283 // Disconnects the device, terminating the low-level ACL connection
284 // and any application connections using it, and then discards link keys
285 // and other pairing information. The device object remainds valid until
286 // returing from the calling function, after which it should be assumed to
287 // have been deleted. If the request fails, |error_callback| will be called.
288 // There is no callback for success beause this object is often deleted
289 // before that callback would be called.
290 virtual void Forget(const ErrorCallback
& error_callback
) = 0;
292 // Attempts to open a socket to a service matching |uuid| on this device. If
293 // the connection is successful, |callback| is called with a BluetoothSocket.
294 // Otherwise |callback| is called with NULL. The socket is closed as soon as
295 // all references to the BluetoothSocket are released. Note that the
296 // BluetoothSocket object can outlive both this BluetoothDevice and the
297 // BluetoothAdapter for this device.
298 virtual void ConnectToService(const std::string
& service_uuid
,
299 const SocketCallback
& callback
) = 0;
301 // Sets the Out Of Band pairing data for this device to |data|. Exactly one
302 // of |callback| or |error_callback| will be run.
303 virtual void SetOutOfBandPairingData(
304 const BluetoothOutOfBandPairingData
& data
,
305 const base::Closure
& callback
,
306 const ErrorCallback
& error_callback
) = 0;
308 // Clears the Out Of Band pairing data for this device. Exactly one of
309 // |callback| or |error_callback| will be run.
310 virtual void ClearOutOfBandPairingData(
311 const base::Closure
& callback
,
312 const ErrorCallback
& error_callback
) = 0;
317 // Returns the Bluetooth class of the device, used by GetDeviceType().
318 virtual uint32
GetBluetoothClass() const = 0;
320 // Returns the internal name of the Bluetooth device, used by GetName().
321 virtual std::string
GetDeviceName() const = 0;
324 // Returns a localized string containing the device's bluetooth address and
325 // a device type for display when |name_| is empty.
326 string16
GetAddressWithLocalizedDeviceTypeName() const;
329 } // namespace device
331 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_