Android Question [SOLVED] Force WiFi To Stay Connected

RandomCoder

Well-Known Member
Licensed User
Longtime User
It's taken a while for me to work out whats happening, but I'm pretty sure my wireless connection is terminating several minutes after the screen turns off.
I'm acquiring a PartialLock which I understand should keep the CPU running but allow the screen to turn off and I'm also calling my service to start in the foreground. Is there anything else I can do to force the WiFi to stay connected? Maybe an entry in the manifest file?
My problem is that I am creating a playlist of music to stream from my network and after about 4 songs the wireless connection is turned off. I've debugged using the B4ABridge and this also loses connection at the time when the wireless is switched off. When I turn the screen on again I can briefly see that although the WiFi indicator is on in the notification area, it has no signal. This is not a signal problem though as I always have three bars of signal strength. The fault only occurs if the screen is allowed to turn off.
My device is a Nexus 7 2013 and I've include an unfiltered debug log below...
B4X:
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2959.mp3?albumid=114]
      [title      =Sleeping in My Car]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/C7CCBBE607D6DF4A73E5980FC1EC84ED/12%2520-%2520Sleeping%2520in%2520My%2520Car.mp3]
** Service (music_service) Create **
** Service (music_service) Start **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NOW PLAYING :- Sleeping in My Car
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2958.mp3?albumid=114]
      [title      =Almost Unreal]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/A8BCB1FD13A9A3D9D253C646583E0775/11%2520-%2520Almost%2520Unreal.mp3]
GC_CONCURRENT freed 384K, 6% free 9326K/9864K, paused 2ms+2ms, total 25ms
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2957.mp3?albumid=114]
      [title      =How Do You Do!]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/DDA6E4BB83256604FD7E73064FC11B8A/10%2520-%2520How%2520Do%2520You%2520Do%21.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2956.mp3?albumid=114]
      [title      =Spending My Time]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/7FC0B768BD4A19442597D8416CDB76DF/09%2520-%2520Spending%2520My%2520Time.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2955.mp3?albumid=114]
      [title      =Fading Like a Flower (Every Time You Leave)]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/CC106A79FD0D753DDEB2A9142600ABAF/08%2520-%2520Fading%2520Like%2520a%2520Flower%2520(Every%2520Time%2520You%2520Leave).mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2954.mp3?albumid=114]
      [title      =Joyride]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/50DB25331BA818C9933596BD0F711F61/07%2520-%2520Joyride.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2953.mp3?albumid=114]
      [title      =It Must Have Been Love]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/368A47203B0AE73661431AB0AF467B78/06%2520-%2520It%2520Must%2520Have%2520Been%2520Love.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2952.mp3?albumid=114]
      [title      =Dangerous]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/0634390E296D2D77CE08AED13E5BE424/05%2520-%2520Dangerous.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2951.mp3?albumid=114]
      [title      =Listen to Your Heart]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/631C94D47230B3FA3C3D7A2C66CA6639/04%2520-%2520Listen%2520to%2520Your%2520Heart.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2950.mp3?albumid=114]
      [title      =Dressed for Success]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/BF8DB7F356661377FDE7B702CB3A9D3F/03%2520-%2520Dressed%2520for%2520Success.mp3]
RemoveItems
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2948.mp3?albumid=114]
      [title      =One Wish]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/FC7BC740930FD0A880776571B3487C94/01%2520-%2520One%2520Wish.mp3]
##### SELECTED AUDIO FILE DETAILS  #####
      [id        =musicdb://albums/114/2949.mp3?albumid=114]
      [title      =The Look]
      [class      =object.item.audioItem.musicTrack]
      [location  =http://192.168.0.100:1309/%25/82CB81A5795F0B750F5051A39EB375E5/02%2520-%2520The%2520Look.mp3]
** Activity (main) Pause, UserClosed = false **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NOW PLAYING :- Almost Unreal
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NOW PLAYING :- How Do You Do!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GC_CONCURRENT freed 386K, 5% free 9326K/9816K, paused 4ms+4ms, total 68ms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NOW PLAYING :- Spending My Time
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PackageAdded: package:com.avast.android.mobilesecurity
java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.readNumberOfBytes(AsyncStreams.java:272)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:198)
    at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
    at libcore.io.Posix.recvfromBytes(Native Method)
    at libcore.io.Posix.recvfrom(Posix.java:141)
    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
    ... 6 more
libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
MediaPlayerStream error: MEDIA_ERROR_UNKNOWN, -1004
ignoring event: updateip
ignoring event: updateip
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
GC_CONCURRENT freed 312K, 4% free 9518K/9864K, paused 4ms+3ms, total 83ms

All/any help greatly appreciated :D

RandomCoder
 

DonManfred

Expert
Licensed User
Longtime User
Have you had a look at the extended wifi-settings of your device?
I have here an option (sorry for translation):

WIFI in standbymode aktivated
- Always

If i click this option i can select out of three
- Always
- Only when plugged
- Never (only mobile data)

Have a look at this also.

Maybe it helps!?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Thanks Manfred, (translation was just fine!)

I've checked the forum pretty thoroughly before posting and I'm already using a PartialWakeLock but this doesn't appear to have worked. Other Apps such as TuneIn Radio appear to be able to continuously use WiFi whilst the screen is in sleep state so I'm most probably doing something wrong (thats usually the case :confused:)?

Currently I initialise a MediaPlayerStream in Service_Create and in Service_Start I make sure to call Service.StartForeGround and set the PartialWakeLock. I then load a stream and on the Stream_Ready event I start to play it. When the Stream_Complete event fires I then load the next stream. I'm wondering if its possibly this brief moment between one track finishing and the next starting that is giving an opportunity for the system to close the WiFi connection?

Anyway, I've checked my settings and there is an option to keep the WiFi on when in sleep mode. I'm currently testing and hopeful this will work, but ideally I'd like an automated solution. My intention is to share my code once I'm happy all the bugs have been ironed out as there are plenty of different elements involved which I'm sure would be of use to other users. The App is a UPNP Media Control Point which communicates with all UPNP media devices on the network, it navigates the XML files and then streams video and music files.

Thanks,
RandomCoder
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User

@Erel, Once again you've proved that I really must search harder before posting :oops:. Your sample code has worked perfectly on the Nexus 7 and I am currently testing on other devices. I don't know how you find time to see that no question goes unanswered, your an inspiration to us all and I thank you for your help yet again :D

Regards,
RandomCoder
 
Upvote 0
Top