B2G/Bluetooth/WebBluetooth-v2/BluetoothDevice
< B2G | Bluetooth | WebBluetooth-v2
Jump to navigation
Jump to search
Overview
The BluetoothDevice API provides information regarding a given remote bluetooth device.
Interface
BluetoothDevice
interface BluetoothDevice: EventTarget
{
readonly attribute DOMString address;
readonly attribute BluetoothClassOfDevice cod;
readonly attribute DOMString name;
readonly attribute boolean paired;
readonly attribute any uuids; // array of DOMString
attribute EventHandler onattributechanged;
DOMRequest fetchUuids();
};
BluetoothDeviceAttribute
enum BluetoothDeviceAttribute
{
"cod",
"name",
"paired",
"uuids"
}
Properties
- 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.
- Value type
- DOMString
- Default value
- Empty string ("")
- Sample
var address = instanceOfBluetoothDevice.address;
cod
- Description
- The cod attribute is a BluetoothClassOfDevice object that provides much information about the device's capabilities.
- Value type
- BluetoothClassOfDevice
- Default value
- A BluetoothClassOfDevice object whose attributes are default values.
- Sample
var cod = instanceOfBluetoothDevice.cod; var majorDeviceClass = cod.BluetoothMajorDeviceClass; var majorServiceClass = cod.BluetoothMajorServiceClass; var minorDeviceClass = cod.BluetoothMinorDeviceClass;
name
- Description
- The name attribute is 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 whether this remote device is paired to 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 that the remote device provides. If the up-to-date UUID list is required, 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 to trigger when an attribute of the remote device is changed. Note in this event handler, access to the changed attribute also gets the updated value.
- Paramter
- aAttributeEvent
- The event is a BluetoothAttributeEvent with attribute attr as a BluetoothDeviceAttribute.
- Sample
function bt_onAttributeChanged(evt) {
var attr = evt.attr;
var value = evt.value;
switch (attr) {
case BluetoothDeviceAttribute.name:
// do your things here
break;
case BluetoothDeviceAttribute.uuids:
// do your things here
break;
default:
break;
}
}
instanceOfBluetoothDevice.onattributechanged = bt_onAttributeChanged;
Methods
fetchUuids()
- Description
- The fetchUuid method fetches the up-to-date UUID list of each bluetooth service that the device provides.
- 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!
}