B2G/Bluetooth/WebBluetooth-v2/BluetoothManager

< B2G‎ | Bluetooth‎ | WebBluetooth-v2
Revision as of 10:13, 25 February 2014 by Btian (talk | contribs)

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 becomes enabled on the device. The event carries the enabled BluetoothAdapter object as evt.adapter.
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 becomes disabled on the device. The event carries address of the disabled BluetoothAdapter as evt.address. The disabled 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?
  • Does the array keeps BluetoothAdapter with state 'disabled'?

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