From 06713fe9187dcda99389505bd76bef98e51db538 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Fri, 6 Jan 2023 12:01:22 -0800 Subject: [PATCH] Log Bluetooth connection status change PiperOrigin-RevId: 500228494 --- .../windows/bluetooth_classic_socket.cc | 50 +++++++++++++++++-- .../windows/bluetooth_classic_socket.h | 8 ++- .../implementation/windows/wifi_lan_medium.cc | 7 ++- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.cc b/internal/platform/implementation/windows/bluetooth_classic_socket.cc index a8f2967b..0599b896 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.cc @@ -15,28 +15,44 @@ #include "internal/platform/implementation/windows/bluetooth_classic_socket.h" #include +#include #include +#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( + 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(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( + 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(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 diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.h b/internal/platform/implementation/windows/bluetooth_classic_socket.h index 6a7ec52f..731a7afc 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.h +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.h @@ -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 bluetooth_device_ = nullptr; + winrt::Windows::Devices::Bluetooth::BluetoothDevice native_bluetooth_device_{ + nullptr}; + winrt::event_token connection_status_changed_token_{}; }; } // namespace windows diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 36e0d229..d0a90952 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -393,8 +393,13 @@ std::unique_ptr WifiLanMedium::ConnectToService( std::unique_ptr wifi_lan_socket = std::make_unique(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