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

The Flutter application cannot start if it has this plugin and there is no bluetooth dongle inserted into the Windows computer. #127

Open
alevlako opened this issue Aug 22, 2022 · 5 comments

Comments

@alevlako
Copy link

How to reproduce:

  1. Run flutter create testapp
  2. flutter pub add quick_blue, you don't need any usage of plugin in your code, this is not a point
  3. Run flutter app with bluetooth dongle (Ok)
  4. Remove bluetooth dongle from computer with started app (Ok)
  5. Close flutter app
  6. Run flutter app without bluetooth dongle (App cannot start)
@Bikram40
Copy link

i am facing the same issue can not run build windows application with this plugin any solution ?

@alevlako
Copy link
Author

alevlako commented Nov 10, 2022

As far as I understand, the application crashes on a level inaccessible for flutter when it try to load this plugin in the absence of a bluetooth device in OS. I did not find any workaround, it is impossible even to display any dialog with error message to the user. I suggest, the problem can only be solved inside the plugin by adding a check for the presence of a BT device before any attempt calling it.

@ghost
Copy link

ghost commented Feb 22, 2023

I faced the same issue and did a little debugging.
I figured out, that the InitializeAsync() function (which is called wheter the plugin is used or not) in the quick_blue_windows_plugin.cpp causes the crash. It calls bluetoothAdapter.GetRadioAsync(), but bluetoothAdapter is null when there is no bluetooth adapter available in the system. You can also easily simulate this by disabling the bluetooth adapter in the device manager.

I could fix it by simply adding a null check:
Old:

winrt::fire_and_forget QuickBlueWindowsPlugin::InitializeAsync() {
  auto bluetoothAdapter = co_await BluetoothAdapter::GetDefaultAsync();
  bluetoothRadio = co_await bluetoothAdapter.GetRadioAsync();
}

New:

winrt::fire_and_forget QuickBlueWindowsPlugin::InitializeAsync() {
  auto bluetoothAdapter = co_await BluetoothAdapter::GetDefaultAsync();
  if (bluetoothAdapter) {
    bluetoothRadio = co_await bluetoothAdapter.GetRadioAsync();
  }
}

However, take this advise with a grain of salt as I am by no means a C++ or Windows System programmer. There might be a better solution, but I got it working with this fix.

@alevlako
Copy link
Author

alevlako commented Feb 25, 2023

I faced the same issue and did a little debugging. I figured out, that the InitializeAsync() function (which is called wheter the plugin is used or not) in the quick_blue_windows_plugin.cpp causes the crash. It calls bluetoothAdapter.GetRadioAsync(), but bluetoothAdapter is null when there is no bluetooth adapter available in the system. You can also easily simulate this by disabling the bluetooth adapter in the device manager.

Great, it works!
I forked master branch and add your fix to code.
Now anyone suffering from this problem can add a plugin to their project's pubspec.yaml file like this:

quick_blue:
git:
url: https://github.com/alevlako/quick_blue.git
ref: master #branch name
path: quick_blue #Folder Path on Github

Updated: Now the solution above does not work, I request the author of the plugin merge the solution to master branch.

alevlako added a commit to alevlako/quick_blue that referenced this issue Apr 18, 2023
Issue woodemi#127: The Flutter application cannot start if it has this plugin and there is no bluetooth dongle inserted into the Windows computer.
@gtedone
Copy link

gtedone commented Oct 20, 2023

Thanks for this fix, it works like a charm!

However, take this advise with a grain of salt as I am by no means a C++ or Windows System programmer. There might be a better solution, but I got it working with this fix.

In my opinion it is a simple and effective solution: if nothing else, with this patch you can launch the application even if bluetooth is disabled. Then you can check the controller activation after the UI comes up (maybe via isBluetoothAvailable static method.

Now anyone suffering from this problem can add a plugin to their project's pubspec.yaml file like this:

@alevlako, you can get it working only if you apply your fork as dep override, rather than the entire package replacement:

dependencies:
  flutter:
    sdk: flutter
  quick_blue: ^0.5.0-dev.2 # leave here the federated plugin

...

dependency_overrides: 
  quick_blue_windows:
    git:
      url: https://github.com/alevlako/quick_blue.git
      ref: issue_127_fix # branch name
      path: quick_blue_windows # Folder Path on Github

Otherwise flutter cannot build the expected patch, since the resulting dep chain is not the desired one.

In other words, I think your pull-request is to be made for the quick_blue_windows sub-project... 😉

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

No branches or pull requests

3 participants