index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, resultDiv, messageInput, sendButton, disconnectButton */
  16. /* global ble, cordova */
  17. /* jshint browser: true , devel: true*/
  18. 'use strict';
  19. // ASCII only
  20. function bytesToString(buffer) {
  21. return String.fromCharCode.apply(null, new Uint8Array(buffer));
  22. }
  23. // ASCII only
  24. function stringToBytes(string) {
  25. var array = new Uint8Array(string.length);
  26. for (var i = 0, l = string.length; i < l; i++) {
  27. array[i] = string.charCodeAt(i);
  28. }
  29. return array.buffer;
  30. }
  31. // this is RedBear Lab's UART service
  32. var redbear = {
  33. serviceUUID: "713D0000-503E-4C75-BA94-3148F18D941E",
  34. txCharacteristic: "713D0003-503E-4C75-BA94-3148F18D941E", // transmit is from the phone's perspective
  35. rxCharacteristic: "713D0002-503E-4C75-BA94-3148F18D941E" // receive is from the phone's perspective
  36. };
  37. var app = {
  38. initialize: function() {
  39. this.bindEvents();
  40. detailPage.hidden = true;
  41. },
  42. bindEvents: function() {
  43. document.addEventListener('deviceready', this.onDeviceReady, false);
  44. refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
  45. sendButton.addEventListener('click', this.sendData, false);
  46. disconnectButton.addEventListener('touchstart', this.disconnect, false);
  47. deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
  48. },
  49. onDeviceReady: function() {
  50. app.refreshDeviceList();
  51. },
  52. refreshDeviceList: function() {
  53. deviceList.innerHTML = ''; // empties the list
  54. if (cordova.platformId === 'android') { // Android filtering is broken
  55. ble.scan([], 5, app.onDiscoverDevice, app.onError);
  56. } else {
  57. ble.scan([redbear.serviceUUID], 5, app.onDiscoverDevice, app.onError);
  58. }
  59. },
  60. onDiscoverDevice: function(device) {
  61. var listItem = document.createElement('li'),
  62. html = '<b>' + device.name + '</b><br/>' +
  63. 'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
  64. device.id;
  65. listItem.dataset.deviceId = device.id;
  66. listItem.innerHTML = html;
  67. deviceList.appendChild(listItem);
  68. },
  69. connect: function(e) {
  70. var deviceId = e.target.dataset.deviceId,
  71. onConnect = function() {
  72. // subscribe for incoming data
  73. ble.startNotification(deviceId, redbear.serviceUUID, redbear.rxCharacteristic, app.onData, app.onError);
  74. sendButton.dataset.deviceId = deviceId;
  75. disconnectButton.dataset.deviceId = deviceId;
  76. app.showDetailPage();
  77. };
  78. ble.connect(deviceId, onConnect, app.onError);
  79. },
  80. onData: function(data) { // data received from Arduino
  81. console.log(data);
  82. resultDiv.innerHTML = resultDiv.innerHTML + "Received: " + bytesToString(data) + "<br/>";
  83. resultDiv.scrollTop = resultDiv.scrollHeight;
  84. },
  85. sendData: function(event) { // send data to Arduino
  86. var success = function() {
  87. console.log("success");
  88. resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "<br/>";
  89. resultDiv.scrollTop = resultDiv.scrollHeight;
  90. };
  91. var failure = function() {
  92. alert("Failed writing data to the redbear hardware");
  93. };
  94. var data = stringToBytes(messageInput.value);
  95. var deviceId = event.target.dataset.deviceId;
  96. ble.writeWithoutResponse(deviceId, redbear.serviceUUID, redbear.txCharacteristic, data, success, failure);
  97. },
  98. disconnect: function(event) {
  99. var deviceId = event.target.dataset.deviceId;
  100. ble.disconnect(deviceId, app.showMainPage, app.onError);
  101. },
  102. showMainPage: function() {
  103. mainPage.hidden = false;
  104. detailPage.hidden = true;
  105. },
  106. showDetailPage: function() {
  107. mainPage.hidden = true;
  108. detailPage.hidden = false;
  109. },
  110. onError: function(reason) {
  111. alert("ERROR: " + reason); // real apps should use notification.alert
  112. }
  113. };