WebSocket in ForegroundService stops

363 Views Asked by At

i am currently building an Android App that connects via a WebSocket (via Java WebSockets from TooTallNate) to a Server.The Problem is that on my Moto Z Play (Android 8.0) and my Acer Iconia A3-A40 (Android 6.0) the WebSocket gets closed after a random time (between 8 and 25 Minutes). On my HTC ONE (Android 5.0) this dont happend after i added wakelock and wifilock (let it run 3h, worked perfectly). The problem does not occure on the Android Studio Emulator. On my Moto Z Play i got the following log before my WebSocket is closed:

> 12-14 19:16:23.409 1443-2088/? I/chatty: uid=1000(system) IpManager.wlan0 expire 1 line  
12-14 19:16:23.411 946-1266/? D/CommandListener: Setting iface cfg  
12-14 19:16:23.412 1443-16705/? I/chatty: uid=1000 system_server expire 1 line  
12-14 19:16:23.412 1443-16703/? I/chatty: uid=1000 system_server expire 1 line  
12-14 19:16:23.461 2125-2125/? E/wpa_supplicant: disconnect rssi= -68  
12-14 19:16:23.462 2125-2125/? I/wpa_supplicant: wlan0: CTRL-EVENT-DISCONNECTED bssid=f5:eb:3d:bf:de:b6 reason=3 locally_generated=1 disconnect_rssi=68  
12-14 19:16:23.464 2125-2125/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory  
    nl80211: Failed to set IPv4 unicast in multicast filter  
12-14 19:16:23.474 946-1257/? I/Netd: Destroyed 1 sockets on <the ipadress of the smartphone> in 1.0 ms  
12-14 19:16:23.479 2125-2125/? E/wpa_supplicant: eap_proxy: eap_proxy_notify_config  
    nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory  
12-14 19:16:23.479 20544-20708 D/SocketSerivce: onClose (Here the WebSocket is closed)  
12-14 19:16:23.479 2125-2125/? E/wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter  
12-14 19:16:23.493 2125-2125/? E/wpa_supplicant: eap_proxy: eap_proxy_notify_config  
    eap_proxy: eap_proxy_notify_config

I did some research and thought it would be a doze problem that suspend the Network access but there are a few things that speak against that:

  • the websocket is started in a ForegroundService (that are excluded from doze?)
  • the device was plugged in (doze does not start when plugged in) and the websocket still stopped
  • even when changing battery optimization, it still occured (some time later but still occured)

(but i could be wrong)

What i did so far:
start the ForegroundService via:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      startForegroundService(intent);
}else{
      startService(intent);
}

acquire wakeLock and wifilock (and release when websocket is closed):

powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakeLockMyApp");

wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "WifiLockMyApp");

wakeLock.acquire();
wifiLock.acquire();

checked the onStartCommand-Method

startForeground(..)
return START_STICKY;

thanks in advance

0

There are 0 best solutions below