├── README.md
├── config.xml
├── www
├── css
│ └── index.css
├── index.html
└── js
│ └── index.js
└── hooks
└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | # HRV-BLE-Cordova
2 |
3 | Cross platform HRV calcuation with physiological and subjective data logging to server
4 |
5 | Connects to a peripherial providing the BLE [Heart Rate Service](http://goo.gl/wKH3X7).
6 |
7 | Works with iOS or Android 4.3+.
8 |
9 | $ cordova platform add android
10 | $ cordova plugin add cordova-plugin-ble-central
11 | $ cordova run
12 |
--------------------------------------------------------------------------------
/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | HeartRate
4 |
5 | BLE Heart Rate Monitor Demo
6 |
7 |
8 | Don Coleman
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/www/css/index.css:
--------------------------------------------------------------------------------
1 | * {
2 | -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
3 | }
4 |
5 | body {
6 | -webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
7 | -webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
8 | -webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
9 | font-family:'HelveticaNeue-Light', 'HelveticaNeue', Helvetica, Arial, sans-serif;
10 | height:100%;
11 | margin:0px;
12 | padding:0px;
13 | width:100%;
14 | text-align: center;
15 | }
16 |
17 | div {
18 | padding: 20px;
19 | }
20 |
21 | h1 {
22 | font-size:3em;
23 | }
24 |
25 | h2 {
26 | font-size:2em;
27 | }
28 |
--------------------------------------------------------------------------------
/www/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | Heart Rate
10 |
11 |
12 |
13 |
BLE Heart Rate Demo
14 |
[HR]
15 |
[HRV]
16 |
17 |
18 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/www/js/index.js:
--------------------------------------------------------------------------------
1 | // (c) 2015 Don Coleman
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /* global ble, statusDiv, beatsPerMinute */
16 | /* jshint browser: true , devel: true*/
17 |
18 | // See BLE heart rate service http://goo.gl/wKH3X7
19 | var heartRate = {
20 | service: '180d',
21 | measurement: '2a37'
22 | };
23 |
24 | var intervalID;
25 |
26 | // How often to send data to the server, in ms
27 | const SYNC_INTERVAL = 2000;
28 | // The server URL
29 | const SYNC_URL = 'http://35.164.69.103:5000/heartbeats';
30 |
31 | var app = {
32 | initialize: function() {
33 | this.bindEvents();
34 | this.data = ['hello world']; // Whatever data you want to send to the server
35 | intervalID = window.setInterval(this.sendData.bind(this), SYNC_INTERVAL)
36 | },
37 | bindEvents: function() {
38 | document.addEventListener('deviceready', this.onDeviceReady, false);
39 | //document.addEventListener('deviceready', this.onDeviceReady, false);
40 | },
41 | onDeviceReady: function() {
42 | app.scan();
43 | },
44 | scan: function() {
45 | app.status("Scanning for Heart Rate Monitor");
46 |
47 | var foundHeartRateMonitor = false;
48 |
49 | function onScan(peripheral) {
50 | // this is demo code, assume there is only one heart rate monitor
51 | console.log("Found " + JSON.stringify(peripheral));
52 | foundHeartRateMonitor = true;
53 |
54 | ble.connect(peripheral.id, app.onConnect, app.onDisconnect);
55 | }
56 |
57 | function scanFailure(reason) {
58 | alert("BLE Scan Failed");
59 | }
60 |
61 | ble.scan([heartRate.service], 5, onScan, scanFailure);
62 |
63 | setTimeout(function() {
64 | if (!foundHeartRateMonitor) {
65 | app.status("Did not find a heart rate monitor.");
66 | }
67 | }, 5000);
68 | },
69 | onConnect: function(peripheral) {
70 | app.status("Connected to " + peripheral.id);
71 | ble.startNotification(peripheral.id, heartRate.service, heartRate.measurement, app.onData, app.onError);
72 | },
73 | onDisconnect: function(reason) {
74 | alert("Disconnected " + reason);
75 | beatsPerMinute.innerHTML = "...";
76 | app.status("Disconnected");
77 | },
78 | onData: function(buffer) {
79 | // assuming heart rate measurement is Uint8 format, real code should check the flags
80 | // See the characteristic specs http://goo.gl/N7S5ZS
81 | var data = new Uint8Array(buffer);
82 | beatsPerMinute.innerHTML = data[1];
83 | this.data.push(data[1])
84 | },
85 | sendData: function() {
86 | var xhr = new XMLHttpRequest();
87 | var params = 'data=' + JSON.stringify(this.data);
88 | xhr.open("POST", SYNC_URL);
89 | xhr.setRequestHeader('Content-Type', 'application/json')
90 | xhr.send(params);
91 | },
92 | onError: function(reason) {
93 | alert("There was an error " + reason);
94 | },
95 | status: function(message) {
96 | console.log(message);
97 | statusDiv.innerHTML = message;
98 | },
99 | standardDeviation: function(data) {
100 | const avg = average(data)
101 | const squareDiffs = data.map(function (value) {
102 | var diff = value - avg
103 | return diff * diff
104 | })
105 | const avgSquareDiff = average(squareDiffs)
106 | return Math.sqrt(avgSquareDiff)
107 | }
108 | };
109 |
110 | app.initialize();
111 |
--------------------------------------------------------------------------------
/hooks/README.md:
--------------------------------------------------------------------------------
1 |
21 | # Cordova Hooks
22 |
23 | Cordova Hooks represent special scripts which could be added by application and plugin developers or even by your own build system to customize cordova commands. Hook scripts could be defined by adding them to the special predefined folder (`/hooks`) or via configuration files (`config.xml` and `plugin.xml`) and run serially in the following order:
24 | * Application hooks from `/hooks`;
25 | * Application hooks from `config.xml`;
26 | * Plugin hooks from `plugins/.../plugin.xml`.
27 |
28 | __Remember__: Make your scripts executable.
29 |
30 | __Note__: `.cordova/hooks` directory is also supported for backward compatibility, but we don't recommend using it as it is deprecated.
31 |
32 | ## Supported hook types
33 | The following hook types are supported:
34 |
35 | after_build/
36 | after_compile/
37 | after_docs/
38 | after_emulate/
39 | after_platform_add/
40 | after_platform_rm/
41 | after_platform_ls/
42 | after_plugin_add/
43 | after_plugin_ls/
44 | after_plugin_rm/
45 | after_plugin_search/
46 | after_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
47 | after_prepare/
48 | after_run/
49 | after_serve/
50 | before_build/
51 | before_compile/
52 | before_docs/
53 | before_emulate/
54 | before_platform_add/
55 | before_platform_rm/
56 | before_platform_ls/
57 | before_plugin_add/
58 | before_plugin_ls/
59 | before_plugin_rm/
60 | before_plugin_search/
61 | before_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
62 | before_plugin_uninstall/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being uninstalled
63 | before_prepare/
64 | before_run/
65 | before_serve/
66 | pre_package/ <-- Windows 8 and Windows Phone only.
67 |
68 | ## Ways to define hooks
69 | ### Via '/hooks' directory
70 | To execute custom action when corresponding hook type is fired, use hook type as a name for a subfolder inside 'hooks' directory and place you script file here, for example:
71 |
72 | # script file will be automatically executed after each build
73 | hooks/after_build/after_build_custom_action.js
74 |
75 |
76 | ### Config.xml
77 |
78 | Hooks can be defined in project's `config.xml` using `` elements, for example:
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 | ...
89 |
90 |
91 |
92 |
93 |
94 |
95 | ...
96 |
97 |
98 | ### Plugin hooks (plugin.xml)
99 |
100 | As a plugin developer you can define hook scripts using `` elements in a `plugin.xml` like that:
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | ...
109 |
110 |
111 | `before_plugin_install`, `after_plugin_install`, `before_plugin_uninstall` plugin hooks will be fired exclusively for the plugin being installed/uninstalled.
112 |
113 | ## Script Interface
114 |
115 | ### Javascript
116 |
117 | If you are writing hooks in Javascript you should use the following module definition:
118 | ```javascript
119 | module.exports = function(context) {
120 | ...
121 | }
122 | ```
123 |
124 | You can make your scipts async using Q:
125 | ```javascript
126 | module.exports = function(context) {
127 | var Q = context.requireCordovaModule('q');
128 | var deferral = new Q.defer();
129 |
130 | setTimeout(function(){
131 | console.log('hook.js>> end');
132 | deferral.resolve();
133 | }, 1000);
134 |
135 | return deferral.promise;
136 | }
137 | ```
138 |
139 | `context` object contains hook type, executed script full path, hook options, command-line arguments passed to Cordova and top-level "cordova" object:
140 | ```json
141 | {
142 | "hook": "before_plugin_install",
143 | "scriptLocation": "c:\\script\\full\\path\\appBeforePluginInstall.js",
144 | "cmdLine": "The\\exact\\command\\cordova\\run\\with arguments",
145 | "opts": {
146 | "projectRoot":"C:\\path\\to\\the\\project",
147 | "cordova": {
148 | "platforms": ["wp8"],
149 | "plugins": ["com.plugin.withhooks"],
150 | "version": "0.21.7-dev"
151 | },
152 | "plugin": {
153 | "id": "com.plugin.withhooks",
154 | "pluginInfo": {
155 | ...
156 | },
157 | "platform": "wp8",
158 | "dir": "C:\\path\\to\\the\\project\\plugins\\com.plugin.withhooks"
159 | }
160 | },
161 | "cordova": {...}
162 | }
163 |
164 | ```
165 | `context.opts.plugin` object will only be passed to plugin hooks scripts.
166 |
167 | You can also require additional Cordova modules in your script using `context.requireCordovaModule` in the following way:
168 | ```javascript
169 | var Q = context.requireCordovaModule('q');
170 | ```
171 |
172 | __Note__: new module loader script interface is used for the `.js` files defined via `config.xml` or `plugin.xml` only.
173 | For compatibility reasons hook files specified via `/hooks` folders are run via Node child_process spawn, see 'Non-javascript' section below.
174 |
175 | ### Non-javascript
176 |
177 | Non-javascript scripts are run via Node child_process spawn from the project's root directory and have the root directory passes as the first argument. All other options are passed to the script using environment variables:
178 |
179 | * CORDOVA_VERSION - The version of the Cordova-CLI.
180 | * CORDOVA_PLATFORMS - Comma separated list of platforms that the command applies to (e.g.: android, ios).
181 | * CORDOVA_PLUGINS - Comma separated list of plugin IDs that the command applies to (e.g.: org.apache.cordova.file, org.apache.cordova.file-transfer)
182 | * CORDOVA_HOOK - Path to the hook that is being executed.
183 | * CORDOVA_CMDLINE - The exact command-line arguments passed to cordova (e.g.: cordova run ios --emulate)
184 |
185 | If a script returns a non-zero exit code, then the parent cordova command will be aborted.
186 |
187 | ## Writing hooks
188 |
189 | We highly recommend writting your hooks using Node.js so that they are
190 | cross-platform. Some good examples are shown here:
191 |
192 | [http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/](http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/)
193 |
194 | Also, note that even if you are working on Windows, and in case your hook scripts aren't bat files (which is recommended, if you want your scripts to work in non-Windows operating systems) Cordova CLI will expect a shebang line as the first line for it to know the interpreter it needs to use to launch the script. The shebang line should match the following example:
195 |
196 | #!/usr/bin/env [name_of_interpreter_executable]
197 |
--------------------------------------------------------------------------------