Package ej.bluetooth

Class 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 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.