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 #include "device/bluetooth/bluetooth_device.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/bluetooth_utils.h"
11 #include "grit/device_bluetooth_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
17 bool BluetoothDevice::IsUUIDValid(const std::string
& uuid
) {
18 return !bluetooth_utils::CanonicalUuid(uuid
).empty();
21 BluetoothDevice::BluetoothDevice() {
24 BluetoothDevice::~BluetoothDevice() {
27 base::string16
BluetoothDevice::GetName() const {
28 std::string name
= GetDeviceName();
30 return base::UTF8ToUTF16(name
);
32 return GetAddressWithLocalizedDeviceTypeName();
36 base::string16
BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
37 base::string16 address_utf16
= base::UTF8ToUTF16(GetAddress());
38 BluetoothDevice::DeviceType device_type
= GetDeviceType();
39 switch (device_type
) {
41 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER
,
44 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE
,
47 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM
,
50 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO
,
52 case DEVICE_CAR_AUDIO
:
53 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO
,
56 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO
,
59 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK
,
62 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD
,
65 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD
,
68 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE
,
71 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET
,
73 case DEVICE_KEYBOARD_MOUSE_COMBO
:
74 return l10n_util::GetStringFUTF16(
75 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO
, address_utf16
);
77 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN
,
82 BluetoothDevice::DeviceType
BluetoothDevice::GetDeviceType() const {
83 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
84 uint32 bluetooth_class
= GetBluetoothClass();
85 switch ((bluetooth_class
& 0x1f00) >> 8) {
87 // Computer major device class.
88 return DEVICE_COMPUTER
;
90 // Phone major device class.
91 switch ((bluetooth_class
& 0xfc) >> 2) {
95 // Cellular, cordless and smart phones.
99 // Modems: wired or voice gateway and common ISDN access.
104 // Audio major device class.
105 switch ((bluetooth_class
& 0xfc) >> 2) {
108 return DEVICE_CAR_AUDIO
;
122 // Peripheral major device class.
123 switch ((bluetooth_class
& 0xc0) >> 6) {
125 // "Not a keyboard or pointing device."
126 switch ((bluetooth_class
& 0x01e) >> 2) {
129 return DEVICE_JOYSTICK
;
132 return DEVICE_GAMEPAD
;
134 return DEVICE_PERIPHERAL
;
139 return DEVICE_KEYBOARD
;
142 switch ((bluetooth_class
& 0x01e) >> 2) {
145 return DEVICE_TABLET
;
153 return DEVICE_KEYBOARD_MOUSE_COMBO
;
158 return DEVICE_UNKNOWN
;
161 bool BluetoothDevice::IsPairable() const {
162 DeviceType type
= GetDeviceType();
164 // Get the vendor part of the address: "00:11:22" for "00:11:22:33:44:55"
165 std::string vendor
= GetAddress().substr(0, 8);
167 // Verbatim "Bluetooth Mouse", model 96674
168 if ((type
== DEVICE_MOUSE
&& vendor
== "00:12:A1") ||
169 // Microsoft "Microsoft Bluetooth Notebook Mouse 5000", model X807028-001
170 (type
== DEVICE_MOUSE
&& vendor
== "7C:ED:8D"))
172 // TODO: Move this database into a config file.
177 bool BluetoothDevice::ProvidesServiceWithUUID(
178 const std::string
& uuid
) const {
179 std::string canonical_uuid
= bluetooth_utils::CanonicalUuid(uuid
);
180 BluetoothDevice::ServiceList services
= GetServices();
181 for (BluetoothDevice::ServiceList::const_iterator iter
= services
.begin();
182 iter
!= services
.end();
184 if (bluetooth_utils::CanonicalUuid(*iter
) == canonical_uuid
)
190 } // namespace device