Reputation: 71
I am building a wear os app as part of a react native project. The handheld application should send some data among it's wearable-nodes whenever it is changed. I am using DataClient and DataItems to do this. On the wearable side I have implemented DataClient.OnDataChangedListener, this is never called though.
This is the method that gets called when the data is changed. I get the onSucces message so I assume this works:
@ReactMethod
public void updateData(String name, Integer age, String description){
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/data");
Long tsLong = System.currentTimeMillis() / 1000;
String timestamp = tsLong.toString();
putDataMapReq.getDataMap().putString("Timestamp", timestamp);
putDataMapReq.getDataMap().putString("Name", name);
putDataMapReq.getDataMap().putInt("Age", age);
putDataMapReq.getDataMap().putString("Description", description);
PutDataRequest dataRequest = putDataMapReq.asPutDataRequest();
dataRequest.setUrgent();
Task<DataItem> putDataTask = Wearable.getDataClient(reactContext).putDataItem(dataRequest);
putDataTask.addOnSuccessListener(new OnSuccessListener<DataItem>() {
@Override
public void onSuccess(DataItem dataItem) {
System.out.println("Great Succes!");
}
});
}
This is the onDataChanged method of the DataClient on the wearable side.
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
System.out.println("DATA CHANGED");
for (DataEvent event : dataEventBuffer) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/data") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
System.out.println(dataMap.getString("Name"));
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
}
These are the dependencies in the gradle files for the handheld:
dependencies {
implementation project(':react-native-background-timer')
implementation project(':react-native-geolocation-service')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+"
implementation 'com.google.android.gms:play-services-wearable:17.0.0'
if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
wearApp project(':watch')
}
And the dependencies in the gradle files for the wearable:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.wear:wear:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
implementation 'com.google.android.gms:play-services-wearable:17.0.0'
compileOnly 'com.google.android.wearable:wearable:2.7.0'
implementation 'com.google.android.support:wearable:2.7.0'
}
So again, the onDataChanged is never called and I can't find the problem. I've tried several things.
Since Google changed a lot on it's DataLayer API a lot of information about this has become irrelevant. Thanks!
Upvotes: 0
Views: 1021
Reputation: 71
Found it! The problem was that the watch application was not signed with the same key as the handheld app. After I did this, it worked instantly!
Upvotes: 1