quick_blue icon indicating copy to clipboard operation
quick_blue copied to clipboard

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

Open alevlako opened this issue 2 years ago • 5 comments

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)

alevlako avatar Aug 22 '22 12:08 alevlako

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

Bikram40 avatar Nov 10 '22 06:11 Bikram40

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.

alevlako avatar Nov 10 '22 09:11 alevlako

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.

ghost avatar Feb 22 '23 14:02 ghost

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 avatar Feb 25 '23 21:02 alevlako

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... :wink:

gtedone avatar Oct 20 '23 18:10 gtedone