From 7e1ca7d458badf0e7ec20ed140dfc7894d2b5d5d Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Tue, 7 Mar 2023 12:36:37 -0800 Subject: [PATCH] Print out GattCommunicationStatus to help with debugging PiperOrigin-RevId: 514809824 --- .../implementation/windows/ble_gatt_client.cc | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/internal/platform/implementation/windows/ble_gatt_client.cc b/internal/platform/implementation/windows/ble_gatt_client.cc index bdf20344..de5d4712 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.cc +++ b/internal/platform/implementation/windows/ble_gatt_client.cc @@ -70,6 +70,21 @@ using ::winrt::Windows::Storage::Streams::DataReader; using ::winrt::Windows::Storage::Streams::IBuffer; using Property = api::ble_v2::GattCharacteristic::Property; using Permission = api::ble_v2::GattCharacteristic::Permission; + +std::string GattCommunicationStatusToString(GattCommunicationStatus status) { + switch (status) { + case GattCommunicationStatus::Success: + return "Success"; + case GattCommunicationStatus::Unreachable: + return "Unreachable"; + case GattCommunicationStatus::ProtocolError: + return "ProtocolError"; + case GattCommunicationStatus::AccessDenied: + return "AccessDenied"; + default: + return "Unknown"; + } +} } // namespace BleGattClient::BleGattClient(BluetoothLEDevice ble_device) @@ -102,9 +117,12 @@ bool BleGattClient::DiscoverServiceAndCharacteristics( // Gets the GATT services on the BLE device. gatt_devices_services_result_ = ble_device_.GetGattServicesAsync(BluetoothCacheMode::Uncached).get(); - if (gatt_devices_services_result_.Status() != GattCommunicationStatus::Success) { + NEARBY_LOGS(ERROR) << __func__ + << ": Failed to get gatt service with error: " + << GattCommunicationStatusToString( + gatt_devices_services_result_.Status()); gatt_devices_services_result_ = nullptr; return false; } @@ -146,7 +164,10 @@ bool BleGattClient::DiscoverServiceAndCharacteristics( service.GetCharacteristicsAsync(BluetoothCacheMode::Uncached).get(); if (gatt_characteristics_result.Status() != GattCommunicationStatus::Success) { - NEARBY_LOGS(ERROR) << __func__ << ": Failed to get characteristics."; + NEARBY_LOGS(ERROR) << __func__ + << ": Failed to get characteristics with error: " + << GattCommunicationStatusToString( + gatt_characteristics_result.Status()); continue; } @@ -294,8 +315,8 @@ absl::optional BleGattClient::ReadCharacteristic( gatt_characteristic->ReadValueAsync(BluetoothCacheMode::Uncached).get(); if (result.Status() != GattCommunicationStatus::Success) { NEARBY_LOGS(ERROR) << __func__ - << ": Failed to read GATT characteristic. status=" - << static_cast(result.Status()); + << ": Failed to read GATT characteristic with error: " + << GattCommunicationStatusToString(result.Status()); return absl::nullopt; } @@ -351,13 +372,14 @@ bool BleGattClient::WriteCharacteristic( std::memcpy(buffer.data(), value.data(), value.size()); buffer.Length(value.size()); - GattCommunicationStatus staus = + GattCommunicationStatus status = gatt_characteristic->WriteValueAsync(buffer).get(); - if (staus != GattCommunicationStatus::Success) { + if (status != GattCommunicationStatus::Success) { NEARBY_LOGS(ERROR) << __func__ << ": Failed to write data to GATT characteristic: " - << std::string(characteristic.uuid); + << std::string(characteristic.uuid) << "with error: " + << GattCommunicationStatusToString(status); return false; } else { NEARBY_LOGS(VERBOSE) << __func__ @@ -505,7 +527,10 @@ std::optional BleGattClient::GetNativeCharacteristic( service.GetCharacteristicsAsync(BluetoothCacheMode::Cached).get(); if (gatt_characteristics_result.Status() != GattCommunicationStatus::Success) { - NEARBY_LOGS(ERROR) << __func__ << ": Failed to get characteristics."; + NEARBY_LOGS(ERROR) + << __func__ << ": Failed to get characteristics with error: " + << GattCommunicationStatusToString( + gatt_characteristics_result.Status()); continue; } @@ -558,7 +583,8 @@ bool BleGattClient::WriteCharacteristicConfigurationDescriptor( } NEARBY_LOGS(VERBOSE) << __func__ << ": Failed to write client characteristic " - "configuration descriptor"; + "configuration descriptor with error: " + << GattCommunicationStatusToString(status); } catch (std::exception exception) { // This usually happens when a device reports that it support notify, but // it actually doesn't.