B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter: Difference between revisions
< B2G | Bluetooth | WebBluetooth-v2
Jump to navigation
Jump to search
mNo edit summary |
|||
| Line 3: | Line 3: | ||
== Interface == | == Interface == | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapter|BluetoothAdapter]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothStartDiscoveryRequest|BluetoothStartDiscoveryRequest]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState|BluetoothAdapterState]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterAttribute|BluetoothAdapterAttribute]] | ||
=== BluetoothAdapter === | === BluetoothAdapter === | ||
interface BluetoothAdapter: EventTarget | interface BluetoothAdapter: EventTarget | ||
{ | { | ||
readonly attribute BluetoothAdapterState [ | readonly attribute BluetoothAdapterState [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|state]]; | ||
readonly attribute DOMString [ | readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address|address]]; | ||
readonly attribute DOMString [ | readonly attribute DOMString [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|name]]; | ||
readonly attribute boolean [ | readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|discoverable]]; | ||
readonly attribute boolean [ | readonly attribute boolean [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|discovering]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|onattributechanged]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|ondevicepaired]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|ondeviceunpaired]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondisplaypasskeyreq|ondisplaypasskeyreq]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq|onenterpasskeyreq]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq|onpairingconfirmationreq]]; | ||
attribute EventHandler [ | attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq|onpairingconsentreq]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|enable()]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29|disable()]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28.29|setName(DOMString aName)]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28.29|setDiscoverable(boolean aDiscoverable)]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|startDiscovery()]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29|stopDiscovery()]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28.29|pair(DOMString aAddress)]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28.29|unpair(DOMString aAddress)]]; | ||
DOMRequest [ | DOMRequest [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#getPairedDevices.28.29|getPairedDevices()]]; | ||
}; | }; | ||
| Line 56: | Line 56: | ||
== Properties == | == Properties == | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#state|BluetoothAdapter.state]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#address|BluetoothAdapter.address]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#name|BluetoothAdapter.name]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discoverable|BluetoothAdapter.discoverable]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#discovering|BluetoothAdapter.discovering]] | ||
=== state === | === state === | ||
| Line 68: | Line 68: | ||
; Value type | ; Value type | ||
: [ | : [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#BluetoothAdapterState|BluetoothAdapterState]] | ||
; Default value | ; Default value | ||
| Line 138: | Line 138: | ||
== Event Handlers == | == Event Handlers == | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onattributechanged|BluetoothAdapter.onattributechanged]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondevicepaired|BluetoothAdapter.ondevicepaired]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondeviceunpaired|BluetoothAdapter.ondeviceunpaired]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#ondisplaypasskeyreq|BluetoothAdapter.ondisplaypasskeyreq]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onenterpasskeyreq|BluetoothAdapter.onenterpasskeyreq]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconfirmationreq|BluetoothAdapter.onpairingconfirmationreq]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#onpairingconsentreq|BluetoothAdapter.onpairingconsentreq]] | ||
=== onattributechanged === | === onattributechanged === | ||
| Line 153: | Line 153: | ||
; Paramter | ; Paramter | ||
: ''aAttributeEvent'' | : ''aAttributeEvent'' | ||
:: The event is a [ | :: 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 [ | :: 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 [ | :: 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 [ | :: 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 [ | :: 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 [ | :: 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 [ | :: 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 == | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#enable.28.29|BluetoothAdapter.enable()]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#disable.28.29|BluetoothAdapter.disable()]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setName.28DOMString_aName.29|BluetoothAdapter.setName(DOMString aName)]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#setDiscoverable.28boolean_aDiscoverable.29|BluetoothAdapter.setDiscoverable(boolean aDiscoverable)]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#startDiscovery.28.29|BluetoothAdapter.startDiscovery()]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#stopDiscovery.28.29|BluetoothAdapter.stopDiscovery()]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#pair.28DOMString_aDeviceAddress.29|BluetoothAdapter.pair(DOMString aDeviceAddress)]] | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothAdapter#unpair.28DOMString_aDeviceAddress.29|BluetoothAdapter.unpair(DOMString aDeviceAddress)]] | ||
* [ | * [[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, [ | : 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, [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 [ | : 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 == | ||
* [ | * [[B2G/Bluetooth/WebBluetooth-v2/BluetoothManager|BluetoothManager]] | ||
* [ | * [[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
- BluetoothAdapter.state
- BluetoothAdapter.address
- BluetoothAdapter.name
- BluetoothAdapter.discoverable
- BluetoothAdapter.discovering
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
- BluetoothAdapter.onattributechanged
- BluetoothAdapter.ondevicepaired
- BluetoothAdapter.ondeviceunpaired
- BluetoothAdapter.ondisplaypasskeyreq
- BluetoothAdapter.onenterpasskeyreq
- BluetoothAdapter.onpairingconfirmationreq
- BluetoothAdapter.onpairingconsentreq
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
- BluetoothAdapter.enable()
- BluetoothAdapter.disable()
- BluetoothAdapter.setName(DOMString aName)
- BluetoothAdapter.setDiscoverable(boolean aDiscoverable)
- BluetoothAdapter.startDiscovery()
- BluetoothAdapter.stopDiscovery()
- BluetoothAdapter.pair(DOMString aDeviceAddress)
- BluetoothAdapter.unpair(DOMString aDeviceAddress)
- BluetoothAdapter.getPairedDevices()
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]);
}
}