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_gatt_service.h"
11 #include "grit/device_bluetooth_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
16 BluetoothDevice::BluetoothDevice() {
19 BluetoothDevice::~BluetoothDevice() {
20 STLDeleteValues(&gatt_services_
);
23 base::string16
BluetoothDevice::GetName() const {
24 std::string name
= GetDeviceName();
26 return base::UTF8ToUTF16(name
);
28 return GetAddressWithLocalizedDeviceTypeName();
32 base::string16
BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
33 base::string16 address_utf16
= base::UTF8ToUTF16(GetAddress());
34 BluetoothDevice::DeviceType device_type
= GetDeviceType();
35 switch (device_type
) {
37 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER
,
40 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE
,
43 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM
,
46 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_AUDIO
,
48 case DEVICE_CAR_AUDIO
:
49 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_CAR_AUDIO
,
52 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_VIDEO
,
55 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_JOYSTICK
,
58 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_GAMEPAD
,
61 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD
,
64 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE
,
67 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET
,
69 case DEVICE_KEYBOARD_MOUSE_COMBO
:
70 return l10n_util::GetStringFUTF16(
71 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO
, address_utf16
);
73 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN
,
78 BluetoothDevice::DeviceType
BluetoothDevice::GetDeviceType() const {
79 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
80 uint32 bluetooth_class
= GetBluetoothClass();
81 switch ((bluetooth_class
& 0x1f00) >> 8) {
83 // Computer major device class.
84 return DEVICE_COMPUTER
;
86 // Phone major device class.
87 switch ((bluetooth_class
& 0xfc) >> 2) {
91 // Cellular, cordless and smart phones.
95 // Modems: wired or voice gateway and common ISDN access.
100 // Audio major device class.
101 switch ((bluetooth_class
& 0xfc) >> 2) {
104 return DEVICE_CAR_AUDIO
;
118 // Peripheral major device class.
119 switch ((bluetooth_class
& 0xc0) >> 6) {
121 // "Not a keyboard or pointing device."
122 switch ((bluetooth_class
& 0x01e) >> 2) {
125 return DEVICE_JOYSTICK
;
128 return DEVICE_GAMEPAD
;
130 return DEVICE_PERIPHERAL
;
135 return DEVICE_KEYBOARD
;
138 switch ((bluetooth_class
& 0x01e) >> 2) {
141 return DEVICE_TABLET
;
149 return DEVICE_KEYBOARD_MOUSE_COMBO
;
154 return DEVICE_UNKNOWN
;
157 bool BluetoothDevice::IsPairable() const {
158 DeviceType type
= GetDeviceType();
160 // Get the vendor part of the address: "00:11:22" for "00:11:22:33:44:55"
161 std::string vendor
= GetAddress().substr(0, 8);
163 // Verbatim "Bluetooth Mouse", model 96674
164 if ((type
== DEVICE_MOUSE
&& vendor
== "00:12:A1") ||
165 // Microsoft "Microsoft Bluetooth Notebook Mouse 5000", model X807028-001
166 (type
== DEVICE_MOUSE
&& vendor
== "7C:ED:8D"))
168 // TODO: Move this database into a config file.
173 std::vector
<BluetoothGattService
*>
174 BluetoothDevice::GetGattServices() const {
175 std::vector
<BluetoothGattService
*> services
;
176 for (GattServiceMap::const_iterator iter
= gatt_services_
.begin();
177 iter
!= gatt_services_
.end(); ++iter
)
178 services
.push_back(iter
->second
);
182 BluetoothGattService
* BluetoothDevice::GetGattService(
183 const std::string
& identifier
) const {
184 GattServiceMap::const_iterator iter
= gatt_services_
.find(identifier
);
185 if (iter
!= gatt_services_
.end())
190 } // namespace device