index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /* global ble, statusDiv, beatsPerMinute */
  15. /* jshint browser: true , devel: true*/
  16. // See BLE heart rate service http://goo.gl/wKH3X7
  17. var heartRate = {
  18. service: "180d",
  19. measurement: "2a37"
  20. };
  21. function stringToBytes(string) {
  22. var array = new Uint8Array(string.length);
  23. for (var i = 0, l = string.length; i < l; i++) {
  24. array[i] = string.charCodeAt(i);
  25. }
  26. return array.buffer;
  27. }
  28. var app = {
  29. initialize: function() {
  30. this.bindEvents();
  31. },
  32. bindEvents: function() {
  33. document.addEventListener("deviceready", this.onDeviceReady, false);
  34. },
  35. onDeviceReady: function() {
  36. app.scan();
  37. },
  38. scan: function() {
  39. app.status("Scanning for Heart Rate Monitor");
  40. var found = false;
  41. function onScan(device) {
  42. // this is demo code, assume there is only one heart rate monitor
  43. console.log("Found " + JSON.stringify(device));
  44. found = true;
  45. if (device.name == "wifitest") {
  46. ble.connect(device.id, app.onConnect, app.onDisconnect);
  47. }
  48. }
  49. function scanFailure(reason) {
  50. alert("BLE Scan Failed");
  51. }
  52. ble.scan([], 5, onScan, scanFailure);
  53. setTimeout(function() {
  54. if (!found) {
  55. app.status("Did not find a wifi config service.");
  56. }
  57. }, 5000);
  58. },
  59. onConnect: function(peripheral) {
  60. app.status("Connected to " + peripheral.id);
  61. var data = stringToBytes("test");
  62. ble.write(
  63. peripheral.id,
  64. "ffffffff-ffff-ffff-ffff-fffffffffff0",
  65. "ffffffff-ffff-ffff-ffff-fffffffffff1",
  66. data,
  67. function() {
  68. console.log("success");
  69. },
  70. function(error) {
  71. console.log(error);
  72. }
  73. );
  74. },
  75. onDisconnect: function(reason) {
  76. alert("Disconnected " + reason);
  77. beatsPerMinute.innerHTML = "...";
  78. app.status("Disconnected");
  79. },
  80. onData: function(buffer) {
  81. // assuming heart rate measurement is Uint8 format, real code should check the flags
  82. // See the characteristic specs http://goo.gl/N7S5ZS
  83. var data = new Uint8Array(buffer);
  84. beatsPerMinute.innerHTML = data[1];
  85. },
  86. onError: function(reason) {
  87. alert("There was an error " + reason);
  88. },
  89. status: function(message) {
  90. console.log(message);
  91. statusDiv.innerHTML = message;
  92. }
  93. };
  94. app.initialize();