2

我已经连续搜索了 5 天,但仍然找不到解决问题的方法,所以我拼命发布我的问题,希望有人能帮助我。

我一直在尝试通过 WearableAPI 将 DataItem 发送到可穿戴的模拟器。我将描述我正在遵循的所有步骤并指定我编写的代码。

提前致谢!

  1. 我启动模拟器。
  2. 我在自己的设备上打开 Android Wear 应用程序并配对模拟器。
  3. 我通过platform-tools文件夹中的 adb -d forward tcp:5601 tcp:5601 转发ADB。
  4. 我通过 Android Studio 在手机上启动移动应用程序
  5. 我通过 Android Studio 在模拟器上启动穿戴应用程序
  6. 我等待某事发生,但什么也没有发生。

手机代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    initComponents();
}


private void initComponents() {
    ...
    initWearLink();
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}


// ---------------------------- WEARABLE PART ----------------------------

private void initWearLink() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            // Request access only to the Wearable API
            .addApi(Wearable.API)
            .build();
    increaseCounter();
}


private static final String COUNT_KEY = "efficiencyaide.dev.pv.studea.count";

private GoogleApiClient mGoogleApiClient;
private int count = 0;


// Create a data map and put data in it
private void increaseCounter() {
    PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
    putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult =
            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq.setUrgent());
}


@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
    public void onDataChanged(DataEventBuffer dataEventBuffer) {

}

移动清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="efficiencyaide.dev.pv.studea">
<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    tools:replace="android:icon"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">>


    <activity
        android:name=".newapp.activities.StartSplashScreen"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

</application>

</manifest>

移动分级:

apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
    applicationId "efficiencyaide.dev.pv.studea"
    minSdkVersion 22
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

repositories {
maven {
    url "https://jitpack.io"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
wearApp project(':wear')

compile 'com.android.support:appcompat-v7:24.2.1'

compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.daimajia.swipelayout:library:1.2.0@aar'
compile 'joda-time:joda-time:2.9.6'
compile 'com.github.clans:fab:1.6.4'
compile 'com.github.jivimberg:autoresizetextview:0.0.2'
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.google.android.gms:play-services-ads:10.0.1'
testCompile 'junit:junit:4.12'

}


可穿戴代码:

 private void initWear() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .enableAutoManage(this,this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}


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

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
protected void onResume() {
    super.onResume();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Log.i("Test", "Connected");
    Wearable.DataApi.addListener(mGoogleApiClient, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
protected void onPause() {
    super.onPause();
    Wearable.DataApi.removeListener(mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // DataItem changed
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo("/count") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                updateCount(dataMap.getInt(COUNT_KEY));
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
        }
    }
}

可穿戴清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="efficiencyaide.dev.pv.studea">

<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">
    <activity
        android:name=".TaskWorkWatchFace"
        android:label="@string/app_name">
    </activity>
    <activity android:name=".TaskSuggestionWatchFace"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </activity>
</application>

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

</manifest>

(对不起,很长的帖子/代码,但我不想错过任何东西)

我已经知道/调试的内容:

  1. 永远不会调用 onDataChanged() 函数
  2. 可穿戴设备中的 onConnected() 函数永远不会被调用
  3. 如果我在可穿戴设备上启动应用程序,它会说:“需要新版本的 Google Play 服务。它会很快自行更新”。收到此消息后,应用程序将启动。
  4. [编辑]:设置 ADB 调试启用没有帮助。

我希望有人可以帮助我提供这些信息。我为代码量道歉。

提前致谢

4

1 回答 1

0

我昨天遇到了完全相同的问题,但找不到原因。

今天我重新开始一切,发现这个链接向我展示了如何将可穿戴模拟器连接到手机。在找到此链接之前,我做了所有事情,除了一件事:在可穿戴设备上启用 ADB 调试。

最后,这导致了我的连接问题。

切入点:在模拟器上启用ADB调试以使其工作。

为此,请转到选项-> 查找“关于”-> 在“内部版本号”上单击几次-> 返回选项-> 开发人员选项-> 启用 ADB 调试。

希望这可以帮助!

于 2016-12-22T10:20:00.317 回答