Renamed ConnectAsync to Connect for the bluetooth socket, since it's not an async method. Cleaned up the connection code for bluetooth socket.

PiperOrigin-RevId: 402346996
This commit is contained in:
jfcarroll
2021-10-11 11:18:33 -07:00
committed by Copybara-Service
parent 99b77bda8e
commit 0a31e427f0
3 changed files with 23 additions and 9 deletions
@@ -182,17 +182,15 @@ std::unique_ptr<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
EnterCriticalSection(&critical_section_);
std::unique_ptr<BluetoothSocket> rfcommSocket =
std::make_unique<BluetoothSocket>(nullptr);
std::make_unique<BluetoothSocket>();
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
@@ -33,6 +33,8 @@ BluetoothSocket::BluetoothSocket(StreamSocket streamSocket)
std::make_unique<BluetoothOutputStream>(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<BluetoothDevice>(
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get());
input_stream_ =
std::make_unique<BluetoothInputStream>(windows_socket_.InputStream());
output_stream_ =
std::make_unique<BluetoothOutputStream>(windows_socket_.OutputStream());
}
BluetoothSocket::BluetoothInputStream::BluetoothInputStream(
@@ -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();