Showing posts with label Web Apps. Show all posts
Showing posts with label Web Apps. Show all posts

Thursday, July 27, 2017

How to Enable Bluetooth in Web Apps

How to Enable Bluetooth in Web Apps
How to Enable Bluetooth in Web Apps 


Some prerequisites

  • HTTPS only: The API only works on pages served over HTTPS. Most privacy-sensitive web APIs are now switching over to the HTTPS-only model, and this one is no exception.
  • Requires user action: We want to make sure that the API doesn’t work in the background without the user knowing about it, which is why there is a prompt shown to user to choose which device to pair with. Additionally, we also don’t want sites to show up this prompt straightaway without any user interaction. That is why methods in this API only work when called in response to a user action (like responding to a click event).

Getting Basic Device Information

Let’s take a look at some code to figure out how to use this API to get some basic information regarding a BLE device.
Here is a very simple example, showing a button:
<button id="the-button">Try it</button>
…and the following JavaScript:
const button = document.querySelector('#the-button');
button.addEventListener('click', function() {
 navigator.bluetooth.requestDevice({
  filters: [{
   services: ['battery_service']
  }]
 }).then(device => {
  console.log('Got device:', device.name);
  console.log('id:', device.id);
 });
});
Ref- Opera Dev https://dev.opera.com/articles/web-bluetooth-intro/https://github.com/WebBluetoothCG/demos