Polymer - Platinum Bluetooth
It is used for interacting with nearby bluetooth devices using the <platinum-bluetooth> element.
You can use this element in your application, by running the following command to install it in your project directory.
bower install --save PolymerElements/platinum-bluetooth
Example
Following example specifies the use of platinum-bluetooth element in Polymer.js. Create an index.html file and add the following code in it.
<!doctype html>
<html>
<head>
<title>Polymer Example</title>
<script src = "bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel = "import" href = "bower_components/polymer/polymer.html">
<link rel = "import" href = "bower_components/paper-styles/demo-pages.html">
<link rel = "import" href = "bower_components/paper-button/paper-button.html">
<link rel = "import" href = "bower_components/platinum-bluetooth/platinum-bluetooth-device.html">
</head>
<body>
<section>
<paper-button raised>Get bluetooth device</paper-button>
</section>
<script src = "platinum_bluetooth.js"></script>
</body>
</html>
Now, create another file called platinum_bluetooth.js and include the following code −
document.addEventListener('WebComponentsReady', function() {
var mybatteryDevice = document.querySelector('platinum-bluetooth-device');
var mybutton = document.querySelector('paper-button');
mybutton.addEventListener('click', function() {
console.log('The requested bluetooth device advertising a battery service...');
mybatteryDevice.request().then(function(device) {
console.log('Bluetooth device has been found...');
console.log('The device name is: ' + device.name);
})
.catch(function(error) {
console.error('Sorry!No device found...', error);
});
});
});
Output
To run the application, navigate to the created project directory and run the following command.
polymer serve
Now open the browser and navigate to http://127.0.0.1:8081/. Following will be the output.
When you click the button, it will display a message as "The requested bluetooth device advertising a battery service..." in the console and display an error message, if no device is found.