WearableListenerService don't call onDataChanged on Wear

595 views Asked by At

I already tryed many ways to solve this, but nothing works.

making-wearablelistenerservice-ondatachanged-call-on-every-putdatamaprequest

wearablelistenerservices-ondatachanged-not-called-on-phone

AndroidManifest.xml

    <service android:name="my.package.ListenerService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />

            <data
                android:host="*"
                android:pathPrefix="/prefix"
                android:scheme="wear" />
        </intent-filter>
    </service>

MainActivity

  IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
  MobillsWearActivity.MessageReceiver messageReceiver = new MobillsWearActivity.MessageReceiver();
  LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);

Service

public class ListenerService extends WearableListenerService {

private final String TAG_WEAR = "[on-wear]";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {


    for (DataEvent event : dataEvents) {

        // Check the data type
        if (event.getType() == DataEvent.TYPE_CHANGED) {

            DataMapItem dataItem = DataMapItem.fromDataItem (event.getDataItem());
            String jsonString = dataItem.getDataMap().getString("listDespesas");

            // Broadcast message to wearable activity for display
            Intent messageIntent = new Intent();
            messageIntent.setAction(Intent.ACTION_SEND);
            messageIntent.putExtra("listDespesas", jsonString);
            LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);

            Log.i(TAG_WEAR, "Recebido no wearable: "+jsonString);
        }

    }
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    Toast.makeText(this, "Received message", Toast.LENGTH_LONG).show();
}


}
1

There are 1 answers

0
Igor Ronner On BEST ANSWER

The answer of Tom in this link works for me

There are two things you can check:

  • packageName of the wear app should be identical to packageName of the device app.
  • onDataChanged() gets only called when the data really changes. If you put the same data into the DataApi multiple times, the method is only called once until you write different data. You could add a timestamp to the data to make sure that the method gets called once for each write operation. However, using the DataApi is potentially more expensive in terms of battery usage than sending a message. So if you just want to trigger an action after putting data into the DataApi, simply send a message when you are done.