Android DataItem不与模拟器可穿戴设备

时间:2016-12-21 02:35:45

标签: android android-emulator data-synchronization android-wear-data-api dataitem

我连续5天谷歌搜索仍然无法找到问题的解决方案所以我拼命发布我的问题,希望有人可以帮助我。

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

提前致谢!

  1. 我启动模拟器。
  2. 我在自己的设备上打开Android Wear应用并配对模拟器。
  3. 我通过adb -d forward tcp:5601 tcp:5601在platform-tools文件夹中转发ADB。
  4. 我通过Android Studio在手机上启动移动应用
  5. 我通过Android Studio
  6. 在模拟器上启动了磨损应用
  7. 我等待事情发生但没有任何反应。
  8. 移动代码:

    @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) {
    
    }
    

    Mobile Manifest:

    <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. 如果我在可穿戴设备上启动该应用,它会说:&#34; 需要新版本的Google Play服务。它将很快更新&#34;。在此消息之后,应用程序启动。
    4. [编辑]:启用设置ADB调试器无效。
    5. 我希望有人可以帮我提供这些信息。我为代码量道歉。

      提前致谢

1 个答案:

答案 0 :(得分:0)

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

今天我重新启动了一切,找到了this链接,告诉我如何将可穿戴模拟器连接到手机。在找到此链接之前,我做了所有事情,除了一件事:在可穿戴设备上启用ADB调试。

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

要点:在仿真器上启用ADB调试以使其正常工作。

要执行此操作,请转到选项 - &gt;找到&#34;关于&#34; - &GT;点击几下&#34; Build number&#34; - &GT;回到选项 - &gt;开发者选项 - &gt;启用ADB调试。

希望这有帮助!