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

From MozillaWiki
Jump to navigation Jump to search
mNo edit summary
Line 3: Line 3:


== Interface ==
== Interface ==
* [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#BluetoothStartDiscoveryRequest BluetoothStartDiscoveryRequest]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothStartDiscoveryRequest|BluetoothStartDiscoveryRequest]]
* [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]]


=== BluetoothAdapter ===
=== BluetoothAdapter ===
  interface BluetoothAdapter: EventTarget
  interface BluetoothAdapter: EventTarget
  {
  {
   readonly attribute BluetoothAdapterState [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state state];
   readonly attribute BluetoothAdapterState [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]];
   readonly attribute DOMString [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address address];
   readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address|address]];
   readonly attribute DOMString [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name name];
   readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|name]];
   readonly attribute boolean [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable discoverable];
   readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]];
   readonly attribute boolean [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering discovering];
   readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]];
   
   
   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 [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondisplaypasskeyreq ondisplaypasskeyreq];
   attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondisplaypasskeyreq|ondisplaypasskeyreq]];
   attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq onenterpasskeyreq];
   attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq|onenterpasskeyreq]];
   attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq onpairingconfirmationreq];
   attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq|onpairingconfirmationreq]];
   attribute EventHandler [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq onpairingconsentreq];
   attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq|onpairingconsentreq]];
   
   
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29 enable()];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|enable()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29 disable()];
   DOMRequest [[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)];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28.29|setName(DOMString aName)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28.29 setDiscoverable(boolean aDiscoverable)];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28.29|setDiscoverable(boolean aDiscoverable)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29 startDiscovery()];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|startDiscovery()]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29 stopDiscovery()];
   DOMRequest [[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)];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28.29|pair(DOMString aAddress)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28.29 unpair(DOMString aAddress)];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28.29|unpair(DOMString aAddress)]];
   DOMRequest [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29 getPairedDevices()];
   DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29|getPairedDevices()]];
  };
  };


Line 56: Line 56:


== 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]]


=== state ===
=== state ===
Line 68: Line 68:


; 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 138: Line 138:


== 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#ondisplaypasskeyreq|BluetoothAdapter.ondisplaypasskeyreq]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq BluetoothAdapter.onenterpasskeyreq]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq|BluetoothAdapter.onenterpasskeyreq]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq BluetoothAdapter.onpairingconfirmationreq]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq|BluetoothAdapter.onpairingconfirmationreq]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq BluetoothAdapter.onpairingconsentreq]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq|BluetoothAdapter.onpairingconsentreq]]


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


; Sample
; Sample
Line 184: Line 184:
; Parameter
; Parameter
: ''aDeviceEvent''
: ''aDeviceEvent''
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent BluetoothDeviceEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#device device] as the paired remote bluetooth device.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent|BluetoothDeviceEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#device|device]] as the paired remote bluetooth device.


=== ondeviceunpaired ===
=== ondeviceunpaired ===
Line 192: Line 192:
; Parameter
; Parameter
: ''aDeviceEvent''
: ''aDeviceEvent''
:: The event is a [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent BluetoothDeviceEvent] with property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#address address] as the unpaired remote bluetooth device's address.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent|BluetoothDeviceEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDeviceEvent#address|address]] as the unpaired remote bluetooth device's address.


=== ondisplaypasskeyreq ===
=== ondisplaypasskeyreq ===
Line 200: Line 200:
; Parameter
; Parameter
: ''aPairingEvent''
: ''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/BluetoothPairingEvent#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.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent|BluetoothPairingEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#address|address]] as the remote bluetooth device's address, and property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#passkey|passkey]] as the passkey to display.


=== onenterpasskeyreq  ===
=== onenterpasskeyreq  ===
Line 208: Line 208:
; Parameter
; Parameter
: ''aPairingEvent''
: ''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/BluetoothPairingEvent#device device] as the remote bluetooth device and method [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#setPasskey.28DOMString_aPasskey.29 setPasskey] to reply the PIN code or passkey entered by user.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent|BluetoothPairingEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#device|device]] as the remote bluetooth device and method [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#setPasskey.28DOMString_aPasskey.29|setPasskey]] to reply the PIN code or passkey entered by user.


=== onpairingconfirmationreq ===
=== onpairingconfirmationreq ===
Line 216: Line 216:
; Parameter
; Parameter
: ''aPairingEvent''
: ''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/BluetoothPairingEvent#device device] as the remote bluetooth device, property [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#passkey passkey] as the passkey to confirm, and method [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#setPairingConfirmation.28boolean_aConfirm.29 setPairingConfirmation] to reply user confirmation.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent|BluetoothPairingEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#device|device]] as the remote bluetooth device, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#passkey|passkey]] as the passkey to confirm, and method [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#setPairingConfirmation.28boolean_aConfirm.29|setPairingConfirmation]] to reply user confirmation.


=== onpairingconsentreq ===
=== onpairingconsentreq ===
Line 224: Line 224:
; Parameter
; Parameter
: ''aPairingEvent''
: ''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/BluetoothPairingEvent#device device] as the remote bluetooth device.
:: The event is a [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent|BluetoothPairingEvent]] with property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothPairingEvent#device|device]] as the remote bluetooth device.


== Methods ==
== Methods ==
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29 BluetoothAdapter.enable()]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|BluetoothAdapter.enable()]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29 BluetoothAdapter.disable()]
* [[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)]
* [[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)]
* [[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()]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|BluetoothAdapter.startDiscovery()]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29 BluetoothAdapter.stopDiscovery()]
* [[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)]
* [[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)]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28DOMString_aDeviceAddress.29|BluetoothAdapter.unpair(DOMString aDeviceAddress)]]
* [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29 BluetoothAdapter.getPairedDevices()]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29|BluetoothAdapter.getPairedDevices()]]


=== enable() ===
=== enable() ===
Line 241: Line 241:
; Description
; Description
: The method turns on the local bluetooth adapter.
: The method turns on the local bluetooth adapter.
: This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>enabling</tt> and a corresponding [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered. If the enable operation succeeds, 3 more [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered before DOMRequest's onsuccess: the first and second ones indicate the address and name has been updated (with random order), and the last one must indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>enabled</tt>. In other words, all adapter's properties must be updated value once [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>enabled</tt>.
: This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] becomes <tt>enabling</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered. If the enable operation succeeds, 3 more [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before DOMRequest's onsuccess: the first and second ones indicate the address and name has been updated (with random order), and the last one must indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] becomes <tt>enabled</tt>. In other words, all adapter's properties must be updated value once [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] becomes <tt>enabled</tt>.


; Return
; Return
Line 261: Line 261:
; Description
; Description
: The method turns off the local bluetooth adapter.
: The method turns off the local bluetooth adapter.
: This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>disabling</tt> and a corresponding [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered. If the disable operation succeeds, several [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered before DOMRequest's onsuccess: the first ''(N-1)'' ones indicate all properties except [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] become default value, and the last one must indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>disabled</tt>. In other words, all adapter's properties are reset to default value once [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state adapter.state] becomes <tt>disabled</tt>. Note the value of ''N'' ranges from 2 to 4, depending on whether adapter was [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering discovering] / [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable discoverable].
: This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.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 DOMRequest's onsuccess: the first ''(N-1)'' ones indicate all properties except [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] become default value, and the last one must indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] becomes <tt>disabled</tt>. In other words, all adapter's properties are reset to default value once [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|adapter.state]] becomes <tt>disabled</tt>. Note the value of ''N'' ranges from 2 to 4, depending on whether adapter was [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]] / [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]].


; Return
; Return
Line 281: Line 281:
; Description
; Description
: The method sets 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.
: This is an asynchronous method and its result is returned via a DOMRequest. If the setName operation succeeds, an [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered before DOMRequest's onsuccess to indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name adapter.name] has changed.
: This is an asynchronous method and its result is returned via a DOMRequest. If the setName operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before DOMRequest's onsuccess to indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|adapter.name]] has changed.


; Parameters
; Parameters
Line 305: Line 305:


; Description
; Description
: The method changes the value of the bluetooth adapter's [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable discoverable] property.
: The method changes the value of the bluetooth adapter's [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]] property.
: This is an asynchronous method and the result is returned via a DOMRequest. If the setDiscoverable operation succeeds, an [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered before DOMRequest's onsuccess to indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable adapter.discoverable] has changed.
: This is an asynchronous method and the result is returned via a DOMRequest. If the setDiscoverable operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before DOMRequest's onsuccess to indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|adapter.discoverable]] has changed.


; Parameters   
; Parameters   
Line 329: Line 329:


; Description
; 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 [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] event would be triggered before [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest BluetoothStartDiscoveryRequest]'s onsuccess to indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering adapter.discovering] becomes false.
: 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 [[B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest|BluetoothStartDiscoveryRequest]]'s onsuccess to indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|adapter.discovering]] becomes false.


; Return
; Return
: A [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest BluetoothStartDiscoveryRequest] which extends DOMRequest to handle the discovered devices. Its onsuccess is fired to indicate startDiscovery operation has completed, and [https://wiki.mozilla.org/index.php?title=B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest#ondevicefound ondevicefound] is fired each time a remote bluetooth device is discovered. If the bluetooth adapter is currently disabled, the DOMRequest's onerror is fired.
: A [[B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest|BluetoothStartDiscoveryRequest]] which extends DOMRequest to handle the discovered devices. Its onsuccess is fired to indicate startDiscovery operation has completed, and [[index.php?title=B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest#ondevicefound|ondevicefound]] is fired each time a remote bluetooth device is discovered. If the bluetooth adapter is currently disabled, the DOMRequest's onerror is fired.


; Sample
; Sample
Line 355: Line 355:
; Description
; Description
: The method makes the device's adapter stop seeking for remote devices.
: The method makes the device's adapter stop seeking for remote devices.
: This is an asynchronous method and its result is returned via a DOMRequest. If the stopDiscovery operation succeeds, an [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged onattributechanged] would be triggered before DOMRequest's onsuccess to indicate [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering adapter.discovering] becomes false. Note adapter may still receive [https://wiki.mozilla.org/B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest#ondevicefound BluetoothStartDiscoveryRequest.ondevicefound] event until DOMRequest's onsuccess is fired.
: This is an asynchronous method and its result is returned via a DOMRequest. If the stopDiscovery operation succeeds, an [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]] would be triggered before DOMRequest's onsuccess to indicate [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|adapter.discovering]] becomes false. Note adapter may still receive [[B2G/Bluetooth/WebBluetooth-v2/BluetoothStartDiscoveryRequest#ondevicefound|BluetoothStartDiscoveryRequest.ondevicefound]] event until DOMRequest's onsuccess is fired.


; Return
; Return
Line 381: Line 381:


; 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 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 [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|ondevicepaired]] event handler is triggered.


; Sample
; Sample
Line 404: Line 404:


; 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 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 [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|ondeviceunpaired]] event handler is triggered.


=== getPairedDevices() ===
=== getPairedDevices() ===
Line 411: Line 411:


; 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 DOMRequest object to handle the success or error of the operation. In case of success, the request's result is an array of [[B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice|BluetoothDevice]] objects.


; Sample
; Sample
Line 428: Line 428:


== 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]]

Revision as of 01:58, 3 April 2014

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.

Interface

BluetoothAdapter

interface BluetoothAdapter: EventTarget
{
  readonly attribute BluetoothAdapterState state;
  readonly attribute DOMString address;
  readonly attribute DOMString name;
  readonly attribute boolean discoverable;
  readonly attribute boolean discovering;

  attribute EventHandler onattributechanged;
  attribute EventHandler ondevicepaired;
  attribute EventHandler ondeviceunpaired;
  attribute EventHandler ondisplaypasskeyreq;
  attribute EventHandler onenterpasskeyreq;
  attribute EventHandler onpairingconfirmationreq;
  attribute EventHandler onpairingconsentreq;

  DOMRequest enable();
  DOMRequest disable();
  DOMRequest setName(DOMString aName);
  DOMRequest setDiscoverable(boolean aDiscoverable);
  DOMRequest startDiscovery();
  DOMRequest stopDiscovery();
  DOMRequest pair(DOMString aAddress);
  DOMRequest unpair(DOMString aAddress);
  DOMRequest getPairedDevices();
};

BluetoothAdapterState

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

BluetoothAdapterAttribute

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

Properties

state

Description
The state of the local bluetooth adapter.
Value type
BluetoothAdapterState
Default value
BluetoothAdapterState.disabled
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
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.getDefaultAdapter();
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.getDefaultAdapter();
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.getDefaultAdapter();
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.getDefaultAdapter();
var discovering = adapter.discovering;

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 attr as a BluetoothAdapterAttribute.
Sample
function bt_onAttributeChanged(evt) {
  var attr = evt.attr;
  var value = evt.value;

  switch (attr) {
    case BluetoothAdapterAttribute.state:
      // do your things here
      break
    case BluetoothAdapterAttribute.name:
      // do your things here
      break;
    case BluetoothAdapterAttribute.discoverable:
      // do your things here
      break;
    default:
      break;
  }
}

var adapter = navigator.mozBluetooth.getDefaultAdapter();
adapter.onattributechanged = bt_onAttributeChanged;

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.

ondisplaypasskeyreq

Description
A handler to trigger when a remote bluetooth device requests to display passkey on the screen during pairing process.
Parameter
aPairingEvent
The event is a BluetoothPairingEvent with property address as the remote bluetooth device's address, and property passkey as the passkey to display.

onenterpasskeyreq

Description
A handler to trigger when a remote bluetooth device requests user enter PIN code or passkey during pairing process.
Parameter
aPairingEvent
The event is a BluetoothPairingEvent with property device as the remote bluetooth device and method setPasskey to reply the PIN code or passkey entered by user.

onpairingconfirmationreq

Description
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 BluetoothPairingEvent with property device as the remote bluetooth device, property passkey as the passkey to confirm, and method setPairingConfirmation to reply user confirmation.

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
aPairingEvent
The event is a BluetoothPairingEvent with property device as the remote bluetooth device.

Methods

enable()

Description
The method turns on the local bluetooth adapter.
This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, adapter.state becomes enabling and a corresponding onattributechanged would be triggered. If the enable operation succeeds, 3 more onattributechanged would be triggered before DOMRequest's onsuccess: the first and second ones indicate the address and name has been updated (with random order), and the last one must indicate adapter.state becomes enabled. In other words, all adapter's properties must be updated value once adapter.state becomes enabled.
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.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var req = adapter.enable();

req.onsuccess = function () {
  // do your things here
}
req.onerror = function () {
  // do your things here
}

disable()

Description
The method turns off the local bluetooth adapter.
This is an asynchronous method and its result is returned via a DOMRequest. Once the method is called, adapter.state becomes disabling and a corresponding onattributechanged would be triggered. If the disable operation succeeds, several onattributechanged would be triggered before DOMRequest's onsuccess: the first (N-1) ones indicate all properties except adapter.state become default value, and the last one must indicate adapter.state becomes disabled. In other words, all adapter's properties are reset to default value once adapter.state becomes disabled. Note the value of N ranges from 2 to 4, depending on whether adapter was discovering / discoverable.
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.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var req = adapter.disable();

req.onsuccess = function () {
  // do your things here
}
req.onerror = function () {
  // do your things here
}

setName(DOMString aName)

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 DOMRequest. If the setName operation succeeds, an onattributechanged would be triggered before DOMRequest's onsuccess to indicate adapter.name has changed.
Parameters
aName
A DOMString representing the new name to set.
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.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var name = "The name of bluetooth adapter";
var req = aAdapter.setName(name);

req.onsuccess = function () {
  // do your things here
}
req.onerror = function () {
  // do your things here
}

setDiscoverable(boolean aDiscoverable)

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 DOMRequest. If the setDiscoverable operation succeeds, an onattributechanged would be triggered before DOMRequest's onsuccess to indicate adapter.discoverable has changed.
Parameters
aDiscoverable
A boolean indicating if the device is discoverable (true) or not (false).
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.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var req = adapter.setDiscoverable(true);

req.onsuccess = function () {
  ...
}
req.onerror = function () {
  ...
}

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 BluetoothStartDiscoveryRequest's onsuccess to indicate adapter.discovering becomes false.
Return
A BluetoothStartDiscoveryRequest which extends DOMRequest to handle the discovered devices. Its onsuccess is fired to indicate startDiscovery operation has completed, and ondevicefound is fired each time a remote bluetooth device is discovered. If the bluetooth adapter is currently disabled, the DOMRequest's onerror is fired.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var req = adapter.startDiscovery();

req.ondevicefound = function (evt) {
  var device = evt.device;
  // do your things here
}

req.onsuccess = function () {
  // do your things here
}
req.onerror = function (evt) {
  var error = evt.error.name;
  // do your things here
}

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 DOMRequest. If the stopDiscovery operation succeeds, an onattributechanged would be triggered before DOMRequest's onsuccess to indicate adapter.discovering becomes false. Note adapter may still receive BluetoothStartDiscoveryRequest.ondevicefound event until DOMRequest's onsuccess is fired.
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.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var request = adapter.stopDiscovery();

req.onsuccess = function () {
  // do your things here
}
req.onerror = function () {
  // do your things here
}

pair(DOMString aDeviceAddress)

Description
The method starts pairing a remote device with the device's adapter.
Parameters
aDeviceAddress
A DOMString object representing the address of the device to pair with.
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 ondevicepaired event handler is triggered.
Sample
var adapter = navigator.mozBluetooth.getDefaultAdapter();
var req = adapter.pair('00:80:98:09:0B:5D');

req.onsuccess = function () {
  // do your things here
}
req.onerror = function () {
  // do your things here
}

unpair(DOMString aDeviceAddress)

Description
The method unpairs a remote device from the device's adapter.
Parameters
aDeviceAddress
A DOMString object representing the address of the device to unpair from.
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 ondeviceunpaired event handler is triggered.

getPairedDevices()

Description
The method retrieves the list of all devices paired with the device's adapter.
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 BluetoothDevice objects.
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