public class BluetoothAdapter
extends java.lang.Object
BluetoothAdapter
class represents a Bluetooth adapter.
The default adapter may be retrieved by calling getDefaultAdapter()
.
This class provides methods to retrieve information on the adapter, such as its name and its address. It also provides methods to retrieve the list of devices known by the adapter and the list of connected devices. A Bluetooth adapter is able to scan in order to find nearby devices. It is also able to advertise in order to be found by other devices. The GATT server of the adapter may be configured by adding services.
Modifier and Type | Method and Description |
---|---|
boolean |
addService(BluetoothService service)
Adds the given service to this adapter.
|
void |
cleanupDevices()
Cleanups the list of devices known by this adapter.
|
BluetoothService |
findService(BluetoothUuid serviceUuid)
Returns the service with the given UUID.
|
BluetoothDevice[] |
getConnectedDevices()
Returns the list of devices to which this adapter is connected.
|
static BluetoothAdapter |
getDefaultAdapter()
Returns the default adapter of the system.
|
BluetoothDevice[] |
getDevices()
Returns the list of devices known by this adapter.
|
BluetoothService[] |
getServices()
Returns the list of services provided by this adapter.
|
boolean |
isAdvertising()
Returns whether this adapter is currently advertising.
|
boolean |
isScanning()
Returns whether this adapter is currently scanning.
|
boolean |
startAdvertising(AdvertisementCallbacks advertisementCallbacks,
ConnectionCallbacks connectionCallbacks,
byte[] payload)
Starts advertising.
|
boolean |
startScanning(ScanCallbacks scanCallbacks)
Starts scanning.
|
boolean |
stopAdvertising()
Stops advertising.
|
boolean |
stopScanning()
Stops scanning.
|
public static BluetoothAdapter getDefaultAdapter()
public BluetoothDevice[] getDevices()
This method does not start scanning and only returns devices which have been found earlier by
scanning
or advertising
.
public void cleanupDevices()
All devices are removed from the list except the devices to which this adapter is connected.
public BluetoothDevice[] getConnectedDevices()
public BluetoothService[] getServices()
This method basically returns the list of services added using addService()
.
findService(BluetoothUuid)
public BluetoothService findService(BluetoothUuid serviceUuid)
This method basically returns the service with the given UUID that was added using addService()
.
serviceUuid
- the UUID of the service to look for.getServices()
public boolean addService(BluetoothService service)
A service may not be added twice.
service
- the service to add.java.lang.IllegalArgumentException
- if the given service has already been added to this adapter.public boolean startScanning(ScanCallbacks scanCallbacks)
The scanCallbacks object will receive the scan events.
scanCallbacks
- the object which will receive the scan events.java.lang.IllegalStateException
- if this adapter is already scanning.ScanCallbacks.onScanResult(BluetoothAdapter, BluetoothDevice, byte[], int)
,
ScanCallbacks.onScanCompleted(BluetoothAdapter)
public boolean stopScanning()
ScanCallbacks.onScanCompleted(BluetoothAdapter)
public boolean isScanning()
public boolean startAdvertising(AdvertisementCallbacks advertisementCallbacks, ConnectionCallbacks connectionCallbacks, byte[] payload)
The advertisementCallbacks object will receive the advertisement events and the connectionCallbacks object will receive the connection events if a remote device connects to this adapter.
advertisementCallbacks
- the object which will receive the advertisement events.connectionCallbacks
- the object which will receive the connection events.payload
- the advertisement payload.java.lang.IllegalStateException
- if this adapter is already advertising.AdvertisementCallbacks.onAdvertisementCompleted(BluetoothAdapter)
,
ConnectionCallbacks.onConnected(BluetoothDevice)
public boolean stopAdvertising()
AdvertisementCallbacks.onAdvertisementCompleted(BluetoothAdapter)
public boolean isAdvertising()