watch face service data layer api

做~自己de王妃 提交于 2020-01-06 02:44:06

问题


I got a problem with the Google android wear Data Layer Api...

I already used it to call handheld activities from a wearable activity and it worked just fine. But now I want call a handheld activity from a watchface service...

I created a Google API Client and added wearableApi:

     public void onCreate(SurfaceHolder holder) {
        super.onCreate(holder);

        setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFace.this)
                .setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
                .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
                .setShowSystemUiTime(false)
                .setAcceptsTapEvents(true)
                .build());
        mGoogleApiClient = new GoogleApiClient.Builder(MyWatchFace.this)
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();

Then I used these methods to send the data:

    public void startActivity()
    {
        HashSet<String> results = new HashSet<String>();

        Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                String handheld = getConnectedNodesResult.getNodes().get(0).getId();
                Toast.makeText(MyWatchFace.this, getConnectedNodesResult.getNodes().get(0).getDisplayName()+": "+ handheld, Toast.LENGTH_SHORT).show();
                sendStartActivityMessage(handheld, giveValueXYZ());

            }
        });

private void sendStartActivityMessage(String nodeId, String data) {
        byte[] dataArray = data.getBytes();
        Toast.makeText(MyWatchFace.this, "ActivityPath: "+ START_ACTIVITY_PATH, Toast.LENGTH_SHORT).show();
        Wearable.MessageApi.sendMessage(

                mGoogleApiClient, nodeId, START_ACTIVITY_PATH, dataArray).setResultCallback(
                new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        if (!sendMessageResult.getStatus().isSuccess()) {
                            Log.e(TAG, "Failed to send message with status code: "
                            + sendMessageResult.getStatus().getStatusCode());
                        }
                    }

                });
    }

On the mobile site I used this code to listen for the messages:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_host_main);
    context = getApplicationContext();

    ...

    //Google Api
    final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API).build();
    googleApiClient.connect();

    Wearable.MessageApi.addListener(googleApiClient, this);

...

public void onMessageReceived(MessageEvent messageEvent) {

    if (messageEvent.getPath().equals(START_ACTIVITY_PATH_WEARDATA)) {
        Log.d(TAG, "getPath: " + messageEvent.getPath() + "|||getData: " + messageEvent.getData());

        someActions();
    }

All this works perfectly fine if I send the message from a wearable activity but does not even trigger the onMessageReceived(...) methode if I send a message from the watchface service...

Could there be a problem with my package names?
mobile :com.example.janik.xxx.xxx

watchface: de.WatchSmart.watch_smart_watch_face_service

How does the client know which Data Layer Api message is adressed to him?

来源:https://stackoverflow.com/questions/35278233/watch-face-service-data-layer-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!