public class BluetoothAdapter extends Object
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.
Modifier and Type | Method and Description |
---|---|
BluetoothService |
addService(BluetoothServiceDefinition serviceDefinition)
Adds the given service to this adapter.
|
boolean |
connect(BluetoothAddress address)
Initiates a connection to the device with the given address.
|
void |
disable()
Disables this adapter.
|
boolean |
enable()
Enables this adapter.
|
static BluetoothAdapter |
getAdapter()
Returns the adapter of the system.
|
void |
setConnectionListener(ConnectionListener connectionListener)
Sets the listener which will receive the GAP events related to this adapter.
|
boolean |
startAdvertising(byte[] advertisementData)
Starts advertising.
|
boolean |
startScanning(BluetoothScanFilter scanFilter)
Starts scanning.
|
boolean |
stopAdvertising()
Stops advertising.
|
boolean |
stopScanning()
Stops scanning.
|
@Nullable public BluetoothService addService(BluetoothServiceDefinition serviceDefinition)
serviceDefinition
- the definition of the service to add.public boolean connect(BluetoothAddress address)
address
- the address of the device to connect to.ConnectionListener.onConnectFailed(BluetoothAddress)
,
ConnectionListener.onConnected(BluetoothConnection)
public void disable()
addService()
.
If this adapter is already disabled, calling this method has no effect.
public boolean enable()
If this adapter is already enabled, this method returns true
immediately.
public static BluetoothAdapter getAdapter()
enabled
before being used.public void setConnectionListener(ConnectionListener connectionListener)
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.
connectionListener
- the object which will receive the events.public boolean startAdvertising(byte[] advertisementData)
advertisementData
- the advertisement data.ConnectionListener.onAdvertisementCompleted()
,
ConnectionListener.onConnected(BluetoothConnection)
public boolean startScanning(BluetoothScanFilter scanFilter)
scanFilter
- the scan filter.ConnectionListener.onScanResult(BluetoothAddress, byte[], int)
,
ConnectionListener.onScanCompleted()
public boolean stopAdvertising()
ConnectionListener.onAdvertisementCompleted()
public boolean stopScanning()
ConnectionListener.onScanCompleted()