Class Bluetooth
java.lang.Object
com.codename1.bluetooth.Bluetooth
- Direct Known Subclasses:
HelperBluetooth
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidRegisters a listener notified on the EDT whenever the adapter state changes.protected final voidfireAdapterStateChanged(AdapterState newState) Called by ports (from any thread) when the adapter transitions to a new state; dispatches to the registered listeners on the EDT.The current state of the local adapter.The classic Bluetooth role entry point -- discovery, bonding and RFCOMM streams.static BluetoothReturns the platform-specific singleton owned by the current port.getLE()The BLE central role entry point -- scanning, connections, GATT client.booleanhasPermission(BluetoothPermission permission) truewhen the given runtime permission is currently granted.booleantruewhen classic Bluetooth (discovery, RFCOMM) is available.booleanConvenience forgetAdapterState() == AdapterState.POWERED_ON.booleantruewhen L2CAP connection-oriented channels are available (Android 10+, iOS 11+).booleantruewhen the BLE central role (scanning, connections, GATT client) is available.booleantruewhen the BLE peripheral role (advertising and a local GATT server) is available.booleantruewhen this port/device exposes any Bluetooth functionality at all.voidRemoves a listener previously added viaaddAdapterStateListener(AdapterStateListener).Asks the user to enable the Bluetooth adapter where the platform supports it (the Android system dialog).requestPermissions(BluetoothPermission... permissions) Requests the given runtime permissions, prompting the user where needed.
-
Constructor Details
-
Bluetooth
protected Bluetooth()Ports construct subclasses. Application code obtains the active instance viagetInstance().
-
-
Method Details
-
getInstance
-
isSupported
public boolean isSupported()truewhen this port/device exposes any Bluetooth functionality at all. Returnsfalseon the fallback base class. -
isLeSupported
public boolean isLeSupported()truewhen the BLE central role (scanning, connections, GATT client) is available. Defaults tofalse. -
isClassicSupported
public boolean isClassicSupported()truewhen classic Bluetooth (discovery, RFCOMM) is available. Alwaysfalseon iOS, which does not expose classic Bluetooth to apps. Defaults tofalse. -
isPeripheralModeSupported
public boolean isPeripheralModeSupported()truewhen the BLE peripheral role (advertising and a local GATT server) is available.falseon tvOS/watchOS and on the JavaScript port. Defaults tofalse. -
isL2capSupported
public boolean isL2capSupported()truewhen L2CAP connection-oriented channels are available (Android 10+, iOS 11+). Defaults tofalse. -
getAdapterState
The current state of the local adapter. The fallback base class reportsAdapterState.UNSUPPORTED. -
isEnabled
public boolean isEnabled()Convenience forgetAdapterState() == AdapterState.POWERED_ON. -
addAdapterStateListener
Registers a listener notified on the EDT whenever the adapter state changes. Listeners stay registered until removed viaremoveAdapterStateListener(AdapterStateListener). -
removeAdapterStateListener
Removes a listener previously added viaaddAdapterStateListener(AdapterStateListener). -
fireAdapterStateChanged
Called by ports (from any thread) when the adapter transitions to a new state; dispatches to the registered listeners on the EDT. -
requestEnable
Asks the user to enable the Bluetooth adapter where the platform supports it (the Android system dialog). Resolvestruewhen the adapter was enabled,falsewhen 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
The BLE central role entry point -- scanning, connections, GATT client. Nevernull: on ports without BLE a no-op instance is returned whose operations fail fast withBluetoothError.NOT_SUPPORTED; branch viaisLeSupported(). -
getClassic
The classic Bluetooth role entry point -- discovery, bonding and RFCOMM streams. Nevernull: on ports without classic Bluetooth (iOS among them) a no-op instance is returned whose operations fail fast withBluetoothError.NOT_SUPPORTED; branch viaisClassicSupported(). -
hasPermission
truewhen the given runtime permission is currently granted. SeeBluetoothPermissionfor the per-platform mapping. Defaults tofalse. -
requestPermissions
Requests the given runtime permissions, prompting the user where needed. Resolvestruewhen every requested permission is granted. Resolvesfalse(rather than failing) when denied or when the platform has no Bluetooth support at all.
-