B2G/Bluetooth/WebBluetooth-v2/BluetoothManager

From MozillaWiki
< B2G‎ | Bluetooth‎ | WebBluetooth-v2
Revision as of 03:49, 26 February 2014 by Btian (talk | contribs)
Jump to navigation Jump to search

Overview

The BluetoothManager API allows to access all Bluetooth adapters available on the device. Adapters are the connection interface to connect a Bluetooth device to that device.

Interface

interface BluetoothManager: EventTarget
{
  attribute EventHandler onadapteradded;
  attribute EventHandler onadapterremoved;

  BluetoothAdapter[] getAdapters();
  BluetoothAdapter   getDefaultAdapter();
};

Event Handlers

onadapteradded

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;

onadapterremoved

Description
A handler for adapterremoved event; it is triggered when a Bluetooth adapter is unplugged from the device. The event carries evt.address as address of the unplugged BluetoothAdapter. The unplugged BluetoothAdapter object is already unavailable.
Sample
function bt_onAdapterRemoved(evt) {
  var address = evt.address;

  // do your thing here!
}

var manager = navigator.mozBluetooth;
manager.onadapterremoved = bt_onAdapterRemoved;

Methods

getAdapters()

Description
The getAdapters 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();
// TODO
  • What would happen if 0 is unplugged and another dongle is plugged?

getDefaultAdapter()

Description
The getDefaultAdapter method is used to retrieve the default adapter used to connect the device to third-party Bluetooth devices.
Return
The default BluetoothAdapter object.
Sample
var manager = navigator.mozBluetooth;
var adapter = manager.getDefaultAdapter();
// TODO
  • How do we know defaultAdapter has been changed?

See also