Class ShadowBluetoothAdapter
@Implements(value=android.bluetooth.BluetoothAdapter.class, looseSignatures=true) public class ShadowBluetoothAdapter extends Object
-
Field Summary
Fields Modifier and Type Field Description static int
RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET
Equivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET
.static int
RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD
Equivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD
.static int
RFCOMM_LISTENER_START_FAILED_UUID_IN_USE
Equivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_START_FAILED_UUID_IN_USE
. -
Constructor Summary
Constructors Constructor Description ShadowBluetoothAdapter()
-
Method Summary
Modifier and Type Method Description BluetoothSocket
addIncomingRfcommConnection(BluetoothDevice remoteDevice, UUID uuid)
Creates an incoming socket connection from the givenBluetoothDevice
to a background Bluetooth server created withBluetoothAdapter.startRfcommServer(String, UUID, PendingIntent)
on the given uuid.protected boolean
cancelDiscovery()
protected static boolean
checkBluetoothAddress(String address)
Validate a Bluetooth address, such as "00:43:A8:23:10:F0" Alphabetic characters must be uppercase to be valid.protected void
closeProfileProxy(int profile, BluetoothProfile proxy)
Overrides behavior ofcloseProfileProxy(int,android.bluetooth.BluetoothProfile)
ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)
has been previously called.protected static Object
createAdapter(Object attributionSource)
Requires LooseSignatures because ofAttributionSource
parameterprotected boolean
disable()
protected boolean
enable()
protected String
getAddress()
protected Set<BluetoothDevice>
getBondedDevices()
protected static BluetoothAdapter
getDefaultAdapter()
protected Object
getDiscoverableTimeout()
Needs looseSignatures because the return value changed fromint
toDuration
starting in T.protected int
getLeMaximumAdvertisingDataLength()
Set<BluetoothAdapter.LeScanCallback>
getLeScanCallbacks()
int
getLeState()
Decides the correct LE state.protected String
getName()
protected int
getProfileConnectionState(int profile)
Returns the connection state for the given Bluetoothprofile
, defaulting toBluetoothProfile.STATE_DISCONNECTED
if the profile's connection state was never set.protected boolean
getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
Overrides behavior ofgetProfileProxy(android.content.Context,android.bluetooth.BluetoothProfile.ServiceListener,int)
ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)
has been previously called.Set<UUID>
getRegisteredRfcommServerUuids()
Returns an immutable set ofUUID
s representing the currently registered RFCOMM servers.protected BluetoothDevice
getRemoteDevice(String address)
protected int
getScanMode()
BluetoothAdapter.LeScanCallback
getSingleLeScanCallback()
protected int
getState()
boolean
hasActiveProfileProxy(int profile)
protected boolean
isBleScanAlwaysAvailable()
When true, overrides the value ofgetLeState()
.protected boolean
isDiscovering()
protected boolean
isEnabled()
protected boolean
isLeExtendedAdvertisingSupported()
Returns the last value ofsetIsLeExtendedAdvertisingSupported(boolean)
, defaulting to true.protected boolean
isMultipleAdvertisementSupported()
protected BluetoothServerSocket
listenUsingInsecureL2capChannel()
protected BluetoothServerSocket
listenUsingInsecureRfcommWithServiceRecord(String serviceName, UUID uuid)
protected BluetoothServerSocket
listenUsingL2capChannel()
protected BluetoothServerSocket
listenUsingRfcommWithServiceRecord(String serviceName, UUID uuid)
static void
reset()
protected BluetoothSocket
retrieveConnectedRfcommSocket(UUID uuid)
void
setAddress(String address)
void
setBleScanAlwaysAvailable(boolean alwaysAvailable)
Sets the value forisBleScanAlwaysAvailable
.void
setBluetoothLeAdvertiser(BluetoothLeAdvertiser advertiser)
Deprecated.use real BluetoothLeAdvertiser insteadvoid
setBondedDevices(Set<BluetoothDevice> bluetoothDevices)
protected void
setDiscoverableTimeout(int timeout)
protected int
setDiscoverableTimeout(Duration timeout)
void
setEnabled(boolean enabled)
Deprecated.static void
setIsBluetoothSupported(boolean supported)
Determines if getDefaultAdapter() returns the default local adapter (true) or null (false).void
setIsLeExtendedAdvertisingSupported(boolean supported)
Sets the isLeExtendedAdvertisingSupported to enable/disable LE extended advertisements featurevoid
setIsMultipleAdvertisementSupported(boolean supported)
Sets the value forisMultipleAdvertisementSupported
.protected boolean
setName(String name)
void
setProfileConnectionState(int profile, int state)
Sets the connection statestate
for the given BluetoothProfileprofile
void
setProfileProxy(int profile, BluetoothProfile proxy)
Sets the active BluetoothProfileproxy
for the givenprofile
.protected Object
setScanMode(int scanMode)
Needs looseSignatures because in Android T the return value of this method was changed from bool to int.protected boolean
setScanMode(int scanMode, int discoverableTimeout)
protected boolean
setScanMode(int scanMode, long durationMillis)
void
setState(int state)
protected boolean
startDiscovery()
protected boolean
startLeScan(BluetoothAdapter.LeScanCallback callback)
protected boolean
startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)
protected int
startRfcommServer(String name, UUID uuid, PendingIntent pendingIntent)
protected void
stopLeScan(BluetoothAdapter.LeScanCallback callback)
protected int
stopRfcommServer(UUID uuid)
-
Field Details
-
RFCOMM_LISTENER_START_FAILED_UUID_IN_USE
public static final int RFCOMM_LISTENER_START_FAILED_UUID_IN_USEEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_START_FAILED_UUID_IN_USE
.- See Also:
- Constant Field Values
-
RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD
public static final int RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORDEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD
.- See Also:
- Constant Field Values
-
RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET
public static final int RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKETEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET
.- See Also:
- Constant Field Values
-
-
Constructor Details
-
ShadowBluetoothAdapter
public ShadowBluetoothAdapter()
-
-
Method Details
-
reset
-
getDefaultAdapter
-
createAdapter
Requires LooseSignatures because ofAttributionSource
parameter -
setIsBluetoothSupported
public static void setIsBluetoothSupported(boolean supported)Determines if getDefaultAdapter() returns the default local adapter (true) or null (false). -
setBluetoothLeAdvertiser
Deprecated.use real BluetoothLeAdvertiser instead -
getRemoteDevice
-
getBondedDevices
-
setBondedDevices
-
listenUsingInsecureRfcommWithServiceRecord
@Implementation protected BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String serviceName, UUID uuid) -
listenUsingRfcommWithServiceRecord
@Implementation protected BluetoothServerSocket listenUsingRfcommWithServiceRecord(String serviceName, UUID uuid) throws IOException- Throws:
IOException
-
listenUsingInsecureL2capChannel
@Implementation(minSdk=29) protected BluetoothServerSocket listenUsingInsecureL2capChannel() throws IOException- Throws:
IOException
-
listenUsingL2capChannel
@Implementation(minSdk=29) protected BluetoothServerSocket listenUsingL2capChannel() throws IOException- Throws:
IOException
-
startDiscovery
-
cancelDiscovery
-
isBleScanAlwaysAvailable
When true, overrides the value ofgetLeState()
. By default, this is false. -
getLeState
Decides the correct LE state. When off, BLE calls will fail or return null.LE is enabled if either Bluetooth or BLE scans are enabled. LE is always off if Airplane Mode is enabled.
-
startLeScan
-
startLeScan
@Implementation(minSdk=18) protected boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) -
stopLeScan
-
getLeScanCallbacks
-
getSingleLeScanCallback
-
isDiscovering
-
isEnabled
-
enable
-
disable
-
getAddress
-
getState
-
getName
-
setName
-
setScanMode
Needs looseSignatures because in Android T the return value of this method was changed from bool to int. -
setScanMode
-
setScanMode
@Implementation(minSdk=30, maxSdk=32) protected boolean setScanMode(int scanMode, long durationMillis) -
getScanMode
-
getDiscoverableTimeout
Needs looseSignatures because the return value changed fromint
toDuration
starting in T. -
setDiscoverableTimeout
-
setDiscoverableTimeout
-
isMultipleAdvertisementSupported
-
checkBluetoothAddress
Validate a Bluetooth address, such as "00:43:A8:23:10:F0" Alphabetic characters must be uppercase to be valid.- Parameters:
address
- Bluetooth address as string- Returns:
- true if the address is valid, false otherwise
-
getProfileConnectionState
Returns the connection state for the given Bluetoothprofile
, defaulting toBluetoothProfile.STATE_DISCONNECTED
if the profile's connection state was never set.Set a Bluetooth profile's connection state via
setProfileConnectionState(int, int)
. -
setAddress
-
setState
public void setState(int state) -
setEnabled
Deprecated. -
setBleScanAlwaysAvailable
public void setBleScanAlwaysAvailable(boolean alwaysAvailable)Sets the value forisBleScanAlwaysAvailable
. If true,getLeState()
will always return true. -
setIsMultipleAdvertisementSupported
public void setIsMultipleAdvertisementSupported(boolean supported)Sets the value forisMultipleAdvertisementSupported
. -
setProfileConnectionState
public void setProfileConnectionState(int profile, int state)Sets the connection statestate
for the given BluetoothProfileprofile
-
setProfileProxy
Sets the active BluetoothProfileproxy
for the givenprofile
. Will always affect behavior ofBluetoothAdapter.getProfileProxy(android.content.Context, android.bluetooth.BluetoothProfile.ServiceListener, int)
andBluetoothAdapter.closeProfileProxy(int, android.bluetooth.BluetoothProfile)
. Call toBluetoothAdapter.closeProfileProxy(int, android.bluetooth.BluetoothProfile)
can remove the set active proxy.- Parameters:
proxy
- can be 'null' to simulate the situation whereBluetoothAdapter.getProfileProxy(android.content.Context, android.bluetooth.BluetoothProfile.ServiceListener, int)
would return 'false'. This can happen on older Android versions for Bluetooth profiles introduced in later Android versions.
-
hasActiveProfileProxy
public boolean hasActiveProfileProxy(int profile)- Returns:
- 'true' if active (non-null) proxy has been set by
setProfileProxy(int, android.bluetooth.BluetoothProfile)
for the givenprofile
AND it has not been "deactivated" by a call toBluetoothAdapter.closeProfileProxy(int, android.bluetooth.BluetoothProfile)
. Only meaningful ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)
has been previously called.
-
getProfileProxy
@Implementation protected boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)Overrides behavior ofgetProfileProxy(android.content.Context,android.bluetooth.BluetoothProfile.ServiceListener,int)
ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)
has been previously called.If active (non-null) proxy has been set by
setProfileProxy(int,android.bluetooth.BluetoothProfile)
for the givenprofile
,getProfileProxy(android.content.Context,android.bluetooth.BluetoothProfile.ServiceListener,int)
will immediately callonServiceConnected
of the given BluetoothProfile.ServiceListenerlistener
.- Returns:
- 'true' if a proxy object has been set by
setProfileProxy(int,android.bluetooth.BluetoothProfile)
for the given BluetoothProfileprofile
-
closeProfileProxy
Overrides behavior ofcloseProfileProxy(int,android.bluetooth.BluetoothProfile)
ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)
has been previously called. If the given non-null BluetoothProfileproxy
was previously set for the givenprofile
bysetProfileProxy(int, android.bluetooth.BluetoothProfile)
, this proxy will be "deactivated". -
isLeExtendedAdvertisingSupported
Returns the last value ofsetIsLeExtendedAdvertisingSupported(boolean)
, defaulting to true. -
setIsLeExtendedAdvertisingSupported
public void setIsLeExtendedAdvertisingSupported(boolean supported)Sets the isLeExtendedAdvertisingSupported to enable/disable LE extended advertisements feature -
getLeMaximumAdvertisingDataLength
-
startRfcommServer
@Implementation(minSdk=33) protected int startRfcommServer(String name, UUID uuid, PendingIntent pendingIntent) -
stopRfcommServer
-
retrieveConnectedRfcommSocket
@Implementation(minSdk=33) @Nullable protected BluetoothSocket retrieveConnectedRfcommSocket(UUID uuid) -
addIncomingRfcommConnection
public BluetoothSocket addIncomingRfcommConnection(BluetoothDevice remoteDevice, UUID uuid) throws PendingIntent.CanceledExceptionCreates an incoming socket connection from the givenBluetoothDevice
to a background Bluetooth server created withBluetoothAdapter.startRfcommServer(String, UUID, PendingIntent)
on the given uuid.Creating this socket connection will invoke the
PendingIntent
provided inBluetoothAdapter.startRfcommServer(String, UUID, PendingIntent)
when the server socket was created for the given UUID. The component provided in the intent can then callBluetoothAdapter.retrieveConnectedRfcommSocket(UUID)
to obtain the server side socket.A
ShadowBluetoothSocket
obtained from the returnedBluetoothSocket
can be used to send data to and receive data from the server side socket. This returnedBluetoothSocket
is the same socket as returned byBluetoothAdapter.retrieveConnectedRfcommSocket(UUID)
and should generally not be used directly outside of obtaining the shadow, as this socket is normally not exposed outside of the component started by the pending intent.ShadowBluetoothSocket.getInputStreamFeeder()
andShadowBluetoothSocket.getOutputStreamSink()
can be used to send data to and from the socket as if it was a remote connection.Warning: The socket returned by this method and the corresponding server side socket retrieved from
BluetoothAdapter.retrieveConnectedRfcommSocket(UUID)
do not support reads and writes from different threads. Once reading or writing is started for a given socket on a given thread, that type of operation on that socket must only be done on that thread.- Returns:
- a server side BluetoothSocket or
null
if theUUID
is not registered. This value should generally not be used directly, and is mainly used to obtain a shadow with which a RFCOMM client can be simulated. - Throws:
IllegalArgumentException
- if a server is not started for the givenUUID
.PendingIntent.CanceledException
- if the pending intent for the server socket was cancelled.
-
getRegisteredRfcommServerUuids
Returns an immutable set ofUUID
s representing the currently registered RFCOMM servers.
-