Revert "Add default timeout in Connect, Pair and IsPaired apis"

This reverts commit b978b81cb1.
This commit is contained in:
Foti Dim
2024-09-25 16:42:29 +02:00
parent 4914687284
commit f41aceaa0e
+10 -27
View File
@@ -76,14 +76,12 @@ class UniversalBle {
/// Connect to a device. /// Connect to a device.
/// It is advised to stop scanning before connecting. /// It is advised to stop scanning before connecting.
/// It might throw errors if device is not connectable. /// It might throw errors if device is not connectable.
/// Default connection timeout is 60 sec /// `connectionTimeout` is supported on Web only.
static Future<bool> connect( static Future<bool> connect(
String deviceId, { String deviceId, {
Duration? connectionTimeout, Duration? connectionTimeout,
}) async { }) async {
connectionTimeout ??= const Duration(seconds: 60);
StreamSubscription? connectionSubscription; StreamSubscription? connectionSubscription;
try { try {
Completer<bool> completer = Completer(); Completer<bool> completer = Completer();
@@ -106,7 +104,10 @@ class UniversalBle {
}, },
); );
return await completer.future.timeout(connectionTimeout); if (connectionTimeout != null) {
return await completer.future.timeout(connectionTimeout);
}
return await completer.future;
} finally { } finally {
connectionSubscription?.cancel(); connectionSubscription?.cancel();
} }
@@ -210,7 +211,6 @@ class UniversalBle {
static Future<bool?> isPaired( static Future<bool?> isPaired(
String deviceId, { String deviceId, {
BleCommand? pairingCommand, BleCommand? pairingCommand,
Duration? connectionTimeout,
}) async { }) async {
if (BleCapabilities.hasSystemPairingApi) { if (BleCapabilities.hasSystemPairingApi) {
return _bleCommandQueue.queueCommand( return _bleCommandQueue.queueCommand(
@@ -218,12 +218,8 @@ class UniversalBle {
deviceId: deviceId, deviceId: deviceId,
); );
} else if (pairingCommand != null) { } else if (pairingCommand != null) {
return _connectAndExecuteBleCommand( return _connectAndExecuteBleCommand(deviceId, pairingCommand,
deviceId, updateCallbackValue: false);
pairingCommand,
connectionTimeout: connectionTimeout,
updateCallbackValue: false,
);
} }
return null; return null;
} }
@@ -240,16 +236,11 @@ class UniversalBle {
static Future<bool?> pair( static Future<bool?> pair(
String deviceId, { String deviceId, {
BleCommand? pairingCommand, BleCommand? pairingCommand,
Duration? connectionTimeout,
}) async { }) async {
if (BleCapabilities.hasSystemPairingApi) { if (BleCapabilities.hasSystemPairingApi) {
return _platform.pair(deviceId); return _platform.pair(deviceId);
} }
return _connectAndExecuteBleCommand( return _connectAndExecuteBleCommand(deviceId, pairingCommand);
deviceId,
pairingCommand,
connectionTimeout: connectionTimeout,
);
} }
/// Unpair a device. /// Unpair a device.
@@ -315,20 +306,12 @@ class UniversalBle {
static Future<bool?> _connectAndExecuteBleCommand( static Future<bool?> _connectAndExecuteBleCommand(
String deviceId, String deviceId,
BleCommand? bleCommand, { BleCommand? bleCommand, {
Duration? connectionTimeout,
bool updateCallbackValue = false, bool updateCallbackValue = false,
}) async { }) async {
try { try {
bool isConnected = if (await getConnectionState(deviceId) != BleConnectionState.connected) {
await getConnectionState(deviceId) != BleConnectionState.connected; await connect(deviceId);
if (!isConnected) {
isConnected = await connect(
deviceId,
connectionTimeout: connectionTimeout,
);
} }
if (!isConnected) return null;
List<BleService> services = await discoverServices(deviceId); List<BleService> services = await discoverServices(deviceId);