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