mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
Keep all discovered and explicitly created BLE peripherals in the same collection. The peripherals are removed when they haven't been used for a while. PiperOrigin-RevId: 538349622
65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
// Copyright 2023 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 "internal/platform/implementation/windows/ble_v2_peripheral.h"
|
|
|
|
#include <string>
|
|
|
|
#include "absl/strings/string_view.h"
|
|
#include "internal/platform/bluetooth_utils.h"
|
|
#include "internal/platform/logging.h"
|
|
|
|
namespace nearby {
|
|
namespace windows {
|
|
namespace {
|
|
constexpr int kMacAddressLength = 17;
|
|
}
|
|
|
|
BleV2Peripheral::BleV2Peripheral(absl::string_view address) {
|
|
if (SetAddress(address)) {
|
|
unique_id_ = static_cast<UniqueId>(BluetoothUtils::ToNumber(address));
|
|
}
|
|
}
|
|
|
|
bool BleV2Peripheral::SetAddress(absl::string_view address) {
|
|
// The address must be in format "00:B0:D0:63:C2:26".
|
|
if (address.size() != kMacAddressLength) {
|
|
NEARBY_LOGS(ERROR) << ": Invalid MAC address length.";
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < kMacAddressLength; ++i) {
|
|
if ((i % 3) == 0 || (i % 3) == 1) {
|
|
if ((address[i] >= '0' && address[i] <= '9') ||
|
|
(address[i] >= 'a' && address[i] <= 'f') ||
|
|
(address[i] >= 'A' && address[i] <= 'F')) {
|
|
continue;
|
|
}
|
|
} else {
|
|
if (address[i] == ':') {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
NEARBY_LOGS(ERROR) << ": Invalid MAC address format.";
|
|
return false;
|
|
}
|
|
|
|
address_ = std::string(address);
|
|
return true;
|
|
}
|
|
|
|
} // namespace windows
|
|
} // namespace nearby
|