# Class Bluetooth

java.lang.Object
com.codename1.bluetooth.Bluetooth

public class Bluetooth extends Object

Entry point for the Codename One Bluetooth API -- adapter state, runtime permissions and the capability queries that let cross-platform code branch cleanly. Obtain the platform implementation via `getInstance()`; the returned subclass is owned by the active port and never `null`.

The API is split by role:

- `com.codename1.bluetooth.le` -- BLE central: scanning, connections and the GATT client, plus L2CAP channels.
- `com.codename1.bluetooth.le.server` -- BLE peripheral: advertising and the local GATT server.
- `com.codename1.bluetooth.classic` -- classic Bluetooth: discovery and RFCOMM stream connections (Android and the simulator only).

##### Quick start: check support and permissions

    Bluetooth bt = Bluetooth.getInstance();
    if (!bt.isLeSupported()) {
        // hide the feature
        return;
    }
    bt.requestPermissions(BluetoothPermission.SCAN, BluetoothPermission.CONNECT)
      .onResult((granted, err) -> {
           if (err == null && granted) {
               // start scanning...
           }
      });

##### Threading

Every callback of the Bluetooth API -- `AsyncResource` results, scan results, connection events, notifications, adapter state changes -- is delivered on the EDT. The only exceptions are the blocking `InputStream`/`OutputStream` pairs of RFCOMM and L2CAP connections, which must be consumed off the EDT.

##### Platform support

- Android -- full stack: BLE central + peripheral, L2CAP (API 29+), classic RFCOMM. Permissions and manifest entries are auto-injected at build time when these packages are referenced.
- iOS / Mac Catalyst -- CoreBluetooth: BLE central + peripheral and L2CAP. Classic RFCOMM is not exposed by iOS. The `NSBluetooth*` plist entries and CoreBluetooth framework are auto-injected at build time.
- JavaSE simulator -- a scriptable virtual Bluetooth stack with a full feature matrix (Simulate -> Bluetooth Simulation), plus an optional native backend that talks to the host machine's real Bluetooth hardware (BLE central only).
- JavaScript -- Web Bluetooth where the browser supports it: central role via the browser's device chooser, GATT client and notifications.
- All other ports -- this base class is returned as-is and reports Bluetooth as unsupported; every operation fails fast with `BluetoothError.NOT_SUPPORTED`.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Bluetooth()`
  Ports construct subclasses.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addAdapterStateListener(AdapterStateListener l)`
  Registers a listener notified on the EDT whenever the adapter state changes.
  `protected final void`
  `fireAdapterStateChanged(AdapterState newState)`
  Called by ports (from any thread) when the adapter transitions to a new state; dispatches to the registered listeners on the EDT.
  `AdapterState`
  `getAdapterState()`
  The current state of the local adapter.
  `BluetoothClassic`
  `getClassic()`
  The classic Bluetooth role entry point -- discovery, bonding and RFCOMM streams.
  `static Bluetooth`
  `getInstance()`
  Returns the platform-specific singleton owned by the current port.
  `BluetoothLE`
  `getLE()`
  The BLE central role entry point -- scanning, connections, GATT client.
  `boolean`
  `hasPermission(BluetoothPermission permission)`
  `true` when the given runtime permission is currently granted.
  `boolean`
  `isClassicSupported()`
  `true` when classic Bluetooth (discovery, RFCOMM) is available.
  `boolean`
  `isEnabled()`
  Convenience for `getAdapterState() == AdapterState.POWERED_ON`.
  `boolean`
  `isL2capSupported()`
  `true` when L2CAP connection-oriented channels are available (Android 10+, iOS 11+).
  `boolean`
  `isLeSupported()`
  `true` when the BLE central role (scanning, connections, GATT client) is available.
  `boolean`
  `isPeripheralModeSupported()`
  `true` when the BLE peripheral role (advertising and a local GATT server) is available.
  `boolean`
  `isSupported()`
  `true` when this port/device exposes any Bluetooth functionality at all.
  `void`
  `removeAdapterStateListener(AdapterStateListener l)`
  Removes a listener previously added via `addAdapterStateListener(AdapterStateListener)`.
  `AsyncResource<Boolean>`
  `requestEnable()`
  Asks the user to enable the Bluetooth adapter where the platform supports it (the Android system dialog).
  `AsyncResource<Boolean>`
  `requestPermissions(BluetoothPermission... permissions)`
  Requests the given runtime permissions, prompting the user where needed.

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### Bluetooth

    protected Bluetooth()
    Ports construct subclasses. Application code obtains the active instance via `getInstance()`.

- ## Method Details

  - ### getInstance

    public static Bluetooth getInstance()
    Returns the platform-specific singleton owned by the current port. On ports that do not implement Bluetooth this returns a base `Bluetooth` instance that reports the feature as unsupported; calling code never needs a `null` check or a platform-specific `if`.

  - ### isSupported

    public boolean isSupported()
    `true` when this port/device exposes any Bluetooth functionality at all. Returns `false` on the fallback base class.

  - ### isLeSupported

    public boolean isLeSupported()
    `true` when the BLE central role (scanning, connections, GATT client) is available. Defaults to `false`.

  - ### isClassicSupported

    public boolean isClassicSupported()
    `true` when classic Bluetooth (discovery, RFCOMM) is available. Always `false` on iOS, which does not expose classic Bluetooth to apps. Defaults to `false`.

  - ### isPeripheralModeSupported

    public boolean isPeripheralModeSupported()
    `true` when the BLE peripheral role (advertising and a local GATT server) is available. `false` on tvOS/watchOS and on the JavaScript port. Defaults to `false`.

  - ### isL2capSupported

    public boolean isL2capSupported()
    `true` when L2CAP connection-oriented channels are available (Android 10+, iOS 11+). Defaults to `false`.

  - ### getAdapterState

    public AdapterState getAdapterState()
    The current state of the local adapter. The fallback base class reports `AdapterState.UNSUPPORTED`.

  - ### isEnabled

    public boolean isEnabled()
    Convenience for `getAdapterState() == AdapterState.POWERED_ON`.

  - ### addAdapterStateListener

    public void addAdapterStateListener(AdapterStateListener l)
    Registers a listener notified on the EDT whenever the adapter state changes. Listeners stay registered until removed via `removeAdapterStateListener(AdapterStateListener)`.

  - ### removeAdapterStateListener

    public void removeAdapterStateListener(AdapterStateListener l)
    Removes a listener previously added via `addAdapterStateListener(AdapterStateListener)`.

  - ### fireAdapterStateChanged

    protected final void fireAdapterStateChanged(AdapterState newState)
    Called by ports (from any thread) when the adapter transitions to a new state; dispatches to the registered listeners on the EDT.

  - ### requestEnable

    public AsyncResource<Boolean> requestEnable()
    Asks the user to enable the Bluetooth adapter where the platform supports it (the Android system dialog). Resolves `true` when the adapter was enabled, `false` when the user declined or the platform offers no programmatic enable flow (iOS, and this fallback base class). Never fails just because the feature is unsupported.

  - ### getLE

    public BluetoothLE getLE()
    The BLE central role entry point -- scanning, connections, GATT client. Never `null`: on ports without BLE a no-op instance is returned whose operations fail fast with `BluetoothError.NOT_SUPPORTED`; branch via `isLeSupported()`.

  - ### getClassic

    public BluetoothClassic getClassic()
    The classic Bluetooth role entry point -- discovery, bonding and RFCOMM streams. Never `null`: on ports without classic Bluetooth (iOS among them) a no-op instance is returned whose operations fail fast with `BluetoothError.NOT_SUPPORTED`; branch via `isClassicSupported()`.

  - ### hasPermission

    public boolean hasPermission(BluetoothPermission permission)
    `true` when the given runtime permission is currently granted. See `BluetoothPermission` for the per-platform mapping. Defaults to `false`.

  - ### requestPermissions

    public AsyncResource<Boolean> requestPermissions(BluetoothPermission... permissions)
    Requests the given runtime permissions, prompting the user where needed. Resolves `true` when every requested permission is granted. Resolves `false` (rather than failing) when denied or when the platform has no Bluetooth support at all.