03-25-2022 20:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-25-2022 20:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Answered! Go to the Best Answer.

Accepted Solutions
03-25-2022 22:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-25-2022 22:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
Gondwana Software
03-25-2022 21:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-25-2022 21:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
That should work. The problem may be elsewhere. Use console.log(...) to trace the flow of execution.
Gondwana Software

03-25-2022 21:53 - edited 03-26-2022 09:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-25-2022 21:53 - edited 03-26-2022 09:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I 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. 🙂

03-25-2022 22:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-25-2022 22:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
Gondwana Software
03-25-2022 23:02
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-25-2022 23:02
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Herewith is my untested submission for bonus points:
btn.getElementById('text').style.fill = txtColor;
Gondwana Software
03-25-2022 23:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-25-2022 23:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
Gondwana Software
03-26-2022 13:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-26-2022 13:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
