diff --git a/cpp/platform/impl/windows/bluetooth_classic_medium.cc b/cpp/platform/impl/windows/bluetooth_classic_medium.cc index c26b827d..12cbcf93 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_medium.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_medium.cc @@ -182,17 +182,15 @@ std::unique_ptr BluetoothClassicMedium::ConnectToService( EnterCriticalSection(&critical_section_); std::unique_ptr rfcommSocket = - std::make_unique(nullptr); + std::make_unique(); location::nearby::CancellationFlagListener cancellationFlagListener( cancellation_flag, [&rfcommSocket]() { rfcommSocket.get()->CancelIOAsync().get(); }); try { - rfcommSocket - ->ConnectAsync(requestedService.ConnectionHostName(), - requestedService.ConnectionServiceName()) - .get(); // .get forces synchonicity so we know if it fails immediately + rfcommSocket->Connect(requestedService.ConnectionHostName(), + requestedService.ConnectionServiceName()); } catch (std::exception exception) { // We will log and eat the exception since the caller // expects nullptr if it fails diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.cc b/cpp/platform/impl/windows/bluetooth_classic_socket.cc index d18dc2fd..098be85e 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.cc @@ -33,6 +33,8 @@ BluetoothSocket::BluetoothSocket(StreamSocket streamSocket) std::make_unique(windows_socket_.OutputStream()); } +BluetoothSocket::BluetoothSocket() {} + BluetoothSocket::~BluetoothSocket() {} // NOTE: @@ -72,11 +74,25 @@ api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() { // Starts an asynchronous operation on a StreamSocket object to connect to a // remote network destination specified by a remote hostname and a remote // service name. -winrt::Windows::Foundation::IAsyncAction BluetoothSocket::ConnectAsync( +void BluetoothSocket::Connect( HostName connectionHostName, winrt::hstring connectionServiceName) { + windows_socket_ = winrt::Windows::Networking::Sockets::StreamSocket(); + // https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.connectasync?view=winrt-20348 - return windows_socket_.ConnectAsync(connectionHostName, - connectionServiceName); + windows_socket_.ConnectAsync(connectionHostName, connectionServiceName).get(); + + auto info = windows_socket_.Information(); + auto hostName = info.RemoteHostName(); + + bluetooth_device_ = std::make_unique( + winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync( + windows_socket_.Information().RemoteHostName()) + .get()); + + input_stream_ = + std::make_unique(windows_socket_.InputStream()); + output_stream_ = + std::make_unique(windows_socket_.OutputStream()); } BluetoothSocket::BluetoothInputStream::BluetoothInputStream( diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.h b/cpp/platform/impl/windows/bluetooth_classic_socket.h index 8ebc7b62..7c174b6e 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.h +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.h @@ -93,7 +93,7 @@ class BluetoothSocket : public api::BluetoothSocket { api::BluetoothDevice* GetRemoteDevice() override; // Connect asynchronously to the target remote device - IAsyncAction ConnectAsync(HostName connectionHostName, + void Connect(HostName connectionHostName, winrt::hstring connectionServiceName); IAsyncAction CancelIOAsync();