Package org.robolectric.shadows
Class ShadowBluetoothDevice
java.lang.Object
org.robolectric.shadows.ShadowBluetoothDevice
@Implements(android.bluetooth.BluetoothDevice.class)
public class ShadowBluetoothDevice
extends Object
Shadow for
BluetoothDevice
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Interceptor interface forBluetoothGatt
objects. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
protected BluetoothGatt
connectGatt
(Context context, boolean autoConnect, BluetoothGattCallback callback) protected BluetoothGatt
connectGatt
(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport) protected BluetoothGatt
connectGatt
(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport, int phy, Handler handler) protected boolean
Returns whether this device has been bonded with.protected BluetoothSocket
createInsecureL2capChannel
(int psm) protected BluetoothSocket
createL2capChannel
(int psm) protected BluetoothSocket
protected boolean
Overrides behavior ofBluetoothDevice.fetchUuidsWithSdp()
.protected String
getAlias()
protected String
protected int
Overrides behavior ofBluetoothDevice.getBluetoothClass()
to return pre-set result.Returns allBluetoothGatt
objects created by callingconnectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback)
.protected int
Overrides behavior ofBluetoothDevice.getBondState()
to return pre-set result.protected int
getConnectionHandle
(int transport) int
Returns the number of times fetchUuidsWithSdp has been called.protected byte[]
getMetadata
(int key) protected String
getName()
Get the confirmation value previously set with a call toBluetoothDevice.setPairingConfirmation(boolean)
, or null if no value is set.byte[]
getPin()
Get the PIN previously set with a call toBluetoothDevice.setPin(byte[])
, or null if no PIN has been set.protected static IBluetooth
Implements getService() in the same way the original method does, but ignores any Exceptions from invokingBluetoothAdapter.getBluetoothService()
.protected int
getType()
Overrides behavior ofBluetoothDevice.getType()
to return pre-set result.protected ParcelUuid[]
getUuids()
Overrides behavior ofBluetoothDevice.getUuids()
to return pre-set result.protected boolean
protected boolean
static BluetoothDevice
newInstance
(String address) Deprecated.protected boolean
static void
reset()
Set the alias for bluetooth device.protected boolean
setAliasBeforeS
(String alias) Sets the alias name of the device.protected int
Sets the alias name of the device for API >= 31.void
setBatteryLevel
(int batteryLevel) void
setBluetoothClass
(BluetoothClass bluetoothClass) Sets the return value forBluetoothDevice.getBluetoothClass()
.void
setBondState
(int bondState) Sets value of bond state forBluetoothDevice.getBondState()
.void
setConnected
(boolean isConnected) void
setConnectionHandle
(int transport, int connectionHandle) void
setCreatedBond
(boolean createdBond) Sets whether this device has been bonded with.void
setFetchUuidsWithSdpResult
(boolean fetchUuidsWithSdpResult) Sets value of the return result forBluetoothDevice.fetchUuidsWithSdp()
.void
Allows tests to intercept theBluetoothDevice.connectGatt
method and set state on both BluetoothDevice and BluetoothGatt objects.protected boolean
setMetadata
(int key, byte[] value) void
boolean
setPairingConfirmation
(boolean confirm) protected boolean
setPin
(byte[] pin) void
setShouldThrowSecurityExceptions
(boolean shouldThrow) Sets if a runtime exception is thrown when bluetooth methods with BLUETOOTH_CONNECT permission pre-requisites are accessed.boolean
setSilenceMode
(boolean isInSilenceMode) void
setThrowOnGetAliasName
(boolean shouldThrow) Sets if a runtime exception is thrown when the alias name of the device is accessed.void
setType
(int type) Sets the return value forBluetoothDevice.getType()
.void
setUuids
(ParcelUuid[] uuids) Sets the return value forBluetoothDevice.getUuids()
.void
simulateGattConnectionChange
(int status, int newState)
-
Constructor Details
-
ShadowBluetoothDevice
public ShadowBluetoothDevice()
-
-
Method Details
-
newInstance
Deprecated. -
reset
-
getService
Implements getService() in the same way the original method does, but ignores any Exceptions from invokingBluetoothAdapter.getBluetoothService()
. -
setName
-
setAlias
Set the alias for bluetooth device.- Parameters:
alias
- The alias name that set to bluetooth device.- Returns:
- If API is larger than or equals to S, it returns [BluetoothStatusCodes] code, otherwise it returns boolean.
-
setAliasBeforeS
Sets the alias name of the device.Alias is the locally modified name of a remote device.
Alias Name is not part of the supported SDK, and accessed via reflection.
- Parameters:
alias
- alias name.
-
setAliasS
Sets the alias name of the device for API >= 31.Alias is the locally modified name of a remote device.
Alias Name is not part of the supported SDK, and accessed via reflection.
- Parameters:
alias
- alias name.
-
setThrowOnGetAliasName
public void setThrowOnGetAliasName(boolean shouldThrow) Sets if a runtime exception is thrown when the alias name of the device is accessed.Intended to replicate what may happen if the unsupported SDK is changed.
Alias is the locally modified name of a remote device.
Alias Name is not part of the supported SDK, and accessed via reflection.
- Parameters:
shouldThrow
- if getAliasName() should throw when called.
-
setShouldThrowSecurityExceptions
public void setShouldThrowSecurityExceptions(boolean shouldThrow) Sets if a runtime exception is thrown when bluetooth methods with BLUETOOTH_CONNECT permission pre-requisites are accessed.Intended to replicate what may happen if user has not enabled nearby device permissions.
- Parameters:
shouldThrow
- if methods should throw SecurityExceptions without enabled permissions when called.
-
getName
-
getAlias
-
getAliasName
- Throws:
ReflectiveOperationException
-
setType
public void setType(int type) Sets the return value forBluetoothDevice.getType()
. -
getType
Overrides behavior ofBluetoothDevice.getType()
to return pre-set result.- Returns:
- Value set by calling
setType(int)
. If setType has not previously been called, will return BluetoothDevice.DEVICE_TYPE_UNKNOWN.
-
setUuids
Sets the return value forBluetoothDevice.getUuids()
. -
getUuids
Overrides behavior ofBluetoothDevice.getUuids()
to return pre-set result.- Returns:
- Value set by calling
setUuids(android.os.ParcelUuid[])
. If setUuids has not previously been called, will return null.
-
setBondState
public void setBondState(int bondState) Sets value of bond state forBluetoothDevice.getBondState()
. -
cancelBondProcess
-
getBondState
Overrides behavior ofBluetoothDevice.getBondState()
to return pre-set result.- Returns:
- Value set by calling
setBondState(int)
. If setBondState has not previously been called, will returnBluetoothDevice.BOND_NONE
to indicate the device is not bonded.
-
setCreatedBond
public void setCreatedBond(boolean createdBond) Sets whether this device has been bonded with. -
createBond
Returns whether this device has been bonded with. -
createInsecureL2capChannel
@Implementation(minSdk=29) protected BluetoothSocket createInsecureL2capChannel(int psm) throws IOException - Throws:
IOException
-
createL2capChannel
- Throws:
IOException
-
removeBond
-
setPin
-
getPin
public byte[] getPin()Get the PIN previously set with a call toBluetoothDevice.setPin(byte[])
, or null if no PIN has been set. -
setPairingConfirmation
-
getPairingConfirmation
Get the confirmation value previously set with a call toBluetoothDevice.setPairingConfirmation(boolean)
, or null if no value is set. -
createRfcommSocketToServiceRecord
@Implementation protected BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException - Throws:
IOException
-
setFetchUuidsWithSdpResult
public void setFetchUuidsWithSdpResult(boolean fetchUuidsWithSdpResult) Sets value of the return result forBluetoothDevice.fetchUuidsWithSdp()
. -
fetchUuidsWithSdp
Overrides behavior ofBluetoothDevice.fetchUuidsWithSdp()
. This method updates the counter which counts the number of invocations of this method.- Returns:
- Value set by calling
setFetchUuidsWithSdpResult(boolean)
. If not previously set, will return false by default.
-
getFetchUuidsWithSdpCount
public int getFetchUuidsWithSdpCount()Returns the number of times fetchUuidsWithSdp has been called. -
connectGatt
@Implementation protected BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) -
connectGatt
@Implementation(minSdk=23) protected BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport) -
connectGatt
@Implementation(minSdk=26) protected BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport, int phy, Handler handler) -
getBluetoothGatts
Returns allBluetoothGatt
objects created by callingconnectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback)
. -
simulateGattConnectionChange
public void simulateGattConnectionChange(int status, int newState) - Parameters:
status
- Status of the GATT operationnewState
- The new state of the GATT profile
-
getBluetoothClass
Overrides behavior ofBluetoothDevice.getBluetoothClass()
to return pre-set result.- Returns:
- Value set by calling
setBluetoothClass(android.bluetooth.BluetoothClass)
. If setType has not previously been called, will return null.
-
setBluetoothClass
Sets the return value forBluetoothDevice.getBluetoothClass()
. -
setMetadata
-
getMetadata
-
setBatteryLevel
public void setBatteryLevel(int batteryLevel) -
getBatteryLevel
-
setSilenceMode
-
isConnected
-
setConnected
public void setConnected(boolean isConnected) -
getConnectionHandle
-
setConnectionHandle
public void setConnectionHandle(int transport, int connectionHandle) -
isInSilenceMode
-
setGattConnectionInterceptor
public void setGattConnectionInterceptor(ShadowBluetoothDevice.BluetoothGattConnectionInterceptor interceptor) Allows tests to intercept theBluetoothDevice.connectGatt
method and set state on both BluetoothDevice and BluetoothGatt objects. This is useful for e2e testing situations where the fine-grained execution of Bluetooth connection logic is onerous.
-