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_
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/string16.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "net/base/net_log.h"
21 class BluetoothGattService
;
22 class BluetoothProfile
;
23 class BluetoothSocket
;
25 struct BluetoothOutOfBandPairingData
;
27 // BluetoothDevice represents a remote Bluetooth device, both its properties and
28 // capabilities as discovered by a local adapter and actions that may be
29 // performed on the remove device such as pairing, connection and disconnection.
31 // The class is instantiated and managed by the BluetoothAdapter class
32 // and pointers should only be obtained from that class and not cached,
33 // instead use the GetAddress() method as a unique key for a device.
35 // Since the lifecycle of BluetoothDevice instances is managed by
36 // BluetoothAdapter, that class rather than this provides observer methods
37 // for devices coming and going, as well as properties being updated.
38 class BluetoothDevice
{
40 // Possible values that may be returned by GetVendorIDSource(),
41 // indicating different organisations that allocate the identifiers returned
49 // Possible values that may be returned by GetDeviceType(), representing
50 // different types of bluetooth device that we support or are aware of
51 // decoded from the bluetooth class information.
66 DEVICE_KEYBOARD_MOUSE_COMBO
69 // Possible errors passed back to an error callback function in case of a
70 // failed call to Connect().
71 enum ConnectErrorCode
{
79 ERROR_UNSUPPORTED_DEVICE
82 // Interface for observing changes from bluetooth devices.
85 virtual ~Observer() {}
87 // Called when a new GATT service |service| is added to the device |device|,
88 // as the service is received from the device. Don't cache |service|. Store
89 // its identifier instead (i.e. BluetoothGattService::GetIdentifier).
90 virtual void GattServiceAdded(BluetoothDevice
* device
,
91 BluetoothGattService
* service
) {}
93 // Called when the GATT service |service| is removed from the device
94 // |device|. This can happen if the attribute database of the remote device
95 // changes or when |device| gets removed.
96 virtual void GattServiceRemoved(BluetoothDevice
* device
,
97 BluetoothGattService
* service
) {}
99 // TODO(keybuk): add observers for pairing and connection.
102 // Interface for negotiating pairing of bluetooth devices.
103 class PairingDelegate
{
105 virtual ~PairingDelegate() {}
107 // This method will be called when the Bluetooth daemon requires a
108 // PIN Code for authentication of the device |device|, the delegate should
109 // obtain the code from the user and call SetPinCode() on the device to
110 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
113 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
114 // for which there is no automatic pairing or special handling.
115 virtual void RequestPinCode(BluetoothDevice
* device
) = 0;
117 // This method will be called when the Bluetooth daemon requires a
118 // Passkey for authentication of the device |device|, the delegate should
119 // obtain the passkey from the user (a numeric in the range 0-999999) and
120 // call SetPasskey() on the device to provide it, or RejectPairing() or
121 // CancelPairing() to reject or cancel the request.
123 // Passkeys are generally required for Bluetooth 2.1 and later devices
124 // which cannot provide input or display on their own, and don't accept
125 // passkey-less pairing.
126 virtual void RequestPasskey(BluetoothDevice
* device
) = 0;
128 // This method will be called when the Bluetooth daemon requires that the
129 // user enter the PIN code |pincode| into the device |device| so that it
130 // may be authenticated.
132 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
133 // |pincode| will always be a six-digit numeric in the range 000000-999999
134 // for compatibility with later specifications.
135 virtual void DisplayPinCode(BluetoothDevice
* device
,
136 const std::string
& pincode
) = 0;
138 // This method will be called when the Bluetooth daemon requires that the
139 // user enter the Passkey |passkey| into the device |device| so that it
140 // may be authenticated.
142 // This is used for Bluetooth 2.1 and later devices that support input
143 // but not display, such as keyboards. The Passkey is a numeric in the
144 // range 0-999999 and should be always presented zero-padded to six
146 virtual void DisplayPasskey(BluetoothDevice
* device
,
149 // This method will be called when the Bluetooth daemon gets a notification
150 // of a key entered on the device |device| while pairing with the device
151 // using a PIN code or a Passkey.
153 // This method will be called only after DisplayPinCode() or
154 // DisplayPasskey() method is called, but is not warranted to be called
155 // on every pairing process that requires a PIN code or a Passkey because
156 // some device may not support this feature.
158 // The |entered| value describes the number of keys entered so far,
159 // including the last [enter] key. A first call to KeysEntered() with
160 // |entered| as 0 will be sent when the device supports this feature.
161 virtual void KeysEntered(BluetoothDevice
* device
,
164 // This method will be called when the Bluetooth daemon requires that the
165 // user confirm that the Passkey |passkey| is displayed on the screen
166 // of the device |device| so that it may be authenticated. The delegate
167 // should display to the user and ask for confirmation, then call
168 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
169 // to reject or CancelPairing() on the device to cancel authentication
170 // for any other reason.
172 // This is used for Bluetooth 2.1 and later devices that support display,
173 // such as other computers or phones. The Passkey is a numeric in the
174 // range 0-999999 and should be always present zero-padded to six
176 virtual void ConfirmPasskey(BluetoothDevice
* device
,
179 // This method will be called when the Bluetooth daemon requires that a
180 // pairing request, usually only incoming, using the just-works model is
181 // authorized. The delegate should decide whether the user should confirm
182 // or not, then call ConfirmPairing() on the device to confirm the pairing
183 // (whether by user action or by default), RejectPairing() on the device to
184 // reject or CancelPairing() on the device to cancel authorization for
186 virtual void AuthorizePairing(BluetoothDevice
* device
) = 0;
189 virtual ~BluetoothDevice();
191 // Adds and removes observers for events on this Bluetooth device. If
192 // monitoring multiple devices, check the |device| parameter of the observer
193 // methods to determine which device is issuing the event.
194 virtual void AddObserver(Observer
* observer
) = 0;
195 virtual void RemoveObserver(Observer
* observer
) = 0;
197 // Returns the Bluetooth class of the device, used by GetDeviceType()
198 // and metrics logging,
199 virtual uint32
GetBluetoothClass() const = 0;
201 // Returns the Bluetooth of address the device. This should be used as
202 // a unique key to identify the device and copied where needed.
203 virtual std::string
GetAddress() const = 0;
205 // Returns the allocation source of the identifier returned by GetVendorID(),
206 // where available, or VENDOR_ID_UNKNOWN where not.
207 virtual VendorIDSource
GetVendorIDSource() const = 0;
209 // Returns the Vendor ID of the device, where available.
210 virtual uint16
GetVendorID() const = 0;
212 // Returns the Product ID of the device, where available.
213 virtual uint16
GetProductID() const = 0;
215 // Returns the Device ID of the device, typically the release or version
216 // number in BCD format, where available.
217 virtual uint16
GetDeviceID() const = 0;
219 // Returns the name of the device suitable for displaying, this may
220 // be a synthesized string containing the address and localized type name
221 // if the device has no obtained name.
222 virtual base::string16
GetName() const;
224 // Returns the type of the device, limited to those we support or are
225 // aware of, by decoding the bluetooth class information. The returned
226 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
227 // DEVICE_PERIPHERAL.
228 DeviceType
GetDeviceType() const;
230 // Indicates whether the device is known to support pairing based on its
231 // device class and address.
232 bool IsPairable() const;
234 // Indicates whether the device is paired with the adapter.
235 virtual bool IsPaired() const = 0;
237 // Indicates whether the device is currently connected to the adapter.
238 // Note that if IsConnected() is true, does not imply that the device is
239 // connected to any application or service. If the device is not paired, it
240 // could be still connected to the adapter for other reason, for example, to
241 // request the adapter's SDP records. The same holds for paired devices, since
242 // they could be connected to the adapter but not to an application.
243 virtual bool IsConnected() const = 0;
245 // Indicates whether the paired device accepts connections initiated from the
246 // adapter. This value is undefined for unpaired devices.
247 virtual bool IsConnectable() const = 0;
249 // Indicates whether there is a call to Connect() ongoing. For this attribute,
250 // we consider a call is ongoing if none of the callbacks passed to Connect()
251 // were called after the corresponding call to Connect().
252 virtual bool IsConnecting() const = 0;
254 // Returns the set of UUIDs that this device supports. For classic Bluetooth
255 // devices this data is collected from both the EIR data and SDP tables,
256 // for Low Energy devices this data is collected from AD and GATT primary
257 // services, for dual mode devices this may be collected from both./
258 typedef std::vector
<BluetoothUUID
> UUIDList
;
259 virtual UUIDList
GetUUIDs() const = 0;
261 // The ErrorCallback is used for methods that can fail in which case it
262 // is called, in the success case the callback is simply not called.
263 typedef base::Callback
<void()> ErrorCallback
;
265 // The ConnectErrorCallback is used for methods that can fail with an error,
266 // passed back as an error code argument to this callback.
267 // In the success case this callback is not called.
268 typedef base::Callback
<void(enum ConnectErrorCode
)> ConnectErrorCallback
;
270 // Indicates whether the device is currently pairing and expecting a
271 // PIN Code to be returned.
272 virtual bool ExpectingPinCode() const = 0;
274 // Indicates whether the device is currently pairing and expecting a
275 // Passkey to be returned.
276 virtual bool ExpectingPasskey() const = 0;
278 // Indicates whether the device is currently pairing and expecting
279 // confirmation of a displayed passkey.
280 virtual bool ExpectingConfirmation() const = 0;
282 // SocketCallback is used by ConnectToService to return a BluetoothSocket to
283 // the caller, or NULL if there was an error. The socket will remain open
284 // until the last reference to the returned BluetoothSocket is released.
285 typedef base::Callback
<void(scoped_refptr
<BluetoothSocket
>)>
288 // Initiates a connection to the device, pairing first if necessary.
290 // Method calls will be made on the supplied object |pairing_delegate|
291 // to indicate what display, and in response should make method calls
292 // back to the device object. Not all devices require user responses
293 // during pairing, so it is normal for |pairing_delegate| to receive no
294 // calls. To explicitly force a low-security connection without bonding,
295 // pass NULL, though this is ignored if the device is already paired.
297 // If the request fails, |error_callback| will be called; otherwise,
298 // |callback| is called when the request is complete.
299 // After calling Connect, CancelPairing should be called to cancel the pairing
300 // process and release the pairing delegate if user cancels the pairing and
301 // closes the pairing UI.
302 virtual void Connect(PairingDelegate
* pairing_delegate
,
303 const base::Closure
& callback
,
304 const ConnectErrorCallback
& error_callback
) = 0;
306 // Sends the PIN code |pincode| to the remote device during pairing.
308 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
309 // for which there is no automatic pairing or special handling.
310 virtual void SetPinCode(const std::string
& pincode
) = 0;
312 // Sends the Passkey |passkey| to the remote device during pairing.
314 // Passkeys are generally required for Bluetooth 2.1 and later devices
315 // which cannot provide input or display on their own, and don't accept
316 // passkey-less pairing, and are a numeric in the range 0-999999.
317 virtual void SetPasskey(uint32 passkey
) = 0;
319 // Confirms to the remote device during pairing that a passkey provided by
320 // the ConfirmPasskey() delegate call is displayed on both devices.
321 virtual void ConfirmPairing() = 0;
323 // Rejects a pairing or connection request from a remote device.
324 virtual void RejectPairing() = 0;
326 // Cancels a pairing or connection attempt to a remote device, releasing
327 // the pairing delegate.
328 virtual void CancelPairing() = 0;
330 // Disconnects the device, terminating the low-level ACL connection
331 // and any application connections using it. Link keys and other pairing
332 // information are not discarded, and the device object is not deleted.
333 // If the request fails, |error_callback| will be called; otherwise,
334 // |callback| is called when the request is complete.
335 virtual void Disconnect(const base::Closure
& callback
,
336 const ErrorCallback
& error_callback
) = 0;
338 // Disconnects the device, terminating the low-level ACL connection
339 // and any application connections using it, and then discards link keys
340 // and other pairing information. The device object remains valid until
341 // returning from the calling function, after which it should be assumed to
342 // have been deleted. If the request fails, |error_callback| will be called.
343 // There is no callback for success because this object is often deleted
344 // before that callback would be called.
345 virtual void Forget(const ErrorCallback
& error_callback
) = 0;
347 // Attempts to initiate an outgoing connection to this device for the profile
348 // identified by |profile|, on success the profile's connection callback
349 // will be called as well as |callback|; on failure |error_callback| will be
351 typedef base::Callback
<void(const std::string
&)>
352 ConnectToProfileErrorCallback
;
353 virtual void ConnectToProfile(
354 BluetoothProfile
* profile
,
355 const base::Closure
& callback
,
356 const ConnectToProfileErrorCallback
& error_callback
) = 0;
358 // Sets the Out Of Band pairing data for this device to |data|. Exactly one
359 // of |callback| or |error_callback| will be run.
360 virtual void SetOutOfBandPairingData(
361 const BluetoothOutOfBandPairingData
& data
,
362 const base::Closure
& callback
,
363 const ErrorCallback
& error_callback
) = 0;
365 // Clears the Out Of Band pairing data for this device. Exactly one of
366 // |callback| or |error_callback| will be run.
367 virtual void ClearOutOfBandPairingData(
368 const base::Closure
& callback
,
369 const ErrorCallback
& error_callback
) = 0;
371 // Returns the list of discovered GATT services.
372 std::vector
<BluetoothGattService
*> GetGattServices() const;
374 // Returns the GATT service with device-specific identifier |identifier|.
375 // Returns NULL, if no such service exists.
376 BluetoothGattService
* GetGattService(const std::string
& identifier
) const;
381 // Returns the internal name of the Bluetooth device, used by GetName().
382 virtual std::string
GetDeviceName() const = 0;
384 // Mapping from the platform-specific GATT service identifiers to
385 // BluetoothGattService objects.
386 typedef std::map
<std::string
, BluetoothGattService
*> GattServiceMap
;
387 GattServiceMap gatt_services_
;
390 // Returns a localized string containing the device's bluetooth address and
391 // a device type for display when |name_| is empty.
392 base::string16
GetAddressWithLocalizedDeviceTypeName() const;
395 } // namespace device
397 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_