GoogleFit可以区分不同设备之间的数据

Pha*_*inh 9 android google-play-services google-fit

我正在使用GoogleFit HistoryAPI来获取用户的步骤数据.它运作良好.
但是,在我的情况下,我想区分不同设备之间的数据(使用相同的帐户),因为我不希望用户为2个或更多设备使用相同的帐户(这是一个基于步骤的游戏,所以我不想要用户在1台设备上玩,并在许多设备上取得成就).

我发现Device from DataSource#getDevice,Device#getLocalDevice可以帮助我区分不同设备之间的数据.以下是官方文档的信息:

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

要为运行活动的设备获取Device实例,请使用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());

        ... 
     }
}
Run Code Online (Sandbox Code Playgroud)

但是,仅来自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)
Run Code Online (Sandbox Code Playgroud)

你可以看到uidfrom datasource是不同的local device uid,所以我无法区分数据.

有没有办法区分不同设备之间的数据?任何帮助或建议将非常感谢.

DEMO项目

Gen*_*mes 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;
}
Run Code Online (Sandbox Code Playgroud)

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

public static String getDeviceImei(Context ctx) {
    TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getDeviceId();
}
Run Code Online (Sandbox Code Playgroud)

最后,但也许是最正确的方法,我建议使用安全 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); 
Run Code Online (Sandbox Code Playgroud)