How can a MAUI app detect when the system is sleeping on Windows? #29986
Unanswered
OwenFromAI
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My MAUI 8 app works with a Bluetooth device. Specifically on Windows, when the system goes to sleep, something causes the Bluetooth data stream to be disrupted and we get a lot of garbage data. I want to handle this by detecting system sleep notifications and responding by telling my Bluetooth device to stop streaming data.
It seemed like the
Microsoft.Win32.SystemEvents.PowerModeChanged
event might be what I want. The documentation doesn't specifically list MAUI or WinUI, so I gave it a try. No matter what I do this event does not seem to be raised. However, I do reliably getSessionSwitch
events when the device locks or unlocks. It seems like on some systems, sleeping can engage a "SessionLock" event, but in my testing I find this to be inconsistent. That happens on my laptop, but does not happen on the tablet customers will be using. I haven't found a device that raisesPowerModeChanged
at all.I found a post for Windows App SDK here that indicated using some P\Invoke code. I tried it out but my app crashes when the device wakes from sleep so either that P\Invoke is wrong (it looks right to me) or this is simply not supported in a MAUI app.
I understand there are App Lifecycle Events but these do not seem to map in a way that will be convenient for me. I had hoped the
Stopped
andResumed
events would work for this, but they are not reliable (as in, they aren't raised every time the device sleeps.) I do getDeactivated
andActivated
, but those also happen if a user switches applications or even uses the Start Menu, and that's far too frequent for me to be shutting down our Bluetooth device every time it happens. Further, if my app is backgrounded (which it is, if the user is using the start menu) I don't seem to get these events upon sleep/wake.What am I missing?
Beta Was this translation helpful? Give feedback.
All reactions