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

BodyPresenceSensor,present backs "null"

ANSWERED

I'd like to use body presence with BodyPresenceSensor. But I got "null" from "bodyPresence.present". (I just followed official procedure blow.)

How to solve this problem?

 

Code:

 

import { BodyPresenceSensor } from "body-presence";

if (appbit.permissions.granted("access_activity")) {
  if (BodyPresenceSensor) {
    console.log("This device has a BodyPresenceSensor!");
    const bodyPresence = new BodyPresenceSensor();
    console.log(bodyPresence.present);
    bodyPresence.addEventListener("reading", () => {
      console.log(`The device is ${bodyPresenceSensor.present ? '' : 'not'} on the user's body.`);
    });
    bodyPresence.start();
  } else {
    console.log("This device does NOT have a BodyPresenceSensor!");
  }
}

 

 

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Sorry, I meant the sensor itself. bodyPresence.activated is false initially; until it's activated, .present seems to return null.

Use onactivate if you need a reading ASAP.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
0 Votes
7 REPLIES 7

This is terminal output when above code was excuted.

 

fitbit$ bi

> my-first-clock@0.1.0 build C:\Users\xxxx\Documents\fitbit_project\my-first-clock
> fitbit-build

[17:44:37][warn][app] "HeartRateSensor" is imported from external module "heart-rate" but never used in "app/activity.js".
[17:44:37][info][app] Building app for Fitbit Versa 3
[17:44:37][info][app] Building app for Fitbit Sense
[17:44:37][info][companion] Building companion
[17:44:38][info][settings] Building settings
[17:44:38][info][build] App UUID: 043ab4b3-11111-b1e6-bf02c7cc2770, BuildID: 0x0012e94582372848
No app package specified. Reloading ./build/app.fba.
Loaded appID:043ab4b3-11111-b1e6-bf02c7cc2770 buildID:0x0012e94582372848
Sideloading app: 100% completed
App install complete (partial)
Sideloading companion: starting...
[17:44:39]       App: Error 22 Invalid attribute 'data-size' in animate-in                    (mnt/sysassets/widgets_common.gui:301,1)
[17:44:39]       App: App Started
Companion install complete (full)
Launching app
[17:44:40]       App: App Closed
[17:44:41]       App: App Started
[17:44:41]       App: This device has a BodyPresenceSensor!                                                    (app/activity.js:144,7)
[17:44:41]       App: null                                                                                     (app/activity.js:146,7)
[17:44:41] Companion: Hello world!                                                                            (companion/index.js:1,1)
Device 'Versa 3 Simulator' disconnected
Phone 'Simulator' disconnected
fitbit$

 

Best Answer
0 Votes

Maybe you need to wait for the sensor to be activated.

(I also suspect that bodyPresenceSensor will cause problems, despite the documentation.)

Peter McLennan
Gondwana Software
Best Answer
0 Votes

When development with fitbit OS simulator, should I wait untill it is activated? Is it activated by excuing "npx fitbit" and just waiting?

Best Answer
0 Votes

Sorry, I meant the sensor itself. bodyPresence.activated is false initially; until it's activated, .present seems to return null.

Use onactivate if you need a reading ASAP.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Thank you for giving useful information.

I've tried to use onactivate property and checked the value of "bodyPresence.onactivate". But it returns "undefined" as below. 

Did I any mistakes? I'm very happy for you to send sample code (js) to get value of "onactivate" (without "undefined").

 

Code:

 

  if (appbit.permissions.granted("access_activity")) {
    if (BodyPresenceSensor) {
      console.log("This device has a BodyPresenceSensor!");
      const bodyPresence = new BodyPresenceSensor();

      setInterval(() => {
        console.log(bodyPresence.onactivate);
      }, 2000);

      
      // console.log(bodyPresence.present);
      bodyPresence.addEventListener("reading", () => {
        console.log(`The device is ${bodyPresenceSensor.present ? '' : 'not'} on the user's body.`);
      });
      bodyPresence.start();
    } else {
      console.log("This device does NOT have a BodyPresenceSensor!");
    }
  }

 

 

Terminal output:

 

> fitbit-build

[10:04:39][warn][app] "HeartRateSensor" is imported from external module "heart-rate" but never used in "app/activity.js".
[10:04:39][info][app] Building app for Fitbit Versa 3
[10:04:40][info][app] Building app for Fitbit Sense
[10:04:40][info][companion] Building companion
[10:04:40][info][settings] Building settings
[10:04:40][info][build] App UUID: 043ab4b3-xxxxx-b1e6-bf02c7cc2770, BuildID: 0x0ae5dc126e9d814d
No app package specified. Using default ./build/app.fba.
Loaded appID:043ab4b3-xxxxx-b1e6-bf02c7cc2770 buildID:0x0ae5dc126e9d814d
App requires a device, connecting...
Auto-connecting only known device: Versa 3 Simulator
App requires a phone, connecting...
Auto-connecting only known phone: Simulator
[10:04:42]       App: undefined                                                                               (app/activity.js:154,20)
Sideloading app: 100% completed
[10:04:44]       App: undefined                                                                               (app/activity.js:154,20)
App install complete (partial)
Sideloading companion: starting...
[10:04:45]       App: Error 22 Invalid attribute 'data-size' in animate-in                    (mnt/sysassets/widgets_common.gui:301,1)
[10:04:45]       App: App Started
Companion install complete (full)
Launching app
[10:04:46]       App: App Closed
[10:04:46]       App: App Started
[10:04:46]       App: This device has a BodyPresenceSensor!                                                    (app/activity.js:145,7)
[10:04:47] Companion: Hello world!                                                                            (companion/index.js:1,1)
[10:04:48]       App: undefined                                                                                (app/activity.js:156,9)
[10:04:50]       App: undefined                                                                                (app/activity.js:156,9)
[10:04:52]       App: undefined                                                                                (app/activity.js:156,9)
[10:04:54]       App: undefined                                                                                (app/activity.js:156,9)
[10:04:56]       App: undefined                                                                                (app/activity.js:156,9)
[10:04:58]       App: undefined                                                                                (app/activity.js:156,9)
[10:05:00]       App: undefined                                                                                (app/activity.js:156,9)
[10:05:02]       App: undefined                                                                                (app/activity.js:156,9)
[10:05:04]       App: undefined                                                                                (app/activity.js:156,9)
(^C again to quit)
fitbit$

 

 

Best Answer
0 Votes

onactivate is a property to which you can assign a listener; ie, a function that you write. The system will then call your function when the sensor is activated. You don't need setInterval, and reading the value of onactivate simply returns the currently-assigned listener function (ie, undefined because you haven't assigned a listener).

Peter McLennan
Gondwana Software
Best Answer

I see.

I didn't do well on fitbit OS Simulator, but was able to deployed on real device with following doc.

Thank you for helping me!

 

Official doc:

https://dev.fitbit.com/build/guides/sensors/heart-rate/ 

Best Answer