GPS - Service Module - Is this right and efficient way?

sangee

Member
Licensed User
Longtime User
I have made an app to update my website of the GPS stats like Lat,Lon and speed every 10 seconds. The code flow is as below..

  • Service autostart at boot time
  • Service is set to start itself again every 10 minutes
  • At service create, I initialize a timer which ticks every 10 seconds once
  • every tick will take the updated lat and lon co-ordiantes and post it to website

Now.. my question.. is this is the efficient way of using GPS inside a app such as this? is reinitializing the GPS every 10 minutes too tricky and eat up lots of battery? (the power charger for this phone is going to be connected always!)

Any help, guidance and advice would be very useful for me... thanks in advance :)


My code...

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Notification1 As Notification
   Dim GPS1 As GPS : GPS1.Initialize("GPS")
   Dim Lat As String 
   Dim Lon As String 
   Dim Speed As String 
   Dim Bearing As String 
   Dim TimerService As Timer

   


End Sub
Sub Service_Create
   Notification1.Initialize
   Notification1.Sound = False
   Service.StartForeground(1,Notification1)
   TimerService.Initialize("TimerService",10000)
   TimerService.Enabled=True

End Sub

Sub TimerService_Tick
   Dim job1 As HttpJob
   Dim tempstr2 As String 
   Dim Hostname As String
   
   Hostname = "somehost"

   'Simple unix lock system. if a file called gpsstop exist then just dont do anything now. else do something
   If File.Exists(File.DirRootExternal & "/download/a3a/", "gpstop") Then
      GPS1.Stop
      'Return
   Else   
      'Access the website and tell the lat and long and where we are now
      GPS1.Start( 0, 0) 'Listen to GPS with no filters.
      tempstr2 = Lat & "," & Lon
      job1.Initialize("Job1", Me)
      job1.Download2("http://www.mywebsite.com/cgi-bin/intercept.pl",Array As String("action", "receive", "devicename", Hostname ,"speed",Speed,"bearing",Bearing,"latlon",tempstr2))
End Sub



Sub Service_Start (StartingIntent As Intent)
   StartServiceAt("", DateTime.Now + 10 * DateTime.TicksPerMinute, True)
End Sub

Sub Service_Destroy
   GPS1.Stop
   Log("Debug: End Of Service (Service_Destroy)")
End Sub


Sub GPS_LocationChanged (Location1 As Location)

Try 

      Dim tempflt As Int 
         Lat =  Location1.Latitude
         If (Lat.Length < 7) Then
            Return
         End If
         Lat = Lat.SubString2(0,7)
            
         Lon =  Location1.Longitude
         If (Lon.Length < 7) Then
            Return
         End If
         Lon = Lon.SubString2(0,7)   

         Speed =  Location1.Speed * 3.6
         Speed = Round(Speed)
         
         tempflt = Location1.Bearing 
         tempflt = Round(tempflt)
      'Convert the bearing degree to directions now
      If (tempflt >0) AND (tempflt < 45) Then
         Bearing = "N"
      End If
      If (tempflt >45) AND (tempflt < 90) Then
         Bearing = "NE"
      End If
      If (tempflt >90) AND (tempflt < 135) Then
         Bearing = "E"
      End If
      If (tempflt >135) AND (tempflt < 180) Then
         Bearing = "SE"
      End If
      If (tempflt >180) AND (tempflt < 225) Then
         Bearing = "S"
      End If
      If (tempflt >225) AND (tempflt < 270) Then
         Bearing = "SW"
      End If
      If (tempflt >270) AND (tempflt < 315) Then
         Bearing = "W"
      End If
      If (tempflt >315) AND (tempflt < 361) Then
         Bearing = "NW"
      End If

Catch
     Return
End Try   
   
   
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    'ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1", "Job2"
            'print the result to the logs
            ToastMessageShow("Uploded GPS info : " & Lat & ":" & Lon ,True)            
            'Log(Job.GetString)
         Case "Job3"
            'Get and set the device status
            'DevStatus(Job.GetString)
         Case "Job4"
            'Send the error trap details to server log
            
      End Select
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub
 

sangee

Member
Licensed User
Longtime User
Hello Erel,

My requirement is to send the GPS coordinates every 10 seconds. I understand it is possible to start and stop the service every 10 seconds. My worry is every time the service is started i have to initialize the GPS and shut it down after 10 seconds only to restart it again. I read in one of your posts that this is not efficient way.. So i went on making the GPS init once in 10 mins but query the changed/updated GPS coordinates using a timer.

Please let me know if this is method advisable... learning :sign0104: :)

Thanks,
Sangee
 
Upvote 0
Top