OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "device/bluetooth/device.h" | 8 #include "device/bluetooth/device.h" |
9 #include "mojo/public/cpp/bindings/strong_binding.h" | 9 #include "mojo/public/cpp/bindings/strong_binding.h" |
10 | 10 |
11 namespace bluetooth { | 11 namespace bluetooth { |
12 | 12 |
13 Device::Device(const std::string& address, | 13 Device::Device(const std::string& address, |
14 scoped_refptr<device::BluetoothAdapter> adapter) | 14 scoped_refptr<device::BluetoothAdapter> adapter) |
15 : address_(address), adapter_(std::move(adapter)) {} | 15 : address_(address), adapter_(std::move(adapter)) { |
| 16 adapter_->AddObserver(this); |
| 17 } |
16 | 18 |
17 Device::~Device() {} | 19 Device::~Device() { |
| 20 adapter_->RemoveObserver(this); |
| 21 adapter_ = nullptr; |
| 22 } |
18 | 23 |
19 // static | 24 // static |
20 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( | 25 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( |
21 const device::BluetoothDevice* device) { | 26 const device::BluetoothDevice* device) { |
22 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); | 27 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
23 | 28 |
24 device_info->name = device->GetName(); | 29 device_info->name = device->GetName(); |
25 device_info->name_for_display = | 30 device_info->name_for_display = |
26 base::UTF16ToUTF8(device->GetNameForDisplay()); | 31 base::UTF16ToUTF8(device->GetNameForDisplay()); |
27 device_info->address = device->GetAddress(); | 32 device_info->address = device->GetAddress(); |
28 | 33 |
29 return device_info; | 34 return device_info; |
30 } | 35 } |
31 | 36 |
| 37 void Device::DeviceChanged(device::BluetoothAdapter* adapter, |
| 38 device::BluetoothDevice* device) { |
| 39 if (client_ && device->GetAddress() == address_) { |
| 40 mojom::AdvertisingPacketPtr packet = mojom::AdvertisingPacket::New(); |
| 41 packet->timestamp = base::Time::Now().ToJavaTime(); |
| 42 packet->rssi = device->GetInquiryRSSI().value_or(0); |
| 43 packet->device = ConstructDeviceInfoStruct(device); |
| 44 client_->DeviceChanged(std::move(packet)); |
| 45 } |
| 46 } |
| 47 |
32 void Device::GetInfo(const GetInfoCallback& callback) { | 48 void Device::GetInfo(const GetInfoCallback& callback) { |
33 device::BluetoothDevice* device = adapter_->GetDevice(address_); | 49 device::BluetoothDevice* device = adapter_->GetDevice(address_); |
34 if (device) { | 50 if (device) { |
35 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); | 51 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); |
36 callback.Run(std::move(device_info)); | 52 callback.Run(std::move(device_info)); |
37 } else { | 53 } else { |
38 callback.Run(nullptr); | 54 callback.Run(nullptr); |
39 } | 55 } |
40 } | 56 } |
41 | 57 |
| 58 void Device::SetClient(mojom::GattClientPtr client) { |
| 59 client_ = std::move(client); |
| 60 } |
| 61 |
42 } // namespace bluetooth | 62 } // namespace bluetooth |
OLD | NEW |