B2G/Bluetooth/WebBluetooth-v2/BluetoothManager

From MozillaWiki
Jump to navigation Jump to search

Overview

BluetoothManager allows to access all bluetooth adapters available on the device. Adapters are the interfaces that connect to the remote device.

Interface

interface BluetoothManager: EventTarget
{
  readonly attribute boolean      defaultAdapter;

           attribute EventHandler onattributechanged;
           attribute EventHandler onadapteradded;
           attribute EventHandler onadapterremoved;

  sequence<BluetoothAdapter>      getAdapters();
};

Event Handlers

onadapteradded

Description
A handler to trigger when a bluetooth adapter is added (i.e., plugged in) to the device.
Parameter
aAdapterEvent
The event is a BluetoothAdapterEvent with property adapter as the added bluetooth adapter and property isDefault indicating whether the added adapter is default adapter.
Sample
function bt_onAdapterAdded(evt) {
  var adapter = evt.adapter;
  var isDefaultAdapter = evt.isDefault;

  if (isDefaultAdapter) {
    // do your things here.
  }
}

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

onadapterremoved

Description
A handler to trigger when a bluetooth adapter is removed (i.e., unplugged) from the device.
Parameter
aAddressEvent
The event is a BluetoothAdapterEvent with property address as the address of removed bluetooth adapter and property isDefault indicating whether the removed adapter is default adapter.
Sample
function bt_onAdapterRemoved(evt) {
  var address = evt.address;
  var isDefaultAdapter = evt.isDefault;

  if (isDefaultAdapter) {
    // do your things here.
  }
}

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

Methods

getAdapters()

Description
The method retrieves the array of all adapters on the device. It returns the same array until an adapter is added/removed. The array grows/shrinks as adapters are added/removed and subsequent getAdapters() calls would get the updated array.
Return
A BluetoothAdapter array.
Sample
var manager = navigator.mozBluetooth;
var adapters = manager.getAdapters();

getDefaultAdapter()

Description
The method retrieves the default adapter used to connect to the remote bluetooth devices.
Return
The default BluetoothAdapter object.
Sample
var manager = navigator.mozBluetooth;
var adapter = manager.getDefaultAdapter();

See also