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
10
<link rel="import" href="../polymer/polymer.html">
12
<dom-module id="platinum-bluetooth-device">
14
<content id="characteristics" select="platinum-bluetooth-characteristic">
23
var SUPPORTED = 'bluetooth' in navigator;
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 = {};
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`.
37
* `<platinum-bluetooth-device>` is used as a parent element for
38
* `<platinum-bluetooth-characteristic>` child elements.
40
* For instance, here's how to request a nearby bluetooth device advertising
44
* <platinum-bluetooth-device
45
* services-filter='["battery_service"]'>
46
* </platinum-bluetooth-device>
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); });
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
65
is: 'platinum-bluetooth-device',
70
* Indicates whether the Web Bluetooth API is supported by
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.
86
observer: '_servicesFilterChanged'
90
* Internal variable used to cache Bluetooth device.
93
type: BluetoothDevice,
94
observer: '_deviceChanged'
100
* Update all characteristics when device changes.
102
_deviceChanged: function() {
103
this._updateCharacteristics();
107
* Reset device when services-filter property is changed.
109
_servicesFilterChanged: function() {
114
* Set the internal device object on each characteristic child.
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;
123
created: function() {
124
this._characteristics = [];
128
* Reset device to pick a new device.
135
* Request a nearby bluetooth device and returns a Promise that will
136
* resolve when user picked one Bluetooth device.
138
* It must be called on user gesture.
140
* @return {Promise<BluetoothDevice>}
142
request: function() {
143
if (!this.supported) {
144
return Promise.reject(new Error('Your browser does not support Bluetooth'));
147
this._updateCharacteristics();
148
// Resolve promise if user already picked one device.
149
return Promise.resolve(this._device);
151
if (!this.servicesFilter || this.servicesFilter.length == 0) {
152
return Promise.reject(new Error('Please set Bluetooth services filter.'));
155
return navigator.bluetooth.requestDevice({filters:[{services: this.servicesFilter}]})
156
.then(function(device) {
157
// Cache device for later use.
158
self._device = device;