Implement FastPairScanner

PiperOrigin-RevId: 490621670
This commit is contained in:
Qin Wang
2022-11-23 18:10:43 -08:00
committed by Copybara-Service
parent 88d5cb9fdd
commit 00b1a1570e
3 changed files with 162 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"])
cc_library(
name = "scanning",
srcs = [
"fast_pair_scanner_impl.cc",
],
hdrs = [
"fast_pair_scanner_impl.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/internal/ble",
"//internal/platform:base",
"//internal/platform:comm",
"//internal/platform:logging",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/strings",
],
)
@@ -0,0 +1,74 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include <string>
#include "absl/functional/bind_front.h"
#include "absl/strings/escaping.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
namespace location {
namespace nearby {
namespace fastpair {
namespace {
constexpr char kServiceId[] = "Fast Pair";
constexpr char kFastPairServiceUuid[] =
"0000FE2C-0000-1000-8000-00805F9B34FB";
} // namespace
bool FastPairScannerImpl::StartScanning() {
if (ble_.Enable() &&
ble_.StartScanning(kServiceId, kFastPairServiceUuid,
{
.peripheral_discovered_cb = absl::bind_front(
&FastPairScannerImpl::OnDeviceFound, this),
.peripheral_lost_cb = absl::bind_front(
&FastPairScannerImpl::OnDeviceLost, this),
})) {
NEARBY_LOGS(INFO)
<< "Started scanning for BLE advertisements for service_id="
<< kServiceId;
return true;
} else {
NEARBY_LOGS(INFO) << "Couldn't start scanning on BLE for service_id="
<< kServiceId;
return false;
}
}
void FastPairScannerImpl::OnDeviceFound(BlePeripheral& peripheral,
const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement) {
if (advertisement_bytes.size() == 3) {
NEARBY_LOGS(VERBOSE) << "Fast Pair 0xFE2C Advertisement discovered. ";
NEARBY_LOGS(VERBOSE) << "BluetoothAddress is " << peripheral.GetName()
<< ", Model id = "
<< absl::BytesToHexString(
advertisement_bytes.AsStringView());
}
}
void FastPairScannerImpl::OnDeviceLost(BlePeripheral& peripheral,
const std::string& service_id) {
NEARBY_LOGS(INFO) << "Lost BlePeripheral with BluetoothAddress is "
<< peripheral.GetName();
}
} // namespace fastpair
} // namespace nearby
} // namespace location
@@ -0,0 +1,52 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_SCANNER_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_SCANNER_IMPL_H_
#include <string>
#include "fastpair/internal/ble/ble.h"
#include "internal/platform/bluetooth_adapter.h"
namespace location {
namespace nearby {
namespace fastpair {
class FastPairScannerImpl {
public:
FastPairScannerImpl() = default;
FastPairScannerImpl(const FastPairScannerImpl&) = default;
FastPairScannerImpl& operator=(const FastPairScannerImpl&) = default;
~FastPairScannerImpl() = default;
bool StartScanning();
bool StopScanning();
void OnDeviceFound(BlePeripheral& peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement);
void OnDeviceLost(BlePeripheral& peripheral,
const std::string& service_id);
private:
Ble ble_;
};
} // namespace fastpair
} // namespace nearby
} // namespace location
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_FASTPAIR_FAST_PAIR_SCANNER_IMPL_H_