onConnected() 在我将数据从 Android 穿戴发送到我的 Android 手机时没有调用



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


为了建立连接,我在 Wear 的主活动中使用了以下代码,但 OnConnected() 方法从未执行,这意味着连接没有发生所以我想知道我们需要在Wear的MainActivity中编写什么代码,以便在将数据从Wear发送到手机的同时将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());
}

我的磨损清单文件

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

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

公共静态最终 int SERVICE_VERSION_UPDATE_REQUIRED

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

常量值:2

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

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

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

相关内容

  • 没有找到相关文章

最新更新