2

I created class "CommunicationService" it extends WearableListenerService. I modify my manifest and added service with this action:

android:name="com.google.android.gms.wearable.BIND_LISTENER"

I overrided onPeerDisconnected and onPeerConnected method like this:

@Override
public void onPeerDisconnected(Node peer) {
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean(ON_PEER_CONNECT, false);
    editor.commit();
    super.onPeerDisconnected(peer);
}

@Override
public void onPeerConnected(Node peer) {
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean(ON_PEER_CONNECT, true);
    editor.commit();
    super.onPeerConnected(peer);
}

I store true on connect and false on disconnect to my sharedpreferences.

In other classes I call methods from this service like this.

if (prefs.getBoolean(CommunicationService.ON_PEER_CONNECT, false))
                    CommunicationService.sendDataToWearable();
                else
                    Toast.makeText(context, "There is no connection to wearable. Please, try reconnecting!", Toast.LENGTH_SHORT).show();

So the problem is that when I am running my app and I am connected to the watch everything is working, when I manualy disable bluetooth and try again everything is working like it should, so Toast is displayed warning about connection error.

But when I walk away from the watch and come back to it (I lost connection and then I got it back when I am close enough) it's displays Toast warrning about connection error, but in Android Wear app it says that wearable is connected to handheld.

So I guess something is wrong with my service and it does not update my sharedpreferences the right way.

Also I don't get what should I do onCreate method in service. Should I check if there are any nodes connected with this:

List<Node> connectedNodes =
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes();

And then just update my sharedpreferences?

TomazStoiljkovic
  • 808
  • 8
  • 21
  • 1
    super calls should always be the first thing you do in a method you're overriding. That might be it? – G_V Nov 20 '14 at 13:23
  • Thanks I didn't see that :), it's kinda working, but still I get some wierd behavior. Still can't figure out know if there is connection in onCreate method. Because .getNodes() return true if called in onCreate. – TomazStoiljkovic Nov 20 '14 at 20:11

0 Answers0