Use an existing database in Android Wear

I am trying to create a wearing app for my existing app. I already have a SQLite database in my Handheld application, now I want to try to use them in my application for wearing.

Do they have the opportunity to send the database to Wear or can I access the database on my PDA from the wearing application?

My current idea is to migrate all the elements using Wearable.DataApi , but that doesn't seem like the best solution.

For example, I do not believe that Google Keep wraps all notes separately.

Any other idea?

+6
source share
2 answers

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) { // TODO Auto-generated catch block e.printStackTrace(); } pl.put(String.valueOf(j), jsonArray); } if(jsonArray.length()<1){ pl.put(String.valueOf(j),new JSONArray()); } } JSONObject result = new JSONObject(); result.put("data",pl); return result; } } 

DemoActivity.java (Phone):

 public class DemoActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { /** Android Wear **/ 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()); // do what you want with the json-string SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor edit = pref.edit(); edit.putString("demo_json",message).apply(); } else { super.onMessageReceived(messageEvent); } } 

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.

+8
source

You probably won’t want to send the entire database to wearable. Rather, you should use the messaging protocols (WearableListenerService) available to you to communicate with a database that is already on the PDA.

Here are the docs about this: http://developer.android.com/training/wearables/data-layer/events.html .

0
source

Source: https://habr.com/ru/post/975790/


All Articles