top of page

HoloLens×Garmin vivosmart4で心拍数を表示する|yamasakikakeruのブログ

Garmin vivosmart4というスマートウォッチを使って心拍数を取得し,それをnode.jsというものを使ってPCに取り込み,さらにUnityに送ることによって実現しました.



vivosmart4がこちら

基本的には有料でする人が多いですが,なんとか自分でコードを書いて実装しました.

下にコードも載せておきます.


node.js側

wsとant-plusをnpmでinstallしてください


const Ant = require('ant-plus');

const stick = new Ant.GarminStick2;

const sensor = new Ant.HeartRateSensor(stick);

var deviceID = 17131;

var ws = require('ws').Server;

var wss = new ws({ port: 8080 });


if (process.argv.length >= 3) {

deviceID = process.argv[2];

}

sensor.on('hbData', function (data) {

if (deviceID == null) {

console.log(data.DeviceID, data.ComputedHeartRate);

} else if (deviceID == data.DeviceID) {

console.log(data.ComputedHeartRate);

}

});


stick.on('startup', function () {

sensor.attach(0, 0);

});


if (!stick.open()) {

console.log('Stick not found!');

}



wss.on('connection', function (ws) {

//ws.on('message', function (message) {

sensor.on('hbData', function (data) {

wss.clients.forEach(function (client) {

//var now = new Date();

//console.log(now.toLocaleString() + ' Received: %s', message);


//if (deviceID == null) {

// console.log(data.DeviceID, data.ComputedHeartRate);

//} else if (deviceID == data.DeviceID) {

// client.send(data.ComputedHeartRate);

//}

client.send(data.ComputedHeartRate);

});

// });

});

});



Unity側

Websocket-sharpを使ってください

using UnityEngine;

using WebSocketSharp;

using UnityEngine.UI;


public class WSscript : MonoBehaviour

{

private WebSocket ws;

private string stackText;

private Text targetText;

void Start()

{

var url = "ws://localhost:8080";

ws = new WebSocket(url);

ws.Connect();

}

void Update()

{

ws.OnMessage += (sender, e) =>

{

stackText = e.Data;

};

this.targetText = this.GetComponent<Text>();

this.targetText.text = stackText.ToString();

}

}



閲覧数:455回0件のコメント
bottom of page