07-03-2020 02:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-03-2020 02:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

07-03-2020 02:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


07-03-2020 02:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
It could just be the Fitbit app (companion) being closed by the operating system. You could try the "run_background" permission, but nothing can overpower the operating system.
Gondwana Software
07-03-2020 04:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-03-2020 04:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thanks 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.

07-03-2020 14:11 - edited 07-03-2020 14:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


07-03-2020 14:11 - edited 07-03-2020 14:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Good! 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.
Gondwana Software
07-03-2020 23:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-03-2020 23:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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
07-04-2020 08:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-04-2020 08:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

07-06-2020 00:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-06-2020 00:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Thank you for the hint!
I'll look into it.
10-24-2020 23:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-24-2020 23:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi morningReign.
Do you have an example of this communication you can share or point to an example?
Thanks.

