B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice

From MozillaWiki
Jump to navigation Jump to search

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 any uuids; // array of type DOMString[]

  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
Value
Return a BluetoothClassOfDevice object.
Sample

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

  • BluetoothDevice.onattributechanged

onattributechanged

Description
A handler for adapteradded event; it is triggered when a Bluetooth adapter is plugged into the device. The event carries evt.adapter as the plugged BluetoothAdapter object.
Sample
function bt_onAdapterAdded(evt) {
  var adapter = evt.adapter;

  // do your thing here!
}

var manager = navigator.mozBluetooth;
manager.onadapteradded = bt_onAdapterAdded;

Methods

fetchUuids()

Description
The fetchUuid method is used to retrieve all adapters of this device.
Return
An array of BluetoothAdapter objects.
Sample
var manager = navigator.mozBluetooth;
var adapter = manager.getAdapters();

See also