Advertisement
Guest User

Untitled

a guest
Aug 7th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////////////////////////////////////////////////
  2. /* Polyfill for Web Bluetooth                                */
  3. (function(navigator) {
  4.   'use strict';
  5.  
  6.   let allowedDevices = [
  7.     { id: '1', name: 'Device A', connectGATT: function() { return new Promise((resolve, reject) => { reject(); }) } },
  8.     { id: '2', name: 'Device B', connectGATT: function() { return new Promise((resolve, reject) => { reject(); }) } },
  9.     { id: '3', name: 'Device C', connectGATT: function() { return new Promise((resolve, reject) => { resolve(); }) } },
  10.   ];
  11.  
  12.   navigator.bluetooth.getAllowedDevices = function() {
  13.     return new Promise((resolve, reject) => {
  14.       resolve(allowedDevices);
  15.     });
  16.   }
  17.  
  18.   navigator.bluetooth.referringDevice = allowedDevices[0];
  19. })(navigator);
  20. ///////////////////////////////////////////////////////////////
  21.  
  22. var button = document.createElement('button');
  23. var index = 0;
  24. var allowedDevices = null;
  25.  
  26. // Get all bluetooth allowed devices first.
  27. navigator.bluetooth.getDevices()
  28. .then(devices => {
  29.  
  30.   // Cache all bluetooth allowed devices.
  31.   allowedDevices = devices;
  32.  
  33.   if (navigator.bluetooth.referringDevice) {
  34.     // Device is coming from Physical Web... let's connect to it now.
  35.     // This assumes this device is the first one in allowedDevices.
  36.     connect(navigator.bluetooth.referringDevice, true);
  37.   } else if (allowedDevices.length > 0) {
  38.     // If website already interacted with bluetooth devices, let's try to connect to them.
  39.     connect(allowedDevices[index], true);
  40.   } else {
  41.     // Show "Scan for devices" button.
  42.     showScanButton();
  43.   }  
  44.  
  45. })
  46. .catch(error => {
  47.   // Show "Scan for devices" button.
  48.   showScanButton();
  49. });
  50.  
  51.  
  52. function connect(bluetoothDevice, toContinue) {
  53.   bluetoothDevice.connectGATT()
  54.   .then(server => {
  55.     // Interact with bluetooth device.
  56.   })
  57.   .catch(error => {
  58.     if (toContinue && ++index < allowedDevices.length) {
  59.       // Try to connect to the next allowed device.
  60.       var nextDevice = allowedDevices[index];
  61.       connect(nextDevice, true);
  62.     } else {
  63.       // Show "scan for devices" button.
  64.       showScanButton();
  65.     }
  66.   })
  67. }
  68.  
  69. function showScanButton() {
  70.   button.style.display = 'block';
  71.   button.addEventListener('click', () => {
  72.     // Start bluetooth discovery session
  73.     var options = { filters: [ { services: [ 'battery_service' ] } ] };
  74.     navigator.bluetooth.requestDevice(options)
  75.     .then(device => {
  76.       connect(device, false);
  77.     })
  78.     .catch(error => {
  79.       console.log(error);
  80.     });
  81.   });
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement