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

From MozillaWiki
Jump to navigation Jump to search
 
(22 intermediate revisions by the same user not shown)
Line 2: Line 2:
'''BluetoothManager''' 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.


== Interface ==
== Interfaces ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManager|BluetoothManager]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManager|BluetoothManager]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]]


=== BluetoothManager ===
=== BluetoothManager ===
  [Func="Navigator::HasBluetoothSupport"]
  [CheckPermissions="bluetooth"]
  interface BluetoothManager: EventTarget
  interface BluetoothManager: EventTarget
  {
  {
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]] [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]];
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]]? [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]];
   
   
             attribute EventHandler     [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|onattributechanged]];
             attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|onattributechanged]];
             attribute EventHandler     [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|onadapteradded]];
             attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|onadapteradded]];
             attribute EventHandler     [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|onadapterremoved]];
             attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|onadapterremoved]];
   
   
   sequence<BluetoothAdapter>         [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29|getAdapters()]];
   sequence<BluetoothAdapter> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#getAdapters.28.29|getAdapters()]];
  };
  };


Line 22: Line 22:
  enum BluetoothManagerAttribute
  enum BluetoothManagerAttribute
  {
  {
  "unknown",
   "defaultAdapter"
   "defaultAdapter"
  }
  };


== Properties ==
== Properties ==
Line 48: Line 49:
=== onattributechanged ===
=== onattributechanged ===
; Description
; Description
: A handler to trigger when bluetooth manager's only property (defaultAdapter) has changed.
: A handler to trigger when bluetooth manager's only property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]] has changed.


; Parameter
; Parameter
: ''aAttributeEvent''
: ''aAttributeEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent|BluetoothAttributeEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent#attr|attr]] as a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]].
:: 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
; Sample
var manager = navigator.mozBluetooth;
  manager.onattributechanged = function onManagerAttributeChanged(evt) {
  manager.onattributechanged = function onManagerAttributeChanged(evt) {
  var attr = evt.attr;
  for (var i in evt.attrs) {
  var value = evt.value;
    switch (evt.attrs[i]) {
      case 'defaultAdapter':
  switch (attr) {
        console.log("defaultAdapter changed. address:", manager.defaultAdapter.address);
    case BluetoothManagerAttribute.defaultAdapter:
        break;
      console.log("defaultAdapter changed. address:", value.address);
      default:
      break;
        break;
    default:
    }
      break;
  }
  }
}
}


=== onadapteradded ===
=== onadapteradded ===
Line 74: Line 75:
; Parameter
; Parameter
: ''aAdapterEvent''
: ''aAdapterEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent|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 to reflect the change of property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]].
:: 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
var manager = navigator.mozBluetooth;
  manager.onadapteradded = function onAdapterAdded(evt) {
  manager.onadapteradded = function onAdapterAdded(evt) {
   var adapter = evt.adapter;
   var adapter = evt.adapter;
   // do your things here.
   console.log("adapter with address", adapter.address, "is added");
  }
  }


Line 88: Line 90:
; Parameter
; Parameter
: ''aAddressEvent''
: ''aAddressEvent''
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent|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 to reflect the change of property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#defaultAdapter|defaultAdapter]].
:: 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
  manager.onadapterremoved = function onAdapterRemoved(evt) {
  manager.onadapterremoved = function onAdapterRemoved(evt) {
   var address = evt.address;
   var address = evt.address;
   // do your things here.
   console.log("adapter with address", address, "is removed");
  }
  }


Line 111: Line 113:
   
   
  for (var i = 0; i < adapters.length; i++) {   
  for (var i = 0; i < adapters.length; i++) {   
   console.log("adapter", i, "address", adapter[i].address);
   console.log("address of adapter", i, ":", adapter[i].address);
  }
  }
== See Also ==
* [[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