mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Convet ByteArray to std::string in ble_gatt_client
PiperOrigin-RevId: 514786088
This commit is contained in:
committed by
Copybara-Service
parent
ead3df1ff8
commit
0c078b3528
@@ -665,7 +665,8 @@ void BleV2::ProcessFetchGattAdvertisementsRequest(
|
||||
auto characteristic_byte =
|
||||
gatt_client->ReadCharacteristic(gatt_characteristic.value());
|
||||
if (characteristic_byte.has_value()) {
|
||||
advertisement_read_result.AddAdvertisement(slot, *characteristic_byte);
|
||||
advertisement_read_result.AddAdvertisement(
|
||||
slot, ByteArray(characteristic_byte.value()));
|
||||
NEARBY_LOGS(VERBOSE) << "Successfully read advertisement at slot="
|
||||
<< slot;
|
||||
} else {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
@@ -219,7 +220,7 @@ class GattClient final {
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
absl::optional<ByteArray> ReadCharacteristic(
|
||||
absl::optional<std::string> ReadCharacteristic(
|
||||
api::ble_v2::GattCharacteristic& characteristic) {
|
||||
return impl_->ReadCharacteristic(characteristic);
|
||||
}
|
||||
@@ -227,14 +228,14 @@ class GattClient final {
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
bool WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const ByteArray& value) {
|
||||
absl::string_view value) {
|
||||
return impl_->WriteCharacteristic(characteristic, value);
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
bool SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) {
|
||||
return impl_->SetCharacteristicSubscription(
|
||||
characteristic, enable, std::move(on_characteristic_changed_cb));
|
||||
|
||||
@@ -574,7 +574,7 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) {
|
||||
|
||||
// Can read the characteristic value.
|
||||
EXPECT_THAT(gatt_client->ReadCharacteristic(*client_characteristic),
|
||||
Optional(server_value));
|
||||
Optional(server_value.string_data()));
|
||||
|
||||
gatt_client->Disconnect();
|
||||
gatt_server->Stop();
|
||||
|
||||
@@ -78,6 +78,7 @@ class ByteArray {
|
||||
}
|
||||
|
||||
char* data() { return &data_[0]; }
|
||||
std::string string_data() const { return data_; }
|
||||
const char* data() const { return data_.data(); }
|
||||
size_t size() const { return data_.size(); }
|
||||
bool Empty() const { return data_.empty(); }
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/hash/hash_testing.h"
|
||||
@@ -60,7 +61,7 @@ TEST(ByteArrayTest, SetFromString) {
|
||||
std::string setup("setup_test");
|
||||
ByteArray bytes{setup}; // array initialized with a copy of string.
|
||||
EXPECT_EQ(setup.size(), bytes.size());
|
||||
EXPECT_EQ(std::string(bytes), setup);
|
||||
EXPECT_EQ(bytes.string_data(), setup);
|
||||
}
|
||||
|
||||
TEST(ByteArrayTest, SetExplicitSize) {
|
||||
|
||||
@@ -182,22 +182,23 @@ class BleMedium : public api::ble_v2::BleMedium {
|
||||
const Uuid &service_uuid, const Uuid &characteristic_uuid) override;
|
||||
|
||||
// NOLINTNEXTLINE
|
||||
absl::optional<ByteArray> ReadCharacteristic(
|
||||
absl::optional<std::string> ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic &characteristic) override;
|
||||
|
||||
bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
|
||||
const ByteArray &value) override;
|
||||
absl::string_view value) override;
|
||||
|
||||
bool SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic &characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray &value)> on_characteristic_changed_cb) override;
|
||||
absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb) override;
|
||||
|
||||
void Disconnect() override;
|
||||
|
||||
private:
|
||||
GNCMBleCentral *central_;
|
||||
std::string peripheral_id_;
|
||||
absl::flat_hash_map<api::ble_v2::GattCharacteristic, ByteArray> gatt_characteristic_values_;
|
||||
absl::flat_hash_map<api::ble_v2::GattCharacteristic, absl::string_view>
|
||||
gatt_characteristic_values_;
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <CoreBluetooth/CoreBluetooth.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@@ -539,7 +540,7 @@ bool BleMedium::GattClient::DiscoverServiceAndCharacteristics(
|
||||
api::ble_v2::GattCharacteristic characteristic = {.uuid = it->second,
|
||||
.service_uuid = service_uuid};
|
||||
gatt_characteristic_values_.insert(
|
||||
{characteristic, ByteArrayFromNSData(cb_characteristic.value)});
|
||||
{characteristic, ByteArrayFromNSData(cb_characteristic.value).string_data()});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,24 +568,24 @@ absl::optional<api::ble_v2::GattCharacteristic> BleMedium::GattClient::GetCharac
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE
|
||||
absl::optional<ByteArray> BleMedium::GattClient::ReadCharacteristic(
|
||||
absl::optional<std::string> BleMedium::GattClient::ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) {
|
||||
auto const it = gatt_characteristic_values_.find(characteristic);
|
||||
if (it == gatt_characteristic_values_.end()) {
|
||||
return absl::nullopt; // NOLINT
|
||||
}
|
||||
return it->second;
|
||||
return std::string(it->second);
|
||||
}
|
||||
|
||||
bool BleMedium::GattClient::WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, const ByteArray& value) {
|
||||
const api::ble_v2::GattCharacteristic& characteristic, absl::string_view value) {
|
||||
// No op.
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BleMedium::GattClient::SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)> on_characteristic_changed_cb) {
|
||||
absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb) {
|
||||
// No op since we can't write characteristics.
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ class GattClient {
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#readCharacteristic(android.bluetooth.BluetoothGattCharacteristic)
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#getValue()
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
virtual absl::optional<ByteArray> ReadCharacteristic(
|
||||
virtual absl::optional<std::string> ReadCharacteristic(
|
||||
const GattCharacteristic& characteristic) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#setValue(byte[])
|
||||
@@ -206,14 +206,14 @@ class GattClient {
|
||||
// Sends a remote characteristic write request to the server and returns
|
||||
// whether or not it was successful.
|
||||
virtual bool WriteCharacteristic(const GattCharacteristic& characteristic,
|
||||
const ByteArray& value) = 0;
|
||||
absl::string_view value) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic,%20boolean)
|
||||
//
|
||||
// Enable or disable notifications/indications for a given characteristic.
|
||||
virtual bool SetCharacteristicSubscription(
|
||||
const GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#disconnect()
|
||||
@@ -270,8 +270,8 @@ class GattServer {
|
||||
struct ClientGattConnectionCallback {
|
||||
public:
|
||||
// Called when the characteristic is changed
|
||||
absl::AnyInvocable<void(ByteArray& value)> on_characteristic_changed_cb =
|
||||
[](ByteArray&) {};
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb = [](absl::string_view) {};
|
||||
|
||||
// Called when the client is disconnected from the GATT server.
|
||||
absl::AnyInvocable<void()> disconnected_cb = []() {};
|
||||
|
||||
@@ -437,7 +437,7 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
|
||||
return characteristic;
|
||||
}
|
||||
|
||||
std::optional<ByteArray> BleV2Medium::GattClient::ReadCharacteristic(
|
||||
std::optional<std::string> BleV2Medium::GattClient::ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!is_connection_alive_) {
|
||||
@@ -451,19 +451,19 @@ std::optional<ByteArray> BleV2Medium::GattClient::ReadCharacteristic(
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value.data());
|
||||
return std::move(value);
|
||||
return value.string_data();
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattClient::WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const ByteArray& value) {
|
||||
absl::string_view value) {
|
||||
// No op.
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattClient::SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) {
|
||||
// No op since we can't write characteristics.
|
||||
return false;
|
||||
|
||||
@@ -235,16 +235,16 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
std::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid) override;
|
||||
|
||||
std::optional<ByteArray> ReadCharacteristic(
|
||||
std::optional<std::string> ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) override;
|
||||
|
||||
bool WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const ByteArray& value) override;
|
||||
absl::string_view value) override;
|
||||
|
||||
bool SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) override;
|
||||
|
||||
void Disconnect() override;
|
||||
|
||||
@@ -275,7 +275,7 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid,
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
absl::optional<ByteArray> BleGattClient::ReadCharacteristic(
|
||||
absl::optional<std::string> BleGattClient::ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Read characteristic="
|
||||
<< std::string(characteristic.uuid);
|
||||
@@ -316,7 +316,7 @@ absl::optional<ByteArray> BleGattClient::ReadCharacteristic(
|
||||
NEARBY_LOGS(VERBOSE) << __func__
|
||||
<< ": Got characteristic value length=" << data.size();
|
||||
|
||||
return ByteArray(data);
|
||||
return data;
|
||||
} catch (std::exception exception) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Failed to read GATT characteristic. exception: "
|
||||
@@ -332,7 +332,7 @@ absl::optional<ByteArray> BleGattClient::ReadCharacteristic(
|
||||
|
||||
bool BleGattClient::WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const ByteArray& value) {
|
||||
absl::string_view value) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": write characteristic: "
|
||||
<< std::string(characteristic.uuid);
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -380,7 +380,7 @@ bool BleGattClient::WriteCharacteristic(
|
||||
|
||||
bool BleGattClient::SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__
|
||||
<< ": Started to set Characteristic Subscription.";
|
||||
@@ -577,7 +577,8 @@ bool BleGattClient::WriteCharacteristicConfigurationDescriptor(
|
||||
|
||||
void BleGattClient::OnCharacteristicValueChanged(
|
||||
GattCharacteristic const& characteristic, GattValueChangedEventArgs args,
|
||||
absl::AnyInvocable<void(ByteArray& value)> on_characteristic_changed_cb) {
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << "Gatt Characteristic value changed.";
|
||||
IBuffer buffer = args.CharacteristicValue();
|
||||
int size = buffer.Length();
|
||||
@@ -589,8 +590,7 @@ void BleGattClient::OnCharacteristicValueChanged(
|
||||
}
|
||||
NEARBY_LOGS(VERBOSE) << __func__
|
||||
<< ": Got characteristic value length= " << data.size();
|
||||
ByteArray value = ByteArray(data);
|
||||
on_characteristic_changed_cb(value);
|
||||
on_characteristic_changed_cb(data);
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
@@ -46,16 +47,16 @@ class BleGattClient : public api::ble_v2::GattClient {
|
||||
absl::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid) override;
|
||||
|
||||
absl::optional<ByteArray> ReadCharacteristic(
|
||||
absl::optional<std::string> ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) override;
|
||||
|
||||
bool WriteCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const ByteArray& value) override;
|
||||
absl::string_view value) override;
|
||||
|
||||
bool SetCharacteristicSubscription(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
|
||||
absl::AnyInvocable<void(const ByteArray& value)>
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb) override;
|
||||
|
||||
void Disconnect() override;
|
||||
@@ -84,7 +85,8 @@ class BleGattClient : public api::ble_v2::GattClient {
|
||||
GattCharacteristic const& characteristic,
|
||||
::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
|
||||
GattValueChangedEventArgs args,
|
||||
absl::AnyInvocable<void(ByteArray& value)> on_characteristic_changed_cb);
|
||||
absl::AnyInvocable<void(absl::string_view value)>
|
||||
on_characteristic_changed_cb);
|
||||
|
||||
absl::Mutex mutex_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user