Android Question Receive from wifi serial in background

poya10

Member
Hi, I want the app to continue running in the background and receive information from the serial WiFi module when I close my app.
---------------------

The starter section codes are as follows:


Starter:
#Region  Service Attributes
    
    #ExcludeFromLibrary: True
    #StartAtBoot: false
    
#End Region

Sub Process_Globals
    
    Dim WiFi_Socket As Socket
    Dim TcpStreams As AsyncStreams
    Dim ServerSocket1 As ServerSocket

    Dim ServerIp As String = "192.168.1.50" 'IP Access point
    Dim Port As Int = 808'    port
    
    Dim noti As Notification
    
    Dim data As String
    Dim connection As Boolean=False
    
    Dim userdisconnect As Boolean=False
    
    
    Dim stg As AriaSharedPreferences
End Sub

Sub Service_Create

    WiFi_Socket.Initialize("WiFi_Socket")
    
    stg.Initialize("s5")
    
    ServerIp=stg.GetString("IpAddress","192.168.1.50")
    Port=stg.GetString("Port","808")
    'Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS 'we are handling it ourselves

End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub


'Sub PhoneEvent_ConnectivityChanged(NetworkType As String, State As String, Intent As Intent)
'   
'    'NetworkType - WIFI or MOBILE.
'    'State - One of the following values: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN.
'   
'    If (NetworkType="MOBILE" And State = "CONNECTED") Then
'        ToastMessageShow("اتصال وجود دارد",False)
'        Log("WIFI IS CONNECT")
'    Else
'        ToastMessageShow("اتصال وجود ندارد",False)
'        Log("WIFI IS NOT CONNECT")
'    End If
'   
'End Sub

Sub WiFi_Socket_Connected (connected As Boolean)
    
    If connected = True Then
        Dim vibr As PhoneVibrate
        vibr.Vibrate(200)
        connection=True
        ToastMessageShow("وصل شدید",True)
        TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")
        Monitoring.connectionState=1
        CallSub(Monitoring,"SetState")
        
'        button1.Text = "متصل"
'        Main.Button1.TextColor = Colors.Green
        
    Else if userdisconnect=False Then
        Dim vibr As PhoneVibrate
        vibr.Vibrate(500)
        connection=False
        ToastMessageShow(("خطا در برقراری ارتباط"&CRLF&"آدرس وارد شده=  "&ServerIp&":"&Port),True)
        Monitoring.connectionState=2
        CallSub(Monitoring,"SetState")
        
    Else If userdisconnect=True Then
        connection=False
        
        Monitoring.connectionState=3
        CallSub(Monitoring,"SetState")
        userdisconnect=False
'        Main.Button1.TextColor = Colors.RED
    End If
        
End Sub

Sub TcpStreams_NewData (Buffer() As Byte)
            
    
    
    Monitoring.inputdata = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    data = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    CallSub(Monitoring , "OnGoingDATA")
    CallSub(Setting,"OnGoingDATA")


    If Monitoring.inputdata.StartsWith("M") Then
        Dim notif As NB6
        
        notif.Initialize("default", Application.LabelName, "MIN").SmallIcon(LoadBitmap(File.DirAssets,"icon apk-Solid.png"))
        notif.OnlyAlertOnce(True)
        notif.SetDefaults(False,False,False)
        Dim cs As CSBuilder
        notif.BigTextStyle("مشخصات کلی دستگاه", cs.Initialize.Append("در زمان "&DateTime.Time(DateTime.Now)).PopAll, "ولتاژ ورودی= "&Monitoring.split(1)&CRLF&"جریان مصرفی= "&Monitoring.split(2)&CRLF&("دمای قطعات= "&Monitoring.split(3)&" درجه سلسیوس"))
        notif.Build("مشخصات کلی", "برای مشاهده اعلان را باز کنید", "tag", Me).Notify(8)
    End If
End Sub

Sub connect

'    ServerIp = Label1.Text
'    Port = Label3.Textl
    userdisconnect=False
    Log("ok")
    If WiFi_Socket.Connected=False Then
        WiFi_Socket.Initialize("WiFi_Socket")
        WiFi_Socket.Connect(ServerIp,Port,1000)
        
        Log(WiFi_Socket.Connected)
        Log (ServerIp)
        Log (Port)
    End If
End Sub

Sub disconnect
    WiFi_Socket.Close
    TcpStreams.Close
    userdisconnect=True
    WiFi_Socket_Connected(False)
    ToastMessageShow("ارتباط را قطع کردید",False)
    Monitoring.connectionState=3
    CallSub(Monitoring,"SetState")
    
End Sub
 

poya10

Member
thanks
First step is to switch to B4XPages. It will allow you to implement everything in one of the pages. The pages are never paused (until the process is killed).
Thanks Erel , So you say using b4x pages solves all the problems and does not require any further steps? Otherwise what are the next steps?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…