~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/platinum-bluetooth/platinum-bluetooth-device.html

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
@license
 
3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
 
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
 
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
 
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
 
7
Code distributed by Google as part of the polymer project is also
 
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
 
9
-->
 
10
<link rel="import" href="../polymer/polymer.html">
 
11
 
 
12
<dom-module id="platinum-bluetooth-device">
 
13
  <template>
 
14
    <content id="characteristics" select="platinum-bluetooth-characteristic">
 
15
    </content>
 
16
  </template>
 
17
</dom-module>
 
18
 
 
19
<script>
 
20
  (function() {
 
21
    'use strict';
 
22
 
 
23
    var SUPPORTED = 'bluetooth' in navigator;
 
24
 
 
25
    // This allows us to use BluetoothDevice related attributes type in
 
26
    // browsers where it is not defined.
 
27
    if (!('BluetoothDevice' in window)) {
 
28
      window.BluetoothDevice = {};
 
29
    }
 
30
 
 
31
    /**
 
32
     * The `<platinum-bluetooth-device>` element allows you to [discover nearby
 
33
     * bluetooth devices][1] thanks to the young [Web Bluetooth API][2]. It is
 
34
     * currently only partially implemented in Chrome OS 45 behind the
 
35
     * experimental flag `chrome://flags/#enable-web-bluetooth`.
 
36
     *
 
37
     * `<platinum-bluetooth-device>` is used as a parent element for
 
38
     * `<platinum-bluetooth-characteristic>` child elements.
 
39
     *
 
40
     * For instance, here's how to request a nearby bluetooth device advertising
 
41
     * Battery service :
 
42
     *
 
43
     * ```html
 
44
     * <platinum-bluetooth-device
 
45
     *     services-filter='["battery_service"]'>
 
46
     * </platinum-bluetooth-device>
 
47
     * ```
 
48
     * ```js
 
49
     * button.addEventListener('click', function() {
 
50
     *   document.querySelector('platinum-bluetooth-device').request()
 
51
     *   .then(function(device) { console.log(device.name); })
 
52
     *   .catch(function(error) { console.error(error); });
 
53
     * });
 
54
     * ```
 
55
     *
 
56
     * [1]: https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web
 
57
     * [2]: https://github.com/WebBluetoothCG/web-bluetooth
 
58
     *
 
59
     * @hero hero.svg
 
60
     * @demo
 
61
     */
 
62
 
 
63
    Polymer({
 
64
 
 
65
      is: 'platinum-bluetooth-device',
 
66
 
 
67
      properties: {
 
68
 
 
69
        /**
 
70
         * Indicates whether the Web Bluetooth API is supported by
 
71
         * this browser.
 
72
         */
 
73
        supported: {
 
74
          readOnly: true,
 
75
          type: Boolean,
 
76
          value: SUPPORTED
 
77
        },
 
78
 
 
79
        /**
 
80
         * Required Bluetooth GATT services filter. You may provide either the
 
81
         * full Bluetooth UUID as a string or a short 16- or 32-bit form as
 
82
         * integers like 0x180d.
 
83
         */
 
84
        servicesFilter: {
 
85
          type: Array,
 
86
          observer: '_servicesFilterChanged'
 
87
        },
 
88
 
 
89
        /**
 
90
         * Internal variable used to cache Bluetooth device.
 
91
         */
 
92
        _device: {
 
93
          type: BluetoothDevice,
 
94
          observer: '_deviceChanged'
 
95
        },
 
96
 
 
97
      },
 
98
 
 
99
      /**
 
100
      * Update all characteristics when device changes.
 
101
       */
 
102
      _deviceChanged: function() {
 
103
        this._updateCharacteristics();
 
104
      },
 
105
 
 
106
      /**
 
107
       * Reset device when services-filter property is changed.
 
108
       */
 
109
      _servicesFilterChanged: function() {
 
110
        this._device = null;
 
111
      },
 
112
 
 
113
      /**
 
114
       * Set the internal device object on each characteristic child.
 
115
       */
 
116
      _updateCharacteristics: function() {
 
117
        this._characteristics = Polymer.dom(this.$.characteristics).getDistributedNodes();
 
118
        for (var i = 0; i < this._characteristics.length; i++) {
 
119
          this._characteristics[i]._device = this._device;
 
120
        }
 
121
      },
 
122
 
 
123
      created: function() {
 
124
        this._characteristics = [];
 
125
      },
 
126
 
 
127
      /**
 
128
       * Reset device to pick a new device.
 
129
       */
 
130
      reset: function() {
 
131
        this._device = null;
 
132
      },
 
133
 
 
134
      /**
 
135
       * Request a nearby bluetooth device and returns a Promise that will
 
136
       * resolve when user picked one Bluetooth device.
 
137
       *
 
138
       * It must be called on user gesture.
 
139
       *
 
140
       * @return {Promise<BluetoothDevice>}
 
141
       */
 
142
      request: function() {
 
143
        if (!this.supported) {
 
144
          return Promise.reject(new Error('Your browser does not support Bluetooth'));
 
145
        }
 
146
        if (this._device) {
 
147
          this._updateCharacteristics();
 
148
          // Resolve promise if user already picked one device.
 
149
          return Promise.resolve(this._device);
 
150
        }
 
151
        if (!this.servicesFilter || this.servicesFilter.length == 0) {
 
152
          return Promise.reject(new Error('Please set Bluetooth services filter.'));
 
153
        }
 
154
        var self = this;
 
155
        return navigator.bluetooth.requestDevice({filters:[{services: this.servicesFilter}]})
 
156
          .then(function(device) {
 
157
            // Cache device for later use.
 
158
            self._device = device;
 
159
            return self._device;
 
160
          });
 
161
      },
 
162
 
 
163
    });
 
164
  })();
 
165
</script>