-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use relay for pinging ble device #845
Conversation
WalkthroughThis update introduces a new mechanism to manage Bluetooth Low Energy (BLE) device connections more efficiently. It adds a timestamp to track the last successful ping response, which helps in determining when to initiate the next ping. This change enhances the connection stability and optimizes the resource usage by avoiding unnecessary pings. Changes
Uplevel your code reviews with Entelligence.AI ProEntelligence.AI ProIf you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, Entelligence.AI Pro is free for open source projects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files selected (2)
- app/lib/services/device_connections.dart (3)
- app/lib/services/devices.dart (1)
Files skipped from review due to trivial changes (1)
- app/lib/services/device_connections.dart
Review comments generated (2)
- Review: 2
- LGTM: 0
Tips
Chat with Entelligence.AI Bot (@Entelligence.AI
)
- Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
- Invite the bot into a review comment chain by tagging
@Entelligence.AI
in a reply.
Code suggestions
- The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
- You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.
Pausing incremental reviews
- Add
@Entelligence.AI: ignore
anywhere in the PR description to pause further reviews from the bot.
if (_connection != null && _connection?.device.id != deviceId) { | ||
throw Exception("There is a connected device ${_connection?.device.id}, you have to disconnect it first."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check for an existing connection to a different device is a good addition. However, the error message could be more informative by specifying which device the user is trying to connect to.
- throw Exception("There is a connected device ${_connection?.device.id}, you have to disconnect it first.");
+ throw Exception("There is a connected device ${_connection?.device.id}, you have to disconnect it before connecting to ${deviceId}.");
if (_connection?.status == DeviceConnectionState.connected) { | ||
var ok = await _connection?.ping() ?? false; | ||
if (!ok) { | ||
await _connection?.disconnect(); | ||
|
||
// try re-connecting | ||
await _connectToDevice(deviceId); | ||
return _connection; | ||
var pongAt = _connection?.pongAt; | ||
var shouldPing = (pongAt == null || pongAt.isBefore(DateTime.now().subtract(const Duration(seconds: 5)))); | ||
if (shouldPing) { | ||
var ok = await _connection?.ping() ?? false; | ||
if (!ok) { | ||
await _connection?.disconnect(); | ||
|
||
// try re-connecting | ||
await _connectToDevice(deviceId); | ||
return _connection; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for pinging a BLE device based on the last recorded pong time (_pongAt
) seems correct. However, there's no handling for the case when _connectToDevice(deviceId)
fails. This could leave the system in an inconsistent state. Consider adding error handling around this call to ensure that any failures are properly managed.
// try re-connecting
- await _connectToDevice(deviceId);
+ try {
+ await _connectToDevice(deviceId);
+ } catch (e) {
+ print('Failed to reconnect: $e');
+ // Handle or rethrow the error as appropriate for your application
+ }
return _connection;
Summary by Entelligence.AI