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

App startup function call

ANSWERED

Hey all I've been looking around but I have not seen an example of something that tells me when the app has launched and is loaded. I am needing to call a function when it opens in order to populated some buttons. Right now I have an

function init() {
.....
}

and I am calling that function at the very end of the JavaScript like

init();

But it never fires off at the start of the app loaded.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Does "init" not appear in the console output? If not, bung a console.log immediately before the init() function call to verify that execution is getting that far.

I suspect that the peersocket may not open right away (it might need to wait until the companion lets the watch know it's alive). If so, you might want to send the initial message from onopen, but if "Watch is listening" isn't appearing, there's some other problem. You'd also need to guard against onopen happening more than once while the app is running, which can happen if the bluetooth connection comes and goes or if the companion gets bored and nods off.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
6 REPLIES 6

That should work. The problem may be elsewhere. Use console.log(...) to trace the flow of execution.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

have my get and post working just fine. The watch and phone talk to each other as they should. Just can’t seem to get it to do a get when it first starts the

 

import * as messaging from "messaging";
import document from "document";
import { vibration } from "haptics";

const btn = document.getElementById("btn");

function init() {
  //Call to get current garage status (up or down)
  //setBtnNameAndColor("Open Garage", "green", "");
  console.log("init");
  
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    console.log("sent request for status");
    messaging.peerSocket.send({status: "garage"});
  }
  
  messaging.peerSocket.send({"": ""});
}

btn.addEventListener("click", () => {
  console.log("clicked");
  //setBtnNameAndColor("You clicked me", "red", "");
  console.log("done1");
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    console.log("hi");
    messaging.peerSocket.send({status: "garage"});
  }
  console.log("done2");
});

// Listen for the onopen event
messaging.peerSocket.onopen = function() {
    console.log("Watch is listening");
    //messaging.peerSocket.send("This is sent from the watch");
}
  
// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
    console.log("Data: " + evt.data.value);
  if (evt.data.value == "close") {
    setBtnNameAndColor("Open Garage", "green", "");
  } else if (evt.data.value == "open")  {
     setBtnNameAndColor("Close Garage", "yellow", "");
  }
}

// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
    console.log("ERROR: " + err);
}

function setBtnNameAndColor(name, color, txtcolor) {
  btn.text = name;
  btn.style.fill = color;
  //btnC.style.fill = txtcolor;
}

init();

 

bonus points if you can tell me how to change the font color on a button in code. 🙂

 

Best Answer
0 Votes

Does "init" not appear in the console output? If not, bung a console.log immediately before the init() function call to verify that execution is getting that far.

I suspect that the peersocket may not open right away (it might need to wait until the companion lets the watch know it's alive). If so, you might want to send the initial message from onopen, but if "Watch is listening" isn't appearing, there's some other problem. You'd also need to guard against onopen happening more than once while the app is running, which can happen if the bluetooth connection comes and goes or if the companion gets bored and nods off.

Peter McLennan
Gondwana Software
Best Answer

Herewith is my untested submission for bonus points:

btn.getElementById('text').style.fill = txtColor;
Peter McLennan
Gondwana Software
Best Answer

After you get messaging working, you might want to consider switching to file transfer. While messaging should be better suited to your project (because small messages), file transfer tends to be more reliable.

Peter McLennan
Gondwana Software
Best Answer

it would seem that I needed to close both the simulator and the web page that I had the studio in and restart them both. Once I ran it after that it works sooo ???


You got the bonus points. 

Best Answer