-
Notifications
You must be signed in to change notification settings - Fork 241
fix: Bluetooth Connection Issue Despite Being Turned On #1324
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
fix: Bluetooth Connection Issue Despite Being Turned On #1324
Conversation
Reviewer's GuideRefines Bluetooth connection handling in badge_message_provider.dart by improving status messages, adding platform-specific enablement flows, and consolidating data transfer logic once the adapter is confirmed ON. Sequence Diagram for Enhanced Bluetooth Enablement LogicsequenceDiagram
title Sequence Diagram for Enhanced Bluetooth Enablement Logic
actor User
participant App as BadgeMessageProvider
participant BTPlugin as FlutterBluePlus
participant OS as OS (Android/iOS)
User->>App: Initiate Send Message
App->>BTPlugin: FlutterBluePlus.adapterState.first
BTPlugin-->>App: Returns initialAdapterState
alt initialAdapterState is not BluetoothAdapterState.on
App->>OS: Determine Platform (Android/iOS/Other)
alt Platform is Android
App->>User: Toast: "Please enable Bluetooth..."
App->>BTPlugin: FlutterBluePlus.turnOn()
BTPlugin->>OS: System prompts/enables Bluetooth
OS-->>BTPlugin: Bluetooth status updated to ON
App->>BTPlugin: FlutterBluePlus.adapterState.where(state == ON).first
BTPlugin-->>App: Confirms adapterState is ON
else Platform is iOS
App->>User: Toast: "Bluetooth is OFF. Please enable it from Settings."
User->>OS: User manually enables Bluetooth in OS Settings
OS-->>BTPlugin: Bluetooth status updated to ON
App->>BTPlugin: FlutterBluePlus.adapterState.where(state == ON).first
BTPlugin-->>App: Confirms adapterState is ON
else Platform is Other (Unsupported)
App->>User: Toast: "Unsupported platform"
App-->>User: Operation Halts
end
end
opt Bluetooth is ON (either initially or after enablement for Android/iOS)
App->>App: generateData(...)
App->>App: Create DataTransferManager(...)
App->>App: transferData(...)
App-->>User: Message transfer process initiated
end
Updated Class Diagram for BadgeMessageProviderclassDiagram
class BadgeMessageProvider {
+sendMessageToDevice(jsonData: Map<String, dynamic>?, isSavedBadge: bool): Future<void>
}
note for BadgeMessageProvider "Internal logic updated for:
- Correct Bluetooth state detection.
- Platform-specific (Android/iOS) Bluetooth enablement prompts.
- Waiting for Bluetooth to be ON before proceeding.
- Consolidated data transfer logic after Bluetooth ON confirmation."
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @samruddhi-Rahegaonkar - I've reviewed your changes - here's some feedback:
- Consider extracting the platform‐specific Bluetooth enablement and state‐waiting logic into a helper method to reduce duplication and improve readability.
- Add a timeout or error handling when awaiting
adapterState.where(...).first
to avoid potential infinite waits if the user never enables Bluetooth.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the platform‐specific Bluetooth enablement and state‐waiting logic into a helper method to reduce duplication and improve readability.
- Add a timeout or error handling when awaiting `adapterState.where(...).first` to avoid potential infinite waits if the user never enables Bluetooth.
## Individual Comments
### Comment 1
<location> `lib/providers/badge_message_provider.dart:116` </location>
<code_context>
+ ToastUtils().showToast('Please enable Bluetooth...');
await FlutterBluePlus.turnOn();
+
+ adapterState = await FlutterBluePlus.adapterState
+ .where((state) => state == BluetoothAdapterState.on)
+ .first;
} else if (Platform.isIOS) {
- ToastUtils().showToast('Please turn on Bluetooth');
</code_context>
<issue_to_address>
Awaiting adapterState change can hang indefinitely
Add a timeout or cancellation mechanism to prevent the app from hanging if Bluetooth is not enabled.
</issue_to_address>
### Comment 2
<location> `lib/providers/badge_message_provider.dart:114` </location>
<code_context>
if (Platform.isAndroid) {
- ToastUtils().showToast('Turning on Bluetooth...');
+ ToastUtils().showToast('Please enable Bluetooth...');
await FlutterBluePlus.turnOn();
+
+ adapterState = await FlutterBluePlus.adapterState
</code_context>
<issue_to_address>
Wrap `turnOn()` in error handling
Consider using try/catch or result checking to handle possible exceptions from `turnOn()`.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
adapterState = await FlutterBluePlus.adapterState | ||
.where((state) => state == BluetoothAdapterState.on) | ||
.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.
issue (bug_risk): Awaiting adapterState change can hang indefinitely
Add a timeout or cancellation mechanism to prevent the app from hanging if Bluetooth is not enabled.
if (Platform.isAndroid) { | ||
ToastUtils().showToast('Turning on Bluetooth...'); | ||
ToastUtils().showToast('Please enable Bluetooth...'); | ||
await FlutterBluePlus.turnOn(); |
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.
issue (bug_risk): Wrap turnOn()
in error handling
Consider using try/catch or result checking to handle possible exceptions from turnOn()
.
Build StatusBuild successful. APKs to test: https://github.com/fossasia/badgemagic-app/actions/runs/16645269630/artifacts/3656678168. Screenshots (Android)
Screenshots (iPhone)
Screenshots (iPad)
|
Enabling Bluetooth or performing file operations may take several seconds, but there is no indication (such as a loading spinner or progress message) to inform the user that something is happening. This could lead to confusion or repeated actions. |
@nope3472 Thank you for the feedback. While enabling Bluetooth or performing file operations may take a few seconds, the current implementation already includes toast messages to inform the user of the ongoing actions. |
3425ed0
to
9a6fb7e
Compare
@samruddhi-Rahegaonkar please update this |
@nope3472 please review the latest changes |
@hpdang yeah will do it |
@samruddhi-Rahegaonkar update the branch please |
fix: update Bluetooth flow as suggested by Sourcery AI
f65d8c5
to
79aed82
Compare
Fixes #1268
Changes
badge_message_provider.dart
to ensure proper Bluetooth state handling.Screenshots / Recordings
Checklist:
constants.dart
without hard coding any value.Summary by Sourcery
Fix Bluetooth connection handling by checking adapter state, enabling it on Android, prompting iOS users, and handling unsupported platforms gracefully.
Bug Fixes:
Enhancements: