I'm working on an update for my app "Save Location" where I move the gelocation part from the watch app to a companion and send the location info to an API to display further information on the watch.
This works fine while in Fitbit Emulator and on my Versa 2 connected to my (Android 10) Phone as long as the Fitbit app is actively opened. When Fitbit app runs in background it only works in 1 out of 10 cases.
Is there something I can to about this in my code or is the connection from my watch to the Fitbit app just horribly unstable?
I followed the messaging guide:
Watch app:
// Listen for the onopen event
messaging.peerSocket.onopen = function() {
console.log("messaging open");
// Fetch location when the connection opens
fetchLocation();
}
// Listen for messages from the companion
messaging.peerSocket.onmessage = function(evt) {
console.log("message received from phone");
if (evt.data) {
processLocationData(evt.data);
}
}
// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
console.log("messaging peer error");
// Handle any errors
saveButton.style.display="none";
showButton.style.display="inline";
backButton.style.display="inline";
w3w.text="";
location.text="";
latitude.text="Error"+err.code;
longitude.text=err.message;
accuracy.text="Error";
}
Companion app:
function returnLocationData(data){
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
console.log("return data to device");
// Send a command to the device
messaging.peerSocket.send(data);
} else {
console.log("Error: Connection is not open");
}
}
// Listen for messages from the device
messaging.peerSocket.onmessage = function(evt) {
if (evt.data && evt.data.command == "location") {
console.log("fetchLocation message received");
// The device requested weather data
getLocation();
}
}
// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
// Handle any errors
console.log("messaging error");
function locationError(error)
}
I'd be thankful for any hint.
Best AnswerThanks for your suggestion. The "run_background" permission didn't solve the issue so I ended up reinstalling the Fitbit app on my phone and now it works much better.
Best AnswerGood! My suggestion was very much a long-shot.
Bluetooth performance can also be reduced when the Fitbit app is not running in the foreground (ie, it might be slower). That could be a problem if you were trying to pump lots of data through, but I doubt that you're anywhere close to the limit.
The communications aren't 'horribly unstable' as much as they are asynchronous, and you do need to account for that.
You have a good starting point where you send on connection of the peer socket, but communications work best when you mark information as dirty when changed, and continue to attempt to send on reconnects (and periodically when connected), until you receive a receipt back from the other side.
My code sends back a message with the name of the telegram sent, and upon receipt, the outgoing telegram is marked as no longer 'dirty'
Regards,
Reign
Another thing I am finding is that adding permissions after you install an app do not seem to work. When you reinstalled the ftibit app, you may have reset the permissions, and if you changed nothing else, that may have been a viable fix, it just was not implemented until you reset the permissions.
Best AnswerHi morningReign.
Do you have an example of this communication you can share or point to an example?
Thanks.
Best Answer