Android Question Socket closed error when adding GPS

rzv25

Member
Licensed User
Longtime User
Hello,
I am working on a car alarm application running on a old Android phone (Galaxy S4 mini - Android 4.4.2) and I am at the very early stages. I have progressively added support for intercepting sms and calls, monitoring battery level and accelerometer. Now I want to add support for monitoring GPS and I face a strange issue. I receive an error in the log file, although the program does not crash and seems to work correctly. Here's the error:
B4X:
Lat = 44:23.65651
Lon = 26:5.74051
Speed =0
java.lang.RuntimeException: java.net.SocketException: Socket closed
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.phone.Phone$PhoneSensors$1.onSensorChanged(Phone.java:1098)
    at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:474)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:138)
    at android.os.Looper.loop(Looper.java:131)
    at android.app.ActivityThread.main(ActivityThread.java:5593)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.net.SocketException: Socket closed
    at libcore.io.Posix.sendtoBytes(Native Method)
    at libcore.io.Posix.sendto(Posix.java:156)
    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
    at libcore.io.IoBridge.sendto(IoBridge.java:466)
    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:508)
    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:270)
    at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
    at anywheresoftware.b4a.shell.ShellConnector.sendControlMessage(ShellConnector.java:62)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:297)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    ... 12 more
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
BatteryChanged. Level: 94, Plugged: false
** Service (starter) Start **
Lat = 44:23.65913
Lon = 26:5.74388
Speed =0

What is confusing me is that even if I hit Clear button in the Logs window and I restart on the phone the B4A-Bridge, when I run the application, it seem to display some older logged data. As you can see above, before the error is reported there are some Lat, Lon, Speed info logged from the previous run of the program. Also, one note is that the error is reported before *** Service (starter) Create *** event, so I suspect it has something to do with the B4A-Bridge application and not with my program.
I have tried to stop and start the B4A-Bridge on the phone, put this seems to be a process running in background and I could not kill it.

Here is how my manifest looks like:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

'CarAlarm specific permissions
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)

AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(Starter,
<intent-filter android:priority="2147483647">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

Starter module code:


B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #ExcludeFromLibrary: 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.
    Dim notif As Notification
    Dim phoneId As PhoneId
    Dim phoneEvents As PhoneEvents
    Dim smsInterceptor As SmsInterceptor
    Dim accelerometer As PhoneSensors
    Dim gps As GPS
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    Starter_PhoneEvents_Init
    Starter_SmsInterceptor_Init
    Starter_Accelerometer_Init
    Starter_Gps_Init

    ' Start running the service in foreground, in order not to be killed by Android OS
    Starter_RunForeground
End Sub

Sub Service_Start (StartingIntent As Intent)

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 Starter_PhoneEvents_Init
    ' Initialize PhoneEvents in order to be able to capure events
    phoneEvents.InitializeWithPhoneState("PhoneEvents", phoneId)
End Sub

Sub Starter_SmsInterceptor_Init
    smsInterceptor.Initialize("SmsInterceptor") 
End Sub

Sub Starter_Accelerometer_Init
    accelerometer.Initialize(accelerometer.TYPE_Accelerometer)
    If accelerometer.StartListening("Accelerometer") = False Then
        LogColor("Accelerometer sensor is not supported!", Colors.Red) 
    End If
End Sub

Sub Starter_Gps_Init
    gps.Initialize ("GPS")
    gps.Start(0, 0)
End Sub

Sub Starter_RunForeground
    notif.Initialize
    notif.Icon = "icon"
    notif.SetInfo("Car Alarm", "Tap to access application", Main)
    notif.Sound=False
    notif.Vibrate=False
    notif.Light=False
    notif.OnGoingEvent=True
    Service.StartForeground(1, notif)
End Sub

'----------------------------------------------------------------------------
' Events
'----------------------------------------------------------------------------
Sub PhoneEvents_BatteryChanged(Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    ' Pass information to BatteryMonitor module
    modBatteryMonitor.Process(Level, Plugged)
End Sub

Sub PhoneEvents_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    ' Pass information to PhoneCallMonitor module
    modPhoneCallMonitor.Process(State, IncomingNumber, Intent)
End Sub

Sub SmsInterceptor_MessageReceived (From As String, Body As String) As Boolean
    ' Pass information to SmsMonitor module
    modSmsMonitor.Process(From, Body)
    Return True
End Sub

Sub Accelerometer_SensorChanged (Values() As Float)
    ' Pass information to AccelerometerMonitor module
    modAccelerometerMonitor.Process(Values)
End Sub

Sub GPS_LocationChanged (location As Location)
    ' Pass information to GpsMonitor module
    modGpsMonitor.Process(location)
End Sub

I am using latest version of B4A, namely 7.01 and B4A-Bridge v2.31

Thank you for any hint you have on what is the cause of the error.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Starter module code:


B4X:
#Region  Service Attributes
    #StartAtBoot: True
You should not set startatboot in the starter service. Use another service and set start at boot there.
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
You should not set startatboot in the starter service. Use another service and set start at boot there.
Thank you DonManfred for the quick reply.

I have disabled startAtBoot, but I get the same error in the log, so I am in the same point :(.
I have enabled startAtBoot when I created the project and I have successfully added support and tested sms and call monitoring, battery level monitoring and accelerometer monitoring without any issues. I only get this error when I added GPS. So, is there a connection between using GPS and enabling startAtBoot ?

I am also confused about the way the error is reported. Even though I clear the Logs window, when I start debugging a new session, the Logs window displays information from the previous session first. Then the error is logged. Then the lines that show my app being started appear and everything seems to work fine, no other error is logged.

Do you know if there is any way to kill the B4A-Bridge process on the phone ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So, is there a connection between using GPS and enabling startAtBoot ?
I guess no. But i know (i read it some times in the past) that setting startatboot in the starter service is not correct. Instead we should use startatboot in another service. I saw issues in the past. The suggestion not to use startatboot in the "Starter service" was always correct.

I´m reading this thread as i´m interested in the solution too.

Yesterday i helped a customer to update his app. We are facing similar problems (Socket Closed exception).

Do you know if there is any way to kill the B4A-Bridge process on the phone ?
Sorry, i can´t help on any Bridge issues. I used the bridge years ago when i started with B4A. For a few months. Then i discovered the power of USB-Debugging and from then i never used the bridge anymore. I always have my device connected over USB-Cable.
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
I always have my device connected over USB-Cable.

Thank you for the tip, DonManfred.

There are no real errors in the logs posted.

You are seeing the end of the previous debugging session. In some cases it is printed in the logs and you see it on the next run.

Thanks for the answer, Erel, but:
1. Why has the previous debugging session ended with an error ?
2. I hit 'Clear' button in the Logs list. Why is the Logs window displaying 'old' information from a previous run ?

Thank you.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. It didn't end with a real error. The app is connected to the IDE debugger with a network socket. When the debugger ends the socket is broken so an error is printed to the system logs.
2. The logs in B4A are written to the system logs. When the bridge logger is connected it shows the last X messages. It cannot know when the new debugging session has started.
 
Upvote 0
Top