Class ShadowBluetoothAdapter
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET.static final intEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD.static final intEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_START_FAILED_UUID_IN_USE.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionaddIncomingRfcommConnection(BluetoothDevice remoteDevice, UUID uuid) Creates an incoming socket connection from the givenBluetoothDeviceto a background Bluetooth server created withBluetoothAdapter.startRfcommServer(String, UUID, PendingIntent)on the given uuid.protected booleanprotected static booleancheckBluetoothAddress(String address) Validate a Bluetooth address, such as "00:43:A8:23:10:F0" Alphabetic characters must be uppercase to be valid.protected voidcloseProfileProxy(int profile, BluetoothProfile proxy) Overrides behavior ofcloseProfileProxy(int, android.bluetooth.BluetoothProfile)ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)has been previously called.protected booleandisable()protected booleandisable(boolean persist) protected booleanenable()protected Stringprotected Objectprotected IBluetoothGattprotected Set<BluetoothDevice> protected static BluetoothAdapterprotected intprotected DurationReturn value changed frominttoDurationstarting in T.protected intintDecides the correct LE state.protected List<BluetoothDevice> protected StringgetName()protected intprotected IBindergetProfile(int profile) protected intgetProfileConnectionState(int profile) Returns the connection state for the given Bluetoothprofile, defaulting toBluetoothProfile.STATE_DISCONNECTEDif the profile's connection state was never set.protected booleangetProfileProxy(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.Returns an immutable set ofUUIDs representing the currently registered RFCOMM servers.protected BluetoothDevicegetRemoteDevice(String address) protected intprotected intgetState()booleanhasActiveProfileProxy(int profile) protected booleanWhen true, overrides the value ofgetLeState().protected booleanprotected intprotected booleanprotected booleanReturns the last value ofsetIsLe2MPhySupported(boolean), defaulting to true.protected intprotected booleanReturns the last value ofsetIsLeCodedPhySupported(boolean), defaulting to true.protected booleanReturns the last value ofsetIsLeExtendedAdvertisingSupported(boolean), defaulting to true.protected booleanprotected BluetoothServerSocketprotected BluetoothServerSocketlistenUsingInsecureRfcommWithServiceRecord(String serviceName, UUID uuid) protected BluetoothServerSocketprotected BluetoothServerSocketlistenUsingRfcommWithServiceRecord(String serviceName, UUID uuid) static voidreset()protected BluetoothSocketvoidsetAddress(String address) voidsetBleScanAlwaysAvailable(boolean alwaysAvailable) Sets the value forisBleScanAlwaysAvailable.voidsetBluetoothLeAdvertiser(BluetoothLeAdvertiser advertiser) Deprecated.use real BluetoothLeAdvertiser insteadvoidsetBondedDevices(Set<BluetoothDevice> bluetoothDevices) protected voidsetDiscoverableTimeout(int timeout) protected intsetDiscoverableTimeout(Duration timeout) voidsetDistanceMeasurementSupported(int supported) Sets whether the distance measurement is supported or not.voidsetEnabled(boolean enabled) Sets the enabled state of the adapter.static voidsetIsBluetoothSupported(boolean supported) Determines if getDefaultAdapter() returns the default local adapter (true) or null (false).voidsetIsLe2MPhySupported(boolean supported) Sets theisLe2MPhySupportedto enable/disable LE 2M phy supported featured.voidsetIsLeCodedPhySupported(boolean supported) Sets theisLeCodedPhySupportedto enable/disable LE coded phy supported featured.voidsetIsLeExtendedAdvertisingSupported(boolean supported) Sets the isLeExtendedAdvertisingSupported to enable/disable LE extended advertisements featurevoidsetIsMultipleAdvertisementSupported(boolean supported) Sets the value forisMultipleAdvertisementSupported.voidsetLeAudioSupported(int supported) Sets whether the Le Audio is supported or not.voidprotected booleanvoidsetProfileConnectionState(int profile, int state) Sets the connection statestatefor the given BluetoothProfileprofilevoidsetProfileProxy(int profile, BluetoothProfile proxy) Sets the active BluetoothProfileproxyfor the givenprofile.intsetScanMode(int scanMode) protected booleansetScanMode(int scanMode, int discoverableTimeout) protected booleansetScanMode(int scanMode, long durationMillis) protected booleansetScanModeSV2(int scanMode) Needs `methodName` because in Android T the return value was changed from bool to int.voidsetState(int state) protected booleanprotected booleanstartLeScan(BluetoothAdapter.LeScanCallback callback) protected booleanstartLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) protected intstartRfcommServer(String name, UUID uuid, PendingIntent pendingIntent) protected voidstopLeScan(BluetoothAdapter.LeScanCallback callback) protected intstopRfcommServer(UUID uuid) 
- 
Field Details- 
RFCOMM_LISTENER_START_FAILED_UUID_IN_USEpublic static final int RFCOMM_LISTENER_START_FAILED_UUID_IN_USEEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_START_FAILED_UUID_IN_USE.- See Also:
 
- 
RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORDpublic 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:
 
- 
RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKETpublic static final int RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKETEquivalent value to internal SystemApiBluetoothStatusCodes.RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET.- See Also:
 
 
- 
- 
Constructor Details- 
ShadowBluetoothAdapterpublic ShadowBluetoothAdapter()
 
- 
- 
Method Details- 
reset
- 
getDefaultAdapter
- 
setLeAudioSupportedpublic void setLeAudioSupported(int supported) Sets whether the Le Audio is supported or not. Minimum sdk version required is TIRAMISU.
- 
isLeAudioSupported
- 
setIsBluetoothSupportedpublic static void setIsBluetoothSupported(boolean supported) Determines if getDefaultAdapter() returns the default local adapter (true) or null (false).
- 
setDistanceMeasurementSupportedpublic void setDistanceMeasurementSupported(int supported) Sets whether the distance measurement is supported or not. Minimum sdk version required is UPSIDE_DOWN_CAKE.
- 
isDistanceMeasurementSupported
- 
setBluetoothLeAdvertiserDeprecated.use real BluetoothLeAdvertiser instead
- 
getRemoteDevice
- 
setMostRecentlyConnectedDevices
- 
getMostRecentlyConnectedDevices
- 
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
- 
isBleScanAlwaysAvailableWhen true, overrides the value ofgetLeState(). By default, this is false.
- 
getLeStateDecides 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 protected boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) 
- 
stopLeScan
- 
getLeScanCallbacks
- 
getSingleLeScanCallback
- 
isDiscovering
- 
isEnabled
- 
enable
- 
disable
- 
disable
- 
getAddress
- 
getState
- 
getName
- 
setName
- 
setScanMode
- 
setScanModeSV2Needs `methodName` because in Android T the return value was changed from bool to int.
- 
setScanMode
- 
setScanMode@Implementation(minSdk=30, maxSdk=32) protected boolean setScanMode(int scanMode, long durationMillis) 
- 
getScanMode
- 
getDiscoverableTimeout
- 
getDiscoverableTimeoutT@Implementation(minSdk=33, methodName="getDiscoverableTimeout") protected Duration getDiscoverableTimeoutT()Return value changed frominttoDurationstarting in T.
- 
setDiscoverableTimeout
- 
setDiscoverableTimeout
- 
isMultipleAdvertisementSupported
- 
checkBluetoothAddressValidate 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
 
- 
getProfileConnectionStateReturns the connection state for the given Bluetoothprofile, defaulting toBluetoothProfile.STATE_DISCONNECTEDif the profile's connection state was never set.Set a Bluetooth profile's connection state via setProfileConnectionState(int, int).
- 
setAddress
- 
setStatepublic void setState(int state) 
- 
setEnabledpublic void setEnabled(boolean enabled) Sets the enabled state of the adapter.
- 
setBleScanAlwaysAvailablepublic void setBleScanAlwaysAvailable(boolean alwaysAvailable) Sets the value forisBleScanAlwaysAvailable. If true,getLeState()will always return true.
- 
setIsMultipleAdvertisementSupportedpublic void setIsMultipleAdvertisementSupported(boolean supported) Sets the value forisMultipleAdvertisementSupported.
- 
setProfileConnectionStatepublic void setProfileConnectionState(int profile, int state) Sets the connection statestatefor the given BluetoothProfileprofile
- 
setProfileProxySets the active BluetoothProfileproxyfor the givenprofile. Will always affect behavior ofBluetoothAdapter.getProfileProxy(android.content.Context, android.bluetooth.BluetoothProfile.ServiceListener, int)andBluetoothAdapter.closeProfileProxy(android.bluetooth.BluetoothProfile). Call toBluetoothAdapter.closeProfileProxy(android.bluetooth.BluetoothProfile)can remove the set active proxy.- Parameters:
- proxy- can be 'null' to simulate the situation where- BluetoothAdapter.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.
 
- 
hasActiveProfileProxypublic boolean hasActiveProfileProxy(int profile) - Returns:
- 'true' if active (non-null) proxy has been set by setProfileProxy(int, android.bluetooth.BluetoothProfile)for the givenprofileAND it has not been "deactivated" by a call toBluetoothAdapter.closeProfileProxy(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 callonServiceConnectedof the given BluetoothProfile.ServiceListenerlistener.- Returns:
- 'true' if a proxy object has been set by setProfileProxy(int, android.bluetooth.BluetoothProfile)for the given BluetoothProfileprofile
 
- 
closeProfileProxyOverrides behavior ofcloseProfileProxy(int, android.bluetooth.BluetoothProfile)ifsetProfileProxy(int, android.bluetooth.BluetoothProfile)has been previously called.If the given non-null BluetoothProfile proxywas previously set for the givenprofilebysetProfileProxy(int, android.bluetooth.BluetoothProfile), this proxy will be "deactivated".
- 
getProfile
- 
isLeExtendedAdvertisingSupportedReturns the last value ofsetIsLeExtendedAdvertisingSupported(boolean), defaulting to true.
- 
setIsLeExtendedAdvertisingSupportedpublic void setIsLeExtendedAdvertisingSupported(boolean supported) Sets the isLeExtendedAdvertisingSupported to enable/disable LE extended advertisements feature
- 
isLeCodedPhySupportedReturns the last value ofsetIsLeCodedPhySupported(boolean), defaulting to true.
- 
setIsLeCodedPhySupportedpublic void setIsLeCodedPhySupported(boolean supported) Sets theisLeCodedPhySupportedto enable/disable LE coded phy supported featured.
- 
isLe2MPhySupportedReturns the last value ofsetIsLe2MPhySupported(boolean), defaulting to true.
- 
setIsLe2MPhySupportedpublic void setIsLe2MPhySupported(boolean supported) Sets theisLe2MPhySupportedto enable/disable LE 2M phy supported featured.
- 
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) 
- 
addIncomingRfcommConnectionpublic BluetoothSocket addIncomingRfcommConnection(BluetoothDevice remoteDevice, UUID uuid) throws PendingIntent.CanceledException Creates an incoming socket connection from the givenBluetoothDeviceto a background Bluetooth server created withBluetoothAdapter.startRfcommServer(String, UUID, PendingIntent)on the given uuid.Creating this socket connection will invoke the PendingIntentprovided 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 ShadowBluetoothSocketobtained from the returnedBluetoothSocketcan be used to send data to and receive data from the server side socket. This returnedBluetoothSocketis 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 nullif theUUIDis 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 given- UUID.
- PendingIntent.CanceledException- if the pending intent for the server socket was cancelled.
 
- 
getRegisteredRfcommServerUuidsReturns an immutable set ofUUIDs representing the currently registered RFCOMM servers.
- 
getNameLengthForAdvertise
- 
getBluetoothGatt
- 
getBluetoothAdvertise
 
-