0

my app has been working fine until BIND_LISTENER deprecated and I have done any change needed but my app is not working. I send the data from wear successfully but nothing happen on the phone side.

My main code on the wear (the sender):

public class MainActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private TextView mTextView;
GoogleApiClient googleClient;
String WEARABLE_DATA_PATH = "/wearable_data";
ArrayList<String> dataBlock = new ArrayList();
Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });


    startButton = (Button) findViewById(R.id.startButton);
    // Build a new GoogleApiClient that includes the Wearable API
    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}
@Override
public void onConnected(Bundle connectionHint) {
    Log.v("CONNECTED", "CONNECTED");
    DataMap map = new DataMap();
    map.putLong("Time",System.currentTimeMillis());
    new SendToDataLayerThread(WEARABLE_DATA_PATH, map).start();
}

public void onStartClicked(View view) {
    Log.v("StartClicked", "in on start clicked");
    googleClient.connect();
}
@Override
public void onConnectionSuspended(int cause) {
    Log.v("Suspended", "Suspended");

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.v("Failed", "onConnectionFailed: ConnectionResult.getErrorCode() = "
            + connectionResult.getErrorCode());
}


class SendToDataLayerThread extends Thread {
    String path;
    DataMap dataMap;


    // Constructor for sending data objects to the data layer
    SendToDataLayerThread(String p, DataMap data) {
        path = p;
        dataMap = data;
    }

    public void run() {
        // Construct a DataRequest and send over the data layer
        PutDataMapRequest putDMR = PutDataMapRequest.create(path);
        putDMR.getDataMap().putAll(dataMap);
        putDMR.setUrgent();
        PutDataRequest request = putDMR.asPutDataRequest();
        DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
        if (result.getStatus().isSuccess()) {
            Log.v("myTag", "DataMap: " + dataMap + " sent successfully to data layer ");
        }
        else {
            // Log an error
            Log.v("myTag", "ERROR: failed to send DataMap to data layer");
        }
    }
}
protected void onPause() {
    super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
protected void onResume() {
    super.onResume();
}
@Override
protected void onStart() {
    super.onStart();
}
}

My wear Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.datacollector">
<uses-feature android:name="android.hardware.type.watch" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>
</manifest> 

The listener service on the phone side:

public class ListenerService extends WearableListenerService {
//private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String WEARABLE_DATA_PATH = "/wearable_data";

public void onMessageReceived(MessageEvent messageEvent) {
    Log.v("myTag", "Test");
}

public void onDataChanged(DataEventBuffer dataEvents) {
    DataMap dataMap;
    Log.v("myTag", "DataMap received from watch: ");
    for (DataEvent event : dataEvents) {

        // Check the data type
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // Check the data path
            String path = event.getDataItem().getUri().getPath();
            if (path.equals(WEARABLE_DATA_PATH)) {}
            dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
            Log.v("myTag", "DataMap received on watch: " + dataMap);
        }
    }
}
}

My phone manifest:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.name.datacollector">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".ListenerService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:scheme="wear"
                android:host="*"/>
        </intent-filter>
    </service>
</application>
</manifest>
Timmy
  • 107
  • 4
  • 18

2 Answers2

0

The same happened to me when I had to stop using the bind listener. The phone is not receiving anything because you have android:exported="false" in your phone's manifest. Either remove it or set it to true and you should be able to receive data again.

EDIT: You are also missing the path in the phone's manifest. You're using the path /wearable_data to send the message to the phone, but it will never receive it the path isn't listed in the manifest. Just add android:pathPrefix="/wearable_data" after android:host="*" in the data section of your intent-filter. You can read more about it here.

Eric Abreu
  • 171
  • 1
  • 10
  • Just saw that you forgot to add the path to the phone's manifest. The edit above might help. – Eric Abreu Jun 27 '16 at 17:11
  • Hi Eric, thanks for the comment, this did not make a difference for me. Anyway, I changed this way I'm sending the data and started from scratch following [this](https://gist.github.com/gabrielemariotti/117b05aad4db251f7534) and it is working fine now – Timmy Jul 01 '16 at 08:46
0

In order to send and sync data between the mobile and wear apps in your project THEY MUST HAVE THE SAME PACKAGE NAME, as stated in the Developer notes here,

Packaging a Wear 1.x app with Android Studio

To package a Wear 1.x app in Android Studio:

  1. Ensure that both the watch and phone app modules have the same package name.

and here

If your Wear 2.0 app has an accompanying phone app, use the same package name for your Wear app and that phone app.

Change the wear & mobile app to have the same package name:

com.example.myname.datacollector

or

com.example.name.datacollector

This stack overflow question will help you if you are unsure how to change an existing project module's package name here

Community
  • 1
  • 1
flopshot
  • 1,153
  • 1
  • 16
  • 23