【问题标题】:GoogleFit distinguish data between from different devicesGoogleFit 区分来自不同设备的数据
【发布时间】:2018-06-25 06:51:58
【问题描述】:

我正在使用GoogleFit HistoryAPI 从用户那里获取步骤数据。它运作良好。
但是,就我而言,我想区分来自不同设备(使用相同帐户)的数据,因为我不希望用户在 2 个或更多设备上使用相同帐户(它是基于步骤的游戏,所以我不希望用户在一台设备上玩并在多台设备上取得成就。

我发现DeviceDataSource#getDeviceDevice#getLocalDevice 可以帮助我区分来自不同设备的数据。以下是来自官方文档的信息:

要获取有关数据源设备的信息,请使用 DataSource.getDevice 方法。设备信息有助于区分不同设备上的类似传感器,向用户显示来自传感器的设备信息,或根据设备以不同方式处理数据。例如,您可能有兴趣专门从可穿戴设备上的传感器读取数据,而不是从手机上相同类型的传感器读取数据。

要获取运行活动的设备的设备实例,请使用 Device.getLocalDevice 静态方法。当您要检查数据源是否与您的应用运行在同一设备上时,这很有用。

所以,

// My idea is comparing the uid of local device and uid of device in datasource
// if it is same => step come from THIS device
// if not => step come from OTHER device
private void dumpDataSet(DataSet dataSet) {
    for (DataPoint dp : dataSet.getDataPoints()) {
        Log.i(Constant.TAG, "Origin type uid: " + dp.getOriginalDataSource().getDevice().getUid());
        Log.i(Constant.TAG, "Local device uid: " + Device.getLocalDevice(getApplicationContext()).getUid());

        ... 
     }
}

但是,只有 1 DEVICE 的数据源的 logcat 结果是

Origin type uid: 9ff67c48
Local device uid: f2b01c49ecd9c389
// Only the model, manufacturer is same (but I can not use 2 fields because user can use 2 devices with same model, manufacturer)

您可以看到datasource中的uidlocal device uid不同,所以我无法区分数据。

有什么方法可以区分来自不同设备的数据吗?任何帮助或建议将不胜感激。

DEMO project

【问题讨论】:

  • Google FIT 旨在为您提供它负责删除重复数据的所有设备的组合步数。 Surly 这比尝试自己删除重复数据更容易使用。
  • @Ifor 抱歉回复晚了。我不是要删除重复的数据。我只想distinguish data between from different devices(我想知道步骤来自设备A还是步骤来自设备B)
  • 您可以为每个设备分配一个唯一的 ID,并且在发送步骤时也只需发送 ID。

标签: android google-play-services google-fit


【解决方案1】:

据我所知,我认为每个设备的 ID 看起来都很稳定。使用 ID 的方法很少,您可以在这些方法上进行中继。

public static String getSerial() {
    final String serial;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        serial = Build.getSerial();
    } else { 
        serial = Build.SERIAL;    
    }
    return serial;
}

作为另一个示例,通常使用 IMEI 来中继来自 Telephony Manager 的唯一 ID。

public static String getDeviceImei(Context ctx) {
    TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getDeviceId();
}

最后,但也许是最正确的方式,我建议使用 Secure ID。这提供了使用此值的稳定背景。来自文档On devices that have multiple users, each user appears as a completely separate device, so the ANDROID_ID value is unique to each user.Settings.Secure#ANDROID_ID

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

【讨论】:

  • 谢谢,但这不是我要找的。抱歉,解释不好,但这个问题与GoogleFit -> orgin data source -> origin type uid 特别相关。我认为,如果您从未使用过 GoogleFit,那么真的很难理解这种情况。
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多