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

Add API to disable Bluetooth #113

Merged
merged 5 commits into from
Dec 9, 2024
Merged

Conversation

rohitsangwan01
Copy link
Contributor

@rohitsangwan01 rohitsangwan01 commented Dec 9, 2024

User description

Implement on

  • Windows
  • Linux
  • Android

PR Type

enhancement


Description

  • Added a new API method disableBluetooth across multiple platforms, including Windows, Linux, and Android.
  • Updated the UI to include a button for disabling Bluetooth when it is enabled.
  • Implemented the disableBluetooth functionality in the platform-specific code for Windows and Linux.
  • Updated Pigeon-generated files to include the new disableBluetooth method.

Changes walkthrough 📝

Relevant files
Enhancement
15 files
mock_universal_ble.dart
Add disableBluetooth method to MockUniversalBle class       

example/lib/data/mock_universal_ble.dart

  • Added disableBluetooth method with UnimplementedError.
+5/-0     
home.dart
Add UI button for disabling Bluetooth                                       

example/lib/home/home.dart

  • Added UI button for disabling Bluetooth.
  • Modified logic to toggle between enabling and disabling Bluetooth.
  • +18/-9   
    universal_ble.dart
    Add disableBluetooth method to UniversalBle class               

    lib/src/universal_ble.dart

    • Added disableBluetooth method to UniversalBle class.
    +9/-0     
    universal_ble_linux.dart
    Implement disableBluetooth method for Linux platform         

    lib/src/universal_ble_linux/universal_ble_linux.dart

    • Implemented disableBluetooth method for Linux platform.
    +17/-0   
    universal_ble.g.dart
    Add Pigeon method for disableBluetooth                                     

    lib/src/universal_ble_pigeon/universal_ble.g.dart

    • Added Pigeon method for disableBluetooth.
    +28/-1   
    universal_ble_pigeon_channel.dart
    Add disableBluetooth method to Pigeon channel                       

    lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart

    • Added disableBluetooth method to Pigeon channel.
    +8/-0     
    universal_ble_platform_interface.dart
    Add abstract disableBluetooth method to interface               

    lib/src/universal_ble_platform_interface.dart

    • Added abstract disableBluetooth method.
    +2/-0     
    universal_ble_web.dart
    Add disableBluetooth method to UniversalBleWeb class         

    lib/src/universal_ble_web/universal_ble_web.dart

    • Added disableBluetooth method with UnimplementedError.
    +5/-0     
    universal_ble.dart
    Add disableBluetooth method to Pigeon interface                   

    pigeon/universal_ble.dart

    • Added disableBluetooth method to Pigeon interface.
    +3/-0     
    universal_ble.g.cpp
    Add Pigeon handler for disableBluetooth in Windows             

    windows/src/generated/universal_ble.g.cpp

    • Added Pigeon handler for disableBluetooth.
    +23/-1   
    universal_ble_plugin.cpp
    Implement DisableBluetooth method for Windows                       

    windows/src/universal_ble_plugin.cpp

    • Implemented DisableBluetooth method for Windows.
    +28/-0   
    UniversalBle.g.kt
    Add Pigeon handler for disableBluetooth in Android             

    android/src/main/kotlin/com/navideck/universal_ble/UniversalBle.g.kt

    • Added Pigeon handler for disableBluetooth.
    +20/-1   
    UniversalBle.g.swift
    Add Pigeon handler for disableBluetooth in Darwin               

    darwin/Classes/UniversalBle.g.swift

    • Added Pigeon handler for disableBluetooth.
    +17/-1   
    universal_ble.g.h
    Add DisableBluetooth method to Pigeon interface in Windows

    windows/src/generated/universal_ble.g.h

    • Added DisableBluetooth method to Pigeon interface.
    +2/-1     
    universal_ble_plugin.h
    Add DisableBluetooth method to plugin interface in Windows

    windows/src/universal_ble_plugin.h

    • Added DisableBluetooth method to plugin interface.
    +1/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Duplicate Error Handling
    The DisableBluetooth method calls both result(FlutterError) and result(false) when Bluetooth is not available, which could cause issues with multiple responses being sent

    Error Handling
    The disableBluetooth method should handle the case where adapter.setPowered(false) succeeds but adapter.powered is still true, which could indicate a failure to disable

    UI State Handling
    The Bluetooth enable/disable button state may not update immediately after the action completes since it depends on bleAvailabilityState which may have a delay in updating

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Remove redundant result callback after error handling to prevent multiple responses

    Remove the redundant result(false) call after result(FlutterError(...)). The
    function should return after sending an error to avoid sending multiple responses.

    windows/src/universal_ble_plugin.cpp [94-98]

     if (!bluetoothRadio)
     {
       result(FlutterError("Bluetooth is not available"));
    -  result(false);
       return;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Calling result() twice can lead to serious issues as the callback should only be invoked once. The second call could crash the application or cause undefined behavior.

    9
    General
    Use proper Exception objects instead of string literals for error handling

    Use throw Exception(...) instead of throwing a string literal to follow Effective
    Dart guidelines for error handling.

    lib/src/universal_ble_linux/universal_ble_linux.dart [61-62]

     if (adapter == null) {
    -  throw "Adapter not available";
    +  throw Exception('Adapter not available');
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using proper Exception objects instead of string literals is a Dart best practice that provides better error handling and debugging capabilities, though the current code would still function.

    5

    💡 Need additional feedback ? start a PR chat

    @fotiDim fotiDim changed the title Add api to disable Bluetooth Add APIto disable Bluetooth Dec 9, 2024
    @fotiDim fotiDim changed the title Add APIto disable Bluetooth Add API to disable Bluetooth Dec 9, 2024
    @rohitsangwan01 rohitsangwan01 merged commit 1292199 into main Dec 9, 2024
    1 check passed
    @rohitsangwan01 rohitsangwan01 deleted the add-api-to-disable-bluetooth branch December 9, 2024 15:48
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants