Android Question Gps Current Time

khwarizmi

Active Member
Licensed User
Longtime User
Hi all
When I use GPS code to get the current location with time. Is the given time taken from the Internet, or the device’s time, or the GPS itself?
B4X:
Sub GPS_LocationChanged (Location1 As Location)
    Dim currentDateTime As Long
    currentDateTime = Location1.Time
    Log(DateTime.Time(currentDateTime))
End Sub
 
Solution
When I use GPS code to get the current location with time. Is the given time taken from the Internet, or the device’s time, or the GPS itself?

Bearing in mind that I used the GNSS library cf your code sample looks like it refers to the deprecated GPS library...

I found that Location.Time is NOT from the device, and thus presumably is from the GNSS system (but: which one? my phone reports 8 GPS and 7 Glonass satellites).

I tested this by disabling the automatic-time-update options and setting the phone time back a few hours so that it is obviously different to both local time and UTC time.

Bonus confirmation of the change is that when I ran the GNSS test program after changing the phone time, it took longer than usual...

khwarizmi

Active Member
Licensed User
Longtime User
I found that time is taken from the device’s time.
In fact, I want to check the real time without using the device time, which the user may change, and without the Internet because the network is weak for the target users, and without the network time for the same reason. is there a solution?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
any time/date derived from a source outside the device
will be subject to network vagaries. unless you maintain a
small atomic clock in your basement, there is no way for
the device to know the real time on its own.

location.time refers to a date when that location was registered.
it's not a substitute (necessarily) for getting the date/time of day.
and according to android's documentation, the time for the
location may be based on satellites' times, and not necessarily
the device's time.

the os purports to provide the so-called currentGnssTimeClock()
"Returns a Clock that starts at January 1, 1970 00:00:00.0 UTC,
synchronized using the device's location provider."

check android's dox; perhaps you might want to look into that method.
 
Upvote 0

emexes

Expert
Licensed User
When I use GPS code to get the current location with time. Is the given time taken from the Internet, or the device’s time, or the GPS itself?

Bearing in mind that I used the GNSS library cf your code sample looks like it refers to the deprecated GPS library...

I found that Location.Time is NOT from the device, and thus presumably is from the GNSS system (but: which one? my phone reports 8 GPS and 7 Glonass satellites).

I tested this by disabling the automatic-time-update options and setting the phone time back a few hours so that it is obviously different to both local time and UTC time.

Bonus confirmation of the change is that when I ran the GNSS test program after changing the phone time, it took longer than usual to find the satellites.

I'll have a bit more of a play, eg compare it to UTC, see if the GNSS times really are precisely on-the-second ie millisecond component of Location.Time is always .000
 
Last edited:
Upvote 1
Solution

emexes

Expert
Licensed User
GpsStatus class was deprecated in API level 24 and Google is now enforcing the deprecation.
The GNSS library is an updated GPS library that allows you to get information about visible satellites from the phone's GNSS device using an enhancement introduced in Android API version 24.
This library is a strict superset of the original GPS library and can be used as a drop-in replacement for the GPS library merely by deselecting the GPS library, selecting the GNSS library and changing the GPS type to GNSS in the code.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Just for your interest....

When you close a GPS system down it commits the location and time to memory. When you turn it back on again, it uses this information and the system time, to figure out which satellites it should be in view.

If you move the GPS receiver to a different location while it is switched off, it can't do this. The system then reverts to first principles and scans the sky for what satellites it can see. This process takes three to five times as long as a normal power up.

As the GPS receiver is probably the greatest current drain on the device battery, the system will switch it off at any opportunity.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
Just for your interest....

When you close a GPS system down it commits the location and time to memory. When you turn it back on again, it uses this information and the system time, to figure out which satellites it should be in view.

If you move the GPS receiver to a different location while it is switched off, it can't do this. The system then reverts to first principles and scans the sky for what satellites it can see. This process takes three to five times as long as a normal power up.

As the GPS receiver is probably the greatest current drain on the device battery, the system will switch it off at any opportunity.
you are right
Initially, I turned off the GPS and reopened it, and it gave the same result, but I confirmed the result when I removed the battery and restarted the device.
In the case of the GNSS library, it gave a correct value for the time even though the device’s time was not set and the Internet connection was closed.
 
Upvote 0
Top