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_win.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/strings/stringprintf.h"
13 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
14 #include "device/bluetooth/bluetooth_profile_win.h"
15 #include "device/bluetooth/bluetooth_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_win.h"
17 #include "device/bluetooth/bluetooth_task_manager_win.h"
21 const int kSdpBytesBufferSize
= 1024;
27 BluetoothDeviceWin::BluetoothDeviceWin(
28 const BluetoothTaskManagerWin::DeviceState
& state
)
31 address_
= state
.address
;
32 bluetooth_class_
= state
.bluetooth_class
;
33 visible_
= state
.visible
;
34 connected_
= state
.connected
;
35 paired_
= state
.authenticated
;
37 for (ScopedVector
<BluetoothTaskManagerWin::ServiceRecordState
>::const_iterator
38 iter
= state
.service_record_states
.begin();
39 iter
!= state
.service_record_states
.end();
41 uint8 sdp_bytes_buffer
[kSdpBytesBufferSize
];
42 std::copy((*iter
)->sdp_bytes
.begin(),
43 (*iter
)->sdp_bytes
.end(),
45 BluetoothServiceRecord
* service_record
= new BluetoothServiceRecordWin(
48 (*iter
)->sdp_bytes
.size(),
50 service_record_list_
.push_back(service_record
);
51 service_uuids_
.push_back(service_record
->uuid());
55 BluetoothDeviceWin::~BluetoothDeviceWin() {
58 void BluetoothDeviceWin::SetVisible(bool visible
) {
62 uint32
BluetoothDeviceWin::GetBluetoothClass() const {
63 return bluetooth_class_
;
66 std::string
BluetoothDeviceWin::GetDeviceName() const {
70 std::string
BluetoothDeviceWin::GetAddress() const {
74 BluetoothDevice::VendorIDSource
75 BluetoothDeviceWin::GetVendorIDSource() const {
76 return VENDOR_ID_UNKNOWN
;
79 uint16
BluetoothDeviceWin::GetVendorID() const {
83 uint16
BluetoothDeviceWin::GetProductID() const {
87 uint16
BluetoothDeviceWin::GetDeviceID() const {
91 bool BluetoothDeviceWin::IsPaired() const {
95 bool BluetoothDeviceWin::IsConnected() const {
99 bool BluetoothDeviceWin::IsConnectable() const {
103 bool BluetoothDeviceWin::IsConnecting() const {
107 BluetoothDevice::ServiceList
BluetoothDeviceWin::GetServices() const {
108 return service_uuids_
;
111 void BluetoothDeviceWin::GetServiceRecords(
112 const ServiceRecordsCallback
& callback
,
113 const ErrorCallback
& error_callback
) {
114 callback
.Run(service_record_list_
);
117 void BluetoothDeviceWin::ProvidesServiceWithName(
118 const std::string
& name
,
119 const ProvidesServiceCallback
& callback
) {
120 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
121 iter
!= service_record_list_
.end();
123 if ((*iter
)->name() == name
) {
131 bool BluetoothDeviceWin::ExpectingPinCode() const {
136 bool BluetoothDeviceWin::ExpectingPasskey() const {
141 bool BluetoothDeviceWin::ExpectingConfirmation() const {
146 void BluetoothDeviceWin::Connect(
147 PairingDelegate
* pairing_delegate
,
148 const base::Closure
& callback
,
149 const ConnectErrorCallback
& error_callback
) {
153 void BluetoothDeviceWin::SetPinCode(const std::string
& pincode
) {
157 void BluetoothDeviceWin::SetPasskey(uint32 passkey
) {
161 void BluetoothDeviceWin::ConfirmPairing() {
165 void BluetoothDeviceWin::RejectPairing() {
169 void BluetoothDeviceWin::CancelPairing() {
173 void BluetoothDeviceWin::Disconnect(
174 const base::Closure
& callback
,
175 const ErrorCallback
& error_callback
) {
179 void BluetoothDeviceWin::Forget(const ErrorCallback
& error_callback
) {
183 void BluetoothDeviceWin::ConnectToService(
184 const std::string
& service_uuid
,
185 const SocketCallback
& callback
) {
186 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
187 iter
!= service_record_list_
.end();
189 if ((*iter
)->uuid() == service_uuid
) {
190 // If multiple service records are found, use the first one that works.
191 scoped_refptr
<BluetoothSocket
> socket(
192 BluetoothSocketWin::CreateBluetoothSocket(**iter
));
193 if (socket
.get() != NULL
) {
194 callback
.Run(socket
);
201 void BluetoothDeviceWin::ConnectToProfile(
202 device::BluetoothProfile
* profile
,
203 const base::Closure
& callback
,
204 const ErrorCallback
& error_callback
) {
205 if (static_cast<BluetoothProfileWin
*>(profile
)->Connect(this))
208 error_callback
.Run();
211 void BluetoothDeviceWin::SetOutOfBandPairingData(
212 const BluetoothOutOfBandPairingData
& data
,
213 const base::Closure
& callback
,
214 const ErrorCallback
& error_callback
) {
218 void BluetoothDeviceWin::ClearOutOfBandPairingData(
219 const base::Closure
& callback
,
220 const ErrorCallback
& error_callback
) {
224 const BluetoothServiceRecord
* BluetoothDeviceWin::GetServiceRecord(
225 const std::string
& uuid
) const {
226 for (ServiceRecordList::const_iterator iter
= service_record_list_
.begin();
227 iter
!= service_record_list_
.end();
229 if ((*iter
)->uuid().compare(uuid
) == 0)
235 } // namespace device