Package ej.bluetooth

## Class BluetoothAdapter

- java.lang.Object
- - ej.bluetooth.BluetoothAdapter

-     public class BluetoothAdapter
      extends Object

  The `BluetoothAdapter` class represents a Bluetooth adapter.

  The adapter of the system may be retrieved by calling `getAdapter()`. The GAP events related to the adapter may be handled by `setting the connection listener` of the adapter. The adapter can be `enabled` or `disabled`.

  A Bluetooth adapter is able to scan nearby devices and to initiate a connection with a device. It is also able to send advertisements to nearby devices. GATT services may be added to the adapter so that they can be used by connected devices. This class also provides a method to retrieve the list of active connections.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `BluetoothAdapter()` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `BluetoothService` | `addService​(BluetoothServiceDefinition serviceDefinition)` | Adds the given service to this adapter.
    `boolean` | `connect​(BluetoothAddress address)` | Initiates a connection to the device with the given address.
    `void` | `disable()` | Disables this adapter.
    `boolean` | `enable()` | Enables this adapter.
    `static BluetoothAdapter` | `getAdapter()` | Returns the adapter of the system.
    `void` | `setConnectionListener​(ConnectionListener connectionListener)` | Sets the listener which will receive the GAP events related to this adapter.
    `boolean` | `startAdvertising​(byte[] advertisementData)` | Starts advertising.
    `boolean` | `startScanning​(BluetoothScanFilter scanFilter)` | Starts scanning.
    `boolean` | `stopAdvertising()` | Stops advertising.
    `boolean` | `stopScanning()` | Stops scanning.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### BluetoothAdapter

          public BluetoothAdapter()

  - ### Method Detail

    - #### getAdapter

          public static BluetoothAdapter getAdapter()

      Returns the adapter of the system. The adapter has to be `enabled` before being used.
      Returns:
        the adapter of the system.

    - #### enable

          public boolean enable()

      Enables this adapter.

      If this adapter is already enabled, this method returns `true` immediately.

      Returns:
        true if this adapter was enabled successfully, false otherwise.

    - #### disable

          public void disable()

      Disables this adapter. This method frees up all resources allocated by this adapter, including all the services added using `addService()`.

      If this adapter is already disabled, calling this method has no effect.

    - #### addService

          @Nullable
          public BluetoothService addService​(BluetoothServiceDefinition serviceDefinition)

      Adds the given service to this adapter.
      Parameters:
        `serviceDefinition` - the definition of the service to add.
      Returns:
        the service instance, or null if the service could not be added.

    - #### setConnectionListener

          public void setConnectionListener​(ConnectionListener connectionListener)

      Sets the listener which will receive the GAP events related to this adapter.

      Only one listener may be set at the same time. If the connection listener has not been set, the `DefaultConnectionListener` will be used to handle the events.

      Parameters:
        `connectionListener` - the object which will receive the events.

    - #### startAdvertising

          public boolean startAdvertising​(byte[] advertisementData)

      Starts advertising. If a device connects while advertising, the advertisement is stopped. It may be started again once the connection is established.
      Parameters:
        `advertisementData` - the advertisement data.
      Returns:
        true if advertising was started successfully, false otherwise.
      See Also:
        `ConnectionListener.onAdvertisementCompleted()`, `ConnectionListener.onConnected(BluetoothConnection)`

    - #### stopAdvertising

          public boolean stopAdvertising()

      Stops advertising.
      Returns:
        true if advertising was stopped successfully, false otherwise.
      See Also:
        `ConnectionListener.onAdvertisementCompleted()`

    - #### startScanning

          public boolean startScanning​(BluetoothScanFilter scanFilter)

      Starts scanning. If a connection is initiated while scanning, the scan is stopped. It may be started again once the connection is established. The given scan filter is applied to keep or discard scan results.
      Parameters:
        `scanFilter` - the scan filter.
      Returns:
        true if scanning was started successfully, false otherwise.
      See Also:
        `ConnectionListener.onScanResult(BluetoothAddress, byte[], int)`, `ConnectionListener.onScanCompleted()`

    - #### stopScanning

          public boolean stopScanning()

      Stops scanning.
      Returns:
        true if scanning was stopped successfully, false otherwise.
      See Also:
        `ConnectionListener.onScanCompleted()`

    - #### connect

          public boolean connect​(BluetoothAddress address)

      Initiates a connection to the device with the given address.
      Parameters:
        `address` - the address of the device to connect to.
      Returns:
        true if connection was initiated successfully, false otherwise.
      See Also:
        `ConnectionListener.onConnectFailed(BluetoothAddress)`, `ConnectionListener.onConnected(BluetoothConnection)`