Android Question How to realize mqtt communication service unbreakable?

amorosik

Expert
Licensed User
I am trying to make an app 'launcher'
The code includes a:
- configuration activity
- the default Starter service
- a service for mqtt communication
- a service for receiving fcm notifications
- a service for receiving sms
What I would like to obtain is that when the app is started manually, the activity for configuration is displayed and the operator is allowed to enter the necessary data
Restarting the device, only a notification will be visible in the top bar, the sms service will allow you to receive and process any sms received, the fcm service will allow you to receive push notifications sent from the pc, and the mqtt service should always remain active and ready both to receive messages and to respond to commands received from the app
For the connection to the mqtt broker I used the ConnectAndReconnect routine as in the example Erel
To keep the mqtt service alive I tried to follow the 'background location tracking' example
And to check if the service is permanently connected, I added a timer to 1 sec whose only function is to write to the message log, to see the app activity and connection status
I use Mosquitto as a broker
At startup everything seems to go as expected, if I try to turn off the mqtt broker you see the log that signals the disconnection, then when I turn it back on after a few seconds the connection resumes
But occasionally (after minutes or hours of operation) the log seems to stop, signaling the error indicated below
Use B4A 11.0, jdk 11.0.1, jMqtt 1.01
What could the error shown depend on?
How to make Mqtt service unbreakable?

Mqtt:
#Region  Service Attributes
    #StartAtBoot: 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 mqtt_client As MqttClient
    Private serializator As B4XSerializator
    Private mqtt_uri As String
    Private mqtt_name As String
    Private mqtt_comando_ricevuto As String
    Public mqtt_connected As Boolean
    Public flag_connetti_mqtt As Boolean=True
    Public timer1s As Timer
    Private lock As PhoneWakeState
    Private nid As Int = 1
    Type Mqtt_Message (Body As String, From As String)
End Sub

Sub Service_Create
    GlobRob.logga("Mqtt.Service_Create")
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    lock.PartialLock
End Sub

Public Sub SubscribeToTopics
    GlobRob.logga("Mqtt.SubscribeToTopics")
       
    mqtt_uri=IniFile.leggi_ini_locale("AMBIENTE","Mqtt_Uri")
    If mqtt_uri.Length=0 Then
        IniFile.scrivi_ini_locale("AMBIENTE","Mqtt_Uri","tcp://1.2.3.4:63309")
        mqtt_uri="http://1.2.3.4:63309"
        End If

    mqtt_name = GlobRob.right(  IniFile.leggi_ini_locale("AMBIENTE","Numero_Telefono"),9)                                   'GlobRob.right(Main.numero_telefono_configurazione,9)
    If mqtt_name.Length=0 Then
        IniFile.scrivi_ini_locale("AMBIENTE","Numero_Telefono","123456789")
        mqtt_name="123456789"
        End If

    flag_connetti_mqtt=True
    ConnectAndReconnect

    timer1s.Initialize("Timer1sec",1000)
    timer1s.Enabled=True
End Sub

Sub Service_Start (StartingIntent As Intent)
    GlobRob.logga("Mqtt.Service_Start")
    Service.StartForeground(nid, CreateNotification("..."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    SubscribeToTopics
End Sub

Sub Service_Destroy
    GlobRob.logga("Mqtt.Service_Destroy")
    lock.ReleasePartialLock
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("App Launcher in esecuzione", Body, Main)
    Return notification
End Sub

Sub ConnectAndReconnect
    GlobRob.logga("Mqtt.ConnectAndReconnect mqtt_client.connected=" &  mqtt_client.Connected)
   
    Do While flag_connetti_mqtt
        If mqtt_client.IsInitialized Then mqtt_client.Close
        Do While mqtt_client.IsInitialized
            Sleep(200)
            Loop
        mqtt_client.Initialize("EventMqtt", mqtt_uri, mqtt_name & "_LAUNCHER")
        Dim mo As MqttConnectOptions
'        mo.Initialize(username, password)
        mo.Initialize("", "")
        mo.SetLastWill("smartphone/sconnesso" , serializator.ConvertObjectToBytes( mqtt_name & "_LAUNCHER"), 0, False)
        Log("Trying to connect")
        mqtt_client.Connect2(mo)
        Wait For EventMqtt_Connected (Success As Boolean)
       
        If Success Then
            mqtt_connected=True
            LogColor("=============================================",Colors.Green)
            GlobRob.logga("Mqtt.EventMqtt_Connected: " & Success)
            LogColor("=============================================",Colors.Green)
           
            Dim stringa_connected As String= "COM_LAUNCHER=MQTT_CONNESSO;VAL=ON;TEL=" & mqtt_name & ";GUID=" & GlobRob.get_guid2  & ";"
            mqtt_client.Publish2("applauncher_to_consolle", serializator.ConvertObjectToBytes(stringa_connected), 0, False)

            Do While flag_connetti_mqtt And mqtt_client.Connected
                Log ("Ping verso consolle")
                mqtt_client.Publish2("launcher_to_consolle" , Array As Byte(0), 1, False)
                Sleep(5000)
                Loop
               
            GlobRob.logga("Mqtt.EventMqtt_Connected - Disconnected")
            mqtt_connected=False
            Else
            GlobRob.logga("Mqtt.EventMqtt_Connected - Error Connecting")
            mqtt_connected=False
            End If
           
        Sleep(5000)
    Loop
End Sub

Private Sub EventMqtt_MessageArrived (Topic As String, Payload() As Byte)
    GlobRob.logga("Mqtt.EventMqtt_MessageArrived: " & "Topic=" & Topic &  CRLF & "Payload=" & BytesToString(Payload, 0, Payload.Length, "utf8"))
    Dim receivedObject As Object = serializator.ConvertBytesToObject(Payload)
    GlobRob.logga("Mqtt.EventMqtt_MessageArrived: " & "Payload=" & receivedObject)
    GlobRob.logga("---------------------------------------------------------------")
   
    If Topic="consolle_to_telephone" Then
        mqtt_comando_ricevuto=receivedObject
        GlobRob.esegui_comando_ricevuto(mqtt_comando_ricevuto,Topic,"MQTT")
        End If

End Sub

private Sub EventMqtt_Disconnected
    LogColor("=============================================",Colors.Red)
    GlobRob.logga("Mqtt.EventMqtt_Disconnected")
    LogColor("=============================================",Colors.Red)
    mqtt_connected = False
End Sub

Public Sub SendMessage(topic As String,payload As String)
    GlobRob.logga("Mqtt.SendMessage")
    If mqtt_connected Then
        'mqtt_client.Publish(topic, CreateMessage(payload))
        Try
                       
            If topic="telephone_to_consolle" Then
                mqtt_client.Publish(topic, serializator.ConvertObjectToBytes(payload))
                GlobRob.logga("Mqtt.SendMessage - Inviato Topic=" & topic & " - Payload="& payload)
            End If
        Catch
            mqtt_connected =False

            LogColor("=======================================================",Colors.Red)
            GlobRob.logga("Mqtt.SendMessage - Fallito Topic=" & topic & " - Payload="& payload & " - LastException=" & LastException)
            LogColor("=======================================================",Colors.Red)
        End Try
       
    End If
End Sub

Sub Timer1sec_tick()
    Log(GlobRob.data_ora_per_log & " Mqtt.Timer1sec_tick Mqtt_client.Connected=" & mqtt_client.Connected)
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
'

Log:
Ping verso consolle
2021-10-11 07:05:52 038 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
Ping verso consolle
2021-10-11 07:05:53 038 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:05:54 039 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
Ping verso consolle
Ping verso consolle
2021-10-11 07:05:55 040 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
Ping verso consolle
2021-10-11 07:05:56 040 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
=============================================
2021-10-11 07:05:56 675 - Mqtt.EventMqtt_Disconnected
=============================================
2021-10-11 07:05:56 978 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:05:57 041 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:05:57 446 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:05:57 793 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:05:58 042 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:05:59 042 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:05:59 512 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:05:59 516 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:05:59 520 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:06:00 044 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:00 910 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:06:00 914 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:06:01 045 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:01 206 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:06:02 046 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
Trying to connect
2021-10-11 07:06:03 047 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:04 048 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:05 048 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:06 049 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:07 049 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:08 050 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:09 050 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:10 051 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:11 051 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:12 052 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:13 053 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:14 054 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:15 055 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:16 055 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:17 055 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
=============================================
2021-10-11 07:06:17 378 - Mqtt.EventMqtt_Connected: true
=============================================
Ping verso consolle
2021-10-11 07:06:18 055 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:19 056 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:20 057 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:21 056 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:22 057 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
2021-10-11 07:06:23 058 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:24 058 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:25 059 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:26 059 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:27 058 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
=============================================
2021-10-11 07:06:27 779 - Mqtt.EventMqtt_Disconnected
=============================================
2021-10-11 07:06:28 058 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:29 059 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:30 059 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:31 060 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:32 060 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:06:32 389 - Mqtt.EventMqtt_Connected - Disconnected
Trying to connect
=============================================
2021-10-11 07:06:32 637 - Mqtt.EventMqtt_Connected: true
=============================================
Ping verso consolle
Trying to connect
=============================================
2021-10-11 07:06:33 016 - Mqtt.EventMqtt_Connected: true
=============================================
Ping verso consolle
=============================================
2021-10-11 07:06:33 023 - Mqtt.EventMqtt_Disconnected
=============================================
2021-10-11 07:06:33 061 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:06:34 062 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Trying to connect
Trying to connect
=============================================
2021-10-11 07:06:34 661 - Mqtt.EventMqtt_Connected: true
=============================================
mqtt$ResumableSub_ConnectAndReconnectresume (java line: 303)
Client non connesso (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:822)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:829)
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Publish2(MqttAsyncClientWrapper.java:142)
    at aaa.erp2mezzi.applauncher.mqtt$ResumableSub_ConnectAndReconnect.resume(mqtt.java:303)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
2021-10-11 07:06:34 739 - Starter.Application_Error Error:(MqttException) Client non connesso (32104) Stacktrace:Client non connesso (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:822)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:829)
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Publish2(MqttAsyncClientWrapper.java:142)
    at aaa.erp2mezzi.applauncher.mqtt$ResumableSub_ConnectAndReconnect.resume(mqtt.java:303)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
** Service (firebasemessaging) Destroy **
** Receiver (mqtt) OnReceive **
*** Service (mqtt) Create ***
GlobRob Logga lastException:(ErrnoException) android.system.ErrnoException: open failed: EROFS (Read-only file system)
** Service (mqtt) Start **
Service started in foreground mode.
GlobRob Logga lastException:(ErrnoException) android.system.ErrnoException: open failed: EROFS (Read-only file system)
GlobRob Logga lastException:(ErrnoException) android.system.ErrnoException: open failed: EROFS (Read-only file system)
inifile_leggi_ini_locale (java line: 27)
java.io.FileNotFoundException: ini_file.ini: open failed: EROFS (Read-only file system)
    at libcore.io.IoBridge.open(IoBridge.java:496)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
    at anywheresoftware.b4a.objects.streams.File.WriteString(File.java:264)
    at aaa.erp2mezzi.applauncher.inifile._leggi_ini_locale(inifile.java:27)
    at aaa.erp2mezzi.applauncher.mqtt._subscribetotopics(mqtt.java:724)
    at aaa.erp2mezzi.applauncher.mqtt._service_start(mqtt.java:715)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at aaa.erp2mezzi.applauncher.mqtt.handleStart(mqtt.java:100)
    at aaa.erp2mezzi.applauncher.mqtt.access$000(mqtt.java:8)
    at aaa.erp2mezzi.applauncher.mqtt$1.run(mqtt.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:237)
    at aaa.erp2mezzi.applauncher.mqtt.onStartCommand(mqtt.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4415)
    at android.app.ActivityThread.access$1800(ActivityThread.java:270)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7825)
    at libcore.io.IoBridge.open(IoBridge.java:482)
    ... 23 more
java.lang.RuntimeException: Unable to start service aaa.erp2mezzi.applauncher.mqtt@d43502f with Intent { cmp=aaa.erp2mezzi.applauncher/.mqtt (has extras) }: java.lang.RuntimeException: java.io.FileNotFoundException: ini_file.ini: open failed: EROFS (Read-only file system)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4433)
    at android.app.ActivityThread.access$1800(ActivityThread.java:270)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: ini_file.ini: open failed: EROFS (Read-only file system)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:250)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at aaa.erp2mezzi.applauncher.mqtt.handleStart(mqtt.java:100)
    at aaa.erp2mezzi.applauncher.mqtt.access$000(mqtt.java:8)
    at aaa.erp2mezzi.applauncher.mqtt$1.run(mqtt.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:237)
    at aaa.erp2mezzi.applauncher.mqtt.onStartCommand(mqtt.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4415)
    ... 8 more
Caused by: java.io.FileNotFoundException: ini_file.ini: open failed: EROFS (Read-only file system)
    at libcore.io.IoBridge.open(IoBridge.java:496)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
    at anywheresoftware.b4a.objects.streams.File.WriteString(File.java:264)
    at aaa.erp2mezzi.applauncher.inifile._leggi_ini_locale(inifile.java:27)
    at aaa.erp2mezzi.applauncher.mqtt._subscribetotopics(mqtt.java:724)
    at aaa.erp2mezzi.applauncher.mqtt._service_start(mqtt.java:715)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    ... 15 more
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7825)
    at libcore.io.IoBridge.open(IoBridge.java:482)
    ... 23 more
*** Service (starter) Create ***
***************************************************************
********************** START OF PROGRAM ***********************
***************************************************************
2021-10-11 07:07:01 762 - *********************************************
2021-10-11 07:07:01 763 - ************ START OF PROGRAM ***************
2021-10-11 07:07:01 764 - *********************************************
2021-10-11 07:07:01 765 - Starter.Service_Create
2021-10-11 07:07:01 766 - Starter Service_Create
** Service (starter) Start **
2021-10-11 07:07:01 772 - Starter.Service_Start
** Service (mqtt) Create **
2021-10-11 07:07:01 778 - Mqtt.Service_Create
** Service (mqtt) Start **
mqtthandleStart (java line: 99)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.PowerManager$WakeLock.release()' on a null object reference
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent(ServiceHelper.java:155)
    at aaa.erp2mezzi.applauncher.mqtt.handleStart(mqtt.java:99)
    at aaa.erp2mezzi.applauncher.mqtt.access$000(mqtt.java:8)
    at aaa.erp2mezzi.applauncher.mqtt$2.run(mqtt.java:80)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
*** Service (starter) Create ***
***************************************************************
********************** START OF PROGRAM ***********************
***************************************************************
2021-10-11 07:07:03 262 - *********************************************
2021-10-11 07:07:03 265 - ************ START OF PROGRAM ***************
2021-10-11 07:07:03 267 - *********************************************
2021-10-11 07:07:03 269 - Starter.Service_Create
2021-10-11 07:07:03 271 - Starter Service_Create
** Service (starter) Start **
2021-10-11 07:07:03 282 - Starter.Service_Start
** Service (firebasemessaging) Create **
2021-10-11 07:07:03 290 - FirebaseMessaging Service_Create
** Service (firebasemessaging) Start **
2021-10-11 07:07:03 346 - FirebaseMessaging Service_Start
*** Service (mqtt) Create ***
2021-10-11 07:07:03 355 - Mqtt.Service_Create
** Service (mqtt) Start **
2021-10-11 07:07:03 361 - Mqtt.Service_Start
2021-10-11 07:07:03 378 - Mqtt.SubscribeToTopics
2021-10-11 07:07:03 382 - Mqtt.ConnectAndReconnect mqtt_client.connected=false
Trying to connect
2021-10-11 07:07:03 435 - Mqtt.SubscribeToTopics
2021-10-11 07:07:03 437 - Mqtt.ConnectAndReconnect mqtt_client.connected=false
2021-10-11 07:07:03 439 - FirebaseMessaging SubscribeToTopics
=============================================
2021-10-11 07:07:03 630 - Mqtt.EventMqtt_Connected: true
=============================================
Ping verso consolle
2021-10-11 07:07:04 433 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:05 434 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:06 435 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:07 434 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:08 435 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
2021-10-11 07:07:09 436 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:10 436 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:11 436 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:12 437 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:13 437 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
2021-10-11 07:07:14 438 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:15 438 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:16 438 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:17 439 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:18 440 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
Ping verso consolle
2021-10-11 07:07:19 441 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:20 441 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:21 442 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
2021-10-11 07:07:22 443 Mqtt.Timer1sec_tick Mqtt_client.Connected=true
=============================================
2021-10-11 07:07:23 098 - Mqtt.EventMqtt_Disconnected
=============================================
2021-10-11 07:07:23 443 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:23 653 - Mqtt.EventMqtt_Connected - Disconnected
2021-10-11 07:07:24 443 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:25 444 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:26 445 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:27 446 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:28 446 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
Trying to connect
2021-10-11 07:07:29 446 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:30 447 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:31 447 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:32 447 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:33 448 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
Trying to connect
2021-10-11 07:07:34 449 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:35 449 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:36 449 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:37 450 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:38 451 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:39 451 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:40 452 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:41 453 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:42 453 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:43 454 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:44 454 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:45 454 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:46 455 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:47 456 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:48 456 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:49 457 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:50 458 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:51 457 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:52 458 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:53 458 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:54 459 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:55 460 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:56 460 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:57 460 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
2021-10-11 07:07:58 461 Mqtt.Timer1sec_tick Mqtt_client.Connected=false
=============================================
2021-10-11 07:07:58 913 - Mqtt.EventMqtt_Connected: true
=============================================
mqtt$ResumableSub_ConnectAndReconnectresume (java line: 303)
Client non connesso (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:822)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:829)
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Publish2(MqttAsyncClientWrapper.java:142)
    at aaa.erp2mezzi.applauncher.mqtt$ResumableSub_ConnectAndReconnect.resume(mqtt.java:303)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
2021-10-11 07:07:58 935 - Starter.Application_Error Error:(MqttException) Client non connesso (32104) Stacktrace:Client non connesso (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:858)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:822)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish(MqttAsyncClient.java:829)
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Publish2(MqttAsyncClientWrapper.java:142)
    at aaa.erp2mezzi.applauncher.mqtt$ResumableSub_ConnectAndReconnect.resume(mqtt.java:303)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

Mqtt service is started from Starter service like above
Starter:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Private timer1s As Timer
End Sub

Sub Service_Create
    Dim rp As RuntimePermissions
    Main.log_dir =rp.GetSafeDirDefaultExternal(Main.app_name) & "/log"
    If Main.log_dir ="null" Then Main.log_dir=File.DirDefaultExternal & "/log"
    If File.IsDirectory(Main.log_dir,"")=False Then    File.MakeDir("",Main.log_dir)
    
    Main.ini_dir = rp.GetSafeDirDefaultExternal(Main.app_name)
    If Main.ini_dir ="null" Then Main.log_dir=File.DirDefaultExternal
    If File.IsDirectory(Main.ini_dir,"")=False Then    File.MakeDir("",Main.ini_dir)
    
    LogColor("***************************************************************",Colors.Red)
    LogColor("********************** START OF PROGRAM ***********************",Colors.Red)
    LogColor("***************************************************************",Colors.Red)
    
    GlobRob.logga("*********************************************")
    GlobRob.logga("************ START OF PROGRAM ***************")
    GlobRob.logga("*********************************************")
    GlobRob.logga("Starter.Service_Create")
    
    GlobRob.logga("Starter Service_Create")
    CallSubDelayed(Mqtt,"SubscribeToTopics")
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
    IniFile.scrivi_ini_locale("Ambiente","Ultimo_Avviamento",GlobRob.data_ora_per_log)
    
    timer1s.Initialize("Timer1SecEvent",1000)
    timer1s.Enabled=True
End Sub

Sub Service_Start (StartingIntent As Intent)
    GlobRob.logga("Starter.Service_Start")
End Sub

Sub Service_TaskRemoved
    GlobRob.logga("Starter.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
    GlobRob.logga("Starter.Application_Error Error:" & Error & " Stacktrace:" & StackTrace)
    Return True
End Sub

Sub Service_Destroy
    GlobRob.logga("Starter.Service_Destroy")
End Sub

Sub Timer1SecEvent_tick()
'    Log(GlobRob.data_ora_per_log & " Starter.Timer1SecEvent_tick")
End Sub
 
Last edited:

amorosik

Expert
Licensed User
Question: How can a service be unbreakable?
Answer: It cannot. The best you can do is demonstrated in the background location tracking example.

MQTT adds another fragile layer, as devices will turn off the network when the device is sleeping.

lock.PartialLock on Service_Create is this not enough to prevent the device from going into 'sleeping' mode?
 
Upvote 0

amorosik

Expert
Licensed User
On a few devices yes. Most devices will eventually kill the process (and for good reasons).

How is it possible to write a reliable program with this difference between the functioning of one device and another?
Or rather, how to check if the device on which the app is running the lock.PartialLock instruction is executed correctly (without killing the app) by the operating system?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Posting the same questions again and again will not lead to anything useful.
As I already wrote to you many times, the best you can do is to use push notifications.

how to check if the device on which the app is running the lock.PartialLock instruction is executed correctly (without killing the app) by the operating system?
No such thing. There is nothing that you can check.

That was my last answer in this thread.
 
Upvote 0
Top