Changes

Jump to: navigation, search

B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt

246 bytes removed, 10:08, 22 January 2015
Methods
== Methods ==
* [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connect.2828boolean_autoConnect.29|BluetoothGatt.connect(boolean autoConnect)]]
* [[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#abortReliableWrite.28.29|BluetoothGatt.abortReliableWrite()]]
=== connect(boolean autoConnect) ===
; Description
: The method is used to re-connect to the remote LE device after the connection has been dropped.
: 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
: 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) autoConnect is false and the remote LE device is not in range. The re-connection will be triggered once the device is back in range and, if re-connection succeeds, an or 3) [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#onconnectionstatechangeddisconnect.28.29|onconnectionstatechanged]] would be fired to indicate property [[B2G/Bluetooth/WebBluetooth-v2/BluetoothGatt#connectionState|connectionState.disconnect()]] becomes <tt>connected</tt>is issued and succeeded before the connect process is done.
; Sample
var gatt = device.gatt;
if (gatt) {
gatt.onconnectionstatechanged = function onConnectionStateChanged() { console.log("Connection state changed to", gatt.connectionState); if (gatt.connectionState === "disconnected") { console.log("Re-connect to remote LE device."); 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); }); } };
}
; Description
: The method disconnects an established connection or cancels an ongoing 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>.
; 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.
; Sample
var gatt = device.gatt;
if (gatt && gatt.connectionState === "connected") {
gatt.disconnect().then ( function onResolve() {
console.log("Disconnection succeeds. ConnectionState becomes:", gatt.connectionState);
120
edits

Navigation menu