Skip to content

Commit

Permalink
Use retired 1s to prevent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin committed Sep 15, 2024
1 parent 331398b commit d5d9fd0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/lib/providers/device_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption
setConnectedDevice(null);
setIsConnected(false);
updateConnectingStatus(false);
periodicConnect('coming from onDisconnect');
await captureProvider?.resetState(restartBytesProcessing: false);
captureProvider?.setAudioBytesConnected(false);
print('after resetState inside initiateConnectionListener');
Expand All @@ -211,6 +210,11 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption
);
});
MixpanelManager().deviceDisconnected();

// Retired 1s to prevent the race condition made by standby power of ble device
Future.delayed(const Duration(seconds: 1), () {
periodicConnect('coming from onDisconnect');
});
}

void onDeviceReconnected(BTDeviceStruct device) async {
Expand Down
4 changes: 1 addition & 3 deletions app/lib/services/devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ class DeviceService implements IDeviceService {
mutex = true;

try {
if (_connection != null && _connection?.device.id != deviceId) {
throw Exception("There is a connected device ${_connection?.device.id}, you have to disconnect it first.");
}
debugPrint("ensureConnection ${_connection?.device.id} ${_connection?.status}");
if (_connection?.status == DeviceConnectionState.connected) {
var pongAt = _connection?.pongAt;
var shouldPing = (pongAt == null || pongAt.isBefore(DateTime.now().subtract(const Duration(seconds: 5))));
Expand Down

0 comments on commit d5d9fd0

Please sign in to comment.