Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleFit distinguish data between from different devices

I am using GoogleFit HistoryAPI for get step data from user. It's working well.
However, in my case, I want to distinguish data between from different devices (using same account) because I don't want user using same account for 2 or more devices (it is a game base on step so I don't want user play on 1 device and have achievement on many devices).

I found that Device from DataSource#getDevice, Device#getLocalDevice can help me distinguish data between from different devices. Below is info from official docs:

To get information about the device for a data source, use the DataSource.getDevice method. The device information is useful to distinguish from similar sensors on different devices, show the device information from a sensor to the user, or process data differently depending on the device. For example, you may be interested in reading data specifically from the a sensor on a wearable device but not from the same type of sensor on the phone.

To get a Device instance for the device that is running your activity, use the Device.getLocalDevice static method. This is useful when you want to check if a data source is on the same device that your app is running on.

So,

// 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());

        ... 
     }
}

However, the logcat result for datasource from only 1 DEVICE is

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)

You can see that the uid from datasource is different to local device uid, so I can not distinguish data.

Is there any way to distinguish data between from different devices? Any help or suggestion would be great appreciated.

DEMO project

like image 483
Linh Avatar asked Jun 25 '18 06:06

Linh


1 Answers

From what I get, I think you a looking ro stable ID for each Device. There are few ways of using ID, on which you can relay.

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;
}

As another eample, it's common to use IMEI, to relay on Unique ID, from Telephony Manager.

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

And last, but maybe most correct way, I suggest to use Secure ID. Which provides stable background, of using this value. From the Doc 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); 
like image 175
GensaGames Avatar answered Sep 19 '22 15:09

GensaGames