index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // (c) 2014 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 mainPage, deviceList, refreshButton */
  15. /* global detailPage, batteryState, batteryStateButton, disconnectButton */
  16. /* global ble */
  17. /* jshint browser: true , devel: true*/
  18. 'use strict';
  19. var battery = {
  20. service: "180F",
  21. level: "2A19"
  22. };
  23. var app = {
  24. initialize: function() {
  25. this.bindEvents();
  26. detailPage.hidden = true;
  27. },
  28. bindEvents: function() {
  29. document.addEventListener('deviceready', this.onDeviceReady, false);
  30. refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
  31. batteryStateButton.addEventListener('touchstart', this.readBatteryState, false);
  32. disconnectButton.addEventListener('touchstart', this.disconnect, false);
  33. deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
  34. },
  35. onDeviceReady: function() {
  36. app.refreshDeviceList();
  37. },
  38. refreshDeviceList: function() {
  39. deviceList.innerHTML = ''; // empties the list
  40. // scan for all devices
  41. ble.scan([], 5, app.onDiscoverDevice, app.onError);
  42. },
  43. onDiscoverDevice: function(device) {
  44. console.log(JSON.stringify(device));
  45. var listItem = document.createElement('li'),
  46. html = '<b>' + device.name + '</b><br/>' +
  47. 'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
  48. device.id;
  49. listItem.dataset.deviceId = device.id; // TODO
  50. listItem.innerHTML = html;
  51. deviceList.appendChild(listItem);
  52. },
  53. connect: function(e) {
  54. var deviceId = e.target.dataset.deviceId,
  55. onConnect = function() {
  56. // TODO check if we have the battery service
  57. // TODO check if the battery service can notify us
  58. //ble.startNotification(deviceId, battery.service, battery.level, app.onBatteryLevelChange, app.onError);
  59. batteryStateButton.dataset.deviceId = deviceId;
  60. disconnectButton.dataset.deviceId = deviceId;
  61. app.showDetailPage();
  62. };
  63. ble.connect(deviceId, onConnect, app.onError);
  64. },
  65. onBatteryLevelChange: function(data) {
  66. console.log(data);
  67. var message;
  68. var a = new Uint8Array(data);
  69. batteryState.innerHTML = a[0];
  70. },
  71. readBatteryState: function(event) {
  72. console.log("readBatteryState");
  73. var deviceId = event.target.dataset.deviceId;
  74. ble.read(deviceId, battery.service, battery.level, app.onReadBatteryLevel, app.onError);
  75. },
  76. onReadBatteryLevel: function(data) {
  77. console.log(data);
  78. var message;
  79. var a = new Uint8Array(data);
  80. batteryState.innerHTML = a[0];
  81. },
  82. disconnect: function(event) {
  83. var deviceId = event.target.dataset.deviceId;
  84. ble.disconnect(deviceId, app.showMainPage, app.onError);
  85. },
  86. showMainPage: function() {
  87. mainPage.hidden = false;
  88. detailPage.hidden = true;
  89. },
  90. showDetailPage: function() {
  91. mainPage.hidden = true;
  92. detailPage.hidden = false;
  93. },
  94. onError: function(reason) {
  95. alert("ERROR: " + reason); // real apps should use notification.alert
  96. }
  97. };