0

I am sending data from Android wear to Android phone


To make connection i have used below code inside Wear's MainActivity but OnConnected() method is never executing which means Connection is not happening So i want to know what code exactly we need to write inside Wear's MainActivity to connect Android wear to Android device while sending data from Wear to phone.

Whenever i run this code onConnectionFailed gets called and connection is not establishing.

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

GoogleApiClient googleApiClient_to_mob;
private TextView mTextView;
private EditText e1;
private Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

googleApiClient_to_mob = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    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);
            send = (Button) findViewById(R.id.send_m);


        }
    });

}

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

@Override
public void onConnected(Bundle bundle) {
    Log.i("connection_info", "Connection to Google API client connected");

}

@Override
public void onConnectionSuspended(int i) {
    Log.i("connection_info", "Connection to Google API client was suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i("connection_info", "FAILED TO CONNECT" + connectionResult.getErrorCode());

}

My Manifest file for Wear

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radhe.wear_sample">
    <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">
        <uses-library
            android:name="com.google.android.wearable"
            android:required="false" />
        <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>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
      </application>
</manifest>

My Build.gradle file for Wear

apply plugin: 'com.android.application'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.radhe.wear_sample"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:1.3.0'
    compile 'com.google.android.gms:play-services-wearable:8.3.0'
}
Hiren Shah
  • 37
  • 11

2 Answers2

0

According to the documentation you need to update Google Play Services.

public static final int SERVICE_VERSION_UPDATE_REQUIRED

The installed version of Google Play services is out of date. The calling activity should pass this error code to getErrorDialog(Activity, int, int) to get a localized error dialog that will resolve the error when shown.

Constant Value: 2

Make sure that both the phone and watch has the latest version installed.

Community
  • 1
  • 1
TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • Can i have some more clarification about where and what i need to change in my code ?? @TofferJ – Hiren Shah Mar 10 '16 at 04:09
  • When i tried to update those services by confirming the Dialog box it will show me error " This won't work". So i just want to know that how to update play services in Emulator because i am working with emulator for watch @TofferJ – Hiren Shah Mar 10 '16 at 04:46
0

Finally i got my answer which is very simple

Previously I have written compile 'com.google.android.gms:play-services-wearable:8.3.0' in my wear build.gradle that i simply replaced with compile 'com.google.android.gms:play-services-wearable:7.3.0' and i got all the problem solved.........

Hiren Shah
  • 37
  • 11