Fix: Android native connection errors are not displayed (#5)

* fix(android, lib): add error logging, handle running jobs exception

* doc(app_service): add comment

* fix(exampl_full, utils): move getCurrentDevice info and fix logs

* doc: add comments, update changelog

* doc: update changelog

* version: 0.0.7

* doc: update readme

* doc: update readme
This commit is contained in:
Kseniia Nikitina
2024-04-21 16:38:45 +02:00
committed by GitHub
parent 6abb8024dd
commit e4a8c2243f
19 changed files with 398 additions and 87 deletions
+70 -42
View File
@@ -50,10 +50,8 @@ class AppService extends ChangeNotifier {
updateState(
Platform.isAndroid ? AppState.permissions : AppState.selectClientType,
);
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
} finally {
notifyListeners();
}
@@ -62,10 +60,8 @@ class AppService extends ChangeNotifier {
Future<void> getCurrentDeviceInfo() async {
try {
currentDeviceInfo = await _nearbyService.getCurrentDeviceInfo();
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
}
}
@@ -75,10 +71,8 @@ class AppService extends ChangeNotifier {
if (result ?? false) {
updateState(AppState.checkServices);
}
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
}
}
@@ -101,17 +95,41 @@ class AppService extends ChangeNotifier {
updateState(AppState.readyToDiscover);
}
Future<bool> hasRunningJobs() async {
try {
final result = await _nearbyService.getPeers();
// if one of devices is connecting, service is busy (android only)
if (result.any(
(element) => element.status == NearbyDeviceStatus.connecting,
)) {
if (kDebugMode) {
print('Service has already running jobs');
}
return true;
}
return false;
} catch (e, s) {
_log(e, s);
return false;
}
}
Future<void> discover() async {
try {
await getCurrentDeviceInfo();
final result = await _nearbyService.discover();
if (result) {
final hasRunning = await hasRunningJobs();
if (hasRunning) {
updateState(AppState.discoveringPeers);
} else {
final result = await _nearbyService.discover();
if (result) {
updateState(AppState.discoveringPeers);
}
}
} catch (e) {
if (kDebugMode) {
print(e);
}
} on NearbyServiceBusyException catch (_) {
_logBusyException();
} catch (e, s) {
_log(e, s);
}
}
@@ -121,20 +139,20 @@ class AppService extends ChangeNotifier {
if (result) {
updateState(AppState.readyToDiscover);
}
} catch (e) {
if (kDebugMode) {
print(e);
}
} on NearbyServiceBusyException catch (_) {
_logBusyException();
} catch (e, s) {
_log(e, s);
}
}
Future<void> connect(NearbyDevice device) async {
try {
await _nearbyService.connect(device);
} catch (e) {
if (kDebugMode) {
print(e);
}
} on NearbyServiceBusyException catch (_) {
_logBusyException();
} catch (e, s) {
_log(e, s);
}
notifyListeners();
}
@@ -142,10 +160,10 @@ class AppService extends ChangeNotifier {
Future<void> disconnect([NearbyDevice? device]) async {
try {
await _nearbyService.disconnect(device);
} catch (e) {
if (kDebugMode) {
print(e);
}
} on NearbyServiceBusyException catch (_) {
_logBusyException();
} catch (e, s) {
_log(e, s);
} finally {
await stopListeningAll();
}
@@ -194,10 +212,8 @@ extension ConnectionInfoExtension on AppService {
_notify();
},
);
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
}
_notify();
}
@@ -218,10 +234,8 @@ extension PeersExtension on AppService {
},
);
updateState(AppState.streamingPeers);
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
}
}
@@ -310,10 +324,8 @@ extension CommunicationChannelExtension on AppService {
Future<void> endCommunicationChannel() async {
try {
await _nearbyService.endCommunicationChannel();
} catch (e) {
if (kDebugMode) {
print(e);
}
} catch (e, s) {
_log(e, s);
}
_notify();
}
@@ -384,3 +396,19 @@ extension MessagingExtension on AppService {
_notify();
}
}
extension LoggingExtension on AppService {
void _log(e, StackTrace s) {
if (kDebugMode) {
print('$e, \nStacktrace: $s');
}
}
void _logBusyException() {
if (kDebugMode) {
print(
'Nearby service is busy, wait a little and retry (You can implement retry in your code)',
);
}
}
}