Class BluetoothAdapter


  • public abstract class BluetoothAdapter
    extends Object
    This class provides a common interface to the Bluetooth system. Normally an application sets an implementation with setInstance(). Any software components that need the Bluetooth system can then call getInstance() to get access. There are two known implementations: for standard Java and for Android. Before using the adapter, you may need to call prepareThread() and you must call initDefaultAdapter().
    • Constructor Detail

      • BluetoothAdapter

        public BluetoothAdapter()
    • Method Detail

      • getInstance

        public static BluetoothAdapter getInstance()
        Returns a Bluetooth adapter implementation. It returns the adapter that was set with setInstance(). If no adapter was set, this method returns null.
        Returns:
        the Bluetooth adapter
      • setInstance

        public static void setInstance​(BluetoothAdapter instance)
        Sets a Bluetooth adapater implementation. This adapter will be returned by getInstance().
        Parameters:
        instance - the Bluetooth adapter
      • prepareThread

        public abstract void prepareThread()
        The Android implementation requires that Looper.prepare() is called on a thread before the Bluetooth adapter is used. If you create a new thread specifically for Bluetooth operations and you want that code to be independent of Android, you can call this method, so that the Android implementation will call Looper.prepare(). If you need this, you must call this method before initDefaultAdapter().
      • initDefaultAdapter

        public abstract void initDefaultAdapter()
        Initialises the Bluetooth adapter. This method must be called before using the adapter. The Android implementation requires that Looper.prepare() is called on a thread before the adapter is used. See prepareThread() for more information. If you need this, you must call prepareThread() before calling this method.
      • enable

        public abstract void enable​(BluetoothListener listener)
        Enables the Bluetooth system. This method returns immediately and will notify the specified listener when the process has finished.
        Parameters:
        listener - the listener
      • disable

        public abstract void disable​(BluetoothListener listener)
        Disables the Bluetooth system. This method returns immediately and will notify the specified listener when the process has finished.
        Parameters:
        listener - the listener
      • getPairedDevices

        public abstract BluetoothDevice[] getPairedDevices()
        Returns the remote Bluetooth devices that have been paired with this local Bluetooth device.
        Returns:
        the paired devices
      • discoverDevices

        public abstract void discoverDevices​(DiscoverDevicesListener listener)
                                      throws Exception
        Starts the discovery of remote Bluetooth devices in the vicinity of this local Bluetooth device. Events in the discovery process are sent to the specified listener.
        Parameters:
        listener - the listener
        Throws:
        Exception - if the discovery can't be started
      • cancelDiscoverDevices

        public abstract void cancelDiscoverDevices()
        Cancels the discovery of remote Bluetooth devices.
      • openServerSocket

        public abstract BluetoothServerSocket openServerSocket​(String uuid)
                                                        throws IOException
        Opens a Bluetooth server socket for the specified service on this local Bluetooth device. The UUID should be given in the format 00000000-0000-0000-0000-000000000000. That is 32 hexadecimal characters in four groups with lengths 8 - 4 - 4 - 4 - 12.

        The common serial port service has this UUID: 00001101-0000-1000-8000-00805F9B34FB

        Parameters:
        uuid - the UUID of the service
        Returns:
        the Bluetooth server socket
        Throws:
        IOException - if the socket can't be opened
      • isEnabled

        public abstract boolean isEnabled()
        Returns true if the Bluetooth system is enabled.
        Returns:
        true if the Bluetooth system is enabled, false otherwise
      • getRemoteDevice

        public abstract BluetoothDevice getRemoteDevice​(String address)
                                                 throws Exception
        Returns the remote Bluetooth device with the specified address. The address should be given in the format 00:00:00:00:00:00. That is 12 hexadecimal characters in groups of two.
        Parameters:
        address - the address
        Returns:
        the remote device
        Throws:
        Exception - if the remote device can't be created (for example if Bluetooth is not available)
      • close

        public abstract void close()
        Releases any resources used by this adapter.