Android Wear:如何连接和发送数据

Android Wear: How to connect and send data

我尝试了不同的 Android Wear 教程和文档,但总是失败。一点一点地,我在这里迈出了第一步。我想要的(最后)是从我的移动设备发送一个字符串 "hello world" 到 android wear (Moto 360)。到目前为止我在 Android Studio 中做了什么:

移动应用: 添加到清单

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

在我的移动项目的onCreate中:

private GoogleApiClient mGoogleApiClient;
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
        mGoogleApiClient.connect();

当然我会实现

public class HandheldMain extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

在我的 class

@Override
public void onConnected(Bundle bundle)
{
    Log.v("Test", "on Connected");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
    Log.v("Test", "on Connection failed" + connectionResult.toString());
}

  @Override
    public void onConnectionSuspended(int i)
    {
        Log.v("Test", "on suspended");
    }

问题: 我的回调永远不会被调用。当我的手表连接到我的 phone 时,他们应该被呼叫吗?或者我的手表需要某种代码吗?

当然,只要我的手表上没有密码,我就无法发送消息,但我仍然很难连接。我尝试了几个教程,但它们似乎不起作用。例如。 https://www.binpress.com/tutorial/a-guide-to-the-android-wear-message-api/152

编辑: 我发现了一个阻塞连接命令

ConnectionResult a =  mGoogleApiClient.blockingConnect(2000L, TimeUnit.MILLISECONDS );
                Log.v("Test", "on Create" + a.getErrorCode());

当我 运行 在一个线程上它 returns “0” 所以这似乎有效 - 这让我更加困惑。

您是否尝试过 Teleport 简单的数据同步和消息传递库?

尝试像这样向您的 GoogleApiClient 添加 connectionCallbacks

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Wearable.API)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this) // this also can be usefull
    .build();