BluetoothAdapter | API reference | Android Developers
Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform
fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired)
devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for
Bluetooth LE devices.
This class is thread safe.
Nested classes
interface
BluetoothAdapter.LeScanCallback
Callback interface used to deliver LE scan results.
Constants | |
|---|---|
String |
ACTION_CONNECTION_STATE_CHANGED
Intent used to broadcast the change in connection state of the local Bluetooth adapter to a profile of the remote device. |
String |
ACTION_DISCOVERY_FINISHED
Broadcast Action: The local Bluetooth adapter has finished the device discovery process. |
String |
ACTION_DISCOVERY_STARTED
Broadcast Action: The local Bluetooth adapter has started the remote device discovery process. |
String |
ACTION_LOCAL_NAME_CHANGED
Broadcast Action: The local Bluetooth adapter has changed its friendly Bluetooth name. |
String |
ACTION_REQUEST_DISCOVERABLE
Activity Action: Show a system activity that requests discoverable mode. |
String |
ACTION_REQUEST_ENABLE
Activity Action: Show a system activity that allows the user to turn on Bluetooth. |
String |
ACTION_SCAN_MODE_CHANGED
Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter has changed. |
String |
ACTION_STATE_CHANGED
Broadcast Action: The state of the local Bluetooth adapter has been changed. |
int |
ERROR
Sentinel error value for this class. |
String |
EXTRA_CONNECTION_STATE
Extra used by This extra represents the current connection state. |
String |
EXTRA_DISCOVERABLE_DURATION
Used as an optional int extra field in |
String |
EXTRA_LOCAL_NAME
Used as a String extra field in |
String |
EXTRA_PREVIOUS_CONNECTION_STATE
Extra used by This extra represents the previous connection state. |
String |
EXTRA_PREVIOUS_SCAN_MODE
Used as an int extra field in |
String |
EXTRA_PREVIOUS_STATE
Used as an int extra field in |
String |
EXTRA_SCAN_MODE
Used as an int extra field in |
String |
EXTRA_STATE
Used as an int extra field in |
int |
SCAN_MODE_CONNECTABLE
Indicates that inquiry scan is disabled, but page scan is enabled on the local Bluetooth adapter. |
int |
SCAN_MODE_CONNECTABLE_DISCOVERABLE
Indicates that both inquiry scan and page scan are enabled on the local Bluetooth adapter. |
int |
SCAN_MODE_NONE
Indicates that both inquiry scan and page scan are disabled on the local Bluetooth adapter. |
int |
STATE_CONNECTED
The profile is in connected state |
int |
STATE_CONNECTING
The profile is in connecting state |
int |
STATE_DISCONNECTED
The profile is in disconnected state |
int |
STATE_DISCONNECTING
The profile is in disconnecting state |
int |
STATE_OFF
Indicates the local Bluetooth adapter is off. |
int |
STATE_ON
Indicates the local Bluetooth adapter is on, and ready for use. |
int |
STATE_TURNING_OFF
Indicates the local Bluetooth adapter is turning off. |
int |
STATE_TURNING_ON
Indicates the local Bluetooth adapter is turning on. |
Public methods | |
|---|---|
boolean
|
cancelDiscovery()
Cancel the current device discovery process. |
static
boolean
|
checkBluetoothAddress(String address)
Validate a String Bluetooth address, such as "00:43:A8:23:10:F0" Alphabetic characters must be uppercase to be valid. |
void
|
closeProfileProxy(int unusedProfile, BluetoothProfile proxy)
Close the connection of the profile proxy to the Service. |
boolean
|
disable()
This method was deprecated
in API level 33.
Starting with Deprecation Exemptions:
|
boolean
|
enable()
This method was deprecated
in API level 33.
Starting with Deprecation Exemptions:
|
String
|
getAddress()
Returns the hardware address of the local Bluetooth adapter. |
BluetoothLeAdvertiser
|
getBluetoothLeAdvertiser()
Returns a |
BluetoothLeScanner
|
getBluetoothLeScanner()
Returns a |
Set<BluetoothDevice>
|
getBondedDevices()
Return the set of |
static
BluetoothAdapter
|
getDefaultAdapter()
This method was deprecated
in API level 31.
Use |
Duration
|
getDiscoverableTimeout()
Get the timeout duration of the |
int
|
getLeMaximumAdvertisingDataLength()
Return the maximum LE advertising data length in bytes, if LE Extended Advertising feature is supported, 0 otherwise. |
int
|
getMaxConnectedAudioDevices()
Get the maximum number of connected devices per audio profile for this device. |
String
|
getName()
Get the friendly Bluetooth name of the local Bluetooth adapter. |
int
|
getProfileConnectionState(int profile)
Get the current connection state of a profile. |
boolean
|
getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
Get the profile proxy object associated with the profile. |
BluetoothDevice
|
getRemoteDevice(byte[] address)
Get a |
BluetoothDevice
|
getRemoteDevice(String address)
Get a |
BluetoothDevice
|
getRemoteLeDevice(String address, int addressType)
Get a |
int
|
getScanMode()
Get the current Bluetooth scan mode of the local Bluetooth adapter. |
int
|
getState()
Get the current state of the local Bluetooth adapter. |
boolean
|
isDiscovering()
Return true if the local Bluetooth adapter is currently in the device discovery process. |
boolean
|
isEnabled()
Return true if Bluetooth is currently enabled and ready for use. |
boolean
|
isLe2MPhySupported()
Return true if LE 2M PHY feature is supported. |
int
|
isLeAudioBroadcastAssistantSupported()
Returns |
int
|
isLeAudioBroadcastSourceSupported()
Returns |
int
|
isLeAudioSupported()
Returns |
boolean
|
isLeCodedPhySupported()
Return true if LE Coded PHY feature is supported. |
boolean
|
isLeExtendedAdvertisingSupported()
Return true if LE Extended Advertising feature is supported. |
int
|
isLeHighDataThroughputPhySupported()
Checks if LE High Data Throughput (HDT) PHY is supported. |
boolean
|
isLePeriodicAdvertisingSupported()
Return true if LE Periodic Advertising feature is supported. |
boolean
|
isMultipleAdvertisementSupported()
Return true if the multi advertisement is supported by the chipset. |
boolean
|
isOffloadedFilteringSupported()
Return true if offloaded filters are supported. |
boolean
|
isOffloadedScanBatchingSupported()
Return true if offloaded scan batching is supported. |
BluetoothServerSocket
|
listenUsingInsecureL2capChannel()
Create an insecure L2CAP Connection-oriented Channel (CoC) |
BluetoothServerSocket
|
listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
Create a listening, insecure RFCOMM Bluetooth socket with Service Record. |
BluetoothServerSocket
|
listenUsingL2capChannel()
Create a secure L2CAP Connection-oriented Channel (CoC) |
BluetoothServerSocket
|
listenUsingRfcommWithServiceRecord(String name, UUID uuid)
Create a listening, secure RFCOMM Bluetooth socket with Service Record. |
BluetoothServerSocket
|
listenUsingSocketSettings(BluetoothSocketSettings settings)
Creates a listening server channel for Bluetooth connections with the specified socket
settings |
boolean
|
setName(String name)
Set the friendly Bluetooth name of the local Bluetooth adapter. |
boolean
|
startDiscovery()
Start the remote device discovery process. |
boolean
|
startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)
This method was deprecated
in API level 21.
use |
boolean
|
startLeScan(BluetoothAdapter.LeScanCallback callback)
This method was deprecated
in API level 21.
use |
void
|
stopLeScan(BluetoothAdapter.LeScanCallback callback)
This method was deprecated
in API level 21.
Use |
Protected methods | |
|---|---|
void
|
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Inherited methods | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
| |||||||||||||||||||||||
Constants
ACTION_STATE_CHANGED
public static final String ACTION_STATE_CHANGED
Broadcast Action: The state of the local Bluetooth adapter has been changed.
For example, Bluetooth has been turned on or off.
Always contains the extra fields EXTRA_STATE and EXTRA_PREVIOUS_STATE
containing the new and old states respectively.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
Constant Value: "android.bluetooth.adapter.action.STATE_CHANGED"
ERROR
public static final int ERROR
Sentinel error value for this class. Guaranteed to not equal any other integer constant in this class. Provided as a convenience for functions that require a sentinel error value, for example:
Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR)
Constant Value: -2147483648 (0x80000000)
EXTRA_CONNECTION_STATE
public static final String EXTRA_CONNECTION_STATE
Extra used by ACTION_CONNECTION_STATE_CHANGED
This extra represents the current connection state.
Constant Value: "android.bluetooth.adapter.extra.CONNECTION_STATE"
EXTRA_DISCOVERABLE_DURATION
public static final String EXTRA_DISCOVERABLE_DURATION
Used as an optional int extra field in ACTION_REQUEST_DISCOVERABLE intents to
request a specific duration for discoverability in seconds. The current default is 120
seconds, and requests over 300 seconds will be capped. These values could change.
Constant Value: "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION"
EXTRA_LOCAL_NAME
public static final String EXTRA_LOCAL_NAME
Used as a String extra field in ACTION_LOCAL_NAME_CHANGED intents to request the
local Bluetooth name.
Constant Value: "android.bluetooth.adapter.extra.LOCAL_NAME"
EXTRA_PREVIOUS_CONNECTION_STATE
public static final String EXTRA_PREVIOUS_CONNECTION_STATE
Extra used by ACTION_CONNECTION_STATE_CHANGED
This extra represents the previous connection state.
Constant Value: "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE"
SCAN_MODE_CONNECTABLE
public static final int SCAN_MODE_CONNECTABLE
Indicates that inquiry scan is disabled, but page scan is enabled on the local Bluetooth adapter. Therefore this device is not discoverable from remote Bluetooth devices, but is connectable from remote devices that have previously discovered this device.
Constant Value: 21 (0x00000015)
SCAN_MODE_CONNECTABLE_DISCOVERABLE
public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE
Indicates that both inquiry scan and page scan are enabled on the local Bluetooth adapter. Therefore this device is both discoverable and connectable from remote Bluetooth devices.
Constant Value: 23 (0x00000017)
SCAN_MODE_NONE
public static final int SCAN_MODE_NONE
Indicates that both inquiry scan and page scan are disabled on the local Bluetooth adapter. Therefore this device is neither discoverable nor connectable from remote Bluetooth devices.
Constant Value: 20 (0x00000014)
STATE_CONNECTED
public static final int STATE_CONNECTED
The profile is in connected state
Constant Value: 2 (0x00000002)
STATE_CONNECTING
public static final int STATE_CONNECTING
The profile is in connecting state
Constant Value: 1 (0x00000001)
STATE_DISCONNECTED
public static final int STATE_DISCONNECTED
The profile is in disconnected state
Constant Value: 0 (0x00000000)
STATE_DISCONNECTING
public static final int STATE_DISCONNECTING
The profile is in disconnecting state
Constant Value: 3 (0x00000003)
STATE_OFF
public static final int STATE_OFF
Indicates the local Bluetooth adapter is off.
Constant Value: 10 (0x0000000a)
STATE_ON
public static final int STATE_ON
Indicates the local Bluetooth adapter is on, and ready for use.
Constant Value: 12 (0x0000000c)
STATE_TURNING_OFF
public static final int STATE_TURNING_OFF
Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
Constant Value: 13 (0x0000000d)
STATE_TURNING_ON
public static final int STATE_TURNING_ON
Indicates the local Bluetooth adapter is turning on. However local clients should wait for
STATE_ON before attempting to use the adapter.
Constant Value: 11 (0x0000000b)
Public methods
cancelDiscovery
public boolean cancelDiscovery ()
Cancel the current device discovery process.
Because discovery is a heavyweight procedure for the Bluetooth adapter, this method should
always be called before attempting to connect to a remote device with BluetoothSocket.connect(). Discovery is not managed by the Activity, but
is run as a system service, so an application should always call cancel discovery even if it
did not directly request a discovery, just to be sure.
If Bluetooth state is not STATE_ON, this API will return false. After turning on
Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated
value.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_SCAN permission which can be gained with android.app.Activity.requestPermissions(String[],int).
Requires Manifest.permission.BLUETOOTH_SCAN
| Returns | |
|---|---|
boolean |
true on success, false on error |
checkBluetoothAddress
public static boolean checkBluetoothAddress (String address)
Validate a String Bluetooth address, such as "00:43:A8:23:10:F0"
Alphabetic characters must be uppercase to be valid.
| Parameters | |
|---|---|
address |
String: Bluetooth address as string |
| Returns | |
|---|---|
boolean |
true if the address is valid, false otherwise |
disable
public boolean disable ()
This method was deprecated
in API level 33.
Starting with Build.VERSION_CODES.TIRAMISU, applications are
not allowed to enable/disable Bluetooth. Compatibility Note: For applications
targeting Build.VERSION_CODES.TIRAMISU or above, this API will always
fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.S or below), they can continue to use this API.
Deprecation Exemptions:
- Device Owner (DO), Profile Owner (PO) and system apps.
Turn off the local Bluetooth adapter—do not use without explicit user action to turn off Bluetooth.
This gracefully shuts down all Bluetooth connections, stops Bluetooth system services, and powers down the underlying Bluetooth hardware.
Bluetooth should never be disabled without direct user
consent. The disable() method is provided only for applications that
include a user interface for changing system settings, such as a "power manager" app.
This is an asynchronous call: it will return immediately, and clients should listen for
ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this
call returns true, then the adapter state will immediately transition from STATE_ON
to STATE_TURNING_OFF, and some time later transition to either STATE_OFF or
STATE_ON. If this call returns false then there was an immediate problem that will
prevent the adapter from being turned off - such as the adapter already being turned off.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity.requestPermissions(String[],int).
Requires Manifest.permission.BLUETOOTH_CONNECT
| Returns | |
|---|---|
boolean |
true to indicate adapter shutdown has begun, or false on immediate error |
enable
public boolean enable ()
This method was deprecated
in API level 33.
Starting with Build.VERSION_CODES.TIRAMISU, applications are
not allowed to enable/disable Bluetooth. Compatibility Note: For applications
targeting Build.VERSION_CODES.TIRAMISU or above, this API will always
fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.S or below), they can continue to use this API.
Deprecation Exemptions:
- Device Owner (DO), Profile Owner (PO) and system apps.
Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.
This powers on the underlying Bluetooth hardware, and starts all Bluetooth system services.
Bluetooth should never be enabled without direct user
consent. If you want to turn on Bluetooth in order to create a wireless connection,
you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that
requests user permission to turn on Bluetooth. The enable() method is provided only
for applications that include a user interface for changing system settings, such as a "power
manager" app.
This is an asynchronous call: it will return immediately, and clients should listen for
ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this
call returns true, then the adapter state will immediately transition from STATE_OFF
to STATE_TURNING_ON, and some time later transition to either STATE_OFF or
STATE_ON. If this call returns false then there was an immediate problem that will
prevent the adapter from being turned on - such as Airplane mode, or the adapter is already
turned on.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity.requestPermissions(String[],int).
Requires Manifest.permission.BLUETOOTH_CONNECT
| Returns | |
|---|---|
boolean |
true to indicate adapter startup has begun, or false on immediate error |
getLeMaximumAdvertisingDataLength
public int getLeMaximumAdvertisingDataLength ()
Return the maximum LE advertising data length in bytes, if LE Extended Advertising feature is
supported, 0 otherwise.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
int |
the maximum LE advertising data length. |
getProfileProxy
public boolean getProfileProxy (Context context, BluetoothProfile.ServiceListener listener, int profile)
Get the profile proxy object associated with the profile.
The ServiceListener's methods will be invoked on the application's main looper
| Parameters | |
|---|---|
context |
Context: Context of the application |
listener |
BluetoothProfile.ServiceListener: The service listener for connection callbacks. |
profile |
int: The Bluetooth profile to listen for status change |
| Returns | |
|---|---|
boolean |
true on success, false on error |
getRemoteDevice
public BluetoothDevice getRemoteDevice (byte[] address)
Get a BluetoothDevice object for the given Bluetooth hardware address.
Valid Bluetooth hardware addresses must be 6 bytes. This method expects the address in network byte order (MSB first).
A BluetoothDevice will always be returned for a valid hardware address, even if
this adapter has never seen that device.
| Parameters | |
|---|---|
address |
byte: Bluetooth MAC address (6 bytes) |
| Returns | |
|---|---|
BluetoothDevice |
|
| Throws | |
|---|---|
IllegalArgumentException |
if address is invalid |
getRemoteDevice
public BluetoothDevice getRemoteDevice (String address)
Get a BluetoothDevice object for the given Bluetooth hardware address.
Valid Bluetooth hardware addresses must be upper case, in big endian byte order, and in a
format such as "00:11:22:33:AA:BB". The helper checkBluetoothAddress(String) is available to
validate a Bluetooth address.
A BluetoothDevice will always be returned for a valid hardware address, even if
this adapter has never seen that device.
| Parameters | |
|---|---|
address |
String: valid Bluetooth MAC address |
| Returns | |
|---|---|
BluetoothDevice |
|
| Throws | |
|---|---|
IllegalArgumentException |
if address is invalid |
isEnabled
public boolean isEnabled ()
Return true if Bluetooth is currently enabled and ready for use.
Equivalent to: getBluetoothState() == STATE_ON.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if the local adapter is turned on |
isLe2MPhySupported
public boolean isLe2MPhySupported ()
Return true if LE 2M PHY feature is supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports LE 2M PHY feature |
isLeCodedPhySupported
public boolean isLeCodedPhySupported ()
Return true if LE Coded PHY feature is supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports LE Coded PHY feature |
isLeExtendedAdvertisingSupported
public boolean isLeExtendedAdvertisingSupported ()
Return true if LE Extended Advertising feature is supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports LE Extended Advertising feature |
isLePeriodicAdvertisingSupported
public boolean isLePeriodicAdvertisingSupported ()
Return true if LE Periodic Advertising feature is supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports LE Periodic Advertising feature |
isMultipleAdvertisementSupported
public boolean isMultipleAdvertisementSupported ()
Return true if the multi advertisement is supported by the chipset.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if Multiple Advertisement feature is supported |
isOffloadedFilteringSupported
public boolean isOffloadedFilteringSupported ()
Return true if offloaded filters are supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports on-chip filtering |
isOffloadedScanBatchingSupported
public boolean isOffloadedScanBatchingSupported ()
Return true if offloaded scan batching is supported.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
| Returns | |
|---|---|
boolean |
true if chipset supports on-chip scan batching |
listenUsingInsecureRfcommWithServiceRecord
public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord (String name, UUID uuid)
Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
The link key is not required to be authenticated, i.e. the communication may be vulnerable
to Person In the Middle attacks. For Bluetooth 2.1 devices, the link will be encrypted, as
encryption is mandatory. For legacy devices (pre Bluetooth 2.1 devices) the link will not be
encrypted. Use listenUsingRfcommWithServiceRecord(String, UUID), if an encrypted and authenticated
communication channel is desired.
Use BluetoothServerSocket.accept to retrieve incoming connections from a listening
BluetoothServerSocket.
The system will assign an unused RFCOMM channel to listen on.
The system will also register a Service Discovery Protocol (SDP) record with the local SDP server containing the specified UUID, service name, and auto-assigned channel. Remote Bluetooth devices can use the same UUID to query our SDP server and discover which channel to connect to. This SDP record will be removed when this socket is closed, or if this application closes unexpectedly.
Use BluetoothDevice.createInsecureRfcommSocketToServiceRecord to connect to this
socket from another device using the same UUID.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity.requestPermissions(String[],int).
Requires Manifest.permission.BLUETOOTH_CONNECT
| Parameters | |
|---|---|
name |
String: service name for SDP record |
uuid |
UUID: uuid for SDP record |
| Returns | |
|---|---|
BluetoothServerSocket |
a listening RFCOMM BluetoothServerSocket |
| Throws | |
|---|---|
IOException |
on error, for example Bluetooth not available, or insufficient permissions, or channel in use. |
listenUsingRfcommWithServiceRecord
public BluetoothServerSocket listenUsingRfcommWithServiceRecord (String name, UUID uuid)
Create a listening, secure RFCOMM Bluetooth socket with Service Record.
A remote device connecting to this socket will be authenticated and communication on this socket will be encrypted.
Use BluetoothServerSocket.accept to retrieve incoming connections from a listening
BluetoothServerSocket.
The system will assign an unused RFCOMM channel to listen on.
The system will also register a Service Discovery Protocol (SDP) record with the local SDP server containing the specified UUID, service name, and auto-assigned channel. Remote Bluetooth devices can use the same UUID to query our SDP server and discover which channel to connect to. This SDP record will be removed when this socket is closed, or if this application closes unexpectedly.
Use BluetoothDevice.createRfcommSocketToServiceRecord to connect to this socket
from another device using the same UUID.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity.requestPermissions(String[],int).
Requires Manifest.permission.BLUETOOTH_CONNECT
| Parameters | |
|---|---|
name |
String: service name for SDP record |
uuid |
UUID: uuid for SDP record |
| Returns | |
|---|---|
BluetoothServerSocket |
a listening RFCOMM BluetoothServerSocket |
| Throws | |
|---|---|
IOException |
on error, for example Bluetooth not available, or insufficient permissions, or channel in use. |
startDiscovery
public boolean startDiscovery ()
Start the remote device discovery process.
The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each new device to retrieve its Bluetooth name. If BLE is supported, a simultaneous LE scan runs until discovery ends.
This is an asynchronous call, it will return immediately. Register for ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED intents to determine
exactly when the discovery starts and completes. Register for BluetoothDevice.ACTION_FOUND to be notified as remote Bluetooth devices are found.
Device discovery is a heavyweight procedure. New connections to remote Bluetooth devices
should not be attempted while discovery is in progress, and existing connections will
experience limited bandwidth and high latency. Use cancelDiscovery() to cancel an
ongoing discovery. Discovery is not managed by the Activity, but is run as a system service,
so an application should always call BluetoothAdapter.cancelDiscovery() even if it
did not directly request a discovery, just to be sure.
Device discovery will only find remote devices that are currently discoverable (inquiry scan enabled). Many Bluetooth devices are not discoverable by default, and need to be entered into a special mode.
If Bluetooth state is not STATE_ON, this API will return false. After turning on
Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated
value.
If a device is currently bonding, this request will be queued and executed once that
device has finished bonding. If a request is already queued, this request will be ignored.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_SCAN permission which can be gained with android.app.Activity.requestPermissions(String[],int).
In addition, this requires the Manifest.permission.ACCESS_FINE_LOCATION
permission. For apps targeting ERROR(/Build.VERSION_CODES#S) or higher, an alternative is a
strong assertion that you will never derive the physical location of the device, made by
declaring usesPermissionFlags="neverForLocation" on the relevant <uses-permission> manifest tag. However, this assertion may restrict the types of Bluetooth
devices you can interact with.
Requires Manifest.permission.BLUETOOTH_SCAN
| Returns | |
|---|---|
boolean |
true on success, false on error |
startLeScan
public boolean startLeScan (UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)
This method was deprecated
in API level 21.
use BluetoothLeScanner.startScan(List,ScanSettings,ScanCallback)
instead.
Starts a scan for Bluetooth LE devices, looking for devices that advertise given services.
Devices which advertise all specified services are reported using the LeScanCallback.onLeScan callback.
This method requires the calling app to have the Manifest.permission.BLUETOOTH_SCAN permission. Additionally, an app must have the
Manifest.permission.BLUETOOTH_PRIVILEGED if it is used for BLE scan only mode
(when the adapter state is not BluetoothAdapter.STATE_ON).
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_SCAN permission which can be gained with android.app.Activity.requestPermissions(String[],int).
In addition, this requires the Manifest.permission.ACCESS_FINE_LOCATION
permission. For apps targeting ERROR(/Build.VERSION_CODES#S) or higher, an alternative is a
strong assertion that you will never derive the physical location of the device, made by
declaring usesPermissionFlags="neverForLocation" on the relevant <uses-permission> manifest tag. However, this assertion may restrict the types of Bluetooth
devices you can interact with.
| Parameters | |
|---|---|
serviceUuids |
UUID: Array of services to look for |
callback |
BluetoothAdapter.LeScanCallback: the callback LE scan results are delivered |
| Returns | |
|---|---|
boolean |
true, if the scan was started successfully |
startLeScan
public boolean startLeScan (BluetoothAdapter.LeScanCallback callback)
This method was deprecated
in API level 21.
use BluetoothLeScanner.startScan(List,ScanSettings,ScanCallback)
instead.
Starts a scan for Bluetooth LE devices.
Results of the scan are reported using the LeScanCallback.onLeScan callback.
This method requires the calling app to have the Manifest.permission.BLUETOOTH_SCAN permission. Additionally, an app must have the
Manifest.permission.BLUETOOTH_PRIVILEGED if it is used for BLE scan only mode
(when the adapter state is not BluetoothAdapter.STATE_ON).
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH_ADMIN permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the
Manifest.permission.BLUETOOTH_SCAN permission which can be gained with android.app.Activity.requestPermissions(String[],int).
In addition, this requires the Manifest.permission.ACCESS_FINE_LOCATION
permission. For apps targeting ERROR(/Build.VERSION_CODES#S) or higher, an alternative is a
strong assertion that you will never derive the physical location of the device, made by
declaring usesPermissionFlags="neverForLocation" on the relevant <uses-permission> manifest tag. However, this assertion may restrict the types of Bluetooth
devices you can interact with.
| Parameters | |
|---|---|
callback |
BluetoothAdapter.LeScanCallback: the callback LE scan results are delivered |
| Returns | |
|---|---|
boolean |
true, if the scan was started successfully |
Protected methods
finalize
protected void finalize ()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
| Throws | |
|---|---|
Throwable |
|