Reputation: 1
This code is the WearableListenerService code inside the wear os app
package com.example.ans_watch;
import ...
public class MyWearableListenerService extends WearableListenerService {
private static final String TAG = "from Android";
private static final String MESSAGE_PATH = "/message";
private MessageClient.OnMessageReceivedListener listener;
public void setOnMessageReceivedListener(MessageClient.OnMessageReceivedListener listener){
this.listener = listener;
}
public MyWearableListenerService(){
Log.d(TAG, "객체가 생성했음");
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (listener != null) {
listener.onMessageReceived(messageEvent);
}
}
}
This code is one of the activities inside the wear os application, and onMessageReceived() is specified and used.
package com.example.ans_watch;
import ...
public class nodriveActivity extends Activity{
public MyWearableListenerService service = new MyWearableListenerService();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.state_nodrive);
service.setOnMessageReceivedListener(new MessageClient.OnMessageReceivedListener() {
@Override
public void onMessageReceived(@NonNull MessageEvent messageEvent) {
Log.d("데이터 수신 완료", "Message received: " + messageEvent.getPath());
if (messageEvent.getPath().equals("/message")) {
String message = new String(messageEvent.getData(), StandardCharsets.UTF_8);
Log.d("수신 성공!!", "받은 데이터는" + message);
}
}
});
startService(new Intent(this, service.getClass()));
}
Finally, this code is the sendMessage() part of the Android application's MainActivity code.
package com.example.test;
import ...
public class MainActivity extends AppCompatActivity {
private void sendMessage() {
// Create a new thread to avoid blocking the UI thread
new Thread(new Runnable() {
@Override
public void run() {
// Get the connected nodes on the Wear network
Task<List<Node>> nodeListTask = Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
try {
List<Node> nodes = Tasks.await(nodeListTask);
for (Node node : nodes) {
// Build the message
String message = "Hello!";
byte[] payload = message.getBytes();
// Send the message
Task<Integer> sendMessageTask =
Wearable.getMessageClient(getApplicationContext()).sendMessage(node.getId(), "/message", payload);
// Add onCompleteListener to check if the message was successfully sent
sendMessageTask.addOnCompleteListener(new OnCompleteListener<Integer>() {
@Override
public void onComplete(@NonNull Task<Integer> task) {
if (task.isSuccessful()) {
int result = task.getResult();
Log.d("from android", "Message sent to " + node.getDisplayName() + ". Result: " + result + ". message: " +message);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "성공", Toast.LENGTH_SHORT).show();
}
});
} else {
Exception exception = task.getException();
Log.e("from android", "Failed to send message: " + exception);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Toast 메시지 내용", Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
} catch (ExecutionException exception) {
Log.e("from android", "Failed to send message: " + exception);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Toast 메시지 내용", Toast.LENGTH_SHORT).show();
}
});
} catch (InterruptedException exception) {
Log.e("from android", "Failed to send message: " + exception);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Toast 메시지 내용", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
The code above is the code I wrote to send a simple message from the Android app to wear os.
In fact, when you actually run the Android application and execute sendMessage, a log message appears indicating that the message transmission was successful.
However, in the wear os application, log messages appear only when objects are created, and log messages inside onMessageReceived are not visible.
I think maybe it's not receiving in wear os.
Why can't I receive messages on wear os???
The smartphone and smart watch are properly paired. A log message appears indicating that the message was successfully sent from the Android application to the smartwatch.
Upvotes: 0
Views: 835
Reputation: 11
Both of your apps need to have the same applicationId, here is how I found that : WearableListenerService onMessageReceived is not called on device
Upvotes: 1