Android Question How to listen to UDP with screen off?

coupe70

Member
Dear forum experts,

I have some programming experience, but I'm new to B4A and programming for Android.
I have a quite simple app. It listens to UDP and whenever it receives ANYTHING (for the moment), it starts an mp3 with Media Player.
This works fine, but it misses the play trigger with screen off. I would GUESS that it's UDP stopping to listen and not the Media Player being
unable to start playing. While this might be the standard setting and useful for many cases, it's just a waste of battery to keep the screen
on in my case.

Is there any setting, permission or other way to make this work with screen off?

B4X:
#Region  Project Attributes
    #ApplicationLabel: UDP_Player1
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI   
    Dim UDPSocket1 As UDPSocket
    Dim MediaPlayer1 As MediaPlayer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    If FirstTime Then
        UDPSocket1.Initialize("UDP", 12101, 8000)
        MediaPlayer1.Initialize( )
        MediaPlayer1.Load(File.DirAssets,"flash.mp3")
    End If

End Sub

Sub Activity_Resume
    'MediaPlayer1.Play
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    'If MediaPlayer1.IsPlaying Then MediaPlayer1.Pause
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    'msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    'Msgbox("Message received: " & msg, "")
    
    MediaPlayer1.Play
End Sub
 

coupe70

Member
I have to correct myself:
When I send a UDP message to the phone while screen is off, nothing happens.
BUT as soon as I turn on the screen the mp3 starts playing.
So the UDP message seems to be received with screen off, but not processed. Or it is the Media Player not playing with screen off.

Anyway, some help would be appreciated.
 
Upvote 0

coupe70

Member
First step is to switch to B4XPages. Never write communication code in activities.
Well I've got this from the official documentation...
https://www.b4x.com/android/help/network.html (-> UDPSocket)

It seemed legit to me as it's only the initialization of UDP and no further "active" communication.
So I'll have a look at what B4XPages is about.
I was so amazed how simple it was to get UDP and mp3 playback running without diving
in too deep, so I hoped that there was an easy tweak.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
1. Move the communication code to a service an start it as a foreground service
2. Search for wakelock (prevents your phone from sleeping too deep)
3. In the phone's app settings allow your app to run in the background (energy savings)
4. Try
 
Upvote 0

coupe70

Member
1. Move the communication code to a service an start it as a foreground service
2. Search for wakelock (prevents your phone from sleeping too deep)
3. In the phone's app settings allow your app to run in the background (energy savings)
4. Try

Thank you very much for your advice!
I only needed this one app, so I was a bit lazy in the beginning and was hoping for someone to help me out
with some code, but in the end it was quite interesting to invest the time to read about services and what's
going on in Android with processes, activities & services.

It was sufficient to move the code to Starter service. Caring about foreground service was not necessary,
I didn't have to deal with (partial) wakelock and there were no interfering energy settings. It just works in
all situations. But good to have all this in mind if the app should fail on other devices.

Thanks again!
 
Upvote 0

coupe70

Member
Meh...seems functionality ceases after 30 minutes of screen off.
Is this probably related to (partial) wakelock or foreground service being needed to keep this alive?
 
Upvote 0

coupe70

Member
I had similar question some time ago, please see this thread. In a nutshell - I had no success with UDP and used MQTT instead.
@Gandalf Thank you for chiming in.
I think I came across your thread on my search as I did with many others.
This forum is so full with valuable information, but it can also be a bit overwhelming and difficult to keep an overview.
I'm actually a bit confused as I asked about the same thing as you did, but while you got the information "won't work" I
got advice to change certain things to make it work.

And I'm wondering what the culprit is.
May I ask about the problem you had?
"UDP packets are not received when device comes to sleep" - how did you find out the UDP packets were not received?
In my case they obviously were received with screen off, as they were processed when I turned the screen back on.
But I was able to make it work by putting my code in a service. Now it works with screen off, but stops working after
30 minutes. What was the moment you call "when device comes to sleep"? Was it the moment you turned off the screen
or later like for me? And what I'm wondering about: UDP and MQTT are both network protocols - why does MQTT work
for you and UDP doesn't?

At the moment I just don't understand WHAT is turned off after 30 minutes of deactivated screen and how to avoid it.
 
Last edited:
Upvote 0

Gandalf

Member
Licensed User
Longtime User
"UDP packets are not received when device comes to sleep" - how did you find out the UDP packets were not received?
I logged each incoming UDP packet, and log stopped when screen turned off automatically by timer. If I turned screen off manually, packets were logged for several minutes then stopped also. When screen turned back on, packets got received again. I suppose it depends on each particular device type and Android build - how battery saving is implemented there. So it seems your device makes it 30 minutes after screen turns off. Do you see the service stopped and/or process killed in log at this moment? You can try to add REQUEST_IGNORE_BATTERY_OPTIMIZATIONS - maybe it will help with your device.
 
Upvote 0
Top