I found a quick solution to transfer the entire database from the phone to the Smartwatch.
First, I create a helper class that converts the contents of my database into a json string, which can be sent to smartwatch using Wearable.DataApi :
DatabaseToJSON.java:
public class DatabaseToJSON { DatabaseHandler dbhandler; public DatabaseToJSON(Context context) { dbhandler = new DatabaseHandler(context); } public JSONObject getJSON() throws JSONException{ Item[] item = null; JSONObject pl = new JSONObject(); item = dbhandler.getItems(); dbhandler.close(); JSONArray jsonArray = new JSONArray(); for(int i=0;i<item.length;i++){ JSONObject val = new JSONObject(); try { val.put("id", item[i].getID()); val.put("name", item[i].getName()); ... jsonArray.put(val); } catch (JSONException e) {
DemoActivity.java (Phone):
public class DemoActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { GoogleApiClient googleClient; @Override public void onStart(){ super.onStart(); googleClient.connect(); } @Override public void onStop(){ if (null != googleClient && googleClient.isConnected()) { googleClient.disconnect(); } super.onStop(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); googleClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); ... } @Override public void onConnected(Bundle bundle) { DatabaseToJSON dbJson = new DatabaseToJSON(DemoActivity.this); try { JSONObject json = dbJson.getJSON(); new SendToDataLayerThread("/path", json.toString()).start(); } catch (JSONException e) { e.printStackTrace(); } } class SendToDataLayerThread extends Thread { String path; String message; SendToDataLayerThread(String p, String msg) { path = p; message = msg; } public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await(); for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await(); if (result.getStatus().isSuccess()) { Log.v("myTag", "Message: {" + message + "} sent to: " + node.getDisplayName()); } else { Log.v("myTag", "ERROR: failed to send Message"); } } } } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } }
DataLayerListenerService.java (wear)
public class DataLayerListenerService extends WearableListenerService { @Override public void onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals("/path")) { final String message = new String(messageEvent.getData());
Add to AndroidManifest.xml (wear)
<service android:name=".DataLayerListenerService" > <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> </intent-filter> </service>
After receiving the json-string of your wearing, you can save them to the database on your wear or do something else with it ...
I think this is the easiest way to transfer such data between a handheld device and a wear device.