All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.polymer.platinum.widget.PlatinumBluetoothCharacteristic Maven / Gradle / Ivy

There is a newer version: 1.9.3.1
Show newest version
/*
 * This code was generated with Vaadin Web Component GWT API Generator, 
 * from platinum-bluetooth project by The Polymer Authors
 * that is licensed with http://polymer.github.io/LICENSE.txt license.
 */
package com.vaadin.polymer.platinum.widget;

import com.vaadin.polymer.platinum.*;

import com.vaadin.polymer.platinum.widget.event.PlatinumBluetoothAutoWriteErrorEvent;
import com.vaadin.polymer.platinum.widget.event.PlatinumBluetoothAutoWriteErrorEventHandler;

import com.vaadin.polymer.platinum.widget.event.ValueChangedEvent;
import com.vaadin.polymer.platinum.widget.event.ValueChangedEventHandler;

import com.vaadin.polymer.*;
import com.vaadin.polymer.elemental.*;
import com.vaadin.polymer.PolymerWidget;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.core.client.JavaScriptObject;

/**
 * 

The <platinum-bluetooth-characteristic> element allows you to read
and write characteristics on nearby bluetooth devices
thanks to the
young Web Bluetooth API. It is currently partially implemented
behind the experimental flag chrome://flags/#enable-web-bluetooth. It
is also now available in Chrome 53 as an origin trial for Chrome
OS, Android M, and Mac.

*

<platinum-bluetooth-characteristic> needs to be a child of a
<platinum-bluetooth-service> element.

*

For instance, here’s how to read battery level from a nearby bluetooth
device advertising Battery service:

*
<platinum-bluetooth-device services-filter='["battery_service"]'>
 *   <platinum-bluetooth-service service='battery_service'>
 *     <platinum-bluetooth-characteristic characteristic='battery_level'>
 *     </platinum-bluetooth-characteristic>
 *   </platinum-bluetooth-service>
 * </platinum-bluetooth-device>
 * 
*
var bluetoothDevice = document.querySelector('platinum-bluetooth-device');
 * var batteryLevel = document.querySelector('platinum-bluetooth-characteristic');
 * 
 * button.addEventListener('click', function() {
 *   bluetoothDevice.request().then(function() {
 *     return batteryLevel.read().then(function(value) {
 *       console.log('Battery Level is ' + value.getUint8(0) + '%');
 *     });
 *   })
 *   .catch(function(error) { });
 * });
 * 
*

Here’s another example on how to reset energy expended on nearby
bluetooth device advertising Heart Rate service:

*
<platinum-bluetooth-device services-filter='["heart_rate"]'>
 *   <platinum-bluetooth-service service='heart_rate'>
 *     <platinum-bluetooth-characteristic characteristic='heart_rate_control_point'>
 *     </platinum-bluetooth-characteristic>
 *   </platinum-bluetooth-service>
 * </platinum-bluetooth-device>
 * 
*
var bluetoothDevice = document.querySelector('platinum-bluetooth-device');
 * var heartRateCtrlPoint = document.querySelector('platinum-bluetooth-characteristic');
 * 
 * button.addEventListener('click', function() {
 *   bluetoothDevice.request().then(function() {
 *     // Writing 1 is the signal to reset energy expended.
 *     var resetEnergyExpended = new Uint8Array([1]);
 *     return heartRateCtrlPoint.write(resetEnergyExpended).then(function() {
 *       console.log('Energy expended has been reset');
 *     });
 *   })
 *   .catch(function(error) { });
 * });
 * 
*

It is also possible for <platinum-bluetooth-characteristic> to fill in
a data-bound field in response to a read.

*
<platinum-bluetooth-device services-filter='["heart_rate"]'>
 *   <platinum-bluetooth-service service='heart_rate'>
 *     <platinum-bluetooth-characteristic characteristic='body_sensor_location'
 *                                        value={{bodySensorLocation}}>
 *     </platinum-bluetooth-characteristic>
 *   </platinum-bluetooth-service>
 * </platinum-bluetooth-device>
 * ...
 * <span>{{bodySensorLocation}}</span>
 * 
*
var bluetoothDevice = document.querySelector('platinum-bluetooth-device');
 * var bodySensorLocation = document.querySelector('platinum-bluetooth-characteristic');
 * 
 * button.addEventListener('click', function() {
 *   bluetoothDevice.request().then(function() {
 *     return bodySensorLocation.read()
 *   })
 *   .catch(function(error) { });
 * });
 * 
*

Starting and stopping notifications on a <platinum-bluetooth-characteristic> is pretty straightforward when taking advantage of the Polymer Change notification protocol. Here’s how to get your Heart Rate Measurement for instance:

*
<platinum-bluetooth-device services-filter='["heart_rate"]'>
 *   <platinum-bluetooth-service service='heart_rate'>
 *     <platinum-bluetooth-characteristic characteristic='heart_rate_measurement'
 *                                        on-value-changed='parseHeartRate'>
 *     </platinum-bluetooth-characteristic>
 *   </platinum-bluetooth-service>
 * </platinum-bluetooth-device>
 * 
*
var bluetoothDevice = document.querySelector('platinum-bluetooth-device');
 * var heartRate = document.querySelector('platinum-bluetooth-characteristic');
 * 
 * startButton.addEventListener('click', function() {
 *   bluetoothDevice.request().then(function() {
 *     return heartRate.startNotifications().catch(function(error) { });
 *   });
 * });
 * 
 * stopButton.addEventListener('click', function() {
 *   heartRate.stopNotifications().catch(function(error) { });
 * });
 * 
 * function parseHeartRate(event) {
 *  let value = event.target.value;
 *  // Do something with the DataView Object value...
 * }
 * 
*

You can also use changes in value to drive characteristic writes when
auto-write property is set to true.

*
<platinum-bluetooth-device services-filter='["heart_rate"]'>
 *   <platinum-bluetooth-service service='heart_rate'>
 *     <platinum-bluetooth-characteristic characteristic='heart_rate_control_point'
 *                                        auto-write>
 *     </platinum-bluetooth-characteristic>
 *   </platinum-bluetooth-service>
 * </platinum-bluetooth-device>
 * 
*
var bluetoothDevice = document.querySelector('platinum-bluetooth-device');
 * var heartRateCtrlPoint = document.querySelector('platinum-bluetooth-characteristic');
 * 
 * button.addEventListener('click', function() {
 *   bluetoothDevice.request().then(function() {
 *     // Writing 1 is the signal to reset energy expended.
 *     heartRateCtrlPoint.value = new Uint8Array([1]);
 *   })
 *   .catch(function(error) { });
 * });
 * 
 * heartRateCtrlPoint.addEventListener('platinum-bluetooth-auto-write-error',
 *     function(event) {
 *   // Handle error...
 * });
 * 
*/ public class PlatinumBluetoothCharacteristic extends PolymerWidget { /** * Default Constructor. */ public PlatinumBluetoothCharacteristic() { this(""); } /** * Constructor used by UIBinder to create widgets with content. */ public PlatinumBluetoothCharacteristic(String html) { super(PlatinumBluetoothCharacteristicElement.TAG, PlatinumBluetoothCharacteristicElement.SRC, html); } /** * Gets a handle to the Polymer object's underlying DOM element. */ public PlatinumBluetoothCharacteristicElement getPolymerElement() { return (PlatinumBluetoothCharacteristicElement) getElement(); } /** *

If true, automatically perform a write value on the characteristic
when value changes.

* * JavaScript Info: * @property autoWrite * @type Boolean * */ public boolean getAutoWrite() { return getPolymerElement().getAutoWrite(); } /** *

If true, automatically perform a write value on the characteristic
when value changes.

* * JavaScript Info: * @property autoWrite * @type Boolean * */ public void setAutoWrite(boolean value) { getPolymerElement().setAutoWrite(value); } /** *

Value gets populated with the characteristic value when it’s read
and during a notification session.

* * JavaScript Info: * @property value * @type DataView * */ public JavaScriptObject getValue() { return getPolymerElement().getValue(); } /** *

Value gets populated with the characteristic value when it’s read
and during a notification session.

* * JavaScript Info: * @property value * @type DataView * */ public void setValue(JavaScriptObject value) { getPolymerElement().setValue(value); } /** *

Required Bluetooth GATT characteristic for read and write operations.
You may provide either the full Bluetooth UUID as a string or a
short 16- or 32-bit form as integers like 0x2A19.

* * JavaScript Info: * @property characteristic * @type String * */ public String getCharacteristic() { return getPolymerElement().getCharacteristic(); } /** *

Required Bluetooth GATT characteristic for read and write operations.
You may provide either the full Bluetooth UUID as a string or a
short 16- or 32-bit form as integers like 0x2A19.

* * JavaScript Info: * @property characteristic * @type String * */ public void setCharacteristic(String value) { getPolymerElement().setCharacteristic(value); } // Needed in UIBinder /** *

Value gets populated with the characteristic value when it’s read
and during a notification session.

* * JavaScript Info: * @attribute value * */ public void setValue(String value) { Polymer.property(this.getPolymerElement(), "value", value); } /** *

Returns a promise that will resolve when Bluetooth GATT Characteristic
is written.

* * JavaScript Info: * @method write * @param {(ArrayBufferView|ArrayBuffer)} value * * @return {JavaScriptObject} */ public JavaScriptObject write(Object value) { return getPolymerElement().write(value); } /** *

Returns a promise that will resolve when Bluetooth GATT Characteristic
is read.

* * JavaScript Info: * @method read * * @return {JavaScriptObject} */ public JavaScriptObject read() { return getPolymerElement().read(); } /** *

Returns a promise that will resolve when Bluetooth GATT Characteristic
notification session starts.

* * JavaScript Info: * @method startNotifications * * @return {JavaScriptObject} */ public JavaScriptObject startNotifications() { return getPolymerElement().startNotifications(); } /** *

Returns a promise that will resolve when Bluetooth GATT Characteristic
notification session stops.

* * JavaScript Info: * @method stopNotifications * * @return {JavaScriptObject} */ public JavaScriptObject stopNotifications() { return getPolymerElement().stopNotifications(); } /** *

Fired when an error occurs while writing automatically to a characteristic.

* * JavaScript Info: * @event platinum-bluetooth-auto-write-error */ public HandlerRegistration addPlatinumBluetoothAutoWriteErrorHandler(PlatinumBluetoothAutoWriteErrorEventHandler handler) { return addDomHandler(handler, PlatinumBluetoothAutoWriteErrorEvent.TYPE); } /** *

Fired when value gets changed during a notification session.

* * JavaScript Info: * @event value-changed */ public HandlerRegistration addValueChangedHandler(ValueChangedEventHandler handler) { return addDomHandler(handler, ValueChangedEvent.TYPE); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy