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

(remove 'a' prefix for method parameters)
 
(185 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== Overview ==
== Overview ==
The BluetoothAdapter API is used to handle all the operations requested by Bluetooth networks. A bluetooth adapter is the physical interface which is used to interact with local Bluetooth device. In the whole Bluetooth API, it's the most important interface because it is used to manage all the interactions between local bluetooth device and remote bluetooth devices.
'''BluetoothAdapter''' handles all the operations requested by bluetooth networks. A bluetooth adapter is the physical interface used to interact with local bluetooth device. BluetoothAdapter is the most important interface in the whole WebBluetooth API because it manages all the interactions between the local bluetooth device and remote bluetooth devices.


== Interface ==
== Interfaces ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter BluetoothAdapter]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState BluetoothAdapterState]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState|BluetoothAdapterState]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterAttribute BluetoothAdapterAttribute]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterAttribute|BluetoothAdapterAttribute]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdvertisingData|BluetoothAdvertisingData]]


=== BluetoothAdapter ===
=== BluetoothAdapter ===
[CheckPermissions="bluetooth"]
  interface BluetoothAdapter: EventTarget
  interface BluetoothAdapter: EventTarget
  {
  {
   readonly attribute BluetoothAdapterState [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state state];
   [AvailableIn=CertifiedApps] readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address|address]];
   readonly attribute DOMString [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address address];
  [AvailableIn=CertifiedApps] readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingListener#Interface|BluetoothPairingListener]] [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pairingReqs|pairingReqs]];  
   readonly attribute DOMString [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name name];
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState|BluetoothAdapterState]] [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]];
   readonly attribute boolean [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable discoverable];
   readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|name]];
   readonly attribute boolean [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering discovering];
   readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]];
  readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]];
   readonly attribute [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGattServer#BluetoothGattServer|BluetoothGattServer]]? [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#gattServer|gattServer]];
   
   
  attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]];
  attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired ondevicepaired];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|ondevicepaired]];
  attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired ondeviceunpaired];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|ondeviceunpaired]];
            attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingaborted|onpairingaborted]];
   
   
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29 enable()];
   [NewObject, AvailableIn=CertifiedApps] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|enable()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29 disable()];
   [NewObject, AvailableIn=CertifiedApps] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29|disable()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28.29 setName(DOMString aName)];
   [NewObject, AvailableIn=CertifiedApps] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28.29|setName(DOMString name)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28.29 setDiscoverable(boolean aDiscoverable)];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28.29|setDiscoverable(boolean discoverable)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29 startDiscovery()];
   [NewObject] Promise<[[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|startDiscovery()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29 stopDiscovery()];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29|stopDiscovery()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28.29 pair(DOMString aAddress)];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28.29|pair(DOMString address)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28.29 unpair()];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28.29|unpair(DOMString address)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setPasskey.28.29 setPasskey(DOMString aAddress, DOMString aPasskey)];
   sequence<[[B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#BluetoothDevice|BluetoothDevice]]> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29|getPairedDevices()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setPairingConfirmation.28.29 setPairingConfirmation(DOMString aAddress, boolean aConfirm)];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29 getPairedDevices()];
  // BLE methods
   [NewObject] Promise<[[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startLeScan.28sequence.3CDOMString.3E_serviceUuids.29|startLeScan(sequence<DOMString> aServiceUuids)]];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopLeScan.28BluetoothDiscoveryHandle_aDiscoveryHandle.29|stopLeScan(BluetoothDiscoveryHandle discoveryHandle)]];
  [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startAdvertising.28optional_BluetoothAdvertisingData_advData.29|startAdvertising(optional BluetoothAdvertisingData advData)]];
  [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopAdvertising.28.29|stopAdvertising()]];
  };
  };


Line 45: Line 54:
  enum BluetoothAdapterAttribute
  enum BluetoothAdapterAttribute
  {
  {
  "unknown",
   "state",
   "state",
   "address",
   "address",
Line 51: Line 61:
   "discovering"
   "discovering"
  }
  }
=== BluetoothAdvertisingData ===
dictionary BluetoothAdvertisingData
{
  unsigned short appearance = 0;
  boolean includeDevName = false;
  boolean includeTxPower = false;
  ArrayBuffer? manufacturerData = null;
  ArrayBuffer? serviceData = null;
  sequence<DOMString> serviceUuids = [];
};


== Properties ==
== Properties ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state BluetoothAdapter.state]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|BluetoothAdapter.state]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address BluetoothAdapter.address]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address|BluetoothAdapter.address]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name BluetoothAdapter.name]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|BluetoothAdapter.name]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable BluetoothAdapter.discoverable]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|BluetoothAdapter.discoverable]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering BluetoothAdapter.discovering]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|BluetoothAdapter.discovering]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pairingReqs|BluetoothAdapter.pairingReqs]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#gattServer|BluetoothAdapter.gattServer]]


=== state ===
=== state ===
; Description
; Description
: The state of the local bluetooth adapter.  
: The state of the local bluetooth adapter.  


; Value type
; Value type
: [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState BluetoothAdapterState]
: [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState|BluetoothAdapterState]]


; Default value
; Default value
Line 71: Line 93:


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
var state = adapter.state;
if (adapter) {
  var state = adapter.state;
}


=== address ===
=== address ===
; Description
; Description
: The address of the device's adapter on the bluetooth micro-network.
: The address of the device's adapter on the bluetooth micro-network.
Line 86: Line 109:


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
var address = adapter.address;
if (adapter) {
  var address = adapter.address;
}


=== name ===
=== name ===
; Description
; Description
: The human readable name of the device's adapter.
: The human readable name of the device's adapter.
Line 101: Line 125:


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
var name = adapter.name;
if (adapter) {
  var name = adapter.name;
}


=== discoverable ===
=== discoverable ===
; Description
; Description
: Indicates if the device is discoverable (true) or not (false) by other bluetooth devices.
: Indicates if the device is discoverable (true) or not (false) by other bluetooth devices.
Line 116: Line 141:


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
var discoverable = adapter.discoverable;
if (adapter) {
  var discoverable = adapter.discoverable;
}


=== discovering ===
=== discovering ===
; Description
; Description
: Indicates if the device is in the process of discovering (true) or not (false) surrounding bluetooth devices.
: Indicates if the device is in the process of discovering (true) or not (false) surrounding bluetooth devices.
Line 131: Line 157:


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var discovering = adapter.discovering;
if (adapter) {
  var discovering = adapter.discovering;
}
 
=== pairingReqs ===
; Description
: [AvailableIn=CertifiedApps]
: The property wraps event handlers triggered for different pairing types. This [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingListener#Interface|BluetoothPairingListener]] object is created/destroyed as [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]] is created/destroyed. The property is only available in certified applications to keep privileged applications from replying other applications' pairing requests.
 
; Value type
: [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingListener#Interface|BluetoothPairingListener]]
 
; Default value
: Null pointer
 
; Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter && adapter.pairingReqs) {
  adapter.pairingReqs.ondisplaypasskeyreq = function onDisplayPasskeyReq(evt) {
    var device = evt.device;
    var passkey = evt.handle.passkey; // passkey to display
    console.log("Pairing request from", device.name, ": display passkey");
    // display passkey to user
  }
  adapter.pairingReqs.onenterpincodereq = function onEnterPinCodeReq(evt) {
    var device = evt.device;
    console.log("Pairing request from", device.name, ": enter pin code");
    // inform user to enter pin code
   
    var pinCode = ''UserEnteredPinCode'';
    evt.handle.setPinCode(pinCode).then ( function onResolve() {
      console.log("Resolved setPinCode operation");
    }, function onReject(aReason) {
      console.log("Rejected with this reason: " + aReason);
    });
  }
  adapter.pairingReqs.onpairingconfirmationreq = function onPairingConfirmationReq(evt) {
    var device = evt.device;
    var passkey = evt.handle.passkey; // passkey for user to confirm
    console.log("Pairing request from", device.name, ": pairing confirmation");
    // display passkey for user confirm
    var confirm = ''UserConfirmedOrNot'';
    evt.handle.setPairingConfirmation(confirm).then ( function onResolve() {
      console.log("Resolved setPairingConfirmation operation");
    }, function onReject(aReason) {
      console.log("Rejected with this reason: " + aReason);
    });
  }
  adapter.pairingReqs.onpairingconsentreq = function onPairingConsentReq(evt) {
    var device = evt.device;
    console.log("Pairing request from", device.name, ": pairing consent");
    // notify user of just-work pairing
  }
}
 
=== gattServer ===
; Description
: gattServer property is the object to conduct GATT server operations on local bluetooth adapter. This property will be a null pointer if the adapter is not enabled.
 
; Value type
: [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGattServer#BluetoothGattServer|BluetoothGattServer]]
 
; Default value
: Null pointer
 
; Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var gattServer = adapter.gattServer;
}


== Event Handlers ==
== Event Handlers ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged BluetoothAdapter.onattributechanged]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|BluetoothAdapter.onattributechanged]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired BluetoothAdapter.ondevicepaired]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|BluetoothAdapter.ondevicepaired]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired BluetoothAdapter.ondeviceunpaired]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|BluetoothAdapter.ondeviceunpaired]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondisplaypasskeyreq BluetoothAdapter.ondisplaypasskeyreq]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingaborted|BluetoothAdapter.onpairingaborted]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq BluetoothAdapter.onenterpasskeyreq]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq BluetoothAdapter.onpairingconfirmationreq]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq BluetoothAdapter.onpairingconsentreq]


=== onattributechanged ===
=== onattributechanged ===


; Description
; Description
: A handler to trigger when a property of the remote device is changed. Note in this event handler, access to the changed property also gets the updated value.
: A handler to trigger when one of the local bluetooth adapter's properties has changed. Note access to the changed property in this event handler would get the updated value.


; Paramter
; Paramter
: ''aAttributeEvent''
: ''aAttributeEvent''
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent BluetoothAttributeEvent] with attribute [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAttributeEvent#attr attr] as a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice#BluetoothAdapterAttribute BluetoothAdapterAttribute].
:: 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/BluetoothAdapter#BluetoothAdapterAttribute|BluetoothAdapterAttributes]].


; Sample
; Sample
  function bt_onAttributeChanged(evt) {
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var attr = evt.attr;
  var value = evt.value;
   
   
   switch (attr) {
adapter.onattributechanged = function onAdapterAttributeChanged(evt) {
    case BluetoothAdapterAttribute.state:
   for (var i in evt.attrs) {
      // do your things here
    switch (evt.attrs[i]) {
      break
      case 'state':
    case BluetoothAdapterAttribute.name:
        console.log("adapter state changed to", adapter.state);
      // do your things here
        break
      break;
      case 'name':
    case BluetoothAdapterAttribute.discoverable:
        console.log("adapter name changed to", adapter.name);
      // do your things here
        break;
      break;
      case 'discoverable':
    default:
        console.log("adapter discoverable changed to", adapter.discoverable);
      break;
        break;
      default:
        break;
    }
   }
   }
  }
  };
''instanceOfBluetoothAdapter''.onattributechanged = bt_onAttributeChanged;


=== ondevicepaired ===
=== ondevicepaired ===
Line 179: Line 276:


; Parameter
; Parameter
: ''aDevice''
: ''aDeviceEvent''
:: A [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice BluetoothDevice] object representing the paired device.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#Interface|BluetoothDeviceEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#device|device]] as the paired remote bluetooth device.


=== ondeviceunpaired ===
=== ondeviceunpaired ===
Line 187: Line 284:


; Parameter
; Parameter
: ''aAddress''
: ''aDeviceEvent''
:: The address of the unpaired device.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#Interface|BluetoothDeviceEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#address|address]] as the unpaired remote bluetooth device's address.


=== ondisplaypasskeyreq ===
=== onpairingaborted ===
; Description
; Description
: A handler to trigger when a remote bluetooth device requests to display passkey on the screen during pairing process.
: A handler to trigger when pairing fails due to one of following conditions:
: - authentication fails
: - remote device down (bluetooth ACL becomes disconnected)
: - internal error happens


; Parameter
== Methods ==
: ''aPairingEvent''
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|BluetoothAdapter.enable()]]
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent BluetoothPairingEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAddressEvent#address address] as the remote bluetooth device's address, and property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#passkey passkey] as the passkey to display.
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29|BluetoothAdapter.disable()]]
 
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28DOMString_aName.29|BluetoothAdapter.setName(DOMString name)]]
=== onenterpasskeyreq  ===
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28boolean_aDiscoverable.29|BluetoothAdapter.setDiscoverable(boolean discoverable)]]
; Description
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|BluetoothAdapter.startDiscovery()]]
: A handler to trigger when a remote bluetooth device requests user enter PIN code or passkey during pairing process.
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29|BluetoothAdapter.stopDiscovery()]]
 
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28DOMString_aDeviceAddress.29|BluetoothAdapter.pair(DOMString deviceAddress)]]
; Parameter
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28DOMString_aDeviceAddress.29|BluetoothAdapter.unpair(DOMString deviceAddress)]]
: ''aAddressEvent''
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29|BluetoothAdapter.getPairedDevices()]]
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAddressEvent BluetoothAddressEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAddressEvent#address address] as the remote bluetooth device's address.
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startLeScan.28sequence.3CDOMString.3E_aServiceUuids.29|BluetoothAdapter.startLeScan(sequence<DOMString> serviceUuids)]]
 
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopLeScan.28BluetoothDiscoveryHandle_aDiscoveryHandle.29|BluetoothAdapter.stopLeScan(BluetoothDiscoveryHandle discoveryHandle)]]
=== onpairingconfirmationreq ===
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startAdvertising.28optional_BluetoothAdvertisingData_advData.29|startAdvertising(optional BluetoothAdvertisingData advData)]];
; Description
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopAdvertising.28.29|stopAdvertising()]];
: A handler to trigger when a remote bluetooth device requests user confirm passkey during pairing process. Applications may prompt passkey to user for confirmation, or confirm the passkey for user proactively.


; Parameter
: ''aPairingEvent''
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent BluetoothPairingEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAddressEvent#address address] as the remote bluetooth device's address, and property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#passkey passkey] as the passkey to confirm.
=== onpairingconsentreq ===
; Description
: A handler to trigger when a remote bluetooth device requests user confirm pairing during pairing process. Applications may prompt user for confirmation or confirm for user proactively.
; Parameter
: ''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/BluetoothAddressEvent#address address] as the remote bluetooth device's address.
== Methods ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29 BluetoothAdapter.enable()]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29 BluetoothAdapter.disable()]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28DOMString_aName.29 BluetoothAdapter.setName(DOMString aName)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28boolean_aDiscoverable.29 BluetoothAdapter.setDiscoverable(boolean aDiscoverable)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29 BluetoothAdapter.startDiscovery()]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29 BluetoothAdapter.stopDiscovery()]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28DOMString_aDeviceAddress.29 BluetoothAdapter.pair(DOMString aDeviceAddress)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28DOMString_aDeviceAddress.29 BluetoothAdapter.unpair(DOMString aDeviceAddress)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setPasskey.28DOMString_aDeviceAddress.2C_DOMString_aPasskey.29 BluetoothAdapter.setPasskey(DOMString aDeviceAddress, DOMString aPasskey)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setPairingConfirmation.28DOMString_aDeviceAddress.2C_boolean_aConfirm.29 BluetoothAdapter.setPairingConfirmation(DOMString aDeviceAddress, boolean aConfirm)]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29 BluetoothAdapter.getPairedDevices()]


=== enable() ===
=== enable() ===


; Description
; Description
: The <tt>enable</tt> method turns on the local bluetooth adapter.
: The method turns on the local bluetooth adapter.
: DOMRequest onsucess will be returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] which indicates state of the adapter is ''enabled''.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>enabling</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered. If the enable operation succeeds, several [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before the Promise is resolved to indicate the address and name has been updated, and the last one must indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>enabled</tt>. In other words, all adapter's properties except [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pairingReqs|pairingReqs]] and event handlers must be updated value once property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>enabled</tt>.
: ''instanceOfBluetoothAdapter''.state should be enabling when the function finish.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the enable operation has completed.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is not currently disabled, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.enable();
   
request.onsuccess = function () {
adapter.enable().then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== disable() ===
=== disable() ===


; Description
; Description
: The <tt>disable</tt> method turns off the local bluetooth adapter.
: The method turns off the local bluetooth adapter.
: DOMRequest's onsucess is called after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] which indicates state of the adapter is ''disabled''.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>disabling</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered. If the disable operation succeeds, several [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before the Promise is resolved to the indicate all properties (except [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]], [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pairingReqs|pairingReqs]], and event handlers) become default value, and the last one must indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>disabled</tt>. In other words, all adapter's properties except [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pairingReqs|pairingReqs]] and event handlers are reset to default value once property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]] becomes <tt>disabled</tt>.
: ''instanceOfBluetoothAdapter''.state should be ''disabling'' when the function finish.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the disable operation has completed. If the bluetooth adapter is currently disabled, the DOMRequest's onerror is called.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is not currently enabled, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.disable();
   
request.onsuccess = function () {
adapter.disable().then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== setName(DOMString aName) ===
=== setName(DOMString name) ===


; Description
; Description
: Set the human-readable name of the local bluetooth adapter. This name is visible to remote bluetooth devices.
: The method sets the human-readable name of the local bluetooth adapter. This name is visible to remote bluetooth devices.
: DOMRequest onsucess will be returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] is fired with the new name.
: This is an asynchronous method and its result is returned via a Promise. If the setName operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|name]] has changed.


; Parameters
; Parameters
: ''aName''
: ''name''
:: A string representing the new name to set.
:: A DOMString representing the new name to set.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the name has been set. If the bluetooth adapter is currently disabled or ''aName'' is identical to current name attribute, the DOMRequest's onerror is called.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled or ''aName'' is identical to current name attribute, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var name = "The name of Bluetooth adapter";
  var name = "The name of bluetooth adapter";
  var request = aAdapter.setName(name);
   
request.onsuccess = function () {
adapter.setName(name).then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== setDiscoverable(boolean aDiscoverable) ===
=== setDiscoverable(boolean discoverable) ===


; Description
; Description
: The <tt>setDiscoverable</tt> method changes the value of the [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable discoverable] attribute for the device's adapter.
: The method changes the value of the bluetooth adapter's [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]] property.
: DOMRequest onsucess will be returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] which indicates the adapter is discoverable (true) or not (false).
: This is an asynchronous method and the result is returned via a Promise. If the setDiscoverable operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]] has changed.


; Parameters   
; Parameters   
: ''aDiscoverable''
: ''discoverable''
:: A boolean indicating if the device is discoverable (true) or not (false).  
:: A boolean indicating if the device is discoverable (true) or not (false).  


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the adapter could currently be discovered. If the bluetooth adapter is currently disabled or ''aDiscoverable'' is identical to current discoverable attribute, the DOMRequest's onerror is called.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled or ''aDiscoverable'' is identical to current discoverable attribute, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.setDiscoverable(true);
 
request.onsuccess = function () {
  adapter.setDiscoverable(true).then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== startDiscovery() ===
=== startDiscovery() ===


; Description
; Description
: The <tt>startDiscovery</tt> method makes the device's adapter start seeking for remote devices. The discovery process may be terminated after discovering a period of time. An ''onattributechanged'' event would be fired in that case.
: The method makes the device's adapter start seeking for remote devices. The discovery process may be terminated after discovering a period of time. If the startDiscovery operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] event would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]] becomes true.
: DOMRequest onsucess will be returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] which indicates the adapter is discovering.


; Return
; Return
: A ''BluetoothStartDiscoveryRequest'' object which extends from DOMRequest to handle the discovered devices.
: A Promise to indicate whether the operation is resolved or rejected. If the Promise is resolved, it returns a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]. The [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]'s event handler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle#ondevicefound|ondevicefound]] is fired each time a remote bluetooth device is discovered. If the bluetooth adapter is currently discovering or disabled, the Promise would be rejected.
: '''BluetoothStartDiscoveryRequest.ondevicefound'''
:: ''ondevicefound(BluetoothDevice aDevice)'' is a event listener which receive devicefound events.
:: The event occurs with parameter ''aDevice'' when a Bluetooth device is discovered in the surrounding area.
:: It may or may not be fired. It depends on if there is any discoverable Bluetooth devices around.
If the bluetooth adapter is currently disabled, the DOMRequest's onerror is called.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.startDiscovery();
  var discoveryHandle;
  request.ondevicefound = function (evt) {
  var device = evt.device;
adapter.startDiscovery().then ( function onResolve(handle) {
  ''...''
  console.log("Resolved with discoveryHandle");
}
   
  request.onsuccess = function () {
  // Keep reference to handle in order to listen to ondevicefound event handler
   ''...''
  discoveryHandle = handle;
}
  discoveryHandle.ondevicefound = function onDeviceFound(evt) {
request.onerror = function (evt) {
    var device = evt.device;
  var error = evt.error.name;
    console.log("Discovered remote device. Address:", device.address);
  ''...''
  };
  }
  }, function onReject(aReason) {
   console.log("Rejected with this reason: " + aReason);
  });


=== stopDiscovery() ===
=== stopDiscovery() ===


; Description
; Description
: The <tt>stopDiscovery</tt> method makes the device's adapter stop seeking for remote devices.
: The method makes the device's adapter stop seeking for remote devices.
: DOMRequest ''onsucess'' will be returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] which indicates the adapter is not discovering.
: This is an asynchronous method and its result is returned via a Promise. If the stopDiscovery operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]] becomes false. Note adapter may still receive [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle#ondevicefound|BluetoothDiscoveryHandle.ondevicefound]] event until the Promise is resolved.
: Adapter may still receive ''ondevicefound'' event until onsuccess is fired.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. If the bluetooth adapter is currently disabled, the DOMRequest's onerror is called.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.stopDiscovery();
   
request.onsuccess = function () {
adapter.stopDiscovery().then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== pair(DOMString aDeviceAddress) ===
=== pair(DOMString deviceAddress) ===


; Description
; Description
: The <tt>pair</tt> method starts pairing a remote device with the device's adapter.
: The method starts pairing a remote device with the device's adapter.
: ''Note on Bluedroid stack, discovery has to be stopped before pairing (i.e., call stopDiscovery() before pair()) otherwise stack callbacks with pairing failure.''


; Parameters
; Parameters
: ''aDeviceAddress''
: ''deviceAddress''
:: A DOMString object representing the address of the device to pair with.
:: A DOMString object representing the address of the device to pair with.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the pair operation has completed. Note the DOMRequest's onsuccess is returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired ondevicepaired] event handler is triggered.
: A Promise to indicate whether the operation is resolved or rejected. Note event handler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|ondevicepaired]] would be triggered before the Promise is resolved.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  var request = adapter.pair('00:80:98:09:0B:5D');
   
request.onsuccess = function () {
adapter.pair('00:80:98:09:0B:5D').then ( function onResolve() {
   ''...''
   console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
   ''...''
  });
  }


=== unpair(DOMString aDeviceAddress) ===
=== unpair(DOMString deviceAddress) ===


; Description
; Description
: The <tt>unpair</tt> method unpairs a remote device from the device's adapter.
: The method unpairs a remote device from the device's adapter.


; Parameters
; Parameters
: ''aDeviceAddress''
: ''deviceAddress''
:: A DOMString object representing the address of the device to unpair from.
:: A DOMString object representing the address of the device to unpair from.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the unpair operation has completed. Note the DOMRequest's onsuccess is returned after [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired ondeviceunpaired] event handler is triggered.
: A Promise to indicate whether the operation is resolved or rejected. Note event handler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|ondeviceunpaired]] would be triggered before the Promise is resolved.


=== setPasskey(DOMString aDeviceAddress, DOMString aPasskey) ===
=== getPairedDevices() ===
; Description
: The method retrieves the list of all devices paired with the device's adapter.
 
; Return
: sequence<[[B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice|BluetoothDevice]]>
 
; Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
var pairedDevices = adapter.getPairedDevices();
for (var i = 0; i < pairedDevices.length; i++) { 
  console.log("address of paired device " + i + ":" + pairedDevices[i].address);
}
 
=== startLeScan(sequence<DOMString> serviceUuids) ===


; Description
; Description
: The <tt>setPasskey</tt> method replies the requested passkey when the device's adapter is pairing with a remote device.
: The method makes the device's adapter start seeking for remote LE devices advertising given services.


; Parameters
; Parameters
: ''aDeviceAddress''
: ''serviceUuids''
:: A DOMString object representing the address of the device to pair with.
:: A DOMString array of service UUIDs to seek for. Standardized service UUIDs are listed in [https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx here]. An empty array means to seek for devices advertising any service.
: ''aPasskey''
:: A DOMString object representing the passkey code set by user.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the setPasskey operation has completed.
: A Promise to indicate whether the operation is resolved or rejected. If the Promise is resolved, it returns a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]. The [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]]'s event handler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle#ondevicefound|ondevicefound]] is fired each time a remote bluetooth LE device is found. If the bluetooth adapter is currently disabled, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
const HEART_RATE_SERVICE = "0000180d-0000-1000-8000-00805f9b34fb";
  var request = adapter.setPasskey('00:80:98:09:0B:5D', '0000');
const BATTERY_SERVICE = "0000180f-0000-1000-8000-00805f9b34fb";
  request.onsuccess = function () {
  ''...''
  var adapter = navigator.mozBluetooth.defaultAdapter;
}
  var serviceUuids = [HEART_RATE_SERVICE, BATTERY_SERVICE];
  request.onerror = function () {
var discoveryHandle;
   ''...''
  }
adapter.startLeScan(serviceUuids).then ( function onResolve(handle) {
  console.log("Resolved with discoveryHandle");
   
  // Keep reference to handle in order to listen to ondevicefound event handler
  discoveryHandle = handle;
  discoveryHandle.ondevicefound = function onDeviceFound(evt) {
    var device = evt.device;
    var rssi = evt.rssi;
    var scanRecord = evt.scanRecord;
    console.log("Found remote LE device. Address:", device.address, "rssi:", rssi);
  };
  }, function onReject(aReason) {
   console.log("Rejected with this reason: " + aReason);
  });


; Note:
=== stopLeScan(BluetoothDiscoveryHandle discoveryHandle) ===
: This method is to reply <tt>onenterpasskeyreq</tt>. The passkey can be considered both PIN key reply (legacy pairing) and SSP passkey reply.
 
=== setPairingConfirmation(DOMString aDeviceAddress, boolean aConfirm) ===


; Description
; Description
: The <tt>setPairingConfirmation</tt> method replies the pairing confirmation when the device's adapter is pairing with a remote device.
: The method makes the device's adapter stop an ongoing scan of remote LE devices.


; Parameters
; Parameters
: ''aDeviceAddress''
: ''discoveryHandle''
:: A DOMString object representing the address of the device to pair with.
:: The [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDiscoveryHandle|BluetoothDiscoveryHandle]] of the ongoing scan to stop.
 
: ''aConfirm''
:: A boolean indicating if user confirms pairing with the remote device (true) or not (false).


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, it means that the setPairingConfirmation operation has completed.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.


; Sample
; Sample
  var adapter = navigator.mozBluetooth.getDefaultAdapter();
  var adapter = navigator.mozBluetooth.defaultAdapter;
  // when users press the confirmation button
  var discoveryHandle = ''discoveryHandle returned from startLeScan method''
  var request = adapter.setPairingConfirmation('00:80:98:09:0B:5D', true);
  request.onsuccess = function () {
  adapter.stopLeScan(discoveryHandle).then ( function onResolve() {
   ''...''
  console.log("Resolved with void value");
  }
  }, function onReject(aReason) {
request.onerror = function () {
   console.log("Rejected with this reason: " + aReason);
  ''...''
  });
}
 
=== startAdvertising(optional BluetoothAdvertisingData advData) ===
 
; Description
: Start to broadcast advertisements with an optional custom advertisement data.
 
; Parameters
: ''advData''
:: The custom advertisement data.


; Note
; Return
: This method is to reply <tt>onpairingconfirmationreq</tt> event
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.


=== getPairedDevices() ===
=== stopAdvertising() ===


; Description
; Description
: The <tt>getPairedDevices</tt> method retrieves the list of all devices paired with the device's adapter.
: Stop broadcasting advertisements.


; Return
; Return
: A DOMRequest object to handle the success or error of the operation. In case of success, the request's result is an array of [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice BluetoothDevice] objects.
: A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.
 
; Sample
var req = defaultAdapter.getPairedDevices();
req.onsuccess = function bt_getPairedSuccess() {
  var paired = req.result.slice();
  var length = paired.length;
  for (var i = 0; i < length; i++) {
    (function(device) {
      console.log(device.address);
    })(paired[i]);
  }
}


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

Latest revision as of 03:41, 22 December 2015

Overview

BluetoothAdapter handles all the operations requested by bluetooth networks. A bluetooth adapter is the physical interface used to interact with local bluetooth device. BluetoothAdapter is the most important interface in the whole WebBluetooth API because it manages all the interactions between the local bluetooth device and remote bluetooth devices.

Interfaces

BluetoothAdapter

[CheckPermissions="bluetooth"]
interface BluetoothAdapter: EventTarget
{
  [AvailableIn=CertifiedApps] readonly attribute DOMString address;
  [AvailableIn=CertifiedApps] readonly attribute BluetoothPairingListener pairingReqs; 
  readonly attribute BluetoothAdapterState state;
  readonly attribute DOMString name;
  readonly attribute boolean discoverable;
  readonly attribute boolean discovering;
  readonly attribute BluetoothGattServer? gattServer;

           attribute EventHandler onattributechanged;
           attribute EventHandler ondevicepaired;
           attribute EventHandler ondeviceunpaired;
           attribute EventHandler onpairingaborted;

  [NewObject, AvailableIn=CertifiedApps] Promise<void> enable();
  [NewObject, AvailableIn=CertifiedApps] Promise<void> disable();
  [NewObject, AvailableIn=CertifiedApps] Promise<void> setName(DOMString name);
  [NewObject] Promise<void> setDiscoverable(boolean discoverable);
  [NewObject] Promise<BluetoothDiscoveryHandle> startDiscovery();
  [NewObject] Promise<void> stopDiscovery();
  [NewObject] Promise<void> pair(DOMString address);
  [NewObject] Promise<void> unpair(DOMString address);
  sequence<BluetoothDevice> getPairedDevices();

  // BLE methods
  [NewObject] Promise<BluetoothDiscoveryHandle> startLeScan(sequence<DOMString> aServiceUuids);
  [NewObject] Promise<void> stopLeScan(BluetoothDiscoveryHandle discoveryHandle);
  [NewObject] Promise<void> startAdvertising(optional BluetoothAdvertisingData advData);
  [NewObject] Promise<void> stopAdvertising();
};

BluetoothAdapterState

enum BluetoothAdapterState
{
  "disabled",
  "disabling",
  "enabled",
  "enabling"
}

BluetoothAdapterAttribute

enum BluetoothAdapterAttribute
{
  "unknown",
  "state",
  "address",
  "name",
  "discoverable",
  "discovering"
}

BluetoothAdvertisingData

dictionary BluetoothAdvertisingData
{
  unsigned short appearance = 0;
  boolean includeDevName = false;
  boolean includeTxPower = false;
  ArrayBuffer? manufacturerData = null;
  ArrayBuffer? serviceData = null;
  sequence<DOMString> serviceUuids = [];
};

Properties

state

Description
The state of the local bluetooth adapter.
Value type
BluetoothAdapterState
Default value
BluetoothAdapterState.disabled
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var state = adapter.state;
}

address

Description
The address of the device's adapter on the bluetooth micro-network.
Value type
DOMString
Default value
Empty string ("")
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var address = adapter.address;
}

name

Description
The human readable name of the device's adapter.
Value type
DOMString
Default value
Empty string ("")
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var name = adapter.name;
}

discoverable

Description
Indicates if the device is discoverable (true) or not (false) by other bluetooth devices.
Value type
boolean
Default value
false
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var discoverable = adapter.discoverable;
}

discovering

Description
Indicates if the device is in the process of discovering (true) or not (false) surrounding bluetooth devices.
Value type
boolean
Default value
false
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var discovering = adapter.discovering;
}

pairingReqs

Description
[AvailableIn=CertifiedApps]
The property wraps event handlers triggered for different pairing types. This BluetoothPairingListener object is created/destroyed as BluetoothAdapter is created/destroyed. The property is only available in certified applications to keep privileged applications from replying other applications' pairing requests.
Value type
BluetoothPairingListener
Default value
Null pointer
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

if (adapter && adapter.pairingReqs) {
  adapter.pairingReqs.ondisplaypasskeyreq = function onDisplayPasskeyReq(evt) {
    var device = evt.device;
    var passkey = evt.handle.passkey; // passkey to display

    console.log("Pairing request from", device.name, ": display passkey");
    // display passkey to user
  }

  adapter.pairingReqs.onenterpincodereq = function onEnterPinCodeReq(evt) {
    var device = evt.device;
    console.log("Pairing request from", device.name, ": enter pin code");
    // inform user to enter pin code

    var pinCode = UserEnteredPinCode;
    evt.handle.setPinCode(pinCode).then ( function onResolve() {
      console.log("Resolved setPinCode operation");
    }, function onReject(aReason) {
      console.log("Rejected with this reason: " + aReason);
    });
  }

  adapter.pairingReqs.onpairingconfirmationreq = function onPairingConfirmationReq(evt) {
    var device = evt.device;
    var passkey = evt.handle.passkey; // passkey for user to confirm
    console.log("Pairing request from", device.name, ": pairing confirmation");
    // display passkey for user confirm

    var confirm = UserConfirmedOrNot;
    evt.handle.setPairingConfirmation(confirm).then ( function onResolve() {
      console.log("Resolved setPairingConfirmation operation");
    }, function onReject(aReason) {
      console.log("Rejected with this reason: " + aReason);
    });
  }

  adapter.pairingReqs.onpairingconsentreq = function onPairingConsentReq(evt) {
    var device = evt.device;
    console.log("Pairing request from", device.name, ": pairing consent");
    // notify user of just-work pairing
  }
}

gattServer

Description
gattServer property is the object to conduct GATT server operations on local bluetooth adapter. This property will be a null pointer if the adapter is not enabled.
Value type
BluetoothGattServer
Default value
Null pointer
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
if (adapter) {
  var gattServer = adapter.gattServer;
}

Event Handlers

onattributechanged

Description
A handler to trigger when one of the local bluetooth adapter's properties has changed. Note access to the changed property in this event handler would get the updated value.
Paramter
aAttributeEvent
The event is a BluetoothAttributeEvent with property attrs that contains changed BluetoothAdapterAttributes.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

adapter.onattributechanged = function onAdapterAttributeChanged(evt) {
  for (var i in evt.attrs) {
    switch (evt.attrs[i]) {
      case 'state':
        console.log("adapter state changed to", adapter.state);
        break
      case 'name':
        console.log("adapter name changed to", adapter.name);
        break;
      case 'discoverable':
        console.log("adapter discoverable changed to", adapter.discoverable);
        break;
      default:
        break;
    }
  }
};

ondevicepaired

Description
A handler to trigger when a remote device gets paired with local bluetooth adapter.
Parameter
aDeviceEvent
The event is a BluetoothDeviceEvent with property device as the paired remote bluetooth device.

ondeviceunpaired

Description
A handler to trigger when a remote device gets unpaired from local bluetooth adapter.
Parameter
aDeviceEvent
The event is a BluetoothDeviceEvent with property address as the unpaired remote bluetooth device's address.

onpairingaborted

Description
A handler to trigger when pairing fails due to one of following conditions:
- authentication fails
- remote device down (bluetooth ACL becomes disconnected)
- internal error happens

Methods


enable()

Description
The method turns on the local bluetooth adapter.
This is an asynchronous method and its result is returned via a Promise. Once the method is called, property state becomes enabling and a corresponding onattributechanged would be triggered. If the enable operation succeeds, several onattributechanged would be triggered before the Promise is resolved to indicate the address and name has been updated, and the last one must indicate property state becomes enabled. In other words, all adapter's properties except pairingReqs and event handlers must be updated value once property state becomes enabled.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is not currently disabled, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

adapter.enable().then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

disable()

Description
The method turns off the local bluetooth adapter.
This is an asynchronous method and its result is returned via a Promise. Once the method is called, property state becomes disabling and a corresponding onattributechanged would be triggered. If the disable operation succeeds, several onattributechanged would be triggered before the Promise is resolved to the indicate all properties (except state, pairingReqs, and event handlers) become default value, and the last one must indicate property state becomes disabled. In other words, all adapter's properties except pairingReqs and event handlers are reset to default value once property state becomes disabled.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is not currently enabled, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

adapter.disable().then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

setName(DOMString name)

Description
The method sets the human-readable name of the local bluetooth adapter. This name is visible to remote bluetooth devices.
This is an asynchronous method and its result is returned via a Promise. If the setName operation succeeds, an onattributechanged would be triggered before the Promise is resolved to indicate property name has changed.
Parameters
name
A DOMString representing the new name to set.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled or aName is identical to current name attribute, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
var name = "The name of bluetooth adapter";

adapter.setName(name).then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

setDiscoverable(boolean discoverable)

Description
The method changes the value of the bluetooth adapter's discoverable property.
This is an asynchronous method and the result is returned via a Promise. If the setDiscoverable operation succeeds, an onattributechanged would be triggered before the Promise is resolved to indicate property discoverable has changed.
Parameters
discoverable
A boolean indicating if the device is discoverable (true) or not (false).
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled or aDiscoverable is identical to current discoverable attribute, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
 
adapter.setDiscoverable(true).then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

startDiscovery()

Description
The method makes the device's adapter start seeking for remote devices. The discovery process may be terminated after discovering a period of time. If the startDiscovery operation succeeds, an onattributechanged event would be triggered before the Promise is resolved to indicate property discovering becomes true.
Return
A Promise to indicate whether the operation is resolved or rejected. If the Promise is resolved, it returns a BluetoothDiscoveryHandle. The BluetoothDiscoveryHandle's event handler ondevicefound is fired each time a remote bluetooth device is discovered. If the bluetooth adapter is currently discovering or disabled, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
var discoveryHandle;

adapter.startDiscovery().then ( function onResolve(handle) {
  console.log("Resolved with discoveryHandle");

  // Keep reference to handle in order to listen to ondevicefound event handler 
  discoveryHandle = handle;
  discoveryHandle.ondevicefound = function onDeviceFound(evt) {
    var device = evt.device;
    console.log("Discovered remote device. Address:", device.address);
  };
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

stopDiscovery()

Description
The method makes the device's adapter stop seeking for remote devices.
This is an asynchronous method and its result is returned via a Promise. If the stopDiscovery operation succeeds, an onattributechanged would be triggered before the Promise is resolved to indicate property discovering becomes false. Note adapter may still receive BluetoothDiscoveryHandle.ondevicefound event until the Promise is resolved.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

adapter.stopDiscovery().then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

pair(DOMString deviceAddress)

Description
The method starts pairing a remote device with the device's adapter.
Note on Bluedroid stack, discovery has to be stopped before pairing (i.e., call stopDiscovery() before pair()) otherwise stack callbacks with pairing failure.
Parameters
deviceAddress
A DOMString object representing the address of the device to pair with.
Return
A Promise to indicate whether the operation is resolved or rejected. Note event handler ondevicepaired would be triggered before the Promise is resolved.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;

adapter.pair('00:80:98:09:0B:5D').then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

unpair(DOMString deviceAddress)

Description
The method unpairs a remote device from the device's adapter.
Parameters
deviceAddress
A DOMString object representing the address of the device to unpair from.
Return
A Promise to indicate whether the operation is resolved or rejected. Note event handler ondeviceunpaired would be triggered before the Promise is resolved.

getPairedDevices()

Description
The method retrieves the list of all devices paired with the device's adapter.
Return
sequence<BluetoothDevice>
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
var pairedDevices = adapter.getPairedDevices();
for (var i = 0; i < pairedDevices.length; i++) {   
  console.log("address of paired device " + i + ":" + pairedDevices[i].address);
}

startLeScan(sequence<DOMString> serviceUuids)

Description
The method makes the device's adapter start seeking for remote LE devices advertising given services.
Parameters
serviceUuids
A DOMString array of service UUIDs to seek for. Standardized service UUIDs are listed in here. An empty array means to seek for devices advertising any service.
Return
A Promise to indicate whether the operation is resolved or rejected. If the Promise is resolved, it returns a BluetoothDiscoveryHandle. The BluetoothDiscoveryHandle's event handler ondevicefound is fired each time a remote bluetooth LE device is found. If the bluetooth adapter is currently disabled, the Promise would be rejected.
Sample
const HEART_RATE_SERVICE = "0000180d-0000-1000-8000-00805f9b34fb";
const BATTERY_SERVICE = "0000180f-0000-1000-8000-00805f9b34fb";

var adapter = navigator.mozBluetooth.defaultAdapter;
var serviceUuids = [HEART_RATE_SERVICE, BATTERY_SERVICE];
var discoveryHandle;

adapter.startLeScan(serviceUuids).then ( function onResolve(handle) {
  console.log("Resolved with discoveryHandle");

  // Keep reference to handle in order to listen to ondevicefound event handler 
  discoveryHandle = handle;
  discoveryHandle.ondevicefound = function onDeviceFound(evt) {
    var device = evt.device;
    var rssi = evt.rssi;
    var scanRecord = evt.scanRecord;
    console.log("Found remote LE device. Address:", device.address, "rssi:", rssi);
  };
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

stopLeScan(BluetoothDiscoveryHandle discoveryHandle)

Description
The method makes the device's adapter stop an ongoing scan of remote LE devices.
Parameters
discoveryHandle
The BluetoothDiscoveryHandle of the ongoing scan to stop.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.
Sample
var adapter = navigator.mozBluetooth.defaultAdapter;
var discoveryHandle = discoveryHandle returned from startLeScan method

adapter.stopLeScan(discoveryHandle).then ( function onResolve() {
  console.log("Resolved with void value");
}, function onReject(aReason) {
  console.log("Rejected with this reason: " + aReason);
});

startAdvertising(optional BluetoothAdvertisingData advData)

Description
Start to broadcast advertisements with an optional custom advertisement data.
Parameters
advData
The custom advertisement data.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.

stopAdvertising()

Description
Stop broadcasting advertisements.
Return
A Promise to indicate whether the operation is resolved or rejected. If the bluetooth adapter is currently disabled, the Promise would be rejected.

See also