From 00b1a1570ef5290394b7e865a67392ae4199f4c4 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Wed, 23 Nov 2022 18:08:40 -0800 Subject: [PATCH] Implement FastPairScanner PiperOrigin-RevId: 490621670 --- fastpair/scanning/fastpair/BUILD | 36 +++++++++ .../fastpair/fast_pair_scanner_impl.cc | 74 +++++++++++++++++++ .../fastpair/fast_pair_scanner_impl.h | 52 +++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 fastpair/scanning/fastpair/BUILD create mode 100644 fastpair/scanning/fastpair/fast_pair_scanner_impl.cc create mode 100644 fastpair/scanning/fastpair/fast_pair_scanner_impl.h diff --git a/fastpair/scanning/fastpair/BUILD b/fastpair/scanning/fastpair/BUILD new file mode 100644 index 00000000..4859b0df --- /dev/null +++ b/fastpair/scanning/fastpair/BUILD @@ -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", + ], +) diff --git a/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc b/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc new file mode 100644 index 00000000..1b6f110b --- /dev/null +++ b/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc @@ -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 + +#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 diff --git a/fastpair/scanning/fastpair/fast_pair_scanner_impl.h b/fastpair/scanning/fastpair/fast_pair_scanner_impl.h new file mode 100644 index 00000000..d37fbb44 --- /dev/null +++ b/fastpair/scanning/fastpair/fast_pair_scanner_impl.h @@ -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 + +#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_