Skip to content

Comments

Add Background Device Monitor#216

Draft
fotiDim wants to merge 4 commits intomainfrom
Background-Device-Monitor
Draft

Add Background Device Monitor#216
fotiDim wants to merge 4 commits intomainfrom
Background-Device-Monitor

Conversation

@fotiDim
Copy link
Contributor

@fotiDim fotiDim commented Feb 2, 2026

Add Background Device Monitor feature to the example app that demonstrates BLE scanning from a foreground service on Android.

- Enhanced `PermissionHandler` to be activity-aware, allowing silent success for already granted permissions when no activity is available.
- Updated README with best practices for requesting permissions in foreground before background operations.
…rates BLE scanning from a foreground service on Android.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @fotiDim, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the example application by integrating a background BLE device monitoring capability. It leverages Android's foreground services to continuously scan for user-selected Bluetooth devices, even when the app is not actively in use. The changes include the necessary Android manifest updates, a dedicated Dart service for background operations, and modifications to the UI to manage the monitored device list and service state, providing a more robust and feature-rich demonstration of BLE interaction.

Highlights

  • Background BLE Monitoring: Introduced a new feature to the example application that enables background monitoring of BLE devices on Android using a foreground service.
  • Foreground Service Implementation: Utilizes the flutter_foreground_task package to run a persistent background service that periodically scans for specified BLE devices.
  • Persistent Storage for Monitored Devices: Implemented functionality to save and retrieve a list of monitored device IDs using SharedPreferences, ensuring persistence across app sessions.
  • User Interface Enhancements: Added UI elements in the scanner screen to start/stop the background monitor and allowed users to long-press on scanned devices to add or remove them from the monitored list.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a background device monitoring feature, which is a great addition to the example app. The implementation uses a foreground service on Android to perform periodic BLE scans, which is the correct approach. The code is well-structured, with a clear separation of concerns between the background service logic, UI management, and data storage. I've made a couple of suggestions: one to improve code maintainability by removing a duplicated constant, and another to fix a potential crash in the error handling logic of the background task. Overall, this is a solid implementation of a complex feature.

// Handle scan errors (e.g., Bluetooth off)
FlutterForegroundTask.updateService(
notificationTitle: 'BLE Monitor',
notificationText: 'Scan error: ${e.toString().substring(0, 50)}',
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using substring(0, 50) will throw a RangeError if the error message string is shorter than 50 characters, which could crash the background service. It's safer to use a method that handles shorter strings gracefully, like characters.take().

Suggested change
notificationText: 'Scan error: ${e.toString().substring(0, 50)}',
notificationText: 'Scan error: ${e.toString().characters.take(50)}',

/// Task handler for background BLE device monitoring.
/// Periodically scans for monitored devices and updates the notification.
class BleMonitorTaskHandler extends TaskHandler {
static const String _monitoredDevicesKey = 'monitored_devices';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The key 'monitored_devices' is also defined in StorageService. To avoid duplicating this string literal and improve maintainability, you should define it as a public constant in StorageService and reuse it here.

First, make the key public in example/lib/data/storage_service.dart:

// line 24
static const String monitoredDevicesKey = 'monitored_devices';

Then, you can remove this local constant and use StorageService.monitoredDevicesKey on lines 29 and 52.

_preferences.getStringList('favorite_services') ?? [];

// Monitored devices for background scanning
static const String _monitoredDevicesKey = 'monitored_devices';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This key is also used in BleMonitorTaskHandler. To avoid string duplication and improve maintainability, consider making this constant public so it can be imported and used in other parts of the app.

Suggested change
static const String _monitoredDevicesKey = 'monitored_devices';
static const String monitoredDevicesKey = 'monitored_devices';

Base automatically changed from foreground-task-support to main February 4, 2026 18:12
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