Android Question struggling with the b4a websocket push app

IslandMedic

Member
Licensed User
Longtime User
I am getting really messed up results each time I run the b4a websocket push client. Can someone tell me when sometimes I run this app and I get a socket connection and sometimes like this example I get the service called but no socket connections. Sometimes the connection gets made and it sits in the ping pong loop and never gets back to Activity (main) resume to continue on processing.. and why am I starting the service in Resume if it is sticky and starts at boot time? why does it look like the service is restarting again? I have never been more confused or frustrated in my life. Almost want to give up on this project. Maybe sleep will help.

thanks

B4X:
Sub Activity_Resume
    StartService(PushService)
   
    Select session_map.Get("device_status")
           
        Case "UnAuth"
etc


B4X:
#Region  Service Attributes 
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

B4X:
** Activity (main) Pause, UserClosed = false **
** Service (pushservice) Create **
** Service (pushservice) Start **
38:94:96:92:CC:89
WebSocket Connected
true
{"type":"event","event":"Device_id","params":{"id":"38:94:96:92:CC:89"}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
** Service (pushservice) Start **
38:94:96:92:CC:89
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
{"type":"event","event":"Device_Ping","params":{}}
** Service (pushservice) Start **
38:94:96:92:CC:89

B4X:
** Activity (main) Pause, UserClosed = false **
** Service (pushservice) Create **
** Service (pushservice) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 34 (WebSocketHandler)
java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.objects.WebSocketWrapper.SendText(java.lang.String)' on a null object reference
    at b4a.example.websockethandler._sendeventtoserver(websockethandler.java:77)
    at b4a.example.main._activity_resume(main.java:575)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at b4a.example.main.afterFirstLayout(main.java:108)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6134)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
** Activity (main) Resume **
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code is not the same as the example I posted. Do you get the same errors with the example, as is?
and why am I starting the service in Resume if it is sticky and starts at boot time?
The service can be started many times. Actually the code starts it every two minutes to make sure that Android doesn't kill it.
 
Upvote 0

IslandMedic

Member
Licensed User
Longtime User
I really should have gone to bed before I posted the rant. it was very simple... I was trying to call the send event the wrong way..

B4X:
'was trying to do this.....
wsh.SendEventToServer("life_device_data",session_map)

'when I needed to do this.

CallSub3(PushService,"send_msg","life_device_data",session_map)

the sub send_msg just takes the incoming arg and runds   wsh.SendEventToServer

I also added code to the Pause activity to shutdown the service while I am in the constant uploading and testing phase. I think there was issues of start and stop or error out and then the service keeps running or something. So now I make sure I stop the service each time so it is a clean start when I test again.

I am up and running now to fine tune the process of sending and receiving messages so it just works and I no longer have to worry about it. I can now create my project around this functionality.

Happy Holidays to all.

Brad
 
Upvote 0
Top