2017-07-18 83 views
0

我想從android手機發送消息給可穿戴設備,但WearableListenerService中的onMessageReceived方法從不會被調用。我已驗證手機發送的消息已成功發送到正確的節點,因此問題似乎出現在可穿戴應用程序的接收端。這裏是我在可穿戴項目監聽器類代碼:Android:可佩戴不接收消息Api消息

public class MessageListener extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks { 
@Override 
public void onMessageReceived(MessageEvent messageEvent) { 
    if (messageEvent.getPath().equals("/message_path")) { 
     final String message = new String(messageEvent.getData()); 

     // Broadcast message to wearable activity for display 
     Intent messageIntent = new Intent(); 
     messageIntent.setAction(Intent.ACTION_SEND); 
     messageIntent.putExtra("message", message); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); 
    } 
    else { 
     super.onMessageReceived(messageEvent); 
    } 
} 

@Override 
public void onPeerConnected(Node peer) { 
    Log.v("myTag", "Peer connected: " + peer.getDisplayName()); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    Wearable.MessageApi.addListener(MainActivityWear.client, this); 
} 

@Override 
public void onConnectionSuspended(int i) {} 
} 

通過設置斷點,我發現,無論是onMessageReceived方法也不onConnected方法調用。

下面是可穿戴的項目在MainActivity:

public class MainActivityWear extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private TextView mTextView; 
private MessageReceiver messageReceiver; 
public static GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_wear); 
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); 
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { 
     @Override 
     public void onLayoutInflated(WatchViewStub stub) { 
      mTextView = (TextView) stub.findViewById(R.id.text); 
     } 
    }); 
    client = new GoogleApiClient.Builder(this) 
      .addApi(Wearable.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    // Register the local broadcast receiver 
    IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND); 
    messageReceiver = new MessageReceiver(); 
    LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter); 
} 
// Connect to the data layer when the Activity starts 
@Override 
protected void onStart() { 
    super.onStart(); 
    client.connect(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    unregisterReceiver(messageReceiver); 
} 

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

@Override 
public void onConnectionSuspended(int i) {} 

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


public class MessageReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String message = intent.getStringExtra("message"); 
     // Display message in UI 
     mTextView.setText(message); 
    } 
} 
} 

的的messageReceiver是嵌套類。 所有內容編譯正確,但消息從未收到。先謝謝你!

回答

0

檢查應用程序和設備中的GoogleServicesAPI版本。他們必須是一樣的。

檢查您的穿戴設備和手持設備是否與您的應用具有相同的版本。如果不是 - 使用最老的一個。

PS 不使用onPeerConnectedonConnectionSuspended。這些方法已被棄用,不會被解僱。

+0

謝謝,@Vyacheslav,我查了一下,版本也一樣。我修改了代碼,不包含onPeerConnected和onConnectionSuspended,但它仍然沒有收到。 –