Search code examples
javascriptsdkfitbit

How to send the last sync time through the Fitbit Companion Messaging API to a clock face?


The goal is to get the device.lastSyncTime from the companion index.js to the app/index.js in a Fitbit Sense clockface (SDK 6.0.0).

My simple idea was that the tellSync() function would send a message containing the device.lastSyncTime

function sendVal(data) {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN)
    messaging.peerSocket.send(data);
}

function tellSync() {
  let date = device.lastSyncTime;
  let data = {
    key: 'lastSync',
    newValue: date
  };
  sendVal(data);
}

However, within the app/index.js, the received message doesn't contain the device.lastSyncTime:

messaging.peerSocket.onmessage = evt => {
  console.log(JSON.stringify(evt))
};

And the output is App: {"data":{"key":"lastSync","newValue":{}}}. There are no errors, it's connected to a current version Fitbit Sense device (2021-08-10) and a phone.

Now, if I put the device.lastSyncTime into a console.log(), it would hand me the timestamp (e.g. Tue Aug 10 2021 19:59:40 GMT+0200 (Central European Summer Time) ), so I must be doing something wrong, but I'm not quite sure what.


Solution

  • It was an issue of a data type. A modification of the tellSync() function fixed it:

    newValue: device.lastSyncTime.toString()
    

    The output is now as desired:

    App: {"data":{
        "key":"lastSync",
        "newValue":"Tue Aug 10 2021 20:51:27 GMT+0200 (Central European Summer Time)"}}`