发送消息给Android Wearable

时间:2016-06-21 16:44:55

标签: android wear-os android-wear-notification

我想向两款不同的Android Smart-Watches发送消息。 在移动端,Wearable.MessageApi.sendMessage被调用,sendMessageResult.getStatus().isSuccess()为真,因此发送消息应该有效。 现在的问题是,onMessageReceived的{​​{1}}方法从未收到消息。我究竟做错了什么? 这是我使用的代码:

Android应用的主要活动。

左/右方法正在发送消息

MyWearableListenerService

MyWearableListenerService

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG_CONNECTION = "Connection";
private static final String TAG_NODES = "Nodes";

private static final String START_ACTIVITY_PATH = "/start_MainActivity";

private String leftWatch;
private String rightWatch;
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Setting up the Wearable API Client
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(@Nullable Bundle bundle) {
                    Log.e(TAG_CONNECTION, "Connection established");

                }

                @Override
                public void onConnectionSuspended(int i) {
                    Log.e(TAG_CONNECTION, "Connection suspended");
                }
            })
            .build();

    mGoogleApiClient.connect();


    // Find the connected watches and store their UUIDs to distinguish at a later moment
    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
            List<Node> nodes = getConnectedNodesResult.getNodes();

            if (nodes.isEmpty()) {
                Log.e(TAG_NODES, "No Nodes found");
                return;
            }

            rightWatch = nodes.get(0).getId();
            Log.e(TAG_NODES,"Node is nearby: "+nodes.get(0).isNearby());
            Log.e(TAG_NODES, rightWatch);
            leftWatch = nodes.get(1).getId();
            Log.e(TAG_NODES,"Node is nearby: "+nodes.get(1).isNearby());
            Log.e(TAG_NODES, leftWatch);

        }
    });


}

@Override
public void onConnectionFailed(ConnectionResult result) {

    // Executed upon failed connection to Wearable API
    // e.g. when Android Wear App is missing

    Log.e(TAG_CONNECTION, "Connection failed");
}

public void left(View view) {
    Log.e(TAG_CONNECTION, "Trying to send message to: " + leftWatch);

    if (!mGoogleApiClient.isConnected()) mGoogleApiClient.connect();

    Wearable.MessageApi.sendMessage(mGoogleApiClient, leftWatch, START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
            if (!sendMessageResult.getStatus().isSuccess()) {
                Log.e(TAG_CONNECTION, "Failed to send message with status code: "
                        + sendMessageResult.getStatus().getStatusCode());
                return;
            }
            Log.e(TAG_CONNECTION, "Message successfully sent");
        }
    });

}

public void right(View view) {
    Log.e(TAG_CONNECTION, "Trying to send message to: " + rightWatch);

    if (!mGoogleApiClient.isConnected()) mGoogleApiClient.connect();

    Wearable.MessageApi.sendMessage(mGoogleApiClient, rightWatch, START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
            if (!sendMessageResult.getStatus().isSuccess()) {
                Log.e(TAG_CONNECTION, "Failed to send message with status code: "
                        + sendMessageResult.getStatus().getStatusCode());
                return;
            }
            Log.e(TAG_CONNECTION, "Message successfully sent");
        }
    });


}

public void both(View view) {

    Log.e(TAG_CONNECTION, "Trying to send message to: All currently connected watches");

    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
            List<Node> nodes = getConnectedNodesResult.getNodes();

            for (final Node node : nodes) {
                Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        if (!sendMessageResult.getStatus().isSuccess()) {
                            Log.e(TAG_CONNECTION, "Failed to send message with status code: "
                                    + sendMessageResult.getStatus().getStatusCode());
                            return;
                        }
                        Log.e(TAG_CONNECTION, "Message successfully sent to: " + node.getId());
                    }
                });
            }

        }
    });

}

@Override
protected void onDestroy() {
    mGoogleApiClient.disconnect();
    super.onDestroy();
}
}

AndroidManifest在磨损方面

public class MyWearableListenerService extends WearableListenerService {

private static final String TAG_SIGNAL = "Signal";
private static final String TAG_CONNECTION = "Connection";
private static final String START_ACTIVITY_PATH = "/start_MainActivity";


@Override
public void onMessageReceived(MessageEvent messageEvent) {

    Log.e(TAG_SIGNAL, "Message received: " + messageEvent.getPath());

    if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
        Intent intent = new Intent(this, MainWearActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    } else super.onMessageReceived(messageEvent);


}


@Override
public void onPeerConnected(Node node) {
    Log.e(TAG_CONNECTION, "Peer connected: " + node.getId());
    super.onPeerConnected(node);
}

@Override
public void onPeerDisconnected(Node node) {
    Log.e(TAG_CONNECTION, "Peer disconnected: " + node.getId());
    super.onPeerDisconnected(node);
}
}

1 个答案:

答案 0 :(得分:0)

我相信你应该为你的意图过滤器添加一个路径前缀。在您的可穿戴设备的Android清单中:

 <service
    android:name=".MyWearableListenerService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
        <data android:scheme="wear" android:host="*" android:pathPrefix:"/start_MainActivity" />
    </intent-filter>
</service>

让我知道它是否有效!