Improve callbacks error handling (#130)

* Improve callbacks error handling

* Update Changelog

* Update CHANGELOG.md

---------

Co-authored-by: Foti Dim <foti@navideck.com>
This commit is contained in:
Rohit Sangwan
2025-01-15 21:53:25 +05:30
committed by GitHub
parent 17f834edef
commit b1b6b680c1
5 changed files with 40 additions and 25 deletions
+1
View File
@@ -1,6 +1,7 @@
## 0.15.0
* `getSystemDevices(withServices:)` now sets several generic services by default as filter
* `getConnectionState` on Android will now return `BleConnectionState.disconnected` if device is connected to the system but not to the app
* Improve callback error handling
## 0.14.0
* BREAKING CHANGE: `bleDevice.name` now filters out non-printable characters
+3 -1
View File
@@ -32,7 +32,9 @@ class BleCommandQueue {
Queue _newQueue(String id) {
final queue = Queue();
queue.onRemainingItemsUpdate = (int items) {
onQueueUpdate?.call(id, items);
try {
onQueueUpdate?.call(id, items);
} catch (_) {}
};
_queueMap[id] = queue;
return queue;
@@ -1,8 +1,10 @@
class BleConnectionUpdate {
final String deviceId;
final bool isConnected;
final String? error;
BleConnectionUpdate({
required this.deviceId,
required this.isConnected,
this.error,
});
+7 -1
View File
@@ -101,13 +101,19 @@ class UniversalBle {
}
}
},
onError: (error) {
if (!completer.isCompleted) {
connectionSubscription?.cancel();
completer.completeError(ConnectionException(error));
}
},
);
_platform
.connect(deviceId, connectionTimeout: connectionTimeout)
.catchError(
(error) {
if (completer.isCompleted == false) {
if (!completer.isCompleted) {
connectionSubscription?.cancel();
completer.completeError(ConnectionException(error));
}
+27 -23
View File
@@ -3,10 +3,14 @@ import 'dart:typed_data';
import 'package:universal_ble/universal_ble.dart';
abstract class UniversalBlePlatform {
StreamController<({String deviceId, bool isConnected, String? error})>?
_connectionStreamController;
// Do not use these directly to push updates
OnScanResult? onScanResult;
OnConnectionChange? onConnectionChange;
OnValueChange? onValueChange;
OnAvailabilityChange? onAvailabilityChange;
OnPairingStateChange? onPairingStateChange;
final Map<String, bool> _pairStateMap = {};
StreamController<BleConnectionUpdate>? _connectionStreamController;
Future<AvailabilityState> getBluetoothAvailabilityState();
@@ -62,49 +66,49 @@ abstract class UniversalBlePlatform {
Stream<BleConnectionUpdate> connectionStream(String deviceId) {
_setupConnectionStreamIfRequired();
return _connectionStreamController!.stream
.where((event) => event.deviceId == deviceId)
.map((event) => BleConnectionUpdate(
isConnected: event.isConnected,
error: event.error,
));
return _connectionStreamController!.stream;
}
void updateScanResult(BleDevice bleDevice) {
onScanResult?.call(bleDevice);
try {
onScanResult?.call(bleDevice);
} catch (_) {}
}
void updateConnection(String deviceId, bool isConnected, [String? error]) {
onConnectionChange?.call(deviceId, isConnected, error);
_connectionStreamController?.add((
_connectionStreamController?.add(BleConnectionUpdate(
deviceId: deviceId,
isConnected: isConnected,
error: error,
));
try {
onConnectionChange?.call(deviceId, isConnected, error);
} catch (_) {}
}
void updateCharacteristicValue(
String deviceId, String characteristicId, Uint8List value) {
onValueChange?.call(
deviceId, BleUuidParser.string(characteristicId), value);
try {
onValueChange?.call(
deviceId, BleUuidParser.string(characteristicId), value);
} catch (_) {}
}
void updateAvailability(AvailabilityState state) {
onAvailabilityChange?.call(state);
try {
onAvailabilityChange?.call(state);
} catch (_) {}
}
void updatePairingState(String deviceId, bool isPaired) {
if (_pairStateMap[deviceId] == isPaired) return;
_pairStateMap[deviceId] = isPaired;
onPairingStateChange?.call(deviceId, isPaired);
}
// Do not use these directly to push updates
OnScanResult? onScanResult;
OnConnectionChange? onConnectionChange;
OnValueChange? onValueChange;
OnAvailabilityChange? onAvailabilityChange;
OnPairingStateChange? onPairingStateChange;
try {
onPairingStateChange?.call(deviceId, isPaired);
} catch (_) {}
}
/// Creates an auto disposable streamController
void _setupConnectionStreamIfRequired() {