123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // (c) 2014 Don Coleman
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /* global mainPage, deviceList, refreshButton */
- /* global detailPage, resultDiv, messageInput, sendButton, disconnectButton */
- /* global ble */
- /* jshint browser: true , devel: true*/
- 'use strict';
- // ASCII only
- function bytesToString(buffer) {
- return String.fromCharCode.apply(null, new Uint8Array(buffer));
- }
- // ASCII only
- function stringToBytes(string) {
- var array = new Uint8Array(string.length);
- for (var i = 0, l = string.length; i < l; i++) {
- array[i] = string.charCodeAt(i);
- }
- return array.buffer;
- }
- // this is Nordic's UART service
- var bluefruit = {
- serviceUUID: '6e400001-b5a3-f393-e0a9-e50e24dcca9e',
- txCharacteristic: '6e400002-b5a3-f393-e0a9-e50e24dcca9e', // transmit is from the phone's perspective
- rxCharacteristic: '6e400003-b5a3-f393-e0a9-e50e24dcca9e' // receive is from the phone's perspective
- };
- var app = {
- initialize: function() {
- this.bindEvents();
- detailPage.hidden = true;
- },
- bindEvents: function() {
- document.addEventListener('deviceready', this.onDeviceReady, false);
- refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
- sendButton.addEventListener('click', this.sendData, false);
- disconnectButton.addEventListener('touchstart', this.disconnect, false);
- deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
- },
- onDeviceReady: function() {
- app.refreshDeviceList();
- },
- refreshDeviceList: function() {
- deviceList.innerHTML = ''; // empties the list
- ble.scan([bluefruit.serviceUUID], 5, app.onDiscoverDevice, app.onError);
-
- // if Android can't find your device try scanning for all devices
- // ble.scan([], 5, app.onDiscoverDevice, app.onError);
- },
- onDiscoverDevice: function(device) {
- var listItem = document.createElement('li'),
- html = '<b>' + device.name + '</b><br/>' +
- 'RSSI: ' + device.rssi + ' | ' +
- device.id;
- listItem.dataset.deviceId = device.id;
- listItem.innerHTML = html;
- deviceList.appendChild(listItem);
- },
- connect: function(e) {
- var deviceId = e.target.dataset.deviceId,
- onConnect = function(peripheral) {
- app.determineWriteType(peripheral);
- // subscribe for incoming data
- ble.startNotification(deviceId, bluefruit.serviceUUID, bluefruit.rxCharacteristic, app.onData, app.onError);
- sendButton.dataset.deviceId = deviceId;
- disconnectButton.dataset.deviceId = deviceId;
- resultDiv.innerHTML = "";
- app.showDetailPage();
- };
- ble.connect(deviceId, onConnect, app.onError);
- },
- determineWriteType: function(peripheral) {
- // Adafruit nRF8001 breakout uses WriteWithoutResponse for the TX characteristic
- // Newer Bluefruit devices use Write Request for the TX characteristic
- var characteristic = peripheral.characteristics.filter(function(element) {
- if (element.characteristic.toLowerCase() === bluefruit.txCharacteristic) {
- return element;
- }
- })[0];
- if (characteristic.properties.indexOf('WriteWithoutResponse') > -1) {
- app.writeWithoutResponse = true;
- } else {
- app.writeWithoutResponse = false;
- }
- },
- onData: function(data) { // data received from Arduino
- console.log(data);
- resultDiv.innerHTML = resultDiv.innerHTML + "Received: " + bytesToString(data) + "<br/>";
- resultDiv.scrollTop = resultDiv.scrollHeight;
- },
- sendData: function(event) { // send data to Arduino
- var success = function() {
- console.log("success");
- resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "<br/>";
- resultDiv.scrollTop = resultDiv.scrollHeight;
- };
- var failure = function() {
- alert("Failed writing data to the bluefruit le");
- };
- var data = stringToBytes(messageInput.value);
- var deviceId = event.target.dataset.deviceId;
- if (app.writeWithoutResponse) {
- ble.writeWithoutResponse(
- deviceId,
- bluefruit.serviceUUID,
- bluefruit.txCharacteristic,
- data, success, failure
- );
- } else {
- ble.write(
- deviceId,
- bluefruit.serviceUUID,
- bluefruit.txCharacteristic,
- data, success, failure
- );
- }
- },
- disconnect: function(event) {
- var deviceId = event.target.dataset.deviceId;
- ble.disconnect(deviceId, app.showMainPage, app.onError);
- },
- showMainPage: function() {
- mainPage.hidden = false;
- detailPage.hidden = true;
- },
- showDetailPage: function() {
- mainPage.hidden = true;
- detailPage.hidden = false;
- },
- onError: function(reason) {
- alert("ERROR: " + JSON.stringify(reason)); // real apps should use notification.alert
- }
- };
|