Skip to content

Commit

Permalink
Initial version of the sample project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald authored and Kranti Kambhampati committed Apr 1, 2017
1 parent dc1a54b commit d118c6a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check the project on [Hackster.io](https://www.hackster.io/vensi/getting-started-ab220f)
71 changes: 71 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
angular.module('BlueAppDemo', [])
.controller('MainController', ['$scope', mainController]);

function mainController($scope) {
var main = this;

main.buttonClicked = function () {
if (!isBluetoothEnabled()) {
alert('Bluetooth Not Supported');
return;
}

let filters = [{services: ['0000180a-0000-1000-8000-00805f9b34fb']}];
let options = {};
options.filters = filters;


navigator.bluetooth.requestDevice(options) // Start a scan with options given.
.then(device => {
// Receives device user selected.
main.Name = device.name; // Store name
main.Id = device.id; // Store id
$scope.$apply();

return device.gatt.connect();
})
.then(server => {
// Device has connected.
// Get the service we are looking for to get data from.
return server.getPrimaryService('0000180a-0000-1000-8000-00805f9b34fb');
})
.then(service => {
// Got the service
// Get the characteristic where data is found.
return service.getCharacteristic('00002a29-0000-1000-8000-00805f9b34fb');
})
.then(c1 => {
// Got characteristic.
// Read the value we want.
return c1.readValue();
})
.then(data => {
// Got the value. Let's use it in our application.
main.Data = dataToString(data);
$scope.$apply();
})
.catch(error => {
console.log('Argh! ' + error);
});
}

function isBluetoothEnabled() {
if (navigator.bluetooth) {
console.log("We have bluetooth");
return true;
} else {
return false;
}
}

function dataToString(data) {
var value = '';

for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}

value = value.replace(/\0/g, '');
return value.trim();
}
}
25 changes: 0 additions & 25 deletions demo.html

This file was deleted.

77 changes: 0 additions & 77 deletions demo.js

This file was deleted.

25 changes: 25 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html ng-app="BlueAppDemo">
<head>
<script src="https://blueappio.github.io/blueapp.io/blueapp.io.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<h2>Demo</h2>
<div ng-controller="MainController as main">
<button ng-click="main.buttonClicked()">Find Device</button>
<ul class="unstyled">
<li>
Name: {{main.Name}}
</li>
<li>
Id: {{main.Id}}
</li>
<li>
Data: {{main.Data}}
</li>
</ul>
</div>
</body>
</html>

0 comments on commit d118c6a

Please sign in to comment.