OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/proximity_auth/bluetooth_low_energy_advertiser.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "components/proximity_auth/logging/logging.h" |
| 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 15 |
| 16 namespace proximity_auth { |
| 17 namespace { |
| 18 const char kAdvertisementUUID[] = "0000fe50-0000-1000-8000-00805f9b34fb"; |
| 19 } |
| 20 |
| 21 BluetoothLowEnergyAdvertiser::BluetoothLowEnergyAdvertiser() |
| 22 : weak_ptr_factory_(this) {} |
| 23 |
| 24 BluetoothLowEnergyAdvertiser::~BluetoothLowEnergyAdvertiser() {} |
| 25 |
| 26 void BluetoothLowEnergyAdvertiser::Advertise() { |
| 27 device::BluetoothAdapterFactory::GetAdapter( |
| 28 base::Bind(&BluetoothLowEnergyAdvertiser::OnAdapterInitialized, |
| 29 weak_ptr_factory_.GetWeakPtr())); |
| 30 } |
| 31 |
| 32 void BluetoothLowEnergyAdvertiser::OnAdapterInitialized( |
| 33 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 34 PA_LOG(INFO) << "Adapter ready"; |
| 35 adapter_ = adapter; |
| 36 |
| 37 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement_data = |
| 38 base::MakeUnique<device::BluetoothAdvertisement::Data>( |
| 39 device::BluetoothAdvertisement::AdvertisementType:: |
| 40 ADVERTISEMENT_TYPE_PERIPHERAL); |
| 41 std::unique_ptr<device::BluetoothAdvertisement::UUIDList> list = |
| 42 base::MakeUnique<device::BluetoothAdvertisement::UUIDList>(); |
| 43 list->push_back(std::string(kAdvertisementUUID)); |
| 44 advertisement_data->set_service_uuids(std::move(list)); |
| 45 |
| 46 adapter_->RegisterAdvertisement( |
| 47 std::move(advertisement_data), |
| 48 base::Bind(&BluetoothLowEnergyAdvertiser::OnAdvertisementRegistered, |
| 49 weak_ptr_factory_.GetWeakPtr()), |
| 50 base::Bind(&BluetoothLowEnergyAdvertiser::OnAdvertisementError, |
| 51 weak_ptr_factory_.GetWeakPtr())); |
| 52 } |
| 53 |
| 54 void BluetoothLowEnergyAdvertiser::OnAdvertisementRegistered( |
| 55 scoped_refptr<device::BluetoothAdvertisement> advertisement) { |
| 56 PA_LOG(INFO) << "Advertisement started"; |
| 57 advertisement_ = advertisement; |
| 58 } |
| 59 |
| 60 void BluetoothLowEnergyAdvertiser::OnAdvertisementError( |
| 61 device::BluetoothAdvertisement::ErrorCode error_code) { |
| 62 PA_LOG(INFO) << "Advertisement failed"; |
| 63 } |
| 64 |
| 65 } // namespace proximity_auth |
OLD | NEW |