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

Get Real time HR and calorie count in my Android app

ANSWERED

I want to get the real time Heart Rate on my android app. As per my understanding this is what is needed. 

1. Develop a fitbit device app and make use of Excercise API (since I am making a workout app)

2. Develop a companion app 

3. Establish commication between DeviceApp with companion app.

4. Make Android app as a websocket and get data from Companion app to the Android app. 

Is my understanding correct? 

Also, this will work in Versa, Ionic and sense? 

 

Can I get an example of this entire implementation anywhere? It would be of great help. 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Yes, that will work. Since heart-rate data isn't too frequent and you won't need bidirectional communication, you could get away with fetch() instead of websocket, but websocket is probably better.

Presumably the Fitbit companion code (running in Fitbit app) will be on the same device as your Android app. That will mean you can get away with http (ws) rather than https (wss).

Yes, it will work for Versa, Ionic, Sense.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
0 Votes
13 REPLIES 13

Yes, that will work. Since heart-rate data isn't too frequent and you won't need bidirectional communication, you could get away with fetch() instead of websocket, but websocket is probably better.

Presumably the Fitbit companion code (running in Fitbit app) will be on the same device as your Android app. That will mean you can get away with http (ws) rather than https (wss).

Yes, it will work for Versa, Ionic, Sense.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Thanks for your reply @Gondwana ! 

Yes, Fitbit companion code and Android app will be on the same device. 

Can I get to a bit more explanation about fetch() and websocket? Both are the part of Companion api, so the websocket would be on the Android app. What can be the URL in order to setup the socket connection? Or else how can I make use of fetch(), can you point me to samples of examples? If possible. 

Best Answer
0 Votes

I don't know of any Fitbit-specific examples.

For a websocket, I used ws://127.0.0.1:8080. Obviously what will work for you depends on your setup. Note that there's only a small number of URLs that work without wss.

For fetch(), I used http://127.0.0.1:3000.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Thanks for your reply again @Gondwana ! 

Can you clear my last doubt? Should the code base for Fitbit device (Fitbit app) and companion app different or it should be included in the same code base? I have created a device app with HR, I am not sure how does the companion app works with the communication part with the fitbit device. 

Best Answer
0 Votes

I don't understand the question. Companion code and clockface code goes in one project.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

That's what I wanted to know. 
@Gondwana This is what I am doing now. 

1. I have a fitbit, companion and setting app developed and running on fitbit OS Simulator. 

2. I have a websocket running on Android Simulator on "http://localhost:38302/"

This is what my code looks like on Fitbit Companion app. 

const HOST = "http://localhost:38301/"

export function ServerAPI() { };

function sendToServer(uri, data) {
    console.log("Sending to server : ",data)
    return new Promise((resolve, reject) => {
        fetch(uri, {
            method: "POST",
            body: JSON.stringify(data)
        })
        .then(res => res.json())
        .then(data =>  resolve(data))
        .catch(err => reject (err))
    })
}

function getFromServer(uri) {
      return new Promise((resolve, reject) => {
        fetch(uri, {
            method: "GET"
        })
        .then(res => res.json())
        .then(res => resolve(data))
        .catch(err => reject (err))
      })
}

ServerAPI.prototype.sendHeartRateData = data => {
  console.log("Data in sendHeartRateData : ", data);
  console.log("HOST in sendHeartRateData : ", HOST);
  sendToServer(HOST, data)
        .then(res => console.log('Successful posted!', JSON.stringify(res)))
        .catch(err => console.log('Error at post a heartrate data', err.message))
}

in console log I get this "Error at post a heartrate data Failed to fetch"

I am only concerned about sendToServer and getting the correct json in the

console.log("Sending to server : ",data)

the data is 

{ timestamp: '2020-10-07T04:10:24.964Z', heartRate: 106 }

 

What am I missing here? 

Best Answer
0 Votes

Dunno. Instead of 'localhost', try using one of allowed IPs.

I'm also not sure that fetch() needs to be wrapped in a new Promise, since it already returns a promise. But that shouldn't be a deal-breaker.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Ok. I have also tried on 10.0.0.140:38301 it was still the same. 

I also want to know how can I install the fitbit app and companion app on the a real physical device, in that case both companion app and my Android app would be running on the same device. 

Can you point me to a resource where I can find about this? @Gondwana 

Best Answer
0 Votes

That's not one of the allowed IPs. Unless you can use https, you'll have to stick with one of these. There was talk about broadening the list, but I don't think that's happened yet.

Therefore, you'll almost certainly have to run the companion app on the same device as your Android app (which presumably means your phone). Have you tried that?

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@Gondwana  Thanks for pointing that out. 

Best Answer
0 Votes

Hmm, the url is the address of the host that is running the emulation so that port is used by the debugger. You need to use the port number of the websocket in your android app. 

Best Answer

Well, if you have solved this problem in the mean time, I would really appreciate that you share some code.

 

I am a (butoh) dancer, and I would like to have an Open Source Flutter app that shares live FitBit data in OSC format (Open Sound Control) which makes it possible to control synthesizers, light effects, etc, etc with your heartbeat,  steps, acceleration, gyroscope, and more

 

so [FitBit]--->[Flutter App]---> Open Sound Control --->>

                                * Processing,

                                * Open-CV,

                                * Pure Data,

                                * Interactive Artworks,

                                * Synths,

                                * Super Collider,

                                * Lights,

                                * ...

 

It will be a great toy for the creative community, any advice and help is appreciated creating this...

Best Answer
0 Votes

Do you have sample code for android app to receive this data? I am looking exactly for something similar to this and the watch is sending this data to firebase. Now, I want to make a server on android to receive this data. Let me know if you have any sample code for android.

Best Answer
0 Votes