B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
Line 12: Line 12:
  interface BluetoothDevice: EventTarget
  interface BluetoothDevice: EventTarget
  {
  {
   readonly attribute DOMString address;
   readonly attribute DOMString             [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#address address];
   readonly attribute BluetoothClassOfDevice cod;
   readonly attribute BluetoothClassOfDevice [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#cod cod];
   readonly attribute DOMString name;
   readonly attribute DOMString             [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#name name];
   readonly attribute boolean paired;
   readonly attribute boolean               [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#paired paired];
   readonly attribute DOMString[] uuids;
   readonly attribute DOMString[]           [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#uuids uuids];
   
   
   attribute EventHandler onattributechanged;
   attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#onattributechanged onattributechanged];
   
   
   DOMRequest fetchUuids();
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#fetchUuids.28.29 fetchUuids()];
  };
  };



Revision as of 03:53, 27 February 2014

Overview

The BluetoothDevice API provides information regarding a given Bluetooth device.

Interface

enum BluetoothDeviceAttribute {
  "cod",
  "name",
  "paired",
  "uuids"
}

interface BluetoothDevice: EventTarget
{
  readonly attribute DOMString              address;
  readonly attribute BluetoothClassOfDevice cod;
  readonly attribute DOMString              name;
  readonly attribute boolean                paired;
  readonly attribute DOMString[]            uuids;

  attribute EventHandler onattributechanged;

  DOMRequest fetchUuids();
};

Attributes

address

Description
The address attribute provides the address of the device on the Bluetooth micro-network. This attribute must be non-empty value. Both empty and null string are regarded invalid.
Value
Return a string. The default value is empty string "".
Sample
var address = instanceOfBluetoothDevice.address;
// TODO
  • The attribute cannot be empty but the default value is empty string?

cod

Description
The class attribute value is a BluetoothClassOfDevice object that provides much information about the device's capabilities.
Value
Return a BluetoothClassOfDevice object. The default value is a BluetoothClassOfDevice with all attribute = 0.
Sample
var cod = instanceOfBluetoothDevice.cod;

var majorDeviceClass = cod.BluetoothMajorDeviceClass;
var majorServiceClass = cod.BluetoothMajorServiceClass;
var minorDeviceClass = cod.BluetoothMinorDeviceClass;

name

Description
The name attribute provides the human readable name of the device.
Value
Return a string. The default value is empty string "".
Sample
var name = instanceOfBluetoothDevice.name;

paired

Description
The paired attribute indicates if the device is paired to the current device's adapter (true) or not (false).
Value
Return a boolean. The default value is false.
Sample
var paired = instanceOfBluetoothDevice.paired;

uuids

Description
The uuids attribute stores the cached UUID list of each Bluetooth service which the device is able to provide. To get the up-to-date list, call fetchUuids() to update this attribute.
Value
Return an array of strings. The default value is an empty array (array with length = 0).
Sample
var uuids = instanceOfBluetoothDevice.uuids;

Event Handlers

onattributechanged

Description
A handler for attributechanged event; it is triggered when certain attribute of the device is changed. The event carries evt.attr as the attribute changed and evt.value as the updated value.
Sample
function bt_onAttributeChanged(evt) {
  var attr = evt.attr;
  var value = evt.value;

  if (attr === BluetoothDeviceAttribute.name) {
    // do your thing here!
  }
}

instanceOfBluetoothDevice.onattributechanged = bt_onAttributeChanged;

Methods

fetchUuids()

Description
The fetchUuid method is used to fetch the up-to-date UUID list of each Bluetooth service which the device is able to provide.
Return
A DOMRequest object to handle the success or error of the operation. In case of success, the request's result is an Array of DOMString objects.
Sample
var req = instanceOfBluetoothDevice.fetchUuids();

req.onsuccess = functions(e) {
  var uuids = e.target.result;

  // do your things here!
}

See also