Presenting SmartBand 2 steps on your own website

This guide will show how to fetch logged data from Sony SmartBand 2 and present it on a website. Sony has recently released a heart rate monitor called SmartBand 2. The SmartBand 2 is compatible to both Android and iOS. The SmartBand 2 app can store all activities in Sony Lifelog which provides a REST API that makes it possible to receive all logged data.

What to do

There are some steps to do in order to present the data on a website. The steps are:

  • Setup the SmartBand 2  (Buy a SmartBand 2, install the host app, create a Lifelog account and add it to the app, take a walk in order to log some steps)
  • Register for a free PHP web server account
  • Upload the Lifelog PHP library to the webserver
  • Create a Lifelog app account
  • Download and configure a Lifelog PHP library on the web server

Setup the SmartBand 2

If you’re using Android (which this guide cover), do the following:

  • Download SmartBand 2 and Lifelog
  • Open Lifelog and create an account
  • Open SmartBand 2 and pair your band with the phone
  • When the band is paired and upgraded with the latest firmware, add your Lifelog account to the SmartBand 2 app aswell
  • Configure example Lifelog

If you need more help, please read the user guide.

Register for a free PHP web server account

Now it’s time to develop a web app that can present the steps. I have only found one PHP library for Lifelog and it is Google APIs Client Library for PHP with Lifelog. It’s written in PHP so the webserver need PHP support.
Go to BYET Internet Services and register a free account. This web host provider supports HTTPS which is needed by Lifelog.

Upload the Lifelog PHP library to the webserver

Google APIs Client Library for PHP with Lifelog can be downloaded as a zip from here. Download it, extract it and upload the extracted files to your website through FTP. FileZilla is a good free FTP client.
This guide will use the example in folder /examples/lifelog. And to make it work Lifelog client account data is needed to be inserted into lifelog_config.php.

Create a Lifelog app account

Go to Lifelog API admin page and register an app. E.g.:

  • Application name: MyStepsWebApp
  • Callback URL: https://lifelog.byethost5.com/src/examples/lifelog/callback

The Client ID and Secret you got will be used in next step.

Configure example Lifelog

Open the src/Google/examples/lifelog/lifelog_config.php in a text editor and change the Client ID and Secret to the values recevied in the Lifelog admin page.

Run the example

Now run the example (in this guide https://lifelog.byethost5.com/examples/lifelog/). Click on the “Add my Lifelog account” link and follow the guide with the same account as you used in the SmartBand 2 and Lifelog-app.
After has been authenticated, you should be able to see your steps.

Add code that show SmartBand 2 steps

Pase the in code below on line 181 (just before echo “</pre>”;):



    if ($listDataSources == NULL) {
		//Fetching data sources failed above
		$listDataSources = $dataSources->listUsersDataSources("me");
	}
    echo "
Getting SmartBand 2 steps from data sources
"; //Setting filter paramters $params = array( "start_time" => "2015-11-10T09:00:00.000Z", "end_time" => "2015-11-16T09:00:00.000Z" ); $params = array(); $dataArray = $dataSources->get("me", "activities", $params); //Print array (or loop through it) $results = get_lifelog_results($dataArray); //print_r($results); $day_steps = 0; $current_day = 0; foreach( $results as $r) { if (is_source_name($r, "swr12")) { $start_time = clear_time($r['startTime']); if ($current_day == 0) { $current_day = $start_time; } $day_steps += sum_steps($r); if ($current_day != $start_time) { //New day echo "
steps: " .$day_steps; echo "\tday: " .$current_day .", " .gmdate("Y-m-d\TH:i:s\Z", $current_day);; $day_steps = 0; } $current_day = $start_time; } }

And at the end of the file just before the ?>, add the following help methods:


function clear_time($timestamp) {
	$date = new DateTime();
	$date->setTimestamp(strtotime($timestamp));
	//Clear hour, minutes and seconds
	$date->setTime(0,0,0);
	return $date->getTimestamp();
}

function get_lifelog_results($data_source) {
	if(isset($data_source) && isset($data_source['modelData']) &&
	isset($data_source['modelData']['result'])) {
		return $data_source['modelData']['result'];
	}
	return null;
}

function is_source_name($result, $name)  {
	foreach($result['sources'] as $r) {
		if (strcmp($r['name'], $name) == 0) {
		    return TRUE;
	    }
	}
	return FALSE;
}

function sum_steps($result) {
	$sum = 0;
	foreach($result['details']['steps'] as $steps) {
		$sum += $steps;
	}
	return $sum;
}

Run the example again and at the end of the page, you can now see your steps that were registered by your SmartBand 2.
Feedback and comments are welcome.

Have fun with Lifelog and your SmartBand 2!

Leave a Reply

Your email address will not be published. Required fields are marked *