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

From MozillaWiki
Jump to navigation Jump to search
No edit summary
(remove auto connect)
Line 16: Line 16:
             attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]];
             attribute EventHandler [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]];
   
   
   [NewObject, Throws] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28boolean_autoConnect.29|connect(boolean autoConnect)]];
   [NewObject, Throws] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28.29|connect()]];
   [NewObject, Throws] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|disconnect()]];
   [NewObject, Throws] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|disconnect()]];
   [NewObject, Throws] Promise<short> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|readRemoteRssi()]];
   [NewObject, Throws] Promise<short> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|readRemoteRssi()]];
Line 91: Line 91:


== Methods ==
== Methods ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28boolean_autoConnect.29|BluetoothGatt.connect(boolean autoConnect)]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28.29|BluetoothGatt.connect()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|BluetoothGatt.disconnect()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|BluetoothGatt.disconnect()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|BluetoothGatt.readRemoteRssi()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|BluetoothGatt.readRemoteRssi()]]
Line 98: Line 98:
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#abortReliableWrite.28.29|BluetoothGatt.abortReliableWrite()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#abortReliableWrite.28.29|BluetoothGatt.abortReliableWrite()]]


=== connect(boolean autoConnect) ===
=== connect() ===


; Description
; Description
: The method is used connect to the remote LE device.
: The method is used connect to the remote LE device.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>connecting</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered. If the connect operation succeeds, another [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>connected</tt>.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>connecting</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered. If the connect operation succeeds, another [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>connected</tt>.
; Parameter
: ''autoConnect''
:: Whether to directly connect to the remote device (false) or to automatically connect as soon as the remote device becomes available (true).


; Return
; Return
Line 120: Line 116:
   });
   });
  }
  }
; Note
: Auto connection establishment procedure defined in bluetooth specification(3.C.9.3.5) is not support yet.
: Please see [https://bugzilla.mozilla.org/show_bug.cgi?id=1126123 Bug 1126123] for details.


=== disconnect() ===
=== disconnect() ===


; Description
; Description
: The method disconnects an established connection or cancels an ongoing connection.
: The method disconnects an established connection.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>disconnecting</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered. If the connect operation succeeds, another [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>disconnected</tt>.
: This is an asynchronous method and its result is returned via a Promise. Once the method is called, property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>disconnecting</tt> and a corresponding [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered. If the connect operation succeeds, another [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechanged|onconnectionstatechanged]] would be triggered before the Promise is resolved to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] becomes <tt>disconnected</tt>.


; Return
; Return
: A Promise to indicate whether the operation is resolved or rejected. If the property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] is not <tt>connected</tt> or <tt>connecting</tt> when the method is called, the Promise would be rejected.
: A Promise to indicate whether the operation is resolved or rejected. If the property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] is not <tt>connected</tt> when the method is called, the Promise would be rejected.


; Sample
; Sample

Revision as of 03:12, 27 January 2015

Overview

BluetoothGatt provides bluetooth Generic Attribute Profile (GATT) client functionality to enable communication with a remote LE device.

Interface

BluetoothGatt

[CheckPermissions="bluetooth"]
interface BluetoothGatt : EventTarget
{
  [Cached, Pure]
  readonly attribute sequence<BluetoothGattService> services;
  readonly attribute BluetoothConnectionState connectionState;

           attribute EventHandler oncharacteristicchanged;
           attribute EventHandler onconnectionstatechanged;

  [NewObject, Throws] Promise<void> connect();
  [NewObject, Throws] Promise<void> disconnect();
  [NewObject, Throws] Promise<short> readRemoteRssi();
  // Reliable write
  [NewObject, Throws] Promise<void> beginReliableWrite();
  [NewObject, Throws] Promise<void> executeReliableWrite();
  [NewObject, Throws] Promise<void> abortReliableWrite();
};

BluetoothConnectionState

enum BluetoothConnectionState {
  "disconnected",
  "disconnecting",
  "connected",
  "connecting"
};

Properties

services

Description
The list of GATT services offered by the remote LE device. This property is set to default value (an empty array) before connection is established.
Value type
sequence<BluetoothGattService>
Default Value
An empty array (array with length = 0)

connectionState

Description
The current connection state of GATT client to the remote LE device. This property is set to default value (BluetoothConnectionState.disconnected) before connection is established.
Value type
BluetoothConnectionState
Default Value
BluetoothConnectionState.disconnected

Event Handlers

oncharacteristicchanged

Description
A handler to trigger as a result of a remote characteristic notification.
Parameter
characteristicEvent
The event is a BluetoothGattCharacteristicEvent with property characteristic as the changed characteristic.
Sample
var gatt = device.gatt;
if (gatt) {
  gatt.oncharacteristicchanged = function onCharacteristicChanged(evt) {
    var characteristic = evt.characteristic;
    console.log("The value of characteristic (uuid:", characteristic.uuid, ") changed to", characteristic.value);
  };
}

onconnectionstatechanged

Description
A handler to trigger when current connection state of GATT client to the remote LE device (i.e., property connectionState) has changed.
Sample
var gatt = device.gatt;
if (gatt) {
  gatt.onconnectionstatechanged = function onConnectionStateChanged() {
    console.log("Connection state changed to", gatt.connectionState);
  };
}

Methods

connect()

Description
The method is used connect to the remote LE device.
This is an asynchronous method and its result is returned via a Promise. Once the method is called, property connectionState becomes connecting and a corresponding onconnectionstatechanged would be triggered. If the connect operation succeeds, another onconnectionstatechanged would be triggered before the Promise is resolved to indicate property connectionState becomes connected.
Return
A Promise to indicate whether the operation is resolved or rejected. The Promise would be rejected if 1) property connectionState is not disconnected when the method is called, 2) autoConnect is false and the remote LE device is not in range, or 3) BluetoothGatt.disconnect() is issued and succeeded before the connect process is done.
Sample
var gatt = device.gatt;
if (gatt) {
  gatt.connect(false).then ( function onResolve() {
    console.log("Re-connection succeeds. ConnectionState becomes:", gatt.connectionState);
  }, function onReject(aReason) {
    console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
  });
}
Note
Auto connection establishment procedure defined in bluetooth specification(3.C.9.3.5) is not support yet.
Please see Bug 1126123 for details.

disconnect()

Description
The method disconnects an established connection.
This is an asynchronous method and its result is returned via a Promise. Once the method is called, property connectionState becomes disconnecting and a corresponding onconnectionstatechanged would be triggered. If the connect operation succeeds, another onconnectionstatechanged would be triggered before the Promise is resolved to indicate property connectionState becomes disconnected.
Return
A Promise to indicate whether the operation is resolved or rejected. If the property connectionState is not connected when the method is called, the Promise would be rejected.
Sample
var gatt = device.gatt;
if (gatt) {
  gatt.disconnect().then ( function onResolve() {
    console.log("Disconnection succeeds. ConnectionState becomes:", gatt.connectionState);
  }, function onReject(aReason) {
    console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
  };
}

readRemoteRssi()

Description
The method reads the RSSI for a connected remote LE device.
Return
A Promise to indicate whether the operation is resolved or rejected. If the Promise is resolved, it returns a short integer representing the RSSI value. If the property connectionState is not connected when the method is called, the Promise would be rejected.
Sample
var gatt = device.gatt;
if (gatt && gatt.connectionState === "connected") {
  gatt.readRemoteRssi().then ( function onResolve(rssi) {
    console.log("Remote RSSI value:", rssi);
  }, function onReject(aReason) {
    console.log("Rejected with this reason: ", aReason);
  };
}

beginReliableWrite()

Description
The method initiates a reliable write transaction for the remote LE device.
Once a reliable write transaction has been initiated, all calls to characteristic.writeValue() are sent to the remote device for verification and queued up for atomic execution. An Promise that carries the written value is returned in response to every characteristic.writeValue() call and the application is responsible for verifying whether the value has been transmitted accurately. After all characteristics have been queued up and verified, executeReliableWrite() will execute all writes. If a characteristic was not written correctly, calling abortReliableWrite() will cancel the current transaction without committing any values on the remote LE device.
Return
A Promise to indicate whether the operation is resolved or rejected.

executeReliableWrite()

Description
The method executes a reliable write transaction for the remote LE device. It will commit all queued up characteristic value write operations for the remote LE device.
Return
A Promise to indicate whether the operation is resolved or rejected.

abortReliableWrite()

Description
The method cancels a reliable write transaction for the remote LE device. Calling this method will discard all queued characteristic value write operations for the remote LE device.
Return
A Promise to indicate whether the operation is resolved or rejected.

See Also