我正在将数据从 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'
}