我正在开发一款具有磨损面和手持面的应用程序。该应用程序无需手持设备即可工作,并同步双方连接时获取的数据。现在我在掌上电脑上有一个服务,正在听googleclientAPI,在磨损方面,我有一项服务,可以获取GPS数据并将每个坐标放在googleclientAPI上。当我运行磨损面(没有手持连接)时,应用程序会根据需要将所有数据添加到googleclientAPI。
当我连接双方(通过蓝牙)时,手持终端只接收添加到googleClientAPI的最后一个数据地图......
磨损方:
private void addLocationEntry(double latitude, double longitude, float accuracy, long gpsTime, float speed, double altitude, long time){
String PATH = "/TuneWear/GPSdata";
if(!googleClient.isConnected()){
return;
}
String path = PATH + "/" + time;
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
putDataMapRequest.getDataMap().putLong(KEY_GPSTIME, gpsTime);
putDataMapRequest.getDataMap().putLong(KEY_TIME, time);
putDataMapRequest.getDataMap().putDouble(KEY_LATITUDE, latitude);
putDataMapRequest.getDataMap().putDouble(KEY_LONGITUDE, longitude);
putDataMapRequest.getDataMap().putFloat(KEY_SPEED, speed);
putDataMapRequest.getDataMap().putFloat(KEY_DISTANCE, distance);
putDataMapRequest.getDataMap().putFloat(KEY_ACCURACY, accuracy);
putDataMapRequest.getDataMap().putDouble(KEY_ALTITUDE, altitude);
PutDataRequest request = putDataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(googleClient, request);
mCalendar.setTimeInMillis(System.currentTimeMillis());
}
掌上电脑:
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
for (DataEvent event : dataEventBuffer) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
//Log.d(TAG, "DataItem RECEIVED: " + item.getUri().getPath());
if (item.getUri().getPath().contains(PATH + "/")) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
Log.d(TAG, "DataItem " + item.getUri().getPath() + " changed: GPSTimestamp " + dataMap.getLong(KEY_GPSTIME) + " Time: " + dataMap.getLong(KEY_TIME) +
" LAT " + dataMap.getDouble(KEY_LATITUDE) + " LONG " + dataMap.getDouble(KEY_LONGITUDE) + " Speed: " + dataMap.getFloat(KEY_SPEED) +
" Distance: " + dataMap.getFloat(KEY_DISTANCE) + " Accuracy: " + dataMap.getFloat(KEY_ACCURACY) + " Altitude: " + dataMap.getDouble(KEY_ALTITUDE));
String headerString = "GPSTimestamp\tPhoneTimestamp\tLatitude\tLongitude\tSpeed\tDistance\tAccuracy\tAltitude\n";
String dataPath = Environment.getExternalStorageDirectory().toString() + "/TuneWear/";
String dataString = dataMap.getLong(KEY_GPSTIME) + "\t" + dataMap.getLong(KEY_TIME) + "\t" + dataMap.getDouble(KEY_LATITUDE) + "\t" +
dataMap.getDouble(KEY_LONGITUDE) + "\t" + dataMap.getFloat(KEY_SPEED) + "\t" + dataMap.getFloat(KEY_DISTANCE) + "\t" + dataMap.getFloat(KEY_ACCURACY) +
"\t" + dataMap.getDouble(KEY_ALTITUDE) + "\n";
File myDir = new File(dataPath + PATH);
myDir.mkdirs();
File file = new File(myDir, "data.txt");
Log.i("FILE", "FILE: " + file.getPath());
try {
FileOutputStream out = new FileOutputStream(file, true);
if(file.length() == 0){
out.write(headerString.getBytes());
}
out.write(dataString.getBytes());
out.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
Log.i("FILE", "FILE Changed!!! ");
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
}
//Wearable.DataApi.deleteDataItems(mGoogleApiClient, event.getDataItem().getUri(), DataApi.FILTER_PREFIX);
}
}
如何使所有数据同步?
答案 0 :(得分:1)
如果您使用相同的"时间",则表示您的数据项都具有相同的确切路径,然后每次添加都会更新旧数据,因此您最终只能使用一条记录(最后一个)。如果您希望所有内容都可以单独使用,请确保它们的路径是唯一的(例如,在路径中使用时间戳)。