Log Bluetooth connection status change

PiperOrigin-RevId: 500228494
This commit is contained in:
Guogang Li
2023-01-06 15:09:34 -08:00
committed by Copybara-Service
parent 32ace29ccf
commit 06713fe918
3 changed files with 58 additions and 7 deletions
@@ -15,28 +15,44 @@
#include "internal/platform/implementation/windows/bluetooth_classic_socket.h"
#include <exception>
#include <memory>
#include <utility>
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Bluetooth.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "internal/platform/implementation/windows/generated/winrt/base.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace windows {
namespace {
using ::winrt::Windows::Devices::Bluetooth::BluetoothConnectionStatus;
}
BluetoothSocket::BluetoothSocket(StreamSocket streamSocket)
: windows_socket_(streamSocket) {
bluetooth_device_ = std::make_unique<BluetoothDevice>(
native_bluetooth_device_ =
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get());
.get();
connection_status_changed_token_ =
native_bluetooth_device_.ConnectionStatusChanged(
{this, &BluetoothSocket::Listener_ConnectionStatusChanged});
bluetooth_device_ =
std::make_unique<BluetoothDevice>(native_bluetooth_device_);
input_stream_ = BluetoothInputStream(windows_socket_.InputStream());
output_stream_ = BluetoothOutputStream(windows_socket_.OutputStream());
}
BluetoothSocket::BluetoothSocket() {}
BluetoothSocket::~BluetoothSocket() {}
BluetoothSocket::~BluetoothSocket() {
if (native_bluetooth_device_ != nullptr) {
native_bluetooth_device_.ConnectionStatusChanged(
connection_status_changed_token_);
native_bluetooth_device_ = nullptr;
}
}
// NOTE:
// It is an undefined behavior if GetInputStream() or GetOutputStream() is
@@ -111,10 +127,17 @@ bool BluetoothSocket::Connect(HostName connectionHostName,
auto info = windows_socket_.Information();
auto hostName = info.RemoteHostName();
bluetooth_device_ = std::make_unique<BluetoothDevice>(
native_bluetooth_device_ =
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get());
.get();
connection_status_changed_token_ =
native_bluetooth_device_.ConnectionStatusChanged(
{this, &BluetoothSocket::Listener_ConnectionStatusChanged});
bluetooth_device_ =
std::make_unique<BluetoothDevice>(native_bluetooth_device_);
input_stream_ = BluetoothInputStream(windows_socket_.InputStream());
output_stream_ = BluetoothOutputStream(windows_socket_.OutputStream());
@@ -274,5 +297,22 @@ Exception BluetoothSocket::BluetoothOutputStream::Close() {
}
}
winrt::fire_and_forget BluetoothSocket::Listener_ConnectionStatusChanged(
winrt::Windows::Devices::Bluetooth::BluetoothDevice device,
winrt::Windows::Foundation::IInspectable const& args) {
// During bandwidth upgrade, Bluetooth connection may be dropped due to
// unknown reasons. To track this issue, add a log to track the issue.
// Based on test, the args is empty, so cannot provide more information on
// the status change.
BluetoothConnectionStatus connection_status = device.ConnectionStatus();
NEARBY_LOGS(WARNING) << __func__
<< ": Bluetooth connection status changed to:"
<< ((connection_status ==
BluetoothConnectionStatus::Connected)
? "Connected"
: "Disconnected");
return {};
}
} // namespace windows
} // namespace nearby
@@ -127,11 +127,17 @@ class BluetoothSocket : public api::BluetoothSocket {
IOutputStream winrt_stream_;
};
winrt::fire_and_forget Listener_ConnectionStatusChanged(
winrt::Windows::Devices::Bluetooth::BluetoothDevice device,
winrt::Windows::Foundation::IInspectable const& args);
StreamSocket windows_socket_{nullptr};
BluetoothInputStream input_stream_{nullptr};
BluetoothOutputStream output_stream_{nullptr};
std::unique_ptr<BluetoothDevice> bluetooth_device_ = nullptr;
winrt::Windows::Devices::Bluetooth::BluetoothDevice native_bluetooth_device_{
nullptr};
winrt::event_token connection_status_changed_token_{};
};
} // namespace windows
@@ -393,8 +393,13 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
std::unique_ptr<WifiLanSocket> wifi_lan_socket =
std::make_unique<WifiLanSocket>(socket);
std::string local_address =
winrt::to_string(socket.Information().LocalAddress().DisplayName());
std::string local_port = winrt::to_string(socket.Information().LocalPort());
NEARBY_LOGS(INFO) << "connected to remote service " << ipv4_address << ":"
<< port;
<< port << " with local address " << local_address << ":"
<< local_port;
return wifi_lan_socket;
} catch (...) {
NEARBY_LOGS(ERROR) << "failed to connect remote service " << ipv4_address