I'm trying to figure out how to sync some data from the phone to the Android Wear device and I've read the article on developer.android.com on Data Items however I'm still not clear on exactly how to use them. Specifically where each code segment (the GoogleApiClient, the Sync, and the Listen) should be implemented both where in the flow of the app code and on which device, phone or wear, or both.
Link to developer.android.com page
Have you looked at the samples for API 20? There is a nice demonstration of DataApi
usage in DataLayer
sample located here:
{android-sdk-root}\samples\android-20\wearable\DataLayer
Also I've posted an example for usage of DataApi
in my answer for Android Wear Watchface Settings on host
But probably due to the title of that question, there is no simply relation with DataApi
. So maybe here is the good place to post it again - hopefully more users will find this example. Please see the code below:
Everything pushed to the DataApi
is shared between devices and available of both of them. You can change this data on both sides and the other side will be notified about such change immediately (when devices are connected to each other). You can also read saved data at any moment. Here is a sample code for implementing DataApi
in few simple steps.
public class WatchfaceConfigActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
}
@Override
public void onConnectionSuspended(int cause) {
}
})
.addOnConnectionFailedListener(new OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
}
})
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
and every time you want to sync new fconfiguration with the Android Wear device you have to put a DataRequest via Wearable DataApi
:
private void syncSampleDataItem() {
if(mGoogleApiClient==null)
return;
final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
final DataMap map = putRequest.getDataMap();
map.putInt("color", Color.RED);
map.putString("string_example", "Sample String");
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}
You need to create a class that extends WearableListenerService
:
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
for(DataEvent event : events) {
final Uri uri = event.getDataItem().getUri();
final String path = uri!=null ? uri.getPath() : null;
if("/SAMPLE".equals(path)) {
final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
// read your values from map:
int color = map.getInt("color");
String stringExample = map.getString("string_example");
}
}
}
}
and declare it in your AndroidManifest
:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With