123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- var heartRate = {
- service: '180d',
- measurement: '2a37'
- };
- var app = {
- initialize: function() {
- this.bindEvents();
- },
- bindEvents: function() {
- document.addEventListener('deviceready', this.onDeviceReady, false);
- },
- onDeviceReady: function() {
- app.scan();
- },
- scan: function() {
- app.status("Scanning for Heart Rate Monitor");
- var foundHeartRateMonitor = false;
- function onScan(peripheral) {
-
- console.log("Found " + JSON.stringify(peripheral));
- foundHeartRateMonitor = true;
- ble.connect(peripheral.id, app.onConnect, app.onDisconnect);
- }
- function scanFailure(reason) {
- alert("BLE Scan Failed");
- }
- ble.scan([heartRate.service], 5, onScan, scanFailure);
- setTimeout(function() {
- if (!foundHeartRateMonitor) {
- app.status("Did not find a heart rate monitor.");
- }
- }, 5000);
- },
- onConnect: function(peripheral) {
- app.status("Connected to " + peripheral.id);
- ble.startNotification(peripheral.id, heartRate.service, heartRate.measurement, app.onData, app.onError);
- },
- onDisconnect: function(reason) {
- alert("Disconnected " + reason);
- beatsPerMinute.innerHTML = "...";
- app.status("Disconnected");
- },
- onData: function(buffer) {
-
-
- var data = new Uint8Array(buffer);
- beatsPerMinute.innerHTML = data[1];
- },
- onError: function(reason) {
- alert("There was an error " + reason);
- },
- status: function(message) {
- console.log(message);
- statusDiv.innerHTML = message;
- }
- };
- app.initialize();
|