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/strings/string16.h"
17 class BluetoothProfile
;
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 GetVendorIDSource(),
36 // indicating different organisations that allocate the identifiers returned
44 // Possible values that may be returned by GetDeviceType(), representing
45 // different types of bluetooth device that we support or are aware of
46 // decoded from the bluetooth class information.
61 DEVICE_KEYBOARD_MOUSE_COMBO
64 // Possible errors passed back to an error callback function in case of a
65 // failed call to Connect().
66 enum ConnectErrorCode
{
74 ERROR_UNSUPPORTED_DEVICE
77 // Interface for observing changes from bluetooth devices.
80 virtual ~Observer() {}
82 // TODO(keybuk): add observers for pairing and connection.
85 // Interface for negotiating pairing of bluetooth devices.
86 class PairingDelegate
{
88 virtual ~PairingDelegate() {}
90 // This method will be called when the Bluetooth daemon requires a
91 // PIN Code for authentication of the device |device|, the delegate should
92 // obtain the code from the user and call SetPinCode() on the device to
93 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
96 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
97 // for which there is no automatic pairing or special handling.
98 virtual void RequestPinCode(BluetoothDevice
* device
) = 0;
100 // This method will be called when the Bluetooth daemon requires a
101 // Passkey for authentication of the device |device|, the delegate should
102 // obtain the passkey from the user (a numeric in the range 0-999999) and
103 // call SetPasskey() on the device to provide it, or RejectPairing() or
104 // CancelPairing() to reject or cancel the request.
106 // Passkeys are generally required for Bluetooth 2.1 and later devices
107 // which cannot provide input or display on their own, and don't accept
108 // passkey-less pairing.
109 virtual void RequestPasskey(BluetoothDevice
* device
) = 0;
111 // This method will be called when the Bluetooth daemon requires that the
112 // user enter the PIN code |pincode| into the device |device| so that it
113 // may be authenticated.
115 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
116 // |pincode| will always be a six-digit numeric in the range 000000-999999
117 // for compatibility with later specifications.
118 virtual void DisplayPinCode(BluetoothDevice
* device
,
119 const std::string
& pincode
) = 0;
121 // This method will be called when the Bluetooth daemon requires that the
122 // user enter the Passkey |passkey| into the device |device| so that it
123 // may be authenticated.
125 // This is used for Bluetooth 2.1 and later devices that support input
126 // but not display, such as keyboards. The Passkey is a numeric in the
127 // range 0-999999 and should be always presented zero-padded to six
129 virtual void DisplayPasskey(BluetoothDevice
* device
,
132 // This method will be called when the Bluetooth daemon gets a notification
133 // of a key entered on the device |device| while pairing with the device
134 // using a PIN code or a Passkey.
136 // This method will be called only after DisplayPinCode() or
137 // DisplayPasskey() method is called, but is not warranted to be called
138 // on every pairing process that requires a PIN code or a Passkey because
139 // some device may not support this feature.
141 // The |entered| value describes the number of keys entered so far,
142 // including the last [enter] key. A first call to KeysEntered() with
143 // |entered| as 0 will be sent when the device supports this feature.
144 virtual void KeysEntered(BluetoothDevice
* device
,
147 // This method will be called when the Bluetooth daemon requires that the
148 // user confirm that the Passkey |passkey| is displayed on the screen
149 // of the device |device| so that it may be authenticated. The delegate
150 // should display to the user and ask for confirmation, then call
151 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
152 // to reject or CancelPairing() on the device to cancel authentication
153 // for any other reason.
155 // This is used for Bluetooth 2.1 and later devices that support display,
156 // such as other computers or phones. The Passkey is a numeric in the
157 // range 0-999999 and should be always present zero-padded to six
159 virtual void ConfirmPasskey(BluetoothDevice
* device
,
162 // This method will be called when the Bluetooth daemon requires that a
163 // pairing request, usually only incoming, using the just-works model is
164 // authorized. The delegate should decide whether the user should confirm
165 // or not, then call ConfirmPairing() on the device to confirm the pairing
166 // (whether by user action or by default), RejectPairing() on the device to
167 // reject or CancelPairing() on the device to cancel authorization for
169 virtual void AuthorizePairing(BluetoothDevice
* device
) = 0;
172 // Returns true if uuid is in a a valid canonical format
173 // (see utils::CanonicalUuid).
174 static bool IsUUIDValid(const std::string
& uuid
);
176 virtual ~BluetoothDevice();
178 // Returns the Bluetooth class of the device, used by GetDeviceType()
179 // and metrics logging,
180 virtual uint32
GetBluetoothClass() const = 0;
182 // Returns the Bluetooth of address the device. This should be used as
183 // a unique key to identify the device and copied where needed.
184 virtual std::string
GetAddress() const = 0;
186 // Returns the allocation source of the identifier returned by GetVendorID(),
187 // where available, or VENDOR_ID_UNKNOWN where not.
188 virtual VendorIDSource
GetVendorIDSource() const = 0;
190 // Returns the Vendor ID of the device, where available.
191 virtual uint16
GetVendorID() const = 0;
193 // Returns the Product ID of the device, where available.
194 virtual uint16
GetProductID() const = 0;
196 // Returns the Device ID of the device, typically the release or version
197 // number in BCD format, where available.
198 virtual uint16
GetDeviceID() const = 0;
200 // Returns the name of the device suitable for displaying, this may
201 // be a synthesized string containing the address and localized type name
202 // if the device has no obtained name.
203 virtual base::string16
GetName() const;
205 // Returns the type of the device, limited to those we support or are
206 // aware of, by decoding the bluetooth class information. The returned
207 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
208 // DEVICE_PERIPHERAL.
209 DeviceType
GetDeviceType() const;
211 // Indicates whether the device is known to support pairing based on its
212 // device class and address.
213 bool IsPairable() const;
215 // Indicates whether the device is paired with the adapter.
216 virtual bool IsPaired() const = 0;
218 // Indicates whether the device is currently connected to the adapter.
219 // Note that if IsConnected() is true, does not imply that the device is
220 // connected to any application or service. If the device is not paired, it
221 // could be still connected to the adapter for other reason, for example, to
222 // request the adapter's SDP records. The same holds for paired devices, since
223 // they could be connected to the adapter but not to an application.
224 virtual bool IsConnected() const = 0;
226 // Indicates whether the paired device accepts connections initiated from the
227 // adapter. This value is undefined for unpaired devices.
228 virtual bool IsConnectable() const = 0;
230 // Indicates whether there is a call to Connect() ongoing. For this attribute,
231 // we consider a call is ongoing if none of the callbacks passed to Connect()
232 // were called after the corresponding call to Connect().
233 virtual bool IsConnecting() const = 0;
235 // Returns the set of UUIDs that this device supports. For classic Bluetooth
236 // devices this data is collected from both the EIR data and SDP tables,
237 // for Low Energy devices this data is collected from AD and GATT primary
238 // services, for dual mode devices this may be collected from both./
240 // All UUIDs are returned in the canonical 128-bit format.
241 typedef std::vector
<std::string
> UUIDList
;
242 virtual UUIDList
GetUUIDs() const = 0;
244 // The ErrorCallback is used for methods that can fail in which case it
245 // is called, in the success case the callback is simply not called.
246 typedef base::Callback
<void()> ErrorCallback
;
248 // The ConnectErrorCallback is used for methods that can fail with an error,
249 // passed back as an error code argument to this callback.
250 // In the success case this callback is not called.
251 typedef base::Callback
<void(enum ConnectErrorCode
)> ConnectErrorCallback
;
253 // Indicates whether the device is currently pairing and expecting a
254 // PIN Code to be returned.
255 virtual bool ExpectingPinCode() const = 0;
257 // Indicates whether the device is currently pairing and expecting a
258 // Passkey to be returned.
259 virtual bool ExpectingPasskey() const = 0;
261 // Indicates whether the device is currently pairing and expecting
262 // confirmation of a displayed passkey.
263 virtual bool ExpectingConfirmation() const = 0;
265 // SocketCallback is used by ConnectToService to return a BluetoothSocket to
266 // the caller, or NULL if there was an error. The socket will remain open
267 // until the last reference to the returned BluetoothSocket is released.
268 typedef base::Callback
<void(scoped_refptr
<BluetoothSocket
>)>
271 // Initiates a connection to the device, pairing first if necessary.
273 // Method calls will be made on the supplied object |pairing_delegate|
274 // to indicate what display, and in response should make method calls
275 // back to the device object. Not all devices require user responses
276 // during pairing, so it is normal for |pairing_delegate| to receive no
277 // calls. To explicitly force a low-security connection without bonding,
278 // pass NULL, though this is ignored if the device is already paired.
280 // If the request fails, |error_callback| will be called; otherwise,
281 // |callback| is called when the request is complete.
282 // After calling Connect, CancelPairing should be called to cancel the pairing
283 // process and release the pairing delegate if user cancels the pairing and
284 // closes the pairing UI.
285 virtual void Connect(PairingDelegate
* pairing_delegate
,
286 const base::Closure
& callback
,
287 const ConnectErrorCallback
& error_callback
) = 0;
289 // Sends the PIN code |pincode| to the remote device during pairing.
291 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
292 // for which there is no automatic pairing or special handling.
293 virtual void SetPinCode(const std::string
& pincode
) = 0;
295 // Sends the Passkey |passkey| to the remote device during pairing.
297 // Passkeys are generally required for Bluetooth 2.1 and later devices
298 // which cannot provide input or display on their own, and don't accept
299 // passkey-less pairing, and are a numeric in the range 0-999999.
300 virtual void SetPasskey(uint32 passkey
) = 0;
302 // Confirms to the remote device during pairing that a passkey provided by
303 // the ConfirmPasskey() delegate call is displayed on both devices.
304 virtual void ConfirmPairing() = 0;
306 // Rejects a pairing or connection request from a remote device.
307 virtual void RejectPairing() = 0;
309 // Cancels a pairing or connection attempt to a remote device, releasing
310 // the pairing delegate.
311 virtual void CancelPairing() = 0;
313 // Disconnects the device, terminating the low-level ACL connection
314 // and any application connections using it. Link keys and other pairing
315 // information are not discarded, and the device object is not deleted.
316 // If the request fails, |error_callback| will be called; otherwise,
317 // |callback| is called when the request is complete.
318 virtual void Disconnect(const base::Closure
& callback
,
319 const ErrorCallback
& error_callback
) = 0;
321 // Disconnects the device, terminating the low-level ACL connection
322 // and any application connections using it, and then discards link keys
323 // and other pairing information. The device object remains valid until
324 // returning from the calling function, after which it should be assumed to
325 // have been deleted. If the request fails, |error_callback| will be called.
326 // There is no callback for success because this object is often deleted
327 // before that callback would be called.
328 virtual void Forget(const ErrorCallback
& error_callback
) = 0;
330 // Attempts to open a socket to a service matching |uuid| on this device. If
331 // the connection is successful, |callback| is called with a BluetoothSocket.
332 // Otherwise |callback| is called with NULL. The socket is closed as soon as
333 // all references to the BluetoothSocket are released. Note that the
334 // BluetoothSocket object can outlive both this BluetoothDevice and the
335 // BluetoothAdapter for this device.
336 virtual void ConnectToService(const std::string
& service_uuid
,
337 const SocketCallback
& callback
) = 0;
339 // Attempts to initiate an outgoing connection to this device for the profile
340 // identified by |profile|, on success the profile's connection callback
341 // will be called as well as |callback|; on failure |error_callback| will be
343 virtual void ConnectToProfile(BluetoothProfile
* profile
,
344 const base::Closure
& callback
,
345 const ErrorCallback
& error_callback
) = 0;
347 // Sets the Out Of Band pairing data for this device to |data|. Exactly one
348 // of |callback| or |error_callback| will be run.
349 virtual void SetOutOfBandPairingData(
350 const BluetoothOutOfBandPairingData
& data
,
351 const base::Closure
& callback
,
352 const ErrorCallback
& error_callback
) = 0;
354 // Clears the Out Of Band pairing data for this device. Exactly one of
355 // |callback| or |error_callback| will be run.
356 virtual void ClearOutOfBandPairingData(
357 const base::Closure
& callback
,
358 const ErrorCallback
& error_callback
) = 0;
363 // Returns the internal name of the Bluetooth device, used by GetName().
364 virtual std::string
GetDeviceName() const = 0;
367 // Returns a localized string containing the device's bluetooth address and
368 // a device type for display when |name_| is empty.
369 base::string16
GetAddressWithLocalizedDeviceTypeName() const;
372 } // namespace device
374 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_