Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

UserSensor.Builder

public static class UserSensor.Builder
extends Object

java.lang.Object
   ↳ com.google.android.things.userdriver.sensor.UserSensor.Builder


Builder to allow for flexible UserSensor construction.

Most parameters are given default values and can be skipped if not needed. The only required calls are setDriver(UserSensorDriver) and either setType(int) or setCustomType(int, String, int).

See the Sensor HAL documentation for details on how each parameter is used.

Example usage:

 UserSensor sensor = new UserSensor.Builder()
         .setName("ABC123 Accelerometer")
         .setType(Sensor.TYPE_ACCELEROMETER)
         .setDriver(myDriverObject)
         .build();
 

Summary

Public constructors

UserSensor.Builder()

Public methods

UserSensor build()

Returns a new UserSensor with the configured parameters.

UserSensor.Builder setCustomType(int type, String stringType, int reportingMode)

Sets a custom driver type and reporting mode.

UserSensor.Builder setDriver(UserSensorDriver driver)

Sets the UserSensorDriver that controls the sensor.

UserSensor.Builder setMaxDelay(int maxDelay)

Sets the maximum allowed delay between sensor reads in microseconds.

UserSensor.Builder setMaxRange(float maxRange)

Sets the max sensor range.

UserSensor.Builder setMinDelay(int minDelay)

Sets the minimum allowed delay between sensor reads in microseconds.

UserSensor.Builder setName(String name)

Sets the sensor name.

UserSensor.Builder setPower(float power)

Sets the power cost in milliAmps of enabling the sensor.

UserSensor.Builder setResolution(float resolution)

Sets the sensor resolution.

UserSensor.Builder setType(int type)

Sets the driver type.

UserSensor.Builder setUuid(UUID uuid)

Sets a UUID to identify this sensor.

UserSensor.Builder setVendor(String vendor)

Sets the driver vendor name.

UserSensor.Builder setVersion(int version)

Sets the driver version.

Inherited methods

From class java.lang.Object

Public constructors

UserSensor.Builder

UserSensor.Builder ()

Public methods

build

UserSensor build ()

Returns a new UserSensor with the configured parameters.

Returns
UserSensor new UserSensor object

Throws
IllegalStateException if invalid parameters were given or required parameters were omitted

setCustomType

UserSensor.Builder setCustomType (int type, 
                String stringType, 
                int reportingMode)

Sets a custom driver type and reporting mode.

This can be used to install a custom driver type that is not currently defined as an Android sensor, e.g. a radiation detector. It's up to the sensor to define what the returned float array represents and the user to interpret these values correctly.

Parameters
type int: sensor type int value, must be greater than or equal to android.hardware.Sensor.TYPE_DEVICE_PRIVATE_BASE

stringType String: sensor type description in reverse-domain format e.g. com.companyname.radiationdetector

reportingMode int: android.hardware.Sensor.REPORTING_MODE_* constant

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if type is invalid or stringType is null

setDriver

UserSensor.Builder setDriver (UserSensorDriver driver)

Sets the UserSensorDriver that controls the sensor.

Parameters
driver UserSensorDriver

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if driver is null

setMaxDelay

UserSensor.Builder setMaxDelay (int maxDelay)

Sets the maximum allowed delay between sensor reads in microseconds.

Parameters
maxDelay int

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if negative maxDelay was given

setMaxRange

UserSensor.Builder setMaxRange (float maxRange)

Sets the max sensor range.

The sensor should be able to report values without saturating within [-maxRange, maxRange] in the sensor's reporting units.

Parameters
maxRange float: maximum sensor range in the sensor's reporting units

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if negative or 0 maxRange was given

setMinDelay

UserSensor.Builder setMinDelay (int minDelay)

Sets the minimum allowed delay between sensor reads in microseconds.

Should not be called for one shot sensors ie Sensor.REPORTING_MODE_ONE_SHOT

Parameters
minDelay int

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if negative minDelay was given

setName

UserSensor.Builder setName (String name)

Sets the sensor name. Usually contains the part name and sensor type, e.g. "ABC001 Accelerometer".

Parameters
name String

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if null was given

setPower

UserSensor.Builder setPower (float power)

Sets the power cost in milliAmps of enabling the sensor.

Parameters
power float

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if negative power was given

setResolution

UserSensor.Builder setResolution (float resolution)

Sets the sensor resolution.

Sensor resolution is the smallest discernable difference the sensor can measure. Usually calculated based on max range and the number of bits available.

Parameters
resolution float: sensor resolution in the sensor's reporting units

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if negative or 0 resolution was given

See also:

setType

UserSensor.Builder setType (int type)

Sets the driver type.

Use this to set the driver to be a known Android sensor type. For custom sensor types, use setCustomType(int, String, int) instead.

Parameters
type int: android.hardware.Sensor type

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if the given type is unsupported

setUuid

UserSensor.Builder setUuid (UUID uuid)

Sets a UUID to identify this sensor.

This is only useful when multiple sensors of the exact same type are connected, to be able to distinguish between them. Normally this can be left at the default 0.

Apps will not see this value directly, but can use android.hardware.Sensor.getId() to distinguish between sensors with non-zero UUIDs.

Parameters
uuid UUID: UUID to assign this sensor, or null for 0

Returns
UserSensor.Builder this builder

setVendor

UserSensor.Builder setVendor (String vendor)

Sets the driver vendor name.

Parameters
vendor String

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if null was given

setVersion

UserSensor.Builder setVersion (int version)

Sets the driver version.

Parameters
version int

Returns
UserSensor.Builder this builder

Throws
IllegalArgumentException if null was given