B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice
< B2G | Bluetooth | WebBluetooth-v2
Jump to navigation
Jump to search
Overview
The BluetoothDevice API provides information regarding a given Bluetooth device.
Interface
enum BluetoothDeviceAttribute { "cod", "name", "paired", "uuids" } interface BluetoothDevice: EventTarget { readonly attribute DOMString address; readonly attribute BluetoothClassOfDevice cod; readonly attribute DOMString name; readonly attribute boolean paired; readonly attribute DOMString[] uuids; attribute EventHandler onattributechanged; DOMRequest fetchUuids(); };
Attributes
- BluetoothDevice.address
- BluetoothDevice.cod
- BluetoothDevice.name
- BluetoothDevice.paired
- BluetoothDevice.uuids
address
- Description
- The address attribute provides the address of the device on the Bluetooth micro-network. This attribute must be non-empty value. Both empty and null string are regarded invalid.
- Value type
- DOMString
- Default value
- Empty string ("")
- Sample
var address = instanceOfBluetoothDevice.address;
- // TODO
- The attribute cannot be empty but the default value is empty string?
cod
- Description
- The class attribute value is a BluetoothClassOfDevice object that provides much information about the device's capabilities.
- Value type
- BluetoothClassOfDevice
- Default value
- A BluetoothClassOfDevice object with all attribute = 0
- Sample
var cod = instanceOfBluetoothDevice.cod; var majorDeviceClass = cod.BluetoothMajorDeviceClass; var majorServiceClass = cod.BluetoothMajorServiceClass; var minorDeviceClass = cod.BluetoothMinorDeviceClass;
name
- Description
- The name attribute provides the human readable name of the device.
- Value type
- DOMString
- Default value
- Empty string ("")
- Sample
var name = instanceOfBluetoothDevice.name;
paired
- Description
- The paired attribute indicates if the device is paired to the current device's adapter (true) or not (false).
- Value type
- boolean
- Default value
- false
- Sample
var paired = instanceOfBluetoothDevice.paired;
uuids
- Description
- The uuids attribute stores the cached UUID list of each Bluetooth service which the device is able to provide. To get the up-to-date list, call fetchUuids() to update this attribute.
- Value type
- An array of DOMString
- Default value
- An empty array (array with length = 0)
- Sample
var uuids = instanceOfBluetoothDevice.uuids;
Event Handlers
onattributechanged
- Description
- A handler for attributechanged event; it is triggered when certain attribute of the device is changed. The event carries evt.attr as the attribute changed, and evt.value as the updated value.
- Sample
function bt_onAttributeChanged(evt) { var attr = evt.attr; var value = evt.value; if (attr === BluetoothDeviceAttribute.name) { // do your thing here! } } instanceOfBluetoothDevice.onattributechanged = bt_onAttributeChanged;
Methods
fetchUuids()
- Description
- The fetchUuid method is used to fetch the up-to-date UUID list of each Bluetooth service which the device is able to provide.
- 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 DOMString objects.
- Sample
var req = instanceOfBluetoothDevice.fetchUuids(); req.onsuccess = functions(e) { var uuids = e.target.result; // do your things here! }