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

From MozillaWiki
Jump to navigation Jump to search
Line 4: Line 4:
=== Discussion ===
=== Discussion ===
* Access adapter in onadapterremoved  
* Access adapter in onadapterremoved  
: onadapterremoved(address) -> onadapterremoved(adapter) before adapter is really removed? adapter's attributes are accessible with default values.
** onadapterremoved(address) -> onadapterremoved(adapter) before adapter is really removed? adapter's attributes are accessible with default values.


* Remove adapter from adapters[] array or not?
* Remove adapter from adapters[] array or not?
: gaia has to rebind handlers if adapter is removed from array.
** gaia has to rebind handlers if adapter is removed from array.


* Track default adapter added/removed - options
* Track default adapter added/removed - options

Revision as of 06:40, 19 March 2014

Overview

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

Discussion

  • Access adapter in onadapterremoved
    • onadapterremoved(address) -> onadapterremoved(adapter) before adapter is really removed? adapter's attributes are accessible with default values.
  • Remove adapter from adapters[] array or not?
    • gaia has to rebind handlers if adapter is removed from array.
  • Track default adapter added/removed - options
    • +ondefaultadapteradded/ondefaultadapterremoved
    • onadapteradded/removed(adapter, bool isDefault)
    • bool adapter.isDefault

Interface

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

  any              getAdapters(); // array of BluetoothAdapter
  BluetoothAdapter getDefaultAdapter();
};

Event Handlers

onadapteradded

Description
A handler to trigger when a bluetooth adapter is plugged into the device. The event carries evt.adapter as the plugged BluetoothAdapter. Applications should call getDefaultAdapter() in this event handler to retrieve the current default adapter if there is no adapter plugged in, suggest to call getAdapters() for the most updated array of adapters on the device.
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 to trigger when a bluetooth adapter is unplugged from the device. The event carries evt.address as the address of unplugged BluetoothAdapter. The unplugged BluetoothAdapter object is already unavailable and access to its attributes returns default values. Applications should call getDefaultAdapter() in this event handler to retrieve the latest default adapter, and may call getAdapters() for the latest array of adapters on the device.
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 retrieves all adapters on the device.
Return
An array of BluetoothAdapter objects.
Sample
var manager = navigator.mozBluetooth;
var adapters = manager.getAdapters();

getDefaultAdapter()

Description
The getDefaultAdapter 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