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

Jump to navigation Jump to search
no edit summary
No edit summary
Line 3: Line 3:


== Interface ==
== Interface ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManager|BluetoothManager]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#BluetoothManagerAttribute|BluetoothManagerAttribute]]
=== BluetoothManager ===
[Func="Navigator::HasBluetoothSupport"]
  interface BluetoothManager: EventTarget
  interface BluetoothManager: EventTarget
  {
  {
   readonly attribute boolean      defaultAdapter;
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]] 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()]];
  };
  };
=== BluetoothManagerAttribute ===
enum BluetoothManagerAttribute
{
  "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 ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onattributechanged|BluetoothManager.onattributechanged]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|BluetoothManager.onadapteradded]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapteradded|BluetoothManager.onadapteradded]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|BluetoothManager.onadapterremoved]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager#onadapterremoved|BluetoothManager.onadapterremoved]]
=== onattributechanged ===
; Description
: A handler to trigger when bluetooth manager's only property (defaultAdapter) has changed.
; Parameter
: ''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]].
; Sample
manager.onattributechanged = function onManagerAttributeChanged(evt) {
  var attr = evt.attr;
  var value = evt.value;
  switch (attr) {
    case BluetoothManagerAttribute.defaultAdapter:
      console.log("defaultAdapter changed. address:", value.address);
      break;
    default:
      break;
  }
}


=== onadapteradded ===
=== onadapteradded ===
; Description
; Description
: A handler to trigger when a bluetooth adapter is added (i.e., plugged in) to the device.
: A handler to trigger when a bluetooth adapter is added (i.e., plugged in) to the device.
Line 25: Line 74:
; 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 and property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#isDefault|isDefault]] indicating whether the added adapter is default adapter.
:: 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]].


; Sample
; Sample
  function bt_onAdapterAdded(evt) {
  manager.onadapteradded = function onAdapterAdded(evt) {
   var adapter = evt.adapter;
   var adapter = evt.adapter;
   var isDefaultAdapter = evt.isDefault;
   // do your things here.
  if (isDefaultAdapter) {
    // do your things here.
  }
  }
  }
var manager = navigator.mozBluetooth;
manager.onadapteradded = bt_onAdapterAdded;


=== onadapterremoved ===
=== onadapterremoved ===
Line 46: Line 88:
; 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 and property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#isDefault|isDefault]] indicating whether the removed adapter is default adapter.
:: 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]].


; Sample
; Sample
  function bt_onAdapterRemoved(evt) {
  manager.onadapterremoved = function onAdapterRemoved(evt) {
   var address = evt.address;
   var address = evt.address;
   var isDefaultAdapter = evt.isDefault;
   // do your things here.
  if (isDefaultAdapter) {
    // do your things here.
  }
  }
  }
var manager = navigator.mozBluetooth;
manager.onadapterremoved = bt_onAdapterRemoved;


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


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


; Description
; 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.
: 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
: A [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]] array.
: 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("adapter", i, "address", adapter[i].address);
 
  }
; Description
: The method retrieves the default adapter used to connect to the remote bluetooth devices.
 
; Return
: The default [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]] object.
 
; Sample
  var manager = navigator.mozBluetooth;
var adapter = manager.getDefaultAdapter();
 
== See also ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter|BluetoothAdapter]]
Confirmed users
891

edits

Navigation menu