dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

MAUI Blazor app: Stops working after connecting with bluetooth keyboard #23916

Open reinhardlackner opened 3 months ago

reinhardlackner commented 3 months ago

Description

If you open a MAUI Blazor app and connect a bluetooth keyboard on a real device, the app gets black. You can't use the app any more until you restart it.

Steps to Reproduce

  1. Create new .NET MAUI Blazor Hybrid app (net8) in Visual Studio

  2. Deploy it to a real Android device (tested with Samsung Galaxy Tab S6 Lite and Samsung Galaxy Tab S7 FE)

  3. Disable bluetooth on the Android device

  4. Start the app

  5. Activate bluetooth and connect to a bluetooth keyboard grafik grafik

  6. App gets black immediately after bluetooth keyboard was connected and you can no longer work with the app. Also switching between the Android apps does not help (purple background color - app does not work) - you really need to close the app and to restart it - after that, working with the app is possible again

grafik

Full video with described repo steps:

https://github.com/user-attachments/assets/4e4a29f8-506a-4705-b098-add8bd37570e

Link to public reproduction project repository

No response

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14 with OneUI version 6.1

Did you find any workaround?

No response

Relevant log output

Debug output in Visual Studio (after bluetooth is activated/connected):

[ViewRootImpl@ef2bbcc[MainActivity]] stopped(true) old = false
[ViewRootImpl@ef2bbcc[MainActivity]] WindowStopped on com.companyname.mauiapp3/crc64e73e5e987e2fb5cd.MainActivity set to true
[OpenGLRenderer] CacheManager::trimMemory(20)
[WindowOnBackDispatcher] sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda19@9b671ce
[OpenGLRenderer] CacheManager::trimMemory(20)
[ViewRootImpl@ef2bbcc[MainActivity]] dispatchDetachedFromWindow
[InputTransport] Input channel destroyed: '8f31f66', fd=194
[DecorView] setWindowBackground: isPopOver=false color=ffffffff d=android.graphics.drawable.ColorDrawable@3f90594
Thread finished: <Thread Pool> #11
The thread 11 has exited with code 0 (0x0).
github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

mattleibow commented 3 months ago

@Eilon thoughts?

Eilon commented 3 months ago

Hi @reinhardlackner - this sounds like an issue that many people have run into and we made a recent patch to .NET MAUI 8 where you can enable a workaround for this issue. See https://github.com/dotnet/maui/pull/22496 for full details.

If you are encountering hangs in Android with BlazorWebView, you can enable this AppContext Switch with one line of code in your app's MauiProgram.cs:

AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", isEnabled: true);

Make sure you're updated to the latest patch of .NET MAUI, and try out this switch. If it resolves your issue (or not!), please let us know.

reinhardlackner commented 3 months ago

Hi @Eilon Unfortunately, this does not solve the issue because AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", isEnabled: true); causes a reload of the whole blazor webview. Same result for MAUI version 8.0.71 (=latest) and 8.0.61 - set in csproj file:

8.0.71

E.g. when you open the "Counter" page and connect to a Bluetooth keyboard, the WebView reloads instead of being black (=> the whole app is reloaded - you are on the "Home" page again) and the state is lost.

grafik grafik

Video:

https://github.com/user-attachments/assets/c3a1fe78-ccf5-4b01-9756-414da8ad84ca

reinhardlackner commented 3 months ago

@Eilon @mattleibow Please reopen this issue because it's not resolved.

Eilon commented 3 months ago

@reinhardlackner alright issue is re-opened so we can investigate further.

Can you confirm if this is all true:

  1. Initially the app was hanging when connecting the Bluetooth keyboard
  2. With the patch and switch enabled, the app does not hang anymore, but it refreshes the page (sort of like it resets)

If that's true, then this might have to do with Android restarting the "Activity" that the page is associated with, which then causes it to re-load and start over again.

I'll need to find someone more knowledgeable about Android specifics to understand if there's something BlazorWebView can do to avoid being completely restarted.

reinhardlackner commented 3 months ago

@reinhardlackner alright issue is re-opened so we can investigate further.

Can you confirm if this is all true:

  1. Initially the app was hanging when connecting the Bluetooth keyboard
  2. With the patch and switch enabled, the app does not hang anymore, but it refreshes the page (sort of like it resets)

If that's true, then this might have to do with Android restarting the "Activity" that the page is associated with, which then causes it to re-load and start over again.

I'll need to find someone more knowledgeable about Android specifics to understand if there's something BlazorWebView can do to avoid being completely restarted.

@Eilon Thanks for reopening. I can confirm that this is true (1+2). 2: And after the implementation of AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", isEnabled: true) the refresh of the browser seems to be a restart of the main activity.

Eilon commented 2 months ago

@reinhardlackner thank you for confirming.