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