I have been googling for 5 straight days and still can't find the solution to my problem so I desperately publish my question in the hope that somebody can help me.
I've been trying to send a DataItem to an emulator wearable through the WearableAPI. I will describe all the steps i'm following and specify the code I wrote.
Thanks in advance!
- I start the emulator.
- I open Android Wear app on my own device and pair up the emulator.
- I forward the ADB through adb -d forward tcp:5601 tcp:5601 in the platform-tools folder.
- I start the mobile app on my phone through Android Studio
- I start the wear app on the emulator through Android Studio
- I wait for something to happen but nothing happens.
Mobile Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
initComponents();
}
private void initComponents() {
...
initWearLink();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
// ---------------------------- WEARABLE PART ----------------------------
private void initWearLink() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
// Request access only to the Wearable API
.addApi(Wearable.API)
.build();
increaseCounter();
}
private static final String COUNT_KEY = "efficiencyaide.dev.pv.studea.count";
private GoogleApiClient mGoogleApiClient;
private int count = 0;
// Create a data map and put data in it
private void increaseCounter() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq.setUrgent());
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
}
Mobile Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="efficiencyaide.dev.pv.studea">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.INTERNET" />
<application
tools:replace="android:icon"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme">>
<activity
android:name=".newapp.activities.StartSplashScreen"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
Mobile gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "efficiencyaide.dev.pv.studea"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.daimajia.swipelayout:library:1.2.0@aar'
compile 'joda-time:joda-time:2.9.6'
compile 'com.github.clans:fab:1.6.4'
compile 'com.github.jivimberg:autoresizetextview:0.0.2'
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.google.android.gms:play-services-ads:10.0.1'
testCompile 'junit:junit:4.12'
}
Wearable Code:
private void initWear() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.enableAutoManage(this,this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i("Test", "Connected");
Wearable.DataApi.addListener(mGoogleApiClient, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onPause() {
super.onPause();
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
updateCount(dataMap.getInt(COUNT_KEY));
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
}
Wearable Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="efficiencyaide.dev.pv.studea">
<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=".TaskWorkWatchFace"
android:label="@string/app_name">
</activity>
<activity android:name=".TaskSuggestionWatchFace"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</activity>
</application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
(Sorry for the long post/code but i didn't want to miss anything out)
What I already know/debugged:
- The onDataChanged() function is never called
- The onConnected() function in the wearable device is never called
- If I start the app on the wearable, it says: "new version of Google Play services is needed. It will update itself shortly". After this message, the app starts.
- [EDIT]: Setting ADB debuggin enabled does not help.
I hope that somebody can help me with this information. I apologize for the amount of code.
Thanks in advance
I have had the exact same issue yesterday and couldn't find out why.
Today I started everything up again and found this link that showed me how to connect the wearable emulator to the phone. I did everything before I found this link, except for 1 thing: enable ADB debugging on the wearable.
In the end, this was causing my problems of connectivity.
To the point: Enable ADB debugging on the emulator to make it work.
To do this, go to options --> find "about" --> click a few times on "Build number" --> go back to options --> developer options --> enable ADB debugging.
Hope this helps!