0

I'm learning data communication in android wear. My understanding is that both mobile and wear apps need to connect to DataItem via Google Services API in order to read data from the one or the other.

I have data saved in sharedpreference in the mobile app. Only when I open my wear app, I want to read the data from sharedpreference in the mobile app to display on the wear.

Would it be like, whenever the mobile app updates this data in sharedpreference, have that activity connected to Google Services API and put a request in DataItem. Then the wear app would be listening to changes by WearableListenerService?

I prefer not to have service running the entire time at least not on the mobile side. What would be an approach to accomplish this?

awonderer
  • 665
  • 9
  • 26

2 Answers2

0

That would be the approach to take but to save you the hassle implementing it there's already a library that does this.

WearSharedPreferences

CodeChimp
  • 4,745
  • 6
  • 45
  • 61
  • I am looking to learn instead of using a third party tool. If I only need to update the wear app when opening the activity, wouldn't WearableListenerService be an overkill? – awonderer May 03 '15 at 15:55
0

for data transfer you can use the library Emmet

https://github.com/florent37/emmet

We can imagine a protocol like this

public interface SmartphoneProtocole{
    void getStringPreference(String key);
    void getBooleanPreference(String key);
}

public interface WearProtocole{
    void onStringPreference(String key, String value);
    void onBooleanPreference(String key, boolean value);
}

wear/WearActivity.java

//access "MY_STRING" sharedpreference
SmartphoneProtocole smartphoneProtocol = emmet.createSender(SmartphoneProtocole.class);
emmet.createReceiver(WearProtocole.class, new WearProtocole(){

    @Override
    void onStringPreference(String key, String value){
        //use your received preference value
    }

    @Override
    void onBooleanPreference(String key, boolean value){

    }

});

smartphoneProtocol.getStringPreference("MY_STRING"); //request the "MY_STRING" sharedpreference

mobile/WearService.java

final WearProtocole wearProtocol = emmet.createSender(WearProtocole.class);
emmet.createReceiver(SmartphoneProtocol.class, new SmartphoneProtocol(){

    //on received from wear
    @Override
    void getStringPreference(String key){
        String value = //read the value from sharedpreferences

        wearProtocol.onStringPreference(key,value); //send to wear
    }

    @Override
    void getBooleanPreference(String key){

    }

});    
  • 1
    I'm interested in learning rather than using a abstracted tool. I will take a look at the code in this library. What advantage does this library bring to you? – awonderer May 03 '15 at 15:53
  • it's faster to use, but yes it just enclose a google message api, you can do it manually – florent champigny May 03 '15 at 16:48