WearOS 应用程序不接收来自安卓手机的数据
Posted
技术标签:
【中文标题】WearOS 应用程序不接收来自安卓手机的数据【英文标题】:WearOS app does not receive data from android phone 【发布时间】:2020-01-11 15:13:00 【问题描述】:我有一个简单的程序,我只是从我的手机向我的 Wearos 模拟器发送一条消息,反之亦然,但 onReceive 函数应用程序永远不会在两个设备上被调用。 (是的,我已将手机与模拟器配对)
这是我的安卓手机代码:
public class MainActivity extends AppCompatActivity
Button talkbutton;
TextView textview;
protected Handler myHandler;
int receivedMessageNumber = 1;
int sentMessageNumber = 1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
talkbutton = findViewById(R.id.talkButton);
textview = findViewById(R.id.textView);
//Create a message handler//
myHandler = new Handler(new Handler.Callback()
@Override
public boolean handleMessage(Message msg)
Bundle stuff = msg.getData();
messageText(stuff.getString("messageText"));
return true;
);
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
Receiver messageReceiver = new Receiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
public void messageText(String newinfo)
if (newinfo.compareTo("") != 0)
textview.append("\n" + newinfo);
//Define a nested class that extends BroadcastReceiver//
public class Receiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
String message = "I just received a message from the wearable " + receivedMessageNumber++;;
textview.setText(message);
public void talkClick(View v)
String message = "Sending message.... ";
textview.setText(message);
new NewThread("/my_path", message).start();
//Use a Bundle to encapsulate the message//
public void sendmessage(String messageText)
Bundle bundle = new Bundle();
bundle.putString("messageText", messageText);
Message msg = myHandler.obtainMessage();
msg.setData(bundle);
myHandler.sendMessage(msg);
class NewThread extends Thread
String path;
String message;
//Constructor for sending information to the Data Layer//
NewThread(String p, String m)
path = p;
message = m;
public void run()
//Retrieve the connected devices
Task<List<Node>> wearableList =
Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
try
List<Node> nodes = Tasks.await(wearableList);
for (Node node : nodes)
Task<Integer> sendMessageTask =
//Send the message//
Wearable.getMessageClient(MainActivity.this).sendMessage(node.getId(), path, message.getBytes());
Integer result = Tasks.await(sendMessageTask);
sendmessage("I just sent the wearable a message " + sentMessageNumber++);
catch (Exception exception)
Toast.makeText(getApplicationContext(),exception.getMessage(),Toast.LENGTH_LONG);
MessageService 将消息发送到 wear os 应用程序:
public class MessageService extends WearableListenerService
@Override
public void onMessageReceived(MessageEvent messageEvent)
if (messageEvent.getPath().equals("/my_path"))
final String message = new String(messageEvent.getData());
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
else
super.onMessageReceived(messageEvent);
由于两个应用程序的接收和发送功能非常相同,我认为它足以向您展示来自 android 设备的代码,但如果您喜欢这里是来自 wearos 应用程序的接收方法:
IntentFilter newFilter = new IntentFilter(Intent.ACTION_SEND);
Receiver messageReceiver = new Receiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, newFilter);
public class Receiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
//Display the following when a new message is received//
String onMessageReceived = "I just received a message from the handheld " + receivedMessageNumber++;
textView.setText(onMessageReceived);
【问题讨论】:
【参考方案1】:您是否添加了所需的intent filter in your Manifest.xml for the WearableListenerService?像这样的:
<service android:name="your.package.YourWatchListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
android:host="*"
android:pathPrefix="/my_path"
android:scheme="wear" />
</intent-filter>
</service>
【讨论】:
以上是关于WearOS 应用程序不接收来自安卓手机的数据的主要内容,如果未能解决你的问题,请参考以下文章