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> | 9 #include <BluetoothAPIs.h> |
10 #include <string> | 10 #include <string> |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
16 #include "device/bluetooth/bluetooth_manager_win.h" | |
16 | 17 |
17 # pragma comment(lib, "Bthprops.lib") | 18 # pragma comment(lib, "Bthprops.lib") |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param = | 22 const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param = |
22 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | 23 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
23 | 24 |
24 } // namespace | 25 } // namespace |
25 | 26 |
26 namespace device { | 27 namespace device { |
27 | 28 |
28 const int BluetoothAdapterWin::kPollIntervalMs = 500; | 29 const int BluetoothAdapterWin::kPollIntervalMs = 500; |
29 | 30 |
30 BluetoothAdapterWin::BluetoothAdapterWin() | 31 BluetoothAdapterWin::BluetoothAdapterWin() |
31 : BluetoothAdapter(), | 32 : BluetoothAdapter(), |
32 powered_(false), | 33 powered_(false), |
34 manager_(new BluetoothManagerWin()), | |
35 adapter_handle_(NULL), | |
33 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 36 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
34 } | 37 } |
35 | 38 |
36 BluetoothAdapterWin::~BluetoothAdapterWin() { | 39 BluetoothAdapterWin::~BluetoothAdapterWin() { |
37 } | 40 } |
38 | 41 |
39 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { | 42 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { |
40 NOTIMPLEMENTED(); | 43 NOTIMPLEMENTED(); |
41 } | 44 } |
42 | 45 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
87 return NULL; | 90 return NULL; |
88 } | 91 } |
89 | 92 |
90 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( | 93 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( |
91 const BluetoothOutOfBandPairingDataCallback& callback, | 94 const BluetoothOutOfBandPairingDataCallback& callback, |
92 const ErrorCallback& error_callback) { | 95 const ErrorCallback& error_callback) { |
93 NOTIMPLEMENTED(); | 96 NOTIMPLEMENTED(); |
94 } | 97 } |
95 | 98 |
96 void BluetoothAdapterWin::UpdateAdapterState() { | 99 void BluetoothAdapterWin::UpdateAdapterState(HANDLE adapter_handle) { |
97 HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL; | 100 adapter_handle_ = adapter_handle; |
98 BLUETOOTH_RADIO_INFO bluetooth_adapter_info = | 101 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
99 { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | 102 if (adapter_handle_) { |
100 HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio( | 103 if (ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle_, |
bryeung
2012/11/22 18:13:25
I think we should move all of the code to interfac
youngki
2012/11/23 01:50:47
Done. Now all the Windows specific API calls are m
| |
101 &bluetooth_adapter_param, &bluetooth_adapter_handle); | 104 &adapter_info)) { |
102 | 105 name_ = base::SysWideToUTF8(adapter_info.szName); |
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", | 106 address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
108 bluetooth_adapter_info.address.rgBytes[5], | 107 adapter_info.address.rgBytes[5], |
109 bluetooth_adapter_info.address.rgBytes[4], | 108 adapter_info.address.rgBytes[4], |
110 bluetooth_adapter_info.address.rgBytes[3], | 109 adapter_info.address.rgBytes[3], |
111 bluetooth_adapter_info.address.rgBytes[2], | 110 adapter_info.address.rgBytes[2], |
112 bluetooth_adapter_info.address.rgBytes[1], | 111 adapter_info.address.rgBytes[1], |
113 bluetooth_adapter_info.address.rgBytes[0]); | 112 adapter_info.address.rgBytes[0]); |
114 powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) || | 113 powered_ = BluetoothIsConnectable(adapter_handle_) ? true : false; |
bryeung
2012/11/22 18:13:25
what happened to the check for BluetoothIsDiscover
youngki
2012/11/23 01:50:47
BluetoothIsConnectable() does not exactly return b
| |
115 BluetoothIsDiscoverable(bluetooth_adapter_handle); | |
116 } else { | 114 } else { |
117 name_.clear(); | 115 name_.clear(); |
118 address_.clear(); | 116 address_.clear(); |
119 powered_ = false; | 117 powered_ = false; |
120 } | 118 } |
121 } | 119 } |
122 | |
123 if (bluetooth_handle) | |
124 BluetoothFindRadioClose(bluetooth_handle); | |
125 } | 120 } |
126 | 121 |
127 void BluetoothAdapterWin::TrackDefaultAdapter() { | 122 void BluetoothAdapterWin::TrackDefaultAdapter() { |
128 PollAdapterState(); | 123 if (!manager_->message_loop()) |
124 manager_->Start(); | |
125 | |
126 manager_->message_loop()->PostTask( | |
127 FROM_HERE, | |
128 base::Bind(&BluetoothManagerWin::FindAdapterHandleAndReply, | |
129 base::Unretained(manager_.get()), | |
130 weak_ptr_factory_.GetWeakPtr())); | |
129 } | 131 } |
130 | 132 |
131 void BluetoothAdapterWin::PollAdapterState() { | 133 void BluetoothAdapterWin::OnAdapterHandleAvailable(HANDLE handle) { |
132 UpdateAdapterState(); | 134 UpdateAdapterState(handle); |
133 | 135 |
134 MessageLoop::current()->PostDelayedTask( | 136 // Post another task to BluetoothManagerWin thread to update the adapter |
137 // handle. | |
138 manager_->message_loop()->PostDelayedTask( | |
135 FROM_HERE, | 139 FROM_HERE, |
136 base::Bind(&BluetoothAdapterWin::PollAdapterState, | 140 base::Bind(&BluetoothManagerWin::FindAdapterHandleAndReply, |
141 base::Unretained(manager_.get()), | |
137 weak_ptr_factory_.GetWeakPtr()), | 142 weak_ptr_factory_.GetWeakPtr()), |
138 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 143 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
139 } | 144 } |
140 | 145 |
146 void BluetoothAdapterWin::SetBluetoothManagerForTest( | |
147 BluetoothManagerWin* manager) { | |
148 manager_.reset(manager); | |
149 } | |
150 | |
141 } // namespace device | 151 } // namespace device |
OLD | NEW |