【问题标题】:Pebble app not receiving String from phonePebble 应用程序未从手机接收字符串
【发布时间】:2014-03-02 21:41:32
【问题描述】:

我正在创建一个非常简单的 pebble 应用程序。

目标: 当我点击我的 Android 应用时,Pebble 应用会显示我从我的 Android 应用发送的消息。

问题: 文本不会更改/显示在鹅卵石上。

代码

Pebble .c 代码:

#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"


#define MY_UUID { 0x98, 0x15, 0xA8, 0xDA, 0x6C, 0xAC, 0x40, 0xB0, 0xAD, 0x87, 0xFC, 0xDF, 0x9E, 0x86, 0x3E, 0x91 }
PBL_APP_INFO(MY_UUID,
             "Phone App", "Me",
             1, 0, /* App version */
             DEFAULT_MENU_ICON,
             APP_INFO_STANDARD_APP);

Window window;
TextLayer hello_layer;
AppMessageCallbacksNode messageCallBacks;

void myMessageHandler(DictionaryIterator* received, void* context) {
    vibes_short_pulse();
    Tuple* tuple = dict_find(received, 0);
    text_layer_set_text(&hello_layer, tuple->value->cstring);
}

void handle_init(AppContextRef ctx) {
    messageCallBacks = (AppMessageCallbacksNode) {
        .callbacks = {
            .in_received = myMessageHandler
        },
        .context = NULL
    };
  window_init(&window, "Window Name");
  window_stack_push(&window, true /* Animated */);

  text_layer_init(&hello_layer, GRect(0, 65, 144, 30));
  text_layer_set_text(&hello_layer, "Hello World!");
  layer_add_child(window_get_root_layer(&window), (Layer*)&hello_layer);
}


void pbl_main(void *params) {
  PebbleAppHandlers handlers = {
    .init_handler = &handle_init,
      .messaging_info = {
          .buffer_sizes = {
              .inbound = 64,
              .outbound = 64
          }
      }
  };
  app_event_loop(params, &handlers);
}

安卓代码:

public class MainActivity extends Activity {
    private Button send;
    private Context context;
    UUID AppId = UUID
            .fromString("9815A8DA-6CAC-40B0-AD87-FCDF9E863E91");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.send);
        context = this;

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        PebbleDictionary data = new PebbleDictionary();
                        data.addString(0, "TEST");
                        PebbleKit.sendDataToPebble(getApplicationContext(), AppId, data);

                    }

                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

谢谢!

【问题讨论】:

    标签: java android c pebble-watch pebble-sdk


    【解决方案1】:

    您的handle_init 方法中似乎缺少对app_message_register_callbacks 的调用。

    添加这个应该可以解决问题:

    app_message_register_callbacks(&messageCallBacks);
    

    您仍在使用现已弃用的 SDK 1.x。我强烈建议您升级到 SDK 2.0。开发和调试工具要好得多。您还可以使用更多的 API。

    【讨论】:

    • 谢谢,但它似乎不起作用。我得到:预期的'struct AppMessageCallbacksNode *',但参数的类型是'AppMessageCallbacksNode,所以我尝试将其更改为app_message_register_callbacks(&messageCallBacks);已编译但未从电话中获取字符串。再次感谢。
    • 天哪,我真是太愚蠢了。我从来没有在我的手机上启动线程。非常感谢,不敢相信我犯了这么愚蠢的错误。
    • 很高兴知道它现在正在工作。我已经更新了我的答案以解决类型问题,感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多