Skip to content
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

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

beastoin
Copy link
Collaborator

@beastoin beastoin commented Sep 15, 2024

Summary by Entelligence.AI

  • New Feature: Enhanced the Bluetooth Low Energy (BLE) device connection management. This update improves the efficiency of pinging connected devices, reducing unnecessary traffic and potentially extending battery life.
  • Refactor: Internal changes to the handling of BLE device connections for better performance and reliability. These modifications are not directly visible to users but contribute to a smoother and more stable user experience.

@beastoin beastoin merged commit d60ae4a into main Sep 15, 2024
1 check passed
@beastoin beastoin deleted the krfevzrh_device_connection_787_clean branch September 15, 2024 06:37
Copy link

github-actions bot commented Sep 15, 2024

Image description Entelligence.AI

Walkthrough

This 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

Files Summary
app/lib/services/device_connections.dart, app/lib/services/devices.dart Introduced a _pongAt field to track the last successful ping response from a BLE device. Updated the logic for pinging connected devices, now based on the last pong time. This improves connection management and reduces unnecessary pings.

"In the realm of bytes and bits, 🌐
A small change makes a big hit. 💥
With each ping and pong in the game, 🏓
Our BLE connections are never the same. 📡"


Uplevel your code reviews with Entelligence.AI Pro

Entelligence.AI Pro

If 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.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description Entelligence.AI

Commits Files that changed from the base of the PR and between d7e820b and 331398b commits.
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 Image description 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.

Comment on lines +212 to +214
if (_connection != null && _connection?.device.id != deviceId) {
throw Exception("There is a connected device ${_connection?.device.id}, you have to disconnect it first.");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description Entelligence.AI

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}.");

Comment on lines 215 to +226
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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description Entelligence.AI

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant