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

wire toggle value in settings to bool var in app (begginer)

ANSWERED

Hi there,

sorry for this one...

i m very unfamiliar with programming and totally new to fitbit sdk.

it sounds very basic but i m struggling. i simply want to wire the value of toggle btton in my settings to a var boolin my app.

in my *.jsx

<Toggle
          settingsKey="toggle"
          label="myToggle"
          />

then in my app/index.js

import * as messaging from "messaging";
import document from "document";

let myBool =new Boolean(true);

  messaging.peerSocket.onmessage = evt => {
  console.log(`App received: ${JSON.stringify(evt)}`);

      if (evt.data.key === "toggle" && evt.data.newValue)
       {
      myBool = (JSON.parse(evt.data.newValue)).name;
       console.log(`New date: ${myBool}`); 
      } 
}

that is not working, could someone show me the wright writing for this? 

thank you ! 

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

@Slumberlander wrote:

that is not working, could someone show me the wright writing for this? 

thank you ! 

 


You need some code in your companion\index.js to handle when your settings page changes your settings
 
settingsStorage.onchange = evt => {
  var data = {}
  data[evt.key] = JSON.parse(evt.newValue)
  update(data,false)
}
 
You also need something to send from your companion code to your app code
 
export function transmit(){                        // Send data to device using Messaging API
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN && ( Object.keys(e.dReq).length !== 0 ) ) {
    let keys=Object.keys(e.dReq)
    if(keys.length>0){
      let dReq={}
      dReq[keys[0]] = e.dReq[keys[0]]

//      lg("Tran",keys[0],function(T){console.log(T)})

      messaging.peerSocket.send({dR:dReq})
      delete e.dReq[keys[0]]
      clearTimeout(tmrTimeout)
      tmrTimeout = setTimeout(transmit100)
    }
  }
}

View best answer in original post

Best Answer
1 REPLY 1

@Slumberlander wrote:

that is not working, could someone show me the wright writing for this? 

thank you ! 

 


You need some code in your companion\index.js to handle when your settings page changes your settings
 
settingsStorage.onchange = evt => {
  var data = {}
  data[evt.key] = JSON.parse(evt.newValue)
  update(data,false)
}
 
You also need something to send from your companion code to your app code
 
export function transmit(){                        // Send data to device using Messaging API
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN && ( Object.keys(e.dReq).length !== 0 ) ) {
    let keys=Object.keys(e.dReq)
    if(keys.length>0){
      let dReq={}
      dReq[keys[0]] = e.dReq[keys[0]]

//      lg("Tran",keys[0],function(T){console.log(T)})

      messaging.peerSocket.send({dR:dReq})
      delete e.dReq[keys[0]]
      clearTimeout(tmrTimeout)
      tmrTimeout = setTimeout(transmit100)
    }
  }
}
Best Answer