HttpServer problem [solved]

sirjo66

Well-Known Member
Licensed User
Longtime User
Hello,

I'm using "HttpServer" program on my Android but I have same problems :sign0085:

Sometimes it is very very slow to answer, and sometimes it don't answer :BangHead:

I try some times on my PC browser, but it don't answer, so, on Android, I run "internet browser" and it work again !!

May be a wifi stand-by ??
I have setting "Keep Wi-fi on during sleep" to "Always" (as explain here Set Wi-Fi sleep policy to "Never" – User Central for MightyText ) but the problem is the same :-(

How can I solve it ??

Thanks
Sergio
 

sirjo66

Well-Known Member
Licensed User
Longtime User
Try to acquire a partial lock (PhoneWakeState) and also call Service.StartForeground.

Hello Erel and thanks for your reply, but I am a newbie and I don't understand what you write :-(

Can you explain me better ??

I have found also this code, may be a solution ??

B4X:
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiLock lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag");
lock.acquire();
Thanks !!
Sergio
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I had a similar problem and for me it was WiFi standby. It did not respect the policy that had been set.
I used this library 'ToggleWifiData' to fix it.
 
Last edited:
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I had a similar problem and for me it was WiFi standby. It did not respect the policy that had been set.
I used this library 'ToggleWifiData' to fix it.

Many thanks !
I have take a look on 'ToggleWifiData' but I find only a function for active/deactive wifi connection, and I don't understand what I need to use.

My wifi is always active.

Sergio
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User

You need to try Erel's idea's first. If you are doing these things then I would check the WiFi.

I have a timer that fires every minute and checks the status of the wifi.
Here is my code.

B4X:
Public Sub CheckWifI
   Dim oo As ServerSocket, IP As String = oo.GetMyWifiIP
   Log("Checking IP: " & IP)
   If IP = "127.0.0.1" Then
      ToastMessageShow("Network Disconnected...",True)
      '--- wifi is OFF
      TurnWiFiOn
   End If
End Sub


Public Sub TurnWiFiOn
   Dim myWIFI_Test As ToggleWifiData : myWIFI_Test.Initialize
   Dim WIFIstatus As Boolean = myWIFI_Test.isWIFI_enabled
   
   Dim s As String = "Status of WIFI = " & WIFIstatus &  " --> Will now toggle WIFI..." 
   ToastMessageShow(s,True)
   LogWrite(s,g.ID_LOG_INFO)
   
   myWIFI_Test.toggleWIFI
   
End Sub

For some reason my Wifi shuts off a few times every hour.

But check what Erel said first.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I have changed this routine:

B4X:
Sub Service_Create

   server.Initialize("Server")
   server.Start(port)
   'templates.Initialize
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo("SendSMS attivo", "", Main)
   Service.StartForeground(1, n)
   
   Dim oo As PhoneWakeState 
   oo.PartialLock 
   
End Sub

When the program start it runs, if I call the page after 1 minutes it works, but if I wait an half of hour it don't works and don't answer to me :sign0148:

Sergio
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Your code looks right,
I would add a timer and check to see if the IP resets back to 127.0.0.1

Anybody else have any ideas?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try to add the wifi lock as well. This example is done in an Activity. It should be simple to use it from a service instead:
Manifest editor:
B4X:
AddPermission(android.permission.WAKE_LOCK)

B4X:
Sub Process_Globals
   Private wifilock As Object
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Dim r As Reflector
      r.Target = r.GetContext
      r.Target = r.RunMethod2("getSystemService", "wifi", "java.lang.String")
      wifilock = r.RunMethod4("createWifiLock", Array As Object(1, "locktag"), _
         Array As String("java.lang.int", "java.lang.String"))
   End If
End Sub

Sub AcquireLock
   Dim r As Reflector
   r.Target = wifilock
   r.RunMethod("acquire")
End Sub

Sub ReleaseLock
   Dim r As Reflector
   r.Target = wifilock
   r.RunMethod("release")
End Sub

Sub Activity_Resume
   AcquireLock
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   ReleaseLock
End Sub
 
Upvote 0
Top