Android Question Set date and gps

hub73

Active Member
Licensed User
Longtime User
Hi
i'm trying to determine date and time only from a usb gps connected to my mk808b rooted dongle.

so.

i could determine the date and time now. I need to set it on the system.

1) Into another program (monkey-x) i could automatically set the date directly from java. How to convert this to b4a code ?

B4X:
   static void setdate(String an, String mois, String jour, String heure, String minutes, String secondes)

{

try

{

// date -s YYYYMMDD.HHMMSS

Runtime.getRuntime().exec(new String[]{ "su", "-c", "date -s " + an + mois + jour + "." + heure + minutes + secondes });

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

2) i use DateUtils. what's the code/algo/method of DateUtils.UnixTimeToTicks(UnixTime) ? I need to understand how to code this next into my monkey-x program. (Unix time to ticks)

Many thanks !
 

hub73

Active Member
Licensed User
Longtime User
Hi !

My goal : set the date on an android device without wifi. (the date is never correct at startup as the dongle not save it).

So i use an external gps connected to the usb port of my google tv dongle mk808b.
i consider that date and time are data transmitted by gps (bu 353s4)

I use 'you are here gps apk' (seems to work). Next lauch my application.

but... i don't know why the time is correct but not the date !!! i"ve for example this : 23.05.1985... (french format displayed).

1) So the date transmitted from gps isn't the day date ?
2) i post the code, perhaps something wrong with it...
3) could you help me to use GPS_NMEA and display something ;-) Perhaps i should myself parse this !!!


Many many thanks and sorry for bad english.

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: GPS
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
#End Region

Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblTime As Label
    Dim lblSatellites As Label
    Dim bSetDateTime As Boolean
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GPS1.Initialize("GPS")
    End If
    Activity.LoadLayout("layout_gps")
    bSetDateTime = False
End Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub

Sub Compute_date_time (UnixTime As Long)
   
    Dim targetDate As Long = DateUtils.UnixTimeToTicks(UnixTime)

    DateTime.DateFormat = "yyyyMMdd"
    DateTime.TimeFormat = "HHmmss"
    DateTime.SetTimeZone(1)

    If bSetDateTime = False Then
       
        bSetDateTime = True
        SetDateTime (DateTime.Date(targetDate), DateTime.Time(targetDate))
        ToastMessageShow ("Date réglée.",True)
   
    End If

    DateTime.DateFormat = "dd.MM.yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
    lblTime.Text = DateTime.Date(targetDate) & " / " & DateTime.Time(targetDate)

End Sub

Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    Compute_date_time(Location1.Time / 1000)
   
End Sub

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

Sub GPS_GpsStatus (Satellites As List)
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
            & " " & Satellite.Elevation
    Next
End Sub

Sub SetDateTime(ToDate As String, ToTime As String)
    'date needs to be in yyyymmdd format
    'time needs to be in hhmmss 24-hour
    'date -s 20120419.024012; \n
   
    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder
    Dim Result As Int
    Dim Ph As Phone
   
    StdOut.Initialize
    StdErr.Initialize

    Runner = File.Combine(File.DirInternalCache, "runner")
    Command = File.Combine(File.DirInternalCache, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "date -s " & ToDate & "." & ToTime & "; \n" & CRLF & "exit")
    Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)

End Sub

Sub GPS_NMEA (timestamp As Long, Sentence As String)
End Sub
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
without wifi : 05/19/1976
with wifi 03/18/2016

Do you think that's possible to determine date and time without wifi, only with gps ?
 
Last edited:
Upvote 0

hub73

Active Member
Licensed User
Longtime User
Anyone know how to use '
GPS_NMEA (timestamp As Long, Sentence AsString)' sub to display the strings ? it's an event ?
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
nmea Event seems not fired as nothing appear on log window.

Despite this, i can see them into the globalsat gps application, nmea sentences are listed.

i don't understand very well were is the gps time and date infos. Inside nmea lines or directly inside other 'connected' sattelite data ?

i don't want use wifi. When wifi is active, at home, it connect to my internet box and the ntp service. And i've the time and date. I not have the wifi into my hospital (i'm caregiver). So i try to determine only time and hour from gps (Bu-353s4 sirf iv). Not sure i can do this.

So at home, i'm trying to deconnect the wifi and test. Time seems work, but date is wrong.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of code that reads the NMEA sentences: (put this code in the starter service)
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private gps As GPS
End Sub

Sub Service_Create
   gps.Initialize("gps")
   gps.Start(0, 0)
End Sub

Sub gps_NMEA (TimeStamp As Long, Sentence As String)
   Log($"$DateTime{TimeStamp}"$)
   Log(Sentence)
End Sub
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
Thanks Erel for your help. Should be a problem from my usb gps as nothing appear ! Perhaps i should try with another with bluetooth.

Could you confirm that's possible to have time and date only with gps (without wifi connection). Thanks !

Perhaps somebody here have tested this with another gps brand ?
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
I prefer buy an android box with rtc ! Finally gps not seems to be my solution. Despite this many thanks Erel for your help and all your tech support.
 
Upvote 0
Top