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

File save question

ANSWERED

How do I save information to the watch for use when the companion app is not available?

 

When my clock face is activated, it requires user settings.  If the companion device is nearby, it can retrieve the user's preferences.  However, if the companion is not nearby, the default settings are used.  How do I get the user's preferences to remain?

 

Scenario example:  You set the clock face to match your settings using the companion.  You leave your phone behind to go out to mow the lawn.  You leave the clock face to check your steps.  When you return to the clock face, the user settings are no longer available.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Is see something that looks like it could be a problem...

When you are saving the file 

fs.writeFileSync("mySettings.json",newInfo,'json');

Notice the quotes around the file name "mySettings.json" 

 oldInfo = JSON.parse(fs.readFileSync(mySettings.json,'json'));

But when you are reading the file you don't have quotes around mySettings.json so the code is looking for an object called mySettings with a property named json and not a file named "mySettings.json"

 

You have the read without the quotes in both updateInfo() and restoreSettings() functions.

View best answer in original post

Best Answer
0 Votes
7 REPLIES 7

Use the device API to save settings on the watch. There's a thread on this forum somewhere that goes into details.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

It's not really that hard but there are a few pieces to it.  

Basically you using the messaging API to get the values from the companion app and use the fs API to write and read the files.

 

Example code is always better than a description Smiley Happy...

I have a project in github (its an app not a clockface but I think it'd work the same) that saves and loads the setting from a cache https://github.com/grumpy-coders/pointy

 

In https://github.com/grumpy-coders/pointy/blob/master/app/index.js it calls a to setup a customer settings manager.

 

In https://github.com/grumpy-coders/pointy/blob/master/app/settings-manager.js I use messaging to get the settings then write them to json files.

 

Then when I need the values I read it from the cached json files and not the companion app.

https://github.com/grumpy-coders/pointy/blob/master/app/classes/GameState.js

 

I'm doing this with the players.json and a couple of other settings.

 

 

Best Answer
0 Votes

Thank you for the response and the examples.  I agree...they are the best way to learn.  However, they use read and writeFileSync.  The problem is that the phone still needs to be within range of the tracker when the app is activated for the files to be read. 

 

Any other ideas?

 

Thanks again.

Best Answer
0 Votes

That incorrect; I'm using the read / write file in my app an it reads the file just fine without the phone being in range.

 

In the case of my app the files are only written when the companion app sends a message to the app.  But I have tested just writing and reloading files without the phone in range and it works fine.

 

If you want to give it a try here is the private link to my app https://gam.fitbit.com/gallery/app/6d2d7990-08c9-4b06-992f-8667604edf58

 

You will need to setup players in the companion app once with the watch in range.  Then after that you can you the watch app with out a phone.

 

I would make another suggest create a simple app or watch face that all it does is write a file with data from the companion.  Then opens the file and shows the data.   Then launch the app with the phone out of range.

Best Answer
0 Votes

Sorry for the long delay.  So, I created a very simple clock app that accepts a color from settings and writes it, color coded to match.  When it is attached to my phone, it works fine.  However, when I put my phone in airplane mode, leave the clock face, then go back to my clock face, I get a white "undefined."

 

Here is the app code... (the companion, settings and resources are almost cut and pastes from the sample).  I can add the files if you want them.

 

app/index.js

 

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

let status = document.getElementById("status");

let newInfo;

messaging.peerSocket.onmessage = function (evt) {
  newInfo = evt.data.newValue;
  fs.writeFileSync("mySettings.json",newInfo,'json');
  updateInfo(newInfo);
  console.log("Settings file written");
}

messaging.peerSocket.onopen = function (evt) {
  console.log("App socket open");
} 

messaging.peerSocket.onclose = function (evt) {
  console.log("App socket closed");
}

function updateInfo(newInfo) {
  let myNewInfo = JSON.parse(newInfo);
  let oldInfo;
  
  try {
    oldInfo = JSON.parse(fs.readFileSync(mySettings.json,'json'));
  } catch (error) {
    console.log("No pre-sets");
status.style.fill = "blue"; status.text = "blue"; } status.text = myNewInfo; status.style.fill = myNewInfo; console.log(`myNewInfo: <${myNewInfo}>`); } function restoreSettings() { let oldInfo; try { oldInfo = JSON.parse(fs.readFileSync(mySettings.json,'json')); } catch (error) { console.log("No pre-sets");
status.style.fill = "blue"; status.text = "blue"; } status.text = oldInfo; status.style.fill = oldInfo; console.log(`myNewInfo: ${oldInfo}`); } restoreSettings();

Please tell me I am missing something basic here.

 

Thanks,

Jeff

 

Best Answer
0 Votes

Is see something that looks like it could be a problem...

When you are saving the file 

fs.writeFileSync("mySettings.json",newInfo,'json');

Notice the quotes around the file name "mySettings.json" 

 oldInfo = JSON.parse(fs.readFileSync(mySettings.json,'json'));

But when you are reading the file you don't have quotes around mySettings.json so the code is looking for an object called mySettings with a property named json and not a file named "mySettings.json"

 

You have the read without the quotes in both updateInfo() and restoreSettings() functions.

Best Answer
0 Votes

Thanks!  That fixed my problem.  I will now take this file and try and expand it to my main app (where the reading and writing is not working).

 

 

Best Answer
0 Votes