Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Quick question about messaging

Hello all. I have successfully managed to import weather from the docs available, I have also managed to change colours from the docs available using the messaging. Now my trouble starts when I try to combine both into the same watch face.

The messaging function only sends one response back, (usually the first function to run). One seems to override the other.

Do I need to separate message functions (my weather is automatically sending a fetch every half hour and my theme is to get an instant response from the user.) or can they both be sent via the same request.

As I said, only one of the message function seems to run.

Any guidance would be appreciated.

Best Answer
0 Votes
4 REPLIES 4

You probably just need to distinguish which messages you are sending and receiving, like this.

 

Companion  sends data with a key:

 

function sendValue(key, val) {
  if (val) {
    messaging.peerSocket.send({
      key: key,
      value: JSON.parse(val)
    });
  }
}

Then on the device, you can check the key and do something depending on which key you receive:

 

messaging.peerSocket.addEventListener("message", function(evt) {
const val = evt.data.value;
if (evt.data.key === "something") {
console.log("do something with val");
} else if (evt.data.key === "something else") {
console.log("do something else with val");
} })

 

 

Best Answer
0 Votes

This worked thanks. Just a follow up question, how I do I save the colour change settings so when you go into an app and come back the users selected colour still displays. Using the code in the docs just defaults back to the original colour once the clock face reloads.

 

Thanks in advance

Best Answer
0 Votes

Persist it on the device using the file system api, as per https://github.com/Fitbit/sdk-moment/blob/master/app/simple/device-settings.js

Best Answer
0 Votes

Hello again, I have come back to this as I would like to deploy a watchface with weather included, I am hanging on to add the feature when you click the weather icon it launches the weather app but one thing at a time.

 

Following your advice I successfully installed the colour options and got them to save but I am facing a small issue. The watch face works fine, the colours work and even the weather works but when the console is running there is a slight issue.

My weather fetches every 30 mins and does so successfully. When I change the colours I get this error in the console 

 

[16:38:13]Unhandled TypeError: Cannot read property 'toFixed' of undefined

 

Now I understand this is to do with the fact its not receiving any weather info as I am telling it to do this every half hour.

So, I assume as i am requesting a colour change it automatically runs the messaging for weather and obviously returns nothing, hence the error.

 

This is what sits in my app.index

 

 

messaging.peerSocket.addEventListener("message", function(evt) {
  const val = evt.data.value;
  if (evt.data){
       processWeatherData(evt.data);
  }
  })

 I have tried all sorts based on your advice earlier on in this thread but continue to get this error.

I have used the code from the link you passed on as a base to add the colour options.

 

https://github.com/Fitbit/sdk-moment/blob/master/app/simple/device-settings.js

 

Any help on this would be appreciated. I have learnt a lot from these forums in 12 months but as you can tell some of the finer things I still need to resolve.

 

Thank you

 

Best Answer
0 Votes