0

我正在将数据从 Android Wear 发送到 Android 手机


为了建立连接,我在 Wear 的 MainActivity 中使用了下面的代码,但 OnConnected() 方法从未执行,这意味着连接没有发生所以我想知道我们需要在 Wear 的 MainActivity 中编写什么代码,以便在发送数据时将 Android Wear 连接到 Android 设备从磨损到手机。

每当我运行此代码onConnectionFailed时,都会调用此代码并且未建立连接。

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());

}

我的 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>

用于 Wear 的 Build.gradle 文件

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'
}
4

2 回答 2

0

根据您需要更新 Google Play 服务的文档。

公共静态最终 int SERVICE_VERSION_UPDATE_REQUIRED

安装的 Google Play 服务版本已过期。调用活动应将此错误代码传递给 getErrorDialog(Activity, int, int) 以获取本地化错误对话框,该对话框将在显示时解决错误。

常数值:2

确保手机和手表都安装了最新版本。

于 2016-03-09T16:37:09.603 回答
0

最后我得到了非常简单的答案

以前我 compile 'com.google.android.gms:play-services-wearable:8.3.0'在我的wear build.gradle中写过,我只是用compile 替换了它'com.google.android.gms:play-services-wearable:7.3.0',我解决了所有问题............

于 2016-03-10T05:38:00.920 回答