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

Force clean cached bt device before ble disconnect, prevent the edeca… #847

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

beastoin
Copy link
Collaborator

@beastoin beastoin commented Sep 15, 2024

…se on the fast reconnecting

Summary by Entelligence.AI

- Refactor: Enhanced Bluetooth device disconnection process in the Flutter application. The update ensures a clean state by clearing cached Bluetooth device data before disconnecting, improving reliability on fast reconnects.

@beastoin beastoin merged commit 9180ee5 into main Sep 15, 2024
1 check passed
@beastoin beastoin deleted the krfevzrh_device_connection_787_clean branch September 15, 2024 07:52
Copy link

github-actions bot commented Sep 15, 2024

Image description Entelligence.AI

Walkthrough

This update enhances the Bluetooth device disconnection process in a Flutter application. It ensures that cached device data is cleared only after disconnection, preventing potential issues during rapid reconnections. The changes also include code refactoring to remove redundancy and improve maintainability.

Changes

Files Summary
app/lib/pages/home/device.dart, app/lib/pages/home/device_settings.dart The changes enforce a clean state by setting BTDeviceStruct and deviceName to empty strings before disconnecting the Bluetooth device. Redundant code for setting these values post-disconnection has been removed. The disconnection process now precedes clearing of cached device info, mitigating issues on fast reconnecting.

🎉 Here's to the code that's lean and mean,

To the bugs that are no longer seen.

With each pull request, we strive,

To make our app more alive. 🚀

So here's to the devs, working night and day,

Making sure everything's A-OK! 👏


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 49f66bc and fef6576 commits.
Files selected (2)
  • app/lib/pages/home/device.dart (1)
  • app/lib/pages/home/device_settings.dart (1)
Review comments generated (3)
  • Review: 3
  • 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 119 to +123
SharedPreferencesUtil().btDeviceStruct = BTDeviceStruct(id: '', name: '');
SharedPreferencesUtil().deviceName = '';
if (device != null) {
_bleDisconnectDevice(device!);
}

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 changes made here are logical and seem to improve the handling of Bluetooth device disconnection. However, it's important to note that if _bleDisconnectDevice(device!) throws an exception, the subsequent lines will not be executed. This could leave your application in an inconsistent state. To ensure that the application state is always updated, consider using a try/finally block.

+ try {
    if (device != null) {
      _bleDisconnectDevice(device!);
    }
+ } finally {
    SharedPreferencesUtil().btDeviceStruct = BTDeviceStruct(id: '', name: '');
    SharedPreferencesUtil().deviceName = '';
    context.read<DeviceProvider>().setIsConnected(false);
    context.read<DeviceProvider>().setConnectedDevice(null);
    context.read<DeviceProvider>().updateConnectingStatus(false);
+ }

This way, even if _bleDisconnectDevice(device!) throws an exception, the cached Bluetooth device data will still be cleared and the application state will be updated.

Comment on lines +174 to +175
SharedPreferencesUtil().btDeviceStruct = BTDeviceStruct(id: '', name: '');
SharedPreferencesUtil().deviceName = '';

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 changes made here are good for ensuring that the Bluetooth device data is cleared before disconnection. However, it's important to consider potential race conditions. If another part of your code relies on SharedPreferencesUtil().btDeviceStruct or SharedPreferencesUtil().deviceName, and it runs concurrently with this block, it might encounter null or empty values. To avoid this, you could use a lock mechanism to ensure that only one operation is performed at a time.

+ final _lock = Lock();
...
  onPressed: () async {
+   await _lock.synchronized(() async {
      SharedPreferencesUtil().btDeviceStruct = BTDeviceStruct(id: '', name: '');
      SharedPreferencesUtil().deviceName = '';
      if (widget.device != null) {
        await _bleDisconnectDevice(widget.device!);
      }
      context.read<DeviceProvider>().setIsConnected(false);
      context.read<DeviceProvider>().setConnectedDevice(null);
      context.read<DeviceProvider>().updateConnectingStatus(false);
      Navigator.of(context).pop();
+   });
    MixpanelManager().disconnectFriendClicked();
  },

Comment on lines 176 to 182
if (widget.device != null) {
await _bleDisconnectDevice(widget.device!);
}
context.read<DeviceProvider>().setIsConnected(false);
context.read<DeviceProvider>().setConnectedDevice(null);
context.read<DeviceProvider>().updateConnectingStatus(false);
Navigator.of(context).pop();

Choose a reason for hiding this comment

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

Image description Entelligence.AI

These lines look fine as they handle the disconnection process and update the state accordingly. However, it would be better to wrap these operations in a try-catch block to handle any exceptions that may occur during the disconnection process. This will improve error handling and provide a more robust solution.

+ try {
    if (widget.device != null) {
      await _bleDisconnectDevice(widget.device!);
    }
    context.read<DeviceProvider>().setIsConnected(false);
    context.read<DeviceProvider>().setConnectedDevice(null);
    context.read<DeviceProvider>().updateConnectingStatus(false);
    Navigator.of(context).pop();
+ } catch (e) {
+   // Handle or log the exception
+ }

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