forked from blueappio/hexiwear-beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of the sample project.
- Loading branch information
Ronald
authored and
Kranti Kambhampati
committed
Apr 1, 2017
1 parent
dc1a54b
commit d118c6a
Showing
6 changed files
with
98 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |