通过 GPS 时间同步 Android 设备? [英] Sync Android devices via GPS time?

查看:37
本文介绍了通过 GPS 时间同步 Android 设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:

问题:我需要多个 Android 设备同时执行某些操作(在大约 100 毫秒内).例如,我希望所有设备在预先确定的时间点播放某种声音.

Problem: I need several Android devices to do something at the exact same time (within ca 100 milliseconds). For example, I want all devices to play a certain sound at previously determined point in time.

问题:我可以使用 GPS 时间来确保设备时钟同步吗?

Question: Could I use GPS time to make sure the devices clock's are synced?

更长的版本:

几个拥有 Android 设备的人都在同一个室外位置(针对某些特定的体育赛事).所有设备都应该在完全相同的时间发出某些事件的信号.(什么时间会事先确定,他们在那个时间做并不重要,只要他们同时做就可以了).FWIW:这些活动将帮助人们同时开始他们的体育活动.

Several people having Android devices are in the same outdoor location (for some specific sports event). All devices should signal certain events at the exact same time. (What time will be determined before hand and it is not important that they do it at exactly that time, as long as they do it at the same time). FWIW: those events will help the people to start their sporting activity at the same time.

以下是我可以做出的假设:

Here are assumptions I can make:

  • 可能有也可能没有小区覆盖.(在室外,可能离市区很远)
  • 设备可能会也可能不会连接到互联网.(有些人可能没有数据计划)
  • 设备可以相互通信.如果设备未连接到互联网,它们将通过 wifi 连接(尽管 wifi 可能没有互联网网关).
  • 设备位于外部(而不是建筑物内部).(很好,这意味着所有设备都可以获得 GPS 定位)
  • 这些设备在物理上彼此靠近(即在 300 平方米之内).(不确定这是否重要)

我无法使用内部时钟知道何时播放事件:用户可以手动更改时间,即使设备设置为从蜂窝网络获取时间,该网络也可能无法提供准确的时间.两台设备可能被预订到不同的网络,因此它们的时间可能不会很好地同步.

I cannot use the internal clock to know when to play events: users can manually change the time and even if a device is set to get the time from a cellular network, that network might not provide the exact right time. Two devices might be booked into different networks and hence their time might not be synced very well.

在应用启动时,每台设备都可以要求 GPS 定位并保存 GPS 时间与其内部时钟之间的差异.现在主机可以根据这个 GPS 时间宣布事件(通过使用它的内部时钟并减去它之前保存的偏移量).

At app startup, each device could ask for a GPS fix and save the difference between the GPS time and its internal clock. Now the master could announce events based on this GPS time (by using it's internal clock and substracting the offset it saved earlier).

这是否足够精确和可靠?

Will this be precise enough and reliable?

非常感谢,安德烈亚斯·莱特纳

Many thanks in advance, Andreas Leitner

推荐答案

您可以从 currentTimeMillis()Location.getTime()onLocationChanged() 回调.使用 requestSingleUpdate()

You can get the time difference in milliseconds from currentTimeMillis() and Location.getTime() in the onLocationChanged() callback. Use requestSingleUpdate()

我只想补充一点,如果用户有数据连接,他们可以使用更准确的 NTP 时间,因为 GPS 内部时钟可能会漂移并且需要一段时间进行校正.

I just want to add that, if the user has a data connection they can use NTP time, which is even more accurate, as the GPS internal clock might drift and correcting it takes a while.

我想编辑这个答案.在 Android 操作系统中担任 GNSS 开发人员后,我意识到许多主要"Android OEM 都搞砸了.通常 NMEA 语句是在芯片组或 GNSS 堆栈中生成的.有时时机对了,这取决于实施.

I want to edit this answer. After working as a GNSS developer in Android OS I realized that many 'major' Android OEMs have messed this up. Usually NMEA sentences are generated at the chipset or GNSS stack. Sometimes the time is right, it depends on the implementation.

还有一个闰秒的问题.GPS 时间不是 UTC 时间.并且在此 写它是提前 18 秒.

Also there is a problem of leap second. GPS time is not UTC time. And as of this writing it is 18 seconds ahead.

Location 对象的 GPS 时间现在是系统时间,根据文档

Also GPS time fromLocation objects are now system time as per the documentation

TLDR:使用 NTP 时间

这篇关于通过 GPS 时间同步 Android 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆