我已经连续搜索了 5 天,但仍然找不到解决问题的方法,所以我拼命发布我的问题,希望有人能帮助我。
我一直在尝试通过 WearableAPI 将 DataItem 发送到可穿戴的模拟器。我将描述我正在遵循的所有步骤并指定我编写的代码。
提前致谢!
- 我启动模拟器。
- 我在自己的设备上打开 Android Wear 应用程序并配对模拟器。
- 我通过platform-tools文件夹中的 adb -d forward tcp:5601 tcp:5601 转发ADB。
- 我通过 Android Studio 在手机上启动移动应用程序
- 我通过 Android Studio 在模拟器上启动穿戴应用程序
- 我等待某事发生,但什么也没有发生。
手机代码:
@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>
(对不起,很长的帖子/代码,但我不想错过任何东西)
我已经知道/调试的内容:
- 永远不会调用 onDataChanged() 函数
- 可穿戴设备中的 onConnected() 函数永远不会被调用
- 如果我在可穿戴设备上启动应用程序,它会说:“需要新版本的 Google Play 服务。它会很快自行更新”。收到此消息后,应用程序将启动。
- [编辑]:设置 ADB 调试启用没有帮助。
我希望有人可以帮助我提供这些信息。我为代码量道歉。
提前致谢