Add null checkers to Bluetooth Classic medium ConnectToService

PiperOrigin-RevId: 453542256
This commit is contained in:
aaronyujiaze
2022-06-07 15:44:41 -07:00
committed by Copybara-Service
parent ccca7bef6f
commit 199bbc9cd1
3 changed files with 20 additions and 5 deletions
@@ -237,8 +237,13 @@ std::unique_ptr<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
});
try {
rfcomm_socket->Connect(requested_service.ConnectionHostName(),
requested_service.ConnectionServiceName());
bool success =
rfcomm_socket->Connect(requested_service.ConnectionHostName(),
requested_service.ConnectionServiceName());
if (!success) {
LeaveCriticalSection(&critical_section_);
return nullptr;
}
} catch (std::exception exception) {
// We will log and eat the exception since the caller
// expects nullptr if it fails
@@ -83,8 +83,16 @@ 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.
void BluetoothSocket::Connect(HostName connectionHostName,
bool BluetoothSocket::Connect(HostName connectionHostName,
winrt::hstring connectionServiceName) {
if (connectionHostName == nullptr || connectionServiceName.empty()) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Bluetooth socket connection failed. Attempting to "
"connect to empty HostName/MAC address or ServiceName.";
return false;
}
windows_socket_ = winrt::Windows::Networking::Sockets::StreamSocket();
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.connectasync?view=winrt-20348
@@ -106,6 +114,7 @@ void BluetoothSocket::Connect(HostName connectionHostName,
NEARBY_LOGS(INFO) << __func__
<< ": Bluetooth socket successfully connected to "
<< bluetooth_device_->GetName();
return true;
}
BluetoothSocket::BluetoothInputStream::BluetoothInputStream(
@@ -93,8 +93,9 @@ class BluetoothSocket : public api::BluetoothSocket {
api::BluetoothDevice* GetRemoteDevice() override;
// Connect asynchronously to the target remote device
void Connect(HostName connectionHostName,
winrt::hstring connectionServiceName);
// Returns true if successful, false otherwise
bool Connect(HostName connectionHostName,
winrt::hstring connectionServiceName);
IAsyncAction CancelIOAsync();