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?