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

From MozillaWiki
Jump to navigation Jump to search
 
(42 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Overview ==
== Overview ==
The BluetoothManager API allows to access all bluetooth adapters available on the device. Adapters are the interfaces that connect to the remote device.
'''BluetoothManager''' allows to access all bluetooth adapters available on the device. Adapters are the interfaces that connect to the remote device.


=== Discussion ===
== Interfaces ==
* Access adapter in onadapterremoved
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManager|BluetoothManager]]
: onadapterremoved(address) -> onadapterremoved(adapter) before adapter is really removed? adapter's attributes are accessible with default values.
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]]


* Remove adapter from adapters[] array or not?
=== BluetoothManager ===
: gaia has to rebind handlers if adapter is removed from array.
[CheckPermissions="bluetooth"]
 
* Track default adapter added/removed - options
** +ondefaultadapteradded/ondefaultadapterremoved
** onadapteradded/removed(adapter, bool isDefault)
** bool adapter.isDefault
 
== Interface ==
  interface BluetoothManager: EventTarget
  interface BluetoothManager: EventTarget
  {
  {
   attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded onadapteradded];
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]]? [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]];
  attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved onadapterremoved];
   
   
  any              [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29 getAdapters()]; // array of BluetoothAdapter
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|onattributechanged]];
   BluetoothAdapter [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getDefaultAdapter.28.29 getDefaultAdapter()];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|onadapteradded]];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|onadapterremoved]];
   sequence<BluetoothAdapter> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29|getAdapters()]];
};
 
=== BluetoothManagerAttribute ===
enum BluetoothManagerAttribute
{
  "unknown",
  "defaultAdapter"
  };
  };
== Properties ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|BluetoothManager.defaultAdapter]]
=== defaultAdapter ===
; Description
: The default adapter used to connect to the remote bluetooth devices. 
; Value type
: [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]]
; Default value
: Null pointer if no adapter exists.
; Sample
var defaultAdapter = navigator.mozBluetooth.defaultAdapter;


== Event Handlers ==
== Event Handlers ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded BluetoothManager.onadapteradded]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|BluetoothManager.onattributechanged]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved BluetoothManager.onadapterremoved]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|BluetoothManager.onadapteradded]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|BluetoothManager.onadapterremoved]]
 
=== onattributechanged ===
; Description
: A handler to trigger when bluetooth manager's only property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]] has changed.
 
; Parameter
: ''aAttributeEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent#Interface|BluetoothAttributeEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent#attrs|attrs]] that contains changed [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]].
 
; Sample
var manager = navigator.mozBluetooth;
manager.onattributechanged = function onManagerAttributeChanged(evt) {
  for (var i in evt.attrs) {
    switch (evt.attrs[i]) {
      case 'defaultAdapter':
        console.log("defaultAdapter changed. address:", manager.defaultAdapter.address);
        break;
      default:
        break;
    }
  }
}


=== onadapteradded ===
=== onadapteradded ===
; Description
: A handler to trigger when a bluetooth adapter is added (i.e., plugged in) to the device.


; Description
; Parameter
: A handler to trigger when a bluetooth adapter is plugged into the device. The event carries <tt>evt.adapter</tt> as the plugged [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter]. Applications should call [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getDefaultAdapter.28.29 getDefaultAdapter()] in this event handler to retrieve the current default adapter if there is no adapter plugged in, suggest to call [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29 getAdapters()] for the most updated array of adapters on the device.
: ''aAdapterEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#Interface|BluetoothAdapterEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#adapter|adapter]] as the added bluetooth adapter. If the added adapter becomes default adapter, [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|onattributechanged]] would also be triggered afterwards to reflect the change of property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]].


; Sample
; Sample
  function bt_onAdapterAdded(evt) {
  var manager = navigator.mozBluetooth;
manager.onadapteradded = function onAdapterAdded(evt) {
   var adapter = evt.adapter;
   var adapter = evt.adapter;
   // do your thing here!
   console.log("adapter with address", adapter.address, "is added");
  }
  }
var manager = navigator.mozBluetooth;
manager.onadapteradded = bt_onAdapterAdded;


=== onadapterremoved ===
=== onadapterremoved ===
; Description
: A handler to trigger when a bluetooth adapter is removed (i.e., unplugged) from the device.


; Description
; Parameter
: A handler to trigger when a bluetooth adapter is unplugged from the device. The event carries <tt>evt.address</tt> as the address of unplugged [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter]. The unplugged [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter] object is already unavailable and access to its attributes returns default values. Applications should call [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getDefaultAdapter.28.29 getDefaultAdapter()] in this event handler to retrieve the latest default adapter, and may call [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29 getAdapters()] for the latest array of adapters on the device.
: ''aAddressEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#Interface|BluetoothAdapterEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#address|address]] as the address of removed bluetooth adapter. If the removed adapter is default adapter, [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|onattributechanged]] would also be triggered afterwards to reflect the change of property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]].


; Sample
; Sample
  function bt_onAdapterRemoved(evt) {
  manager.onadapterremoved = function onAdapterRemoved(evt) {
   var address = evt.address;
   var address = evt.address;
   // do your thing here!
   console.log("adapter with address", address, "is removed");
  }
  }
var manager = navigator.mozBluetooth;
manager.onadapterremoved = bt_onAdapterRemoved;


== Methods ==
== Methods ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29 BluetoothManager.getAdapters()]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29|BluetoothManager.getAdapters()]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getDefaultAdapter.28.29 BluetoothManager.getDefaultAdapter()]


=== getAdapters() ===
=== getAdapters() ===


; Description
; Description
: The <tt>getAdapters</tt> method retrieves all adapters on the device.
: The method returns an array representing a snapshot of current adapters list on the device. Each getAdapters() call returns a new array instead of a reference to current adapters list, in order to prevent applications from modifying the shared adapters list.


; Return
; Return
: An array of [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter] objects.
: sequence<[[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]]>


; Sample
; Sample
  var manager = navigator.mozBluetooth;
  var adapters = navigator.mozBluetooth.getAdapters();
var adapters = manager.getAdapters();
 
for (var i = 0; i < adapters.length; i++) { 
=== getDefaultAdapter() ===
  console.log("address of adapter", i, ":", adapter[i].address);
 
  }
; Description
: The <tt>getDefaultAdapter</tt> method retrieves the default adapter used to connect to the remote bluetooth devices.
 
; Return
: The default [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter] object.
 
; Sample
  var manager = navigator.mozBluetooth;
var adapter = manager.getDefaultAdapter();


== See also ==
== See Also ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter BluetoothAdapter]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]]

Latest revision as of 03:20, 20 June 2014

Overview

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

Interfaces

BluetoothManager

[CheckPermissions="bluetooth"]
interface BluetoothManager: EventTarget
{
  readonly attribute BluetoothAdapter? defaultAdapter;

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

  sequence<BluetoothAdapter> getAdapters();
};

BluetoothManagerAttribute

enum BluetoothManagerAttribute
{
  "unknown",
  "defaultAdapter"
};

Properties

defaultAdapter

Description
The default adapter used to connect to the remote bluetooth devices.
Value type
BluetoothAdapter
Default value
Null pointer if no adapter exists.
Sample
var defaultAdapter = navigator.mozBluetooth.defaultAdapter;

Event Handlers

onattributechanged

Description
A handler to trigger when bluetooth manager's only property defaultAdapter has changed.
Parameter
aAttributeEvent
The event is a BluetoothAttributeEvent with property attrs that contains changed BluetoothManagerAttribute.
Sample
var manager = navigator.mozBluetooth;
manager.onattributechanged = function onManagerAttributeChanged(evt) {
  for (var i in evt.attrs) {
    switch (evt.attrs[i]) {
      case 'defaultAdapter':
        console.log("defaultAdapter changed. address:", manager.defaultAdapter.address);
        break;
      default:
        break;
    }
  }
}

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. If the added adapter becomes default adapter, onattributechanged would also be triggered afterwards to reflect the change of property defaultAdapter.
Sample
var manager = navigator.mozBluetooth;
manager.onadapteradded = function onAdapterAdded(evt) {
  var adapter = evt.adapter;
  console.log("adapter with address", adapter.address, "is added");
}

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. If the removed adapter is default adapter, onattributechanged would also be triggered afterwards to reflect the change of property defaultAdapter.
Sample
manager.onadapterremoved = function onAdapterRemoved(evt) {
  var address = evt.address;
  console.log("adapter with address", address, "is removed");
}

Methods

getAdapters()

Description
The method returns an array representing a snapshot of current adapters list on the device. Each getAdapters() call returns a new array instead of a reference to current adapters list, in order to prevent applications from modifying the shared adapters list.
Return
sequence<BluetoothAdapter>
Sample
var adapters = navigator.mozBluetooth.getAdapters();

for (var i = 0; i < adapters.length; i++) {   
  console.log("address of adapter", i, ":", adapter[i].address);
}

See Also