2017-03-06 56 views
0

我從佩戴手錶發送數據字節。我想在手機的後臺服務中接收數據,但不會調用onMesageReceived。任何想法我做錯了什麼?我的第一次嘗試是在手機上運行的應用程序,但並不實用。在移動使用Google Message API接收服務中的數據

服務:

public class LampControlService extends Service implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks { 
     private static final String TAG = "test"; 
     private static final String WEAR_MESSAGE_PATH = "/message"; 
     private GoogleApiClient mGoogleApiClient; 
     private ArrayAdapter<String> mAdapter; 
     @Override 
     public void onCreate(){ 
      HandlerThread thread = new HandlerThread("test"); 
      thread.start(); 

      //---Build a new Google API client-- 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(Wearable.API) 
        .addConnectionCallbacks(this) 
        .build(); 

      if (mGoogleApiClient != null && !(mGoogleApiClient.isConnected() || 
        mGoogleApiClient.isConnecting())) 
        mGoogleApiClient.connect();  
     } 

     public int onStartCommand(Intent intent, int flags, int startId){ 
      return START_STICKY; 
     } 

     @Override 
     public void onDestroy() { 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      return null; 
     } 

     @Override 
     public void onConnected(Bundle bundle) { 
      Wearable.MessageApi.addListener(mGoogleApiClient, this); 
      Log.d(TAG, "onConnected()"); 
     } 

     @Override 
     public void onMessageReceived(final MessageEvent messageEvent) { 
      Log.d(TAG, "onMessageReceived()"); 
      if (messageEvent.getPath().equalsIgnoreCase(WEAR_MESSAGE_PATH)) { 
       String lamp = new String(messageEvent.getData()); 
       sendLampCommand(lamp); 
      } 
      else { 
    //   super.onMessageReceived(messageEvent); 
      } 
     } 
     // @Override 
     public void onConnectionSuspended(int i) { 
     } 

     private static void sendLampCommand(String lamp){ 
      // Send lamp command to web server 
     } 
    } 

線在AndroidManifest補充說:

<service 
    android:name=".LampControlService"> 
    <service android:name=".DataLayerListenerService"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> 
     </intent-filter> 
    </service> 
</service> 

回答

1

我面臨着同樣的問題。 首先,檢查Google服務庫。使用支持您設備的最古老的設備。

和/或檢查您的設備是否處於睡眠狀態。某些設備的行爲不正確。 我的意思是,打開這個目錄:

.\Sdk\extras\google\m2repository\com\google\android\gms\play-services-wearable\

,並檢查您的版本。 請勿使用最新版本。使用8.4.0\或類似的。

+0

好建議! – Henrik

相關問題