Fitbit Investigation

2019-02-27  本文已影响0人  Richard_Wei

工作原理

Communications

In the communication guide we demonstrate how application developers can utilize the Device API on the Fitbit device, and Companion API on the mobile device, to send and receive data, and transfer files.

Communications Guide

目录结构
fitbit studio 目录

A new application typically begins with the following folder structure:
/app/
/common/
/companion/
/resources/
/settings/
This folder structure allows the build process to correctly compile and bundle the
application for installation on the device, and build the companion and settings
for installation on the mobile device (if required).
The /common/, /companion/, and /settings/ folders are optional.

Application Architecture Guide

测试小结
1. app 目录下的js文件仅能处理watch中的事务,但并不能执行网络请求,仅运行在watch端
2. companion 目录下的js文件可以进行网络请求,仅能运行在Fitbit手机端
3. Fitbit APP 由于iOS系统限制 不能长时间在后台运行 大概5-10分钟
4. 由于后台进程无法长存,导致手机端断开连接,和watch的通信无法进行,从而请求无法发送

结论:Fitbit对iOS的支持达不到实时获取心率的要求

测试代码:

app -> index.js

import document from "document";
import { inbox } from "file-transfer";
import fs from "fs";
import { peerSocket } from "messaging";
import * as messaging from "messaging";

function sayHello(){
  
  console.log("Peer Socket=" + peerSocket.readyState + "    " + peerSocket.OPEN);
  
  console.log("Max message size=" + peerSocket.MAX_MESSAGE_SIZE);
  if (peerSocket.readyState === peerSocket.OPEN) {
    
     peerSocket.send("Hello");
  }
}

messaging.peerSocket.onerror = function(err) {
  console.log(`Device ERROR: ${err.code} ${err.message}`);
}

let statusText = document.getElementById("status");
statusText.text = "Waiting...";

// Event occurs when new file(s) are received
inbox.onnewfile = () => {
  console.log("New file!");
  let fileName;
  do {
    // If there is a file, move it from staging into the application folder
    fileName = inbox.nextFile();
    if (fileName) {
      console.log(`Received File: <${fileName}>`);
      let data = fs.readFileSync(fileName, "ascii");
      statusText.text = `Received: ${data}`;
    }
  } while (fileName);
};

setInterval(sayHello, 5 * 1000);

companion -> index.js

import { outbox } from "file-transfer";

import * as messaging from "messaging";

// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
  // Output the message to the console
  console.log(JSON.stringify(evt.data));
  fetch("https://api-staging.fitonapp.com/api/v1/util/show_promo", { mode: 'cors',method: 'GET' }).then(function(response) {
    return response.json();
  }).then(function(text) {
    console.log("Got JSON response from server: " + JSON.stringify(text));
  }).catch(function(error){
    console.log('Got error: ' + error)
  });
}

messaging.peerSocket.onerror = function(err) {
  console.log(`Companion ERROR: ${err.code} ${err.message}`);
}

function sendFile() {
  
  console.log("Peer Socket=" + peerSocket.readyState + "    " + peerSocket.OPEN);
  
  console.log("Sending file...");
  let data = new Uint8Array(26);
  for (let counter = 0; counter < data.length; counter++) {
    data[counter] = "a".charCodeAt(0) + counter;
  }
  outbox.enqueue("alphabits.txt", data);
}

setTimeout(sendFile, 2000);

上一篇下一篇

猜你喜欢

热点阅读