Class BluetoothAdapter
- java.lang.Object
-
- ej.bluetooth.BluetoothAdapter
-
public class BluetoothAdapter extends Object
TheBluetoothAdapterclass 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 bysetting the connection listenerof the adapter. The adapter can beenabledordisabled.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 BluetoothServiceaddService(BluetoothServiceDefinition serviceDefinition)Adds the given service to this adapter.booleanconnect(BluetoothAddress address)Initiates a connection to the device with the given address.voiddisable()Disables this adapter.booleanenable()Enables this adapter.static BluetoothAdaptergetAdapter()Returns the adapter of the system.voidsetConnectionListener(ConnectionListener connectionListener)Sets the listener which will receive the GAP events related to this adapter.booleanstartAdvertising(byte[] advertisementData)Starts advertising.booleanstartScanning(BluetoothScanFilter scanFilter)Starts scanning.booleanstopAdvertising()Stops advertising.booleanstopScanning()Stops scanning.
-
-
-
Method Detail
-
getAdapter
public static BluetoothAdapter getAdapter()
Returns the adapter of the system. The adapter has to beenabledbefore being used.- Returns:
- the adapter of the system.
-
enable
public boolean enable()
Enables this adapter.If this adapter is already enabled, this method returns
trueimmediately.- 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 usingaddService().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
DefaultConnectionListenerwill 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)
-
-