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/strings/string16.h"
15 #include "device/bluetooth/bluetooth_export.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "net/log/net_log.h"
25 class BluetoothGattConnection
;
26 class BluetoothGattService
;
27 class BluetoothSocket
;
30 // BluetoothDevice represents a remote Bluetooth device, both its properties and
31 // capabilities as discovered by a local adapter and actions that may be
32 // performed on the remove device such as pairing, connection and disconnection.
34 // The class is instantiated and managed by the BluetoothAdapter class
35 // and pointers should only be obtained from that class and not cached,
36 // instead use the GetAddress() method as a unique key for a device.
38 // Since the lifecycle of BluetoothDevice instances is managed by
39 // BluetoothAdapter, that class rather than this provides observer methods
40 // for devices coming and going, as well as properties being updated.
41 class DEVICE_BLUETOOTH_EXPORT BluetoothDevice
{
43 // Possible values that may be returned by GetVendorIDSource(),
44 // indicating different organisations that allocate the identifiers returned
50 VENDOR_ID_MAX_VALUE
= VENDOR_ID_USB
53 // Possible values that may be returned by GetDeviceType(), representing
54 // different types of bluetooth device that we support or are aware of
55 // decoded from the bluetooth class information.
70 DEVICE_KEYBOARD_MOUSE_COMBO
73 // The value returned if the RSSI or transmit power cannot be read.
74 static const int kUnknownPower
= 127;
76 struct DEVICE_BLUETOOTH_EXPORT ConnectionInfo
{
79 int max_transmit_power
;
82 ConnectionInfo(int rssi
, int transmit_power
, int max_transmit_power
);
86 // Possible errors passed back to an error callback function in case of a
87 // failed call to Connect().
88 enum ConnectErrorCode
{
96 ERROR_UNSUPPORTED_DEVICE
99 typedef std::vector
<BluetoothUUID
> UUIDList
;
101 // Interface for negotiating pairing of bluetooth devices.
102 class PairingDelegate
{
104 virtual ~PairingDelegate() {}
106 // This method will be called when the Bluetooth daemon requires a
107 // PIN Code for authentication of the device |device|, the delegate should
108 // obtain the code from the user and call SetPinCode() on the device to
109 // provide it, or RejectPairing() or CancelPairing() to reject or cancel
112 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
113 // for which there is no automatic pairing or special handling.
114 virtual void RequestPinCode(BluetoothDevice
* device
) = 0;
116 // This method will be called when the Bluetooth daemon requires a
117 // Passkey for authentication of the device |device|, the delegate should
118 // obtain the passkey from the user (a numeric in the range 0-999999) and
119 // call SetPasskey() on the device to provide it, or RejectPairing() or
120 // CancelPairing() to reject or cancel the request.
122 // Passkeys are generally required for Bluetooth 2.1 and later devices
123 // which cannot provide input or display on their own, and don't accept
124 // passkey-less pairing.
125 virtual void RequestPasskey(BluetoothDevice
* device
) = 0;
127 // This method will be called when the Bluetooth daemon requires that the
128 // user enter the PIN code |pincode| into the device |device| so that it
129 // may be authenticated.
131 // This is used for Bluetooth 2.0 and earlier keyboard devices, the
132 // |pincode| will always be a six-digit numeric in the range 000000-999999
133 // for compatibility with later specifications.
134 virtual void DisplayPinCode(BluetoothDevice
* device
,
135 const std::string
& pincode
) = 0;
137 // This method will be called when the Bluetooth daemon requires that the
138 // user enter the Passkey |passkey| into the device |device| so that it
139 // may be authenticated.
141 // This is used for Bluetooth 2.1 and later devices that support input
142 // but not display, such as keyboards. The Passkey is a numeric in the
143 // range 0-999999 and should be always presented zero-padded to six
145 virtual void DisplayPasskey(BluetoothDevice
* device
,
148 // This method will be called when the Bluetooth daemon gets a notification
149 // of a key entered on the device |device| while pairing with the device
150 // using a PIN code or a Passkey.
152 // This method will be called only after DisplayPinCode() or
153 // DisplayPasskey() method is called, but is not warranted to be called
154 // on every pairing process that requires a PIN code or a Passkey because
155 // some device may not support this feature.
157 // The |entered| value describes the number of keys entered so far,
158 // including the last [enter] key. A first call to KeysEntered() with
159 // |entered| as 0 will be sent when the device supports this feature.
160 virtual void KeysEntered(BluetoothDevice
* device
,
163 // This method will be called when the Bluetooth daemon requires that the
164 // user confirm that the Passkey |passkey| is displayed on the screen
165 // of the device |device| so that it may be authenticated. The delegate
166 // should display to the user and ask for confirmation, then call
167 // ConfirmPairing() on the device to confirm, RejectPairing() on the device
168 // to reject or CancelPairing() on the device to cancel authentication
169 // for any other reason.
171 // This is used for Bluetooth 2.1 and later devices that support display,
172 // such as other computers or phones. The Passkey is a numeric in the
173 // range 0-999999 and should be always present zero-padded to six
175 virtual void ConfirmPasskey(BluetoothDevice
* device
,
178 // This method will be called when the Bluetooth daemon requires that a
179 // pairing request, usually only incoming, using the just-works model is
180 // authorized. The delegate should decide whether the user should confirm
181 // or not, then call ConfirmPairing() on the device to confirm the pairing
182 // (whether by user action or by default), RejectPairing() on the device to
183 // reject or CancelPairing() on the device to cancel authorization for
185 virtual void AuthorizePairing(BluetoothDevice
* device
) = 0;
188 virtual ~BluetoothDevice();
190 // Returns the Bluetooth class of the device, used by GetDeviceType()
191 // and metrics logging,
192 virtual uint32
GetBluetoothClass() const = 0;
194 // Returns the identifier of the bluetooth device.
195 virtual std::string
GetIdentifier() const;
197 // Returns the Bluetooth of address the device. This should be used as
198 // a unique key to identify the device and copied where needed.
199 virtual std::string
GetAddress() const = 0;
201 // Returns the allocation source of the identifier returned by GetVendorID(),
202 // where available, or VENDOR_ID_UNKNOWN where not.
203 virtual VendorIDSource
GetVendorIDSource() const = 0;
205 // Returns the Vendor ID of the device, where available.
206 virtual uint16
GetVendorID() const = 0;
208 // Returns the Product ID of the device, where available.
209 virtual uint16
GetProductID() const = 0;
211 // Returns the Device ID of the device, typically the release or version
212 // number in BCD format, where available.
213 virtual uint16
GetDeviceID() const = 0;
215 // Returns the name of the device suitable for displaying, this may
216 // be a synthesized string containing the address and localized type name
217 // if the device has no obtained name.
218 virtual base::string16
GetName() const;
220 // Returns the type of the device, limited to those we support or are
221 // aware of, by decoding the bluetooth class information. The returned
222 // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
223 // DEVICE_PERIPHERAL.
224 DeviceType
GetDeviceType() const;
226 // Indicates whether the device is known to support pairing based on its
227 // device class and address.
228 bool IsPairable() const;
230 // Indicates whether the device is paired with the adapter.
231 virtual bool IsPaired() const = 0;
233 // Indicates whether the device is currently connected to the adapter.
234 // Note that if IsConnected() is true, does not imply that the device is
235 // connected to any application or service. If the device is not paired, it
236 // could be still connected to the adapter for other reason, for example, to
237 // request the adapter's SDP records. The same holds for paired devices, since
238 // they could be connected to the adapter but not to an application.
239 virtual bool IsConnected() const = 0;
241 // Indicates whether the paired device accepts connections initiated from the
242 // adapter. This value is undefined for unpaired devices.
243 virtual bool IsConnectable() const = 0;
245 // Indicates whether there is a call to Connect() ongoing. For this attribute,
246 // we consider a call is ongoing if none of the callbacks passed to Connect()
247 // were called after the corresponding call to Connect().
248 virtual bool IsConnecting() const = 0;
250 // Indicates whether the device can be trusted, based on device properties,
251 // such as vendor and product id.
252 bool IsTrustable() const;
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 virtual UUIDList
GetUUIDs() const = 0;
260 // The received signal strength, in dBm. This field is avaliable and valid
261 // only during discovery. If not during discovery, or RSSI wasn't reported,
262 // this method will return |kUnknownPower|.
263 virtual int16
GetInquiryRSSI() const = 0;
265 // The transmitted power level. This field is avaliable only for LE devices
266 // that include this field in AD. It is avaliable and valid only during
267 // discovery. If not during discovery, or TxPower wasn't reported, this
268 // method will return |kUnknownPower|.
269 virtual int16
GetInquiryTxPower() const = 0;
271 // The ErrorCallback is used for methods that can fail in which case it
272 // is called, in the success case the callback is simply not called.
273 typedef base::Callback
<void()> ErrorCallback
;
275 // The ConnectErrorCallback is used for methods that can fail with an error,
276 // passed back as an error code argument to this callback.
277 // In the success case this callback is not called.
278 typedef base::Callback
<void(enum ConnectErrorCode
)> ConnectErrorCallback
;
280 typedef base::Callback
<void(const ConnectionInfo
&)> ConnectionInfoCallback
;
282 // Indicates whether the device is currently pairing and expecting a
283 // PIN Code to be returned.
284 virtual bool ExpectingPinCode() const = 0;
286 // Indicates whether the device is currently pairing and expecting a
287 // Passkey to be returned.
288 virtual bool ExpectingPasskey() const = 0;
290 // Indicates whether the device is currently pairing and expecting
291 // confirmation of a displayed passkey.
292 virtual bool ExpectingConfirmation() const = 0;
294 // Returns the RSSI and TX power of the active connection to the device:
296 // The RSSI indicates the power present in the received radio signal, measured
297 // in dBm, to a resolution of 1dBm. Larger (typically, less negative) values
298 // indicate a stronger signal.
300 // The transmit power indicates the strength of the signal broadcast from the
301 // host's Bluetooth antenna when communicating with the device, measured in
302 // dBm, to a resolution of 1dBm. Larger (typically, less negative) values
303 // indicate a stronger signal.
305 // If the device isn't connected, then the ConnectionInfo struct passed into
306 // the callback will be populated with |kUnknownPower|.
307 virtual void GetConnectionInfo(const ConnectionInfoCallback
& callback
) = 0;
309 // Initiates a connection to the device, pairing first if necessary.
311 // Method calls will be made on the supplied object |pairing_delegate|
312 // to indicate what display, and in response should make method calls
313 // back to the device object. Not all devices require user responses
314 // during pairing, so it is normal for |pairing_delegate| to receive no
315 // calls. To explicitly force a low-security connection without bonding,
316 // pass NULL, though this is ignored if the device is already paired.
318 // If the request fails, |error_callback| will be called; otherwise,
319 // |callback| is called when the request is complete.
320 // After calling Connect, CancelPairing should be called to cancel the pairing
321 // process and release the pairing delegate if user cancels the pairing and
322 // closes the pairing UI.
323 virtual void Connect(PairingDelegate
* pairing_delegate
,
324 const base::Closure
& callback
,
325 const ConnectErrorCallback
& error_callback
) = 0;
327 // Sends the PIN code |pincode| to the remote device during pairing.
329 // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
330 // for which there is no automatic pairing or special handling.
331 virtual void SetPinCode(const std::string
& pincode
) = 0;
333 // Sends the Passkey |passkey| to the remote device during pairing.
335 // Passkeys are generally required for Bluetooth 2.1 and later devices
336 // which cannot provide input or display on their own, and don't accept
337 // passkey-less pairing, and are a numeric in the range 0-999999.
338 virtual void SetPasskey(uint32 passkey
) = 0;
340 // Confirms to the remote device during pairing that a passkey provided by
341 // the ConfirmPasskey() delegate call is displayed on both devices.
342 virtual void ConfirmPairing() = 0;
344 // Rejects a pairing or connection request from a remote device.
345 virtual void RejectPairing() = 0;
347 // Cancels a pairing or connection attempt to a remote device, releasing
348 // the pairing delegate.
349 virtual void CancelPairing() = 0;
351 // Disconnects the device, terminating the low-level ACL connection
352 // and any application connections using it. Link keys and other pairing
353 // information are not discarded, and the device object is not deleted.
354 // If the request fails, |error_callback| will be called; otherwise,
355 // |callback| is called when the request is complete.
356 virtual void Disconnect(const base::Closure
& callback
,
357 const ErrorCallback
& error_callback
) = 0;
359 // Disconnects the device, terminating the low-level ACL connection
360 // and any application connections using it, and then discards link keys
361 // and other pairing information. The device object remains valid until
362 // returning from the calling function, after which it should be assumed to
363 // have been deleted. If the request fails, |error_callback| will be called.
364 // There is no callback for success because this object is often deleted
365 // before that callback would be called.
366 virtual void Forget(const ErrorCallback
& error_callback
) = 0;
368 // Attempts to initiate an outgoing L2CAP or RFCOMM connection to the
369 // advertised service on this device matching |uuid|, performing an SDP lookup
370 // if necessary to determine the correct protocol and channel for the
371 // connection. |callback| will be called on a successful connection with a
372 // BluetoothSocket instance that is to be owned by the receiver.
373 // |error_callback| will be called on failure with a message indicating the
375 typedef base::Callback
<void(scoped_refptr
<BluetoothSocket
>)>
376 ConnectToServiceCallback
;
377 typedef base::Callback
<void(const std::string
& message
)>
378 ConnectToServiceErrorCallback
;
379 virtual void ConnectToService(
380 const BluetoothUUID
& uuid
,
381 const ConnectToServiceCallback
& callback
,
382 const ConnectToServiceErrorCallback
& error_callback
) = 0;
384 // Attempts to initiate an insecure outgoing L2CAP or RFCOMM connection to the
385 // advertised service on this device matching |uuid|, performing an SDP lookup
386 // if necessary to determine the correct protocol and channel for the
387 // connection. Unlike ConnectToService, the outgoing connection will request
388 // no bonding rather than general bonding. |callback| will be called on a
389 // successful connection with a BluetoothSocket instance that is to be owned
390 // by the receiver. |error_callback| will be called on failure with a message
391 // indicating the cause.
392 virtual void ConnectToServiceInsecurely(
393 const device::BluetoothUUID
& uuid
,
394 const ConnectToServiceCallback
& callback
,
395 const ConnectToServiceErrorCallback
& error_callback
) = 0;
397 // Opens a new GATT connection to this device. On success, a new
398 // BluetoothGattConnection will be handed to the caller via |callback|. On
399 // error, |error_callback| will be called. The connection will be kept alive,
400 // as long as there is at least one active GATT connection. In the case that
401 // the underlying connection gets terminated, either due to a call to
402 // BluetoothDevice::Disconnect or other unexpected circumstances, the
403 // returned BluetoothGattConnection will be automatically marked as inactive.
404 // To monitor the state of the connection, observe the
405 // BluetoothAdapter::Observer::DeviceChanged method.
406 typedef base::Callback
<void(scoped_ptr
<BluetoothGattConnection
>)>
407 GattConnectionCallback
;
408 virtual void CreateGattConnection(
409 const GattConnectionCallback
& callback
,
410 const ConnectErrorCallback
& error_callback
) = 0;
412 // Returns the list of discovered GATT services.
413 virtual std::vector
<BluetoothGattService
*> GetGattServices() const;
415 // Returns the GATT service with device-specific identifier |identifier|.
416 // Returns NULL, if no such service exists.
417 virtual BluetoothGattService
* GetGattService(
418 const std::string
& identifier
) const;
420 // Returns service data of a service given its UUID.
421 virtual base::BinaryValue
* GetServiceData(BluetoothUUID serviceUUID
) const;
423 // Returns the list UUIDs of services that have service data.
424 virtual UUIDList
GetServiceDataUUIDs() const;
426 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where
427 // each 'X' is a hex digit. If the input |address| is invalid, returns an
429 static std::string
CanonicalizeAddress(const std::string
& address
);
434 // Returns the internal name of the Bluetooth device, used by GetName().
435 virtual std::string
GetDeviceName() const = 0;
437 // Clears the list of service data.
438 void ClearServiceData();
440 // Set the data of a given service designated by its UUID.
441 void SetServiceData(BluetoothUUID serviceUUID
, const char* buffer
,
444 // Mapping from the platform-specific GATT service identifiers to
445 // BluetoothGattService objects.
446 typedef std::map
<std::string
, BluetoothGattService
*> GattServiceMap
;
447 GattServiceMap gatt_services_
;
449 // Mapping from service UUID represented as a std::string of a bluetooth
451 // the specific data. The data is stored as BinaryValue.
452 scoped_ptr
<base::DictionaryValue
> services_data_
;
455 // Returns a localized string containing the device's bluetooth address and
456 // a device type for display when |name_| is empty.
457 base::string16
GetAddressWithLocalizedDeviceTypeName() const;
460 } // namespace device
462 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_