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

From MozillaWiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 18: Line 18:
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28.29|connect()]];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28.29|connect()]];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|disconnect()]];
   [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|disconnect()]];
  [NewObject] Promise<void> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#discoverServices.28.29|discoverServices()]];
   [NewObject] Promise<short> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|readRemoteRssi()]];
   [NewObject] Promise<short> [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|readRemoteRssi()]];
   // Reliable write
   // Reliable write
Line 93: Line 94:
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.28.29|BluetoothGatt.connect()]]
* [[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#discoverServices.28.29|BluetoothGatt.discoverServices()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|BluetoothGatt.readRemoteRssi()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#readRemoteRssi.28.29|BluetoothGatt.readRemoteRssi()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#beginReliableWrite.28.29|BluetoothGatt.beginReliableWrite()]]
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#beginReliableWrite.28.29|BluetoothGatt.beginReliableWrite()]]
Line 105: Line 107:


; Return
; Return
: A Promise to indicate whether the operation is resolved or rejected. The Promise would be rejected if 1) property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] is not <tt>disconnected</tt> when the method is called, 2) autoConnect is false and the remote LE device is not in range, or 3) [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#disconnect.28.29|BluetoothGatt.disconnect()]] is issued and succeeded before the connect process is done.
: A Promise to indicate whether the operation is resolved or rejected. The Promise would be rejected if 1) property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState]] is not <tt>disconnected</tt> when the method is called, or 2) the remote LE device is not in range.


; Sample
; Sample
Line 111: Line 113:
  if (gatt) {
  if (gatt) {
   gatt.connect(false).then ( function onResolve() {
   gatt.connect(false).then ( function onResolve() {
     console.log("Re-connection succeeds. ConnectionState becomes:", gatt.connectionState);
     console.log("Connection established. ConnectionState becomes:", gatt.connectionState);
   }, function onReject(aReason) {
   }, function onReject(aReason) {
     console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
     console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
Line 118: Line 120:


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


Line 137: Line 139:
   }, function onReject(aReason) {
   }, function onReject(aReason) {
     console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
     console.log("Rejected with this reason: ", aReason, ". ConnectionState becomes:", gatt.connectionState);
  };
}
=== discoverServices() ===
; Description
: The method discovers services, characteristics, and descriptors offered by the remote GATT server.
; 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> when the method is called, the Promise would be rejected.
; Sample
var gatt = device.gatt;
if (gatt && gatt.connectionState === "connected") {
  gatt.discoverServices().then ( function onResolve() {
    console.log("Services, characteristics, and descriptors are successfully discovered.");
  }, function onReject(aReason) {
    console.log("Rejected with this reason: ", aReason);
   };
   };
  }
  }

Latest revision as of 08:01, 28 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] Promise<void> connect();
  [NewObject] Promise<void> disconnect();
  [NewObject] Promise<void> discoverServices();
  [NewObject] Promise<short> readRemoteRssi();
  // Reliable write
  [NewObject] Promise<void> beginReliableWrite();
  [NewObject] Promise<void> executeReliableWrite();
  [NewObject] 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 connects 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, or 2) the remote LE device is not in range.
Sample
var gatt = device.gatt;
if (gatt) {
  gatt.connect(false).then ( function onResolve() {
    console.log("Connection established. 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 supported 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);
  };
}

discoverServices()

Description
The method discovers services, characteristics, and descriptors offered by the remote GATT server.
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.connectionState === "connected") {
  gatt.discoverServices().then ( function onResolve() {
    console.log("Services, characteristics, and descriptors are successfully discovered.");
  }, function onReject(aReason) {
    console.log("Rejected with this reason: ", aReason);
  };
}

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