Android wear 无法向 phone 发送消息
Android wear can´t send message to phone
我一直在关注 this tutorial 以从 Android wear 向 Android phone 发送消息,但它不起作用。这是我的代码:
穿
从 Minifest 开始,我实现了 google 播放服务元数据,如下所示:
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
其次,我初始化 GoogleApiClient,如下所示:
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
然后,我实现接口所需的所有方法:GoogleApiClient.ConnectionCallbacks 和
GoogleApiClient.OnConnectionFailedListener,像这样:
@Override
public void onConnected(Bundle connectionHint) {
System.out.println("SESSION CONNECTED");
}
// Disconnect from the data layer when the Activity stops
@Override
protected void onStop() {
if (null != googleClient && googleClient.isConnected()) {
googleClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onStart() {
super.onStart();
googleClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
System.out.println("FAILED TO CONNECT: "+connectionResult.getErrorMessage());
}
最后我实现了将文本发送到 phone:
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await();
if (result.getStatus().isSuccess()) {
System.out.println("Message: {" + message + "} sent to: " + node.getDisplayName());
}
else {
// Log an error
System.out.println("ERROR: failed to send Message");
}
}
问题是它似乎可以工作(当我开始佩戴时我得到“"SESSION CONNECTED" 字符串),但它找不到任何配对设备来发送字符串,因为列表nodes 是空的。这可能是模拟器的问题吗?phone 和 wear 可能都没有同步,因此他们找不到对方。
Phone
为了展示孔示例,我还包括了 phone 部分:
Manifest.xml
<service android:name=".utils.ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"
tools:ignore="WearableBindListener" />
</intent-filter>
</service>
监听服务
public class ListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
System.out.println("Message path received on watch is: " + messageEvent.getPath());
System.out.println("Message received on watch is: " + message);
}
else {
super.onMessageReceived(messageEvent);
}
}
没关系,这是模拟器的问题,它们没有同步。只是尝试使用真实的设备,它成功了。
我的问题是 phone 上的应用程序和手表上的应用程序使用不同的证书签名,尽管它们具有相同的 appid
我一直在关注 this tutorial 以从 Android wear 向 Android phone 发送消息,但它不起作用。这是我的代码:
穿
从 Minifest 开始,我实现了 google 播放服务元数据,如下所示:
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
其次,我初始化 GoogleApiClient,如下所示:
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
然后,我实现接口所需的所有方法:GoogleApiClient.ConnectionCallbacks 和 GoogleApiClient.OnConnectionFailedListener,像这样:
@Override
public void onConnected(Bundle connectionHint) {
System.out.println("SESSION CONNECTED");
}
// Disconnect from the data layer when the Activity stops
@Override
protected void onStop() {
if (null != googleClient && googleClient.isConnected()) {
googleClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onStart() {
super.onStart();
googleClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
System.out.println("FAILED TO CONNECT: "+connectionResult.getErrorMessage());
}
最后我实现了将文本发送到 phone:
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await();
if (result.getStatus().isSuccess()) {
System.out.println("Message: {" + message + "} sent to: " + node.getDisplayName());
}
else {
// Log an error
System.out.println("ERROR: failed to send Message");
}
}
问题是它似乎可以工作(当我开始佩戴时我得到“"SESSION CONNECTED" 字符串),但它找不到任何配对设备来发送字符串,因为列表nodes 是空的。这可能是模拟器的问题吗?phone 和 wear 可能都没有同步,因此他们找不到对方。
Phone
为了展示孔示例,我还包括了 phone 部分:
Manifest.xml
<service android:name=".utils.ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"
tools:ignore="WearableBindListener" />
</intent-filter>
</service>
监听服务
public class ListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
System.out.println("Message path received on watch is: " + messageEvent.getPath());
System.out.println("Message received on watch is: " + message);
}
else {
super.onMessageReceived(messageEvent);
}
}
没关系,这是模拟器的问题,它们没有同步。只是尝试使用真实的设备,它成功了。
我的问题是 phone 上的应用程序和手表上的应用程序使用不同的证书签名,尽管它们具有相同的 appid