From d118c6a47e8347e6e6bf09eb14c67125409de009 Mon Sep 17 00:00:00 2001 From: Ronald Date: Wed, 22 Mar 2017 13:04:49 -0500 Subject: [PATCH] Initial version of the sample project. --- .gitignore | 1 + README.md | 1 + app.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ demo.html | 25 ------------------ demo.js | 77 ------------------------------------------------------ index.html | 25 ++++++++++++++++++ 6 files changed, 98 insertions(+), 102 deletions(-) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.js delete mode 100644 demo.html delete mode 100644 demo.js create mode 100644 index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..585086f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Check the project on [Hackster.io](https://www.hackster.io/vensi/getting-started-ab220f) diff --git a/app.js b/app.js new file mode 100644 index 0000000..04122d3 --- /dev/null +++ b/app.js @@ -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(); + } +} diff --git a/demo.html b/demo.html deleted file mode 100644 index 5871b7d..0000000 --- a/demo.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - -

Demo

-
- - -
- - \ No newline at end of file diff --git a/demo.js b/demo.js deleted file mode 100644 index 97ff1ad..0000000 --- a/demo.js +++ /dev/null @@ -1,77 +0,0 @@ -angular.module('BlueAppDemo', []) - .controller('MainController', ['$scope', mainController]); - - function mainController($scope) { - var main = this; - - main.buttonClicked = function() { - if (isWebBluetoothEnabled()) { - let filters = [{namePrefix: 'Grippack'}]; - let optionalServices = ['0000180a-0000-1000-8000-00805f9b34fb']; - - let options = {}; - options.filters = filters; - options.optionalServices = optionalServices; - - 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 isWebBluetoothEnabled() { - if (navigator.bluetooth) { - console.log("We have webbluetooth"); - return true; - } else { - ChromeSamples.setStatus('Web Bluetooth API is not available.\n' + - 'Please make sure the "Experimental Web Platform features" flag is enabled.'); - 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(); - } - } \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..645b9b8 --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + + + + + + +

Demo

+
+ + +
+ +