OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // TODO(youngki): Implement this file. | 5 // TODO(youngki): Implement this file. |
6 | 6 |
7 #include "device/bluetooth/bluetooth_adapter_win.h" | 7 #include "device/bluetooth/bluetooth_adapter_win.h" |
8 | 8 |
9 #include <BluetoothAPIs.h> | |
10 #include <string> | 9 #include <string> |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "base/message_loop.h" | |
14 #include "base/stringprintf.h" | |
15 #include "base/sys_string_conversions.h" | |
16 | |
17 # pragma comment(lib, "Bthprops.lib") | |
18 | |
19 namespace { | |
20 | |
21 const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param = | |
22 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | |
23 | |
24 } // namespace | |
25 | 11 |
26 namespace device { | 12 namespace device { |
27 | 13 |
28 const int BluetoothAdapterWin::kPollIntervalMs = 500; | |
29 | |
30 BluetoothAdapterWin::BluetoothAdapterWin() | 14 BluetoothAdapterWin::BluetoothAdapterWin() |
31 : BluetoothAdapter(), | 15 : BluetoothAdapter(), |
32 powered_(false), | 16 powered_(false), |
| 17 thread_(new BluetoothPollingThreadWin()), |
33 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 18 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 19 thread_->AddObserver(this); |
34 } | 20 } |
35 | 21 |
36 BluetoothAdapterWin::~BluetoothAdapterWin() { | 22 BluetoothAdapterWin::~BluetoothAdapterWin() { |
| 23 thread_->RemoveObserver(this); |
37 } | 24 } |
38 | 25 |
39 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { | 26 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { |
40 NOTIMPLEMENTED(); | 27 NOTIMPLEMENTED(); |
41 } | 28 } |
42 | 29 |
43 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { | 30 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { |
44 NOTIMPLEMENTED(); | 31 NOTIMPLEMENTED(); |
45 } | 32 } |
46 | 33 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 NOTIMPLEMENTED(); | 73 NOTIMPLEMENTED(); |
87 return NULL; | 74 return NULL; |
88 } | 75 } |
89 | 76 |
90 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( | 77 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( |
91 const BluetoothOutOfBandPairingDataCallback& callback, | 78 const BluetoothOutOfBandPairingDataCallback& callback, |
92 const ErrorCallback& error_callback) { | 79 const ErrorCallback& error_callback) { |
93 NOTIMPLEMENTED(); | 80 NOTIMPLEMENTED(); |
94 } | 81 } |
95 | 82 |
96 void BluetoothAdapterWin::UpdateAdapterState() { | 83 void BluetoothAdapterWin::AdapterStateChanged( |
97 HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL; | 84 const BluetoothPollingThreadWin::AdapterState& state) { |
98 BLUETOOTH_RADIO_INFO bluetooth_adapter_info = | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
99 { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | 86 name_ = state.name; |
100 HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio( | 87 address_ = state.address; |
101 &bluetooth_adapter_param, &bluetooth_adapter_handle); | 88 powered_ = state.powered; |
102 | |
103 if (bluetooth_adapter_handle) { | |
104 if (ERROR_SUCCESS == BluetoothGetRadioInfo(bluetooth_adapter_handle, | |
105 &bluetooth_adapter_info)) { | |
106 name_ = base::SysWideToUTF8(bluetooth_adapter_info.szName); | |
107 address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | |
108 bluetooth_adapter_info.address.rgBytes[5], | |
109 bluetooth_adapter_info.address.rgBytes[4], | |
110 bluetooth_adapter_info.address.rgBytes[3], | |
111 bluetooth_adapter_info.address.rgBytes[2], | |
112 bluetooth_adapter_info.address.rgBytes[1], | |
113 bluetooth_adapter_info.address.rgBytes[0]); | |
114 powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) || | |
115 BluetoothIsDiscoverable(bluetooth_adapter_handle); | |
116 } else { | |
117 name_.clear(); | |
118 address_.clear(); | |
119 powered_ = false; | |
120 } | |
121 } | |
122 | |
123 if (bluetooth_handle) | |
124 BluetoothFindRadioClose(bluetooth_handle); | |
125 } | 89 } |
126 | 90 |
127 void BluetoothAdapterWin::TrackDefaultAdapter() { | 91 void BluetoothAdapterWin::TrackDefaultAdapter() { |
128 PollAdapterState(); | 92 thread_->Init(); |
129 } | 93 } |
130 | 94 |
131 void BluetoothAdapterWin::PollAdapterState() { | 95 void BluetoothAdapterWin::SetBluetoothPollingThreadForTest( |
132 UpdateAdapterState(); | 96 BluetoothPollingThreadWin* thread) { |
133 | 97 thread_.reset(thread); |
134 MessageLoop::current()->PostDelayedTask( | |
135 FROM_HERE, | |
136 base::Bind(&BluetoothAdapterWin::PollAdapterState, | |
137 weak_ptr_factory_.GetWeakPtr()), | |
138 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | |
139 } | 98 } |
140 | 99 |
141 } // namespace device | 100 } // namespace device |
OLD | NEW |