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

From MozillaWiki
Jump to navigation Jump to search
Line 39: Line 39:


=== 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 [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAddressEvent BluetoothAddressEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#adapter adapter] as the added bluetooth adapter and property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapterEvent#isDefault isDefault] indicating whether the added adapter is default adapter.


; Sample
; Sample
  function bt_onAdapterRemoved(evt) {
  function bt_onAdapterAdded(evt) {
   var address = evt.address;
   var address = evt.address;
   // do your thing here!
   var isDefaultAdapter = evt.isDefault;
  if (isDefaultAdapter) {
    // do your things here.
  }
  }
  }
   
   

Revision as of 03:50, 20 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.

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 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 BluetoothAddressEvent 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 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 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