Reputation: 504
I'm sending a Message from my Wear to the Service from my Handheld. The first time I'm clicking a button on my handheld is just calling the onCreate method of the Service, the second time the service receives also the message. I can't figure out the problem.
Here is my Wear part:
@Override
public void onClick(View v)
{
final PendingResult<NodeApi.GetConnectedNodesResult> nodes1 = Wearable.NodeApi.getConnectedNodes(mApiClient);
nodes1.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>()
{
@Override
public void onResult(NodeApi.GetConnectedNodesResult result)
{
for (int i = 0; i < result.getNodes().size(); i++)
{
Node node = result.getNodes().get(i);
PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mApiClient, node.getId(), "/OPEN;"
+ cnt, null);
messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>()
{
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult)
{
Status status = sendMessageResult.getStatus();
Log.d("", "##Status: " + status.toString());
if (status.getStatusCode() != WearableStatusCodes.SUCCESS)
{
//not getting there, always success
}
}
});
}
}
});
}
I have following WearableListenerService:
public class DataLayerListenerService extends WearableListenerService implements ConnectionCallbacks, OnConnectionFailedListener
{
private GoogleApiClient mApiClient;
private SharedPreferences prefs;
@Override
public void onCreate()
{
Log.d("", "##DataService created");
super.onCreate();
if (null == mApiClient)
{
mApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.build();
}
if (!mApiClient.isConnected())
{
mApiClient.connect();
Log.d("", "##Api connecting");
}
}
@Override
public void onDestroy()
{
Log.d("", "##DataService destroyed");
if (null != mApiClient)
{
if (mApiClient.isConnected())
{
mApiClient.disconnect();
}
}
super.onDestroy();
}
@Override
public void onMessageReceived(MessageEvent messageEvent)
{
Log.d("", "##DataService received " + messageEvent.getPath());
super.onMessageReceived(messageEvent);
if (messageEvent.getPath().contains("/OPEN;"))
{
// only if called twice :(
}
}
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
Log.d("", "##DataService Data changed");
}
@Override
public void onConnectionFailed(ConnectionResult arg0)
{
}
@Override
public void onConnected(Bundle arg0)
{
}
@Override
public void onConnectionSuspended(int arg0)
{
}
}
Maybe I also should mention that I ony have to click the button twice when the Service gets destroyed before, which happens some seconds after the click event. So..in fact this is what happens:
wear click
service oncreate
wear click
service onMessageReceived
wear click
service onMessageReceived
//wait some seconds
service ondestroy
wear click
service oncreate
wear click
service onMessageReceived
Hope this is clear :-)
Thanks in advance!!
Upvotes: 1
Views: 1873
Reputation: 142
I've been messing with this some more, and if you're still running into the bug of the listener service not firing on the first try, I've found that increasing the timeout interval helps. I had mine set to 10 milliseconds, but making it a larger value seems to have helped.
Check out the code in WearableMainActivity.java in this gist I created for a project. Good luck! :)
Upvotes: 0
Reputation: 142
I think I may have solved it Phillip Adam - try commenting-out the line in your service in the onMessageReceived() method that makes the call to the superclass. That holds for me. :)
Upvotes: 1