首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >鹅卵石配置页面通信没有响应

鹅卵石配置页面通信没有响应
EN

Stack Overflow用户
提问于 2015-04-16 13:26:44
回答 1查看 234关注 0票数 0

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

我不太熟悉所有的沟通,因为没有任何真正的例子,但我已经尝试了尽可能远的这一点。

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

main.c

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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收到的值.“但不是"MESSAGE_DATA收到.“所以问题可能就在那边某个地方。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-20 21:14:34

您在函数中声明了您的“长寿缓冲区”:

代码语言:javascript
复制
static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
    ...
    //Long lived buffers
    static char title_buffer[64];
    static char message_buffer[124];
    ...
}

如果希望它们停留在作用域中(持久化),则需要与其他全局值一起声明它们:

代码语言:javascript
复制
static Window *s_main_window;
static char title_buffer[64];
static char message_buffer[124];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29676173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档