2014-11-21 70 views
1

我真的可以使用一些幫助。我無法讓我的Android Wear設備識別由我的Android平板電腦上運行的活動所做的數據更改。我對Android比較陌生,對Wear API非常陌生,所以我可能會錯過一些完全明顯或微不足道的東西而不會意識到。我已經從Android Wear文檔頁面和來自Github上的TwoToasters示例的幾個示例中拼湊了一個項目。我只想在設備之間建立數據通信,以便我可以編輯代碼,以便在Wear設備上顯示來自我的平板電腦的圖像。最終,我希望能夠通過平板電腦在我的Wear設備上啓動/停止幻燈片放映,但是一旦我設置好數據通信協議並開始工作,我就能夠自己到達那裏,因此我只是在尋找幫助。這是我到目前爲止的代碼:試圖從Android平板電腦發送數據到Android Wear設備並丟失

手機:

package com.example.administrator.moto360displaycontrol; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.PendingResult; 
import com.google.android.gms.wearable.Asset; 
import com.google.android.gms.wearable.DataApi; 
import com.google.android.gms.wearable.Node; 
import com.google.android.gms.wearable.NodeApi; 
import com.google.android.gms.wearable.PutDataMapRequest; 
import com.google.android.gms.wearable.PutDataRequest; 
import com.google.android.gms.wearable.Wearable; 

import java.io.ByteArrayOutputStream; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

public class MainActivity extends Activity { 

    private static final long CONNECTION_TIME_OUT_MS = 100; 
    private static final String ON_MESSAGE = "On!"; 
    private static final String OFF_MESSAGE = "Off!"; 
    private String message = null; 
    int count = 0; 

    private GoogleApiClient client; 
    private String nodeId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initApi(); 
     setupWidgets(); 
    } 

    /** 
    * Initializes the GoogleApiClient and gets the Node ID of the connected device. 
    */ 
    private void initApi() { 
     client = getGoogleApiClient(this); 
     retrieveDeviceNode(); 
    } 

    /** 
    * Returns a GoogleApiClient that can access the Wear API. 
    * @param context 
    * @return A GoogleApiClient that can make calls to the Wear API 
    */ 
    private GoogleApiClient getGoogleApiClient(Context context) { 
     return new GoogleApiClient.Builder(context) 
      .addApi(Wearable.API) 
      .build(); 
    } 

    /** 
    * Connects to the GoogleApiClient and retrieves the connected device's Node ID. If there are 
    * multiple connected devices, the first Node ID is returned. 
    */ 
    private void retrieveDeviceNode() { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); 
       NodeApi.GetConnectedNodesResult result = 
         Wearable.NodeApi.getConnectedNodes(client).await(); 
       List<Node> nodes = result.getNodes(); 
       if (nodes.size() > 0) { 
        nodeId = nodes.get(0).getId(); 
       } 
       client.disconnect(); 
      } 
     }).start(); 
    } 

    /** 
    * Sets up the button for handling click events.` 
    */ 
    private void setupWidgets() { 
     findViewById(R.id.toggleButton).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
       Asset asset = createAssetFromBitmap(bitmap); 
       PutDataRequest request = PutDataRequest.create("/image"); 
       request.putAsset("profileImage", asset); 
       Wearable.DataApi.putDataItem(client, request); 
       showToast("SentData"); 
      } 
     }); 
    } 

    private static Asset createAssetFromBitmap(Bitmap bitmap) { 
     final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream); 
     return Asset.createFromBytes(byteStream.toByteArray()); 
    } 

    public void showToast(String string) { 
     Toast.makeText(this, string, Toast.LENGTH_LONG).show(); 
    } 
} 

磨損:

package com.example.administrator.moto360displaycontrol; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.wearable.Asset; 
import com.google.android.gms.wearable.DataApi; 
import com.google.android.gms.wearable.DataEvent; 
import com.google.android.gms.wearable.DataEventBuffer; 
import com.google.android.gms.wearable.DataMapItem; 
import com.google.android.gms.wearable.Node; 
import com.google.android.gms.wearable.NodeApi; 
import com.google.android.gms.wearable.Wearable; 
import android.widget.Toast; 

import java.io.InputStream; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

public class MainActivity extends Activity implements 
     DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ 
    private TextView mTextView; 
    private static final long CONNECTION_TIME_OUT_MS = 100; 
    private static final String ON_MESSAGE = "On!"; 
    private static final String OFF_MESSAGE = "Off!"; 
    private static final String TAG = "Moto360DisplayControl"; 
    private GoogleApiClient client; 
    private String nodeId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initApi(); 
    } 

    private void initApi() { 
     client = getGoogleApiClient(this); 
     retrieveDeviceNode(); 
    } 

    private GoogleApiClient getGoogleApiClient(Context context) { 
     return new GoogleApiClient.Builder(context) 
      .addApi(Wearable.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    } 

    private void retrieveDeviceNode() { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); 
       NodeApi.GetConnectedNodesResult result = 
        Wearable.NodeApi.getConnectedNodes(client).await(); 
       List<Node> nodes = result.getNodes(); 
       if (nodes.size() > 0) { 
        nodeId = nodes.get(0).getId(); 
       } 
       client.disconnect(); 
      } 
     }).start(); 
    } 

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

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Wearable.DataApi.addListener(client, this); 
     Toast.makeText(this, "AddedListener!", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onConnectionSuspended(int num) { 
     Toast.makeText(this, "ConnectionSuspended", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult res) { 
     Toast.makeText(this, "ConnectionFailed", Toast.LENGTH_LONG).show(); 
    } 

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

    @Override 
    public void onDataChanged(DataEventBuffer dataEvents) { 
     Toast.makeText(this, "DataChanged!", Toast.LENGTH_LONG).show(); 
     for (DataEvent event : dataEvents) { 
      if (event.getType() == DataEvent.TYPE_CHANGED && event.getDataItem().getUri().getPath().equals("/image")) { 
       DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem()); 
       Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage"); 
       Bitmap bitmap = loadBitmapFromAsset(profileAsset); 
       // Do something with bitmap 
       Toast.makeText(this, "DataChanged!", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

    public Bitmap loadBitmapFromAsset(Asset asset) { 
     if (asset == null) { 
      throw new IllegalArgumentException("Asset must be non-null"); 
     } 

     ConnectionResult result = client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); 
     if (!result.isSuccess()) { 
      return null; 
     } 

     // Convert asset into a file descriptor and block until it's ready 
     InputStream assetInputStream = Wearable.DataApi.getFdForAsset(client, asset).await().getInputStream(); 
     client.disconnect(); 

     if (assetInputStream == null) { 
      Log.w(TAG, "Requested an unknown Asset."); 
      return null; 
     } 

     // Decode the stream into a bitmap 
     return BitmapFactory.decodeStream(assetInputStream); 
    } 
} 

一切「似乎」就成功,我可以告訴,但在磨損的「onDataChanged」敬酒方法永遠不會顯示在Wear的顯示屏上,這使我認爲它出於某種原因沒有看到數據更改。任何幫助將不勝感激!

回答

2

嘗試添加時間戳到手持設備上的您的要求,它的伎倆,我回去以後,我認爲谷歌更新了自己的單證增加信息的該位

+0

我會盡力的,感謝您的答覆 – chesterhobbes 2014-11-24 17:22:22

+0

我嘗試添加一個時間戳: request.getDataMap()。putLong(「time」,new Date()。getTime()); 但它沒有幫助...我也看了這個http://stackoverflow.com/questions/24676165/unable-to-push-data-to-android-wear-emulator問題,並重新安排了一些代碼看起來像他所擁有的,因爲他本質上是在努力做同樣的事情,但我仍然沒有運氣......這讓我瘋狂 – chesterhobbes 2014-11-24 21:57:06

+0

你有一個真正的耐用品設備,還是你正在使用模擬器?當我嘗試Data API時,我記得我無法讓它與模擬器一起工作... – Snow 2014-11-25 08:45:40