Class ShadowBluetoothDevice

java.lang.Object
org.robolectric.shadows.ShadowBluetoothDevice

@Implements(value=android.bluetooth.BluetoothDevice.class, looseSignatures=true) public class ShadowBluetoothDevice extends Object
Shadow for BluetoothDevice.
  • Constructor Details

    • ShadowBluetoothDevice

      public ShadowBluetoothDevice()
  • Method Details

    • newInstance

      @Deprecated public static BluetoothDevice newInstance(String address)
      Deprecated.
    • reset

      @Resetter public static void reset()
    • getService

      @Implementation protected static IBluetooth getService()
      Implements getService() in the same way the original method does, but ignores any Exceptions from invoking BluetoothAdapter.getBluetoothService().
    • setName

      public void setName(String name)
    • setAlias

      @Implementation public Object setAlias(Object alias)
      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.
    • 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

      @Implementation protected String getName()
    • getAlias

      @Implementation protected String getAlias()
    • getAliasName

      @Implementation(maxSdk=29) protected String getAliasName() throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • setType

      public void setType(int type)
      Sets the return value for BluetoothDevice.getType().
    • getType

      @Implementation protected int getType()
      Overrides behavior of BluetoothDevice.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

      public void setUuids(ParcelUuid[] uuids)
      Sets the return value for BluetoothDevice.getUuids().
    • getUuids

      @Implementation protected ParcelUuid[] getUuids()
      Overrides behavior of BluetoothDevice.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 for BluetoothDevice.getBondState().
    • getBondState

      @Implementation protected int getBondState()
      Overrides behavior of BluetoothDevice.getBondState() to return pre-set result.
    • setCreatedBond

      public void setCreatedBond(boolean createdBond)
      Sets whether this device has been bonded with.
    • createBond

      @Implementation protected boolean createBond()
      Returns whether this device has been bonded with.
    • createInsecureL2capChannel

      @Implementation(minSdk=29) protected BluetoothSocket createInsecureL2capChannel(int psm) throws IOException
      Throws:
      IOException
    • createL2capChannel

      @Implementation(minSdk=29) protected BluetoothSocket createL2capChannel(int psm) throws IOException
      Throws:
      IOException
    • removeBond

      @Implementation protected boolean removeBond()
    • setPin

      @Implementation protected boolean setPin(byte[] pin)
    • getPin

      public byte[] getPin()
      Get the PIN previously set with a call to BluetoothDevice.setPin(byte[]), or null if no PIN has been set.
    • setPairingConfirmation

      @Implementation public boolean setPairingConfirmation(boolean confirm)
    • getPairingConfirmation

      public Boolean getPairingConfirmation()
      Get the confirmation value previously set with a call to BluetoothDevice.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 for BluetoothDevice.fetchUuidsWithSdp().
    • fetchUuidsWithSdp

      @Implementation protected boolean fetchUuidsWithSdp()
      Overrides behavior of BluetoothDevice.fetchUuidsWithSdp(). This method updates the counter which counts the number of invocations of this method.
    • 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

      public List<BluetoothGatt> getBluetoothGatts()
    • simulateGattConnectionChange

      public void simulateGattConnectionChange(int status, int newState)
      Parameters:
      status - Status of the GATT operation
      newState - The new state of the GATT profile
    • getBluetoothClass

      @Implementation public BluetoothClass getBluetoothClass()
      Overrides behavior of BluetoothDevice.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

      public void setBluetoothClass(BluetoothClass bluetoothClass)
      Sets the return value for BluetoothDevice.getBluetoothClass().
    • setMetadata

      @Implementation(minSdk=29) protected boolean setMetadata(int key, byte[] value)
    • getMetadata

      @Implementation(minSdk=29) protected byte[] getMetadata(int key)
    • setBatteryLevel

      public void setBatteryLevel(int batteryLevel)
    • getBatteryLevel

      @Implementation(minSdk=27) protected int getBatteryLevel()
    • setSilenceMode

      @Implementation(minSdk=29) public boolean setSilenceMode(boolean isInSilenceMode)
    • isConnected

      @Implementation(minSdk=20) protected boolean isConnected()
    • setConnected

      public void setConnected(boolean isConnected)
    • isInSilenceMode

      @Implementation(minSdk=29) protected boolean isInSilenceMode()
    • setGattConnectionInterceptor

      public void setGattConnectionInterceptor(ShadowBluetoothDevice.BluetoothGattConnectionInterceptor interceptor)
      Allows tests to intercept the BluetoothDevice.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.