【问题标题】:Pebble configuration page communications not respondingPebble 配置页面通信没有响应
【发布时间】:2015-04-16 13:26:44
【问题描述】:

我正在创建我的第一个表盘,它需要一个可以存储两个字符串(标题和消息)的配置页面。

我对所有的交流都不太熟悉,因为那里没有任何完整的例子,但我已经尽可能地了解了这一点。

这是我所有空间的相关代码

main.c

static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
              APP_LOG(APP_LOG_LEVEL_INFO, "Message received!");

              // Get the first pair
              Tuple *t = dict_read_first(iterator);

              //Long lived buffers
              static char title_buffer[64];
              static char message_buffer[124];

              // Process all pairs present
              while(t != NULL) {
                // Process this pair's key
                switch (t->key) {
                  case TITLE_DATA:
                    snprintf(title_buffer, sizeof(title_buffer), "%s", t->value->cstring);
                    text_layer_set_text(title_layer, title_buffer);
                    APP_LOG(APP_LOG_LEVEL_INFO, "TITLE_DATA received with value %d", (int)t->value->int32);
                    break;
                  case MESSAGE_DATA:
                    snprintf(message_buffer, sizeof(message_buffer), "%s", t->value->cstring);
                    text_layer_set_text(message_layer, message_buffer);
                    APP_LOG(APP_LOG_LEVEL_INFO, "MESSAGE_DATA received with value %d", (int)t->value->int32);
                    break;
                }

                // Get next pair, if any
                t = dict_read_next(iterator);
              }
            }

pebbleScript.js

var title = localStorage.getItem('title') ? localStorage.getItem('title') : 'Title',
        message = localStorage.getItem('message') ? localStorage.getItem('message') : "Message that can be changed in watchface 'Settings'";

        Pebble.addEventListener('showConfiguration', function(e) {
          console.log("Showing configuration");
          // Show config page
          Pebble.openURL('https://dl.dropboxusercontent.com/s/kzl44khedt5e22d/config.html?dl=0');
        });

        Pebble.addEventListener('webviewclosed', function(e) {
          var options = JSON.parse(decodeURIComponent(e.response));
          title = encodeURIComponent(options.title);
          message = encodeURIComponent(options.message);

          if(title == 'undefined') {
            title = 'Title';
          } if (message == 'undefined') {
            message = "Message that can be changed in watchface 'Settings'";
          }

          localStorage.setItem('title', title);
          localStorage.setItem('message', message);

          console.log("Configuration window returned: ", JSON.stringify(options));
        });

        Pebble.addEventListener('ready', function(e) {
          console.log("PebbleKit JS Ready!");

          //Construct a dictionary
          var

 dict = {
            'TITLE_DATA' : title,
            'MESSAGE_DATA' : message
          };

      //Send a string to Pebble
      Pebble.sendAppMessage(dict, function(e) {
        console.log("Send successful.");
      }, function(e) {
        console.log("Send failed!");
      });
    });

config.html

<h3>Title:</h3>
        <input type="text" name="title" id="title"></input>
        <h3>Message:</h3>
        <input type="text" name="message" id="message"></input>  
        <br>        
        <input type="submit" id="cancelButton" value="Cancel">
        <input type="submit" id="saveButton" value="Save">

    <script>
        $('#cancelButton').click(function() {
            location.href = 'pebblejs://close';
        });

        $('#saveButton').click(function() {
            var options = {
                title: $('title').val(),
                message: $('#message').val()
            }

            location.href = 'pebblejs://close#' + encodeURIComponent(JSON.stringify(options));
        });

        function getURLVariable(name)  {
            name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
            var regexS = "[\\?&]"+name+"=([^&#]*)",
                regex = new RegExp(regexS),
                results = regex.exec(window.location.href);
            if (results == null) return "";
            else return results[1];
        }
        $(document).ready(function() {
            var priorTitle = getURLVariable('title');
            priorTitle = decodeURI(priorTitle);

            if (priorTitle) {
                $('#title').html(priorTitle);
            }

            var priorMessage = getURLVariable('message');
            priorMessage = decodeURI(priorTitle);

            if (priorMessage) {
                $('#message').html(priorMessage);
            }
        });
    </script>

如果有人知道为什么这不能按预期工作,我将非常感谢帮助 :) 如果有任何其他详细信息我应该包括在内,请告诉我。

我正在使用 CloudPebble 进行开发。我已经在设置中完成了标题和消息键,并在我的 main.c 中定义了它们,所以不是那样的。

我应该注意的是,在应用程序日志中它显示“TITLE_DATA received with value.....”但不是“MESSAGE_DATA received....”所以问题可能出在那边。

【问题讨论】:

    标签: javascript json communication pebble-sdk cloudpebble


    【解决方案1】:

    您在函数内声明了“长期存在的缓冲区”:

    static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
        ...
        //Long lived buffers
        static char title_buffer[64];
        static char message_buffer[124];
        ...
    }
    

    如果您希望它们保持在范围内(持久化),则需要将它们与其他全局变量一起声明:

    static Window *s_main_window;
    static char title_buffer[64];
    static char message_buffer[124];
    

    【讨论】:

    • 谢谢,前几天我修好了,忘记了我已经发布了一个关于它的问题,但这就是问题所在:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多