summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_adapter_win.cc
blob: 5924cd9a433ae428154de364d46c10b43093bb5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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.
//
// TODO(youngki): Implement this file.

#include "device/bluetooth/bluetooth_adapter_win.h"

#include <BluetoothAPIs.h>
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
#include "base/sys_string_conversions.h"

# pragma comment(lib, "Bthprops.lib")

namespace {

const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param =
    { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };

}  // namespace

namespace device {

const int BluetoothAdapterWin::kPollIntervalMs = 500;

BluetoothAdapterWin::BluetoothAdapterWin()
    : BluetoothAdapter(),
      powered_(false),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
}

BluetoothAdapterWin::~BluetoothAdapterWin() {
}

void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) {
  NOTIMPLEMENTED();
}

void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) {
  NOTIMPLEMENTED();
}

bool BluetoothAdapterWin::IsPresent() const {
  return !address_.empty();
}

bool BluetoothAdapterWin::IsPowered() const {
  return powered_;
}

void BluetoothAdapterWin::SetPowered(
    bool powered,
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

bool BluetoothAdapterWin::IsDiscovering() const {
  NOTIMPLEMENTED();
  return false;
}

void BluetoothAdapterWin::SetDiscovering(
    bool discovering,
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

BluetoothAdapter::ConstDeviceList BluetoothAdapterWin::GetDevices() const {
  NOTIMPLEMENTED();
  return BluetoothAdapter::ConstDeviceList();
}

BluetoothDevice* BluetoothAdapterWin::GetDevice(const std::string& address) {
  NOTIMPLEMENTED();
  return NULL;
}

const BluetoothDevice* BluetoothAdapterWin::GetDevice(
    const std::string& address) const {
  NOTIMPLEMENTED();
  return NULL;
}

void BluetoothAdapterWin::ReadLocalOutOfBandPairingData(
    const BluetoothOutOfBandPairingDataCallback& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

void BluetoothAdapterWin::UpdateAdapterState() {
  HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL;
  BLUETOOTH_RADIO_INFO bluetooth_adapter_info =
      { sizeof(BLUETOOTH_RADIO_INFO), 0 };
  HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio(
      &bluetooth_adapter_param, &bluetooth_adapter_handle);

  if (bluetooth_adapter_handle) {
    if (ERROR_SUCCESS == BluetoothGetRadioInfo(bluetooth_adapter_handle,
                                               &bluetooth_adapter_info)) {
      name_ = base::SysWideToUTF8(bluetooth_adapter_info.szName);
      address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
          bluetooth_adapter_info.address.rgBytes[5],
          bluetooth_adapter_info.address.rgBytes[4],
          bluetooth_adapter_info.address.rgBytes[3],
          bluetooth_adapter_info.address.rgBytes[2],
          bluetooth_adapter_info.address.rgBytes[1],
          bluetooth_adapter_info.address.rgBytes[0]);
      powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) ||
          BluetoothIsDiscoverable(bluetooth_adapter_handle);
    } else {
      name_.clear();
      address_.clear();
      powered_ = false;
    }
  }

  if (bluetooth_handle)
    BluetoothFindRadioClose(bluetooth_handle);
}

void BluetoothAdapterWin::TrackDefaultAdapter() {
  PollAdapterState();
}

void BluetoothAdapterWin::PollAdapterState() {
  UpdateAdapterState();

  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&BluetoothAdapterWin::PollAdapterState,
                 weak_ptr_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(kPollIntervalMs));
}

}  // namespace device