Index: device/bluetooth/bluetooth_polling_thread_win.cc |
diff --git a/device/bluetooth/bluetooth_polling_thread_win.cc b/device/bluetooth/bluetooth_polling_thread_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9f03e7d21cd2c03c94e6c4ea329a651fcb0cfaa0 |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_polling_thread_win.cc |
@@ -0,0 +1,114 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "device/bluetooth/bluetooth_polling_thread_win.h" |
+ |
+#include <BluetoothAPIs.h> |
+#include "base/stringprintf.h" |
+#include "base/sys_string_conversions.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "device/bluetooth/bluetooth_adapter_win.h" |
+ |
+# pragma comment(lib, "Bthprops.lib") |
+ |
+namespace { |
+ |
+const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin"; |
+const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = |
+ { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
+ |
+} // namespace |
+ |
+namespace device { |
+ |
+const int BluetoothPollingThreadWin::kPollIntervalMs = 500; |
+ |
+BluetoothPollingThreadWin::BluetoothPollingThreadWin() |
+ : Thread(kBluetoothManagerThreadName), |
+ adapter_handle_(NULL), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
+} |
+ |
+BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { |
+ Stop(); |
+} |
+ |
+void BluetoothPollingThreadWin::AddObserver(Observer* observer) { |
+ DCHECK(observer); |
+ observers_.AddObserver(observer); |
+} |
+ |
+void BluetoothPollingThreadWin::RemoveObserver(Observer* observer) { |
+ DCHECK(observer); |
+ observers_.RemoveObserver(observer); |
+} |
+ |
+void BluetoothPollingThreadWin::Init() { |
+ if (!message_loop()) { |
+ Start(); |
+ message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::FindAdapterHandleAndReply, |
+ base::Unretained(this), |
+ weak_ptr_factory_.GetWeakPtr())); |
+ } |
+} |
+ |
+void BluetoothPollingThreadWin::FindAdapterHandleAndReply( |
bryeung
2012/11/23 13:32:22
This method name no longer makes sense. PollAdapa
youngki
2012/11/23 18:26:38
Done.
|
+ base::WeakPtr<BluetoothPollingThreadWin> weak_ptr) const { |
bryeung
2012/11/23 13:32:22
CHECK for thread would be nice here
youngki
2012/11/23 18:26:38
Done.
|
+ HBLUETOOTH_RADIO_FIND adapter_handle = NULL; |
+ HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, |
+ &adapter_handle); |
+ if (handle) |
+ BluetoothFindRadioClose(handle); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::OnAdapterHandleAvailable, |
+ weak_ptr, |
+ adapter_handle)); |
+} |
+ |
+void BluetoothPollingThreadWin::OnAdapterHandleAvailable( |
+ HANDLE adapter_handle) { |
+ adapter_handle_ = adapter_handle; |
bryeung
2012/11/23 13:32:22
I'd prefer if the only thing that happened on the
youngki
2012/11/23 18:26:38
Done.
|
+ Properties properties; |
+ GetAdapterProperties(&properties); |
+ FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_, |
+ AdapterPropertyChanged(properties)); |
+ |
+ message_loop()->PostDelayedTask( |
bryeung
2012/11/23 13:32:22
This would probably be better up in FindAdapterHan
youngki
2012/11/23 18:26:38
Done.
|
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::FindAdapterHandleAndReply, |
+ base::Unretained(this), |
+ weak_ptr_factory_.GetWeakPtr()), |
+ base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
+} |
+ |
+void BluetoothPollingThreadWin::GetAdapterProperties( |
+ Properties* properties) const { |
+ std::string name; |
+ std::string address; |
+ bool powered = false; |
+ BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
+ if (adapter_handle_ && |
+ ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle_, |
+ &adapter_info)) { |
+ name = base::SysWideToUTF8(adapter_info.szName); |
+ address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
+ adapter_info.address.rgBytes[5], |
+ adapter_info.address.rgBytes[4], |
+ adapter_info.address.rgBytes[3], |
+ adapter_info.address.rgBytes[2], |
+ adapter_info.address.rgBytes[1], |
+ adapter_info.address.rgBytes[0]); |
+ powered = !!BluetoothIsConnectable(adapter_handle_); |
+ } |
+ properties->name = name; |
+ properties->address = address; |
+ properties->powered = powered; |
+} |
+ |
+} // namespace device |