Roll forward up to cl/351408510.

This commit is contained in:
hai007
2021-01-12 11:16:21 -08:00
parent f93ad3beb1
commit 0be1cd03ce
11 changed files with 171 additions and 32 deletions
@@ -332,6 +332,18 @@ bool BluetoothClassic::StopAcceptingConnections(
BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device,
const std::string& service_name) {
for (int attempts_count = 0; attempts_count < kConnectAttemptsLimit;
attempts_count++) {
auto wrapper_result = AttemptToConnect(bluetooth_device, service_name);
if (wrapper_result.IsValid()) {
return wrapper_result;
}
}
return BluetoothSocket();
}
BluetoothSocket BluetoothClassic::AttemptToConnect(
BluetoothDevice& bluetooth_device, const std::string& service_name) {
MutexLock lock(&mutex_);
NEARBY_LOG(INFO, "BluetoothClassic::Connect: device=%p", &bluetooth_device);
// Socket to return. To allow for NRVO to work, it has to be a single object.
+12 -2
View File
@@ -91,8 +91,8 @@ class BluetoothClassic {
return adapter_.IsValid();
}
// Establishes connection to BT service that was might be started on another
// device with StartAcceptingConnections() using the same service_name.
// Establishes connection to BT service with internal retry for maximum
// attempts of kConnectAttemptsLimit.
// Blocks until connection is established, or server-side is terminated.
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
// Called by client.
@@ -112,6 +112,8 @@ class BluetoothClassic {
static constexpr int kMaxConcurrentAcceptLoops = 5;
static constexpr int kConnectAttemptsLimit = 3;
// Constructs UUID object from arbitrary string, using MD5 hash, and then
// converts UUID to a readable UUID string and returns it.
static std::string GenerateUuidFromString(const std::string& data);
@@ -146,6 +148,14 @@ class BluetoothClassic {
// Returns true if device is currently in discovery mode.
bool IsDiscovering() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Establishes connection to BT service that was might be started on another
// device with StartAcceptingConnections() using the same service_name.
// Blocks until connection is established, or server-side is terminated.
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
// Called by client.
BluetoothSocket AttemptToConnect(BluetoothDevice& bluetooth_device,
const std::string& service_name);
mutable Mutex mutex_;
BluetoothRadio& radio_ ABSL_GUARDED_BY(mutex_);
BluetoothAdapter& adapter_ ABSL_GUARDED_BY(mutex_){