This driver supports ROHM BH1750 ambient light sensor.
NOTE: These drivers are not production-ready. They are offered as sample implementations of Android Things user space drivers for common peripherals as part of the Developer Preview release. There is no guarantee of correctness, completeness, or robustness.
How to Use the DriverGradle Dependency
To use the bh1750
driver, simply add the line below to your project's build.gradle
, where <version>
matches the last version of the driver available on jcenter.
dependencies {
compile 'com.alvarowolfx.androidthings:driver-bh1750:<version>'
}
Sample Usage
import com.alvarowolfx.androidthings.driver.bh1750.Bh1750;
// Access the ambient light sensor:
Bh1750 mBh1750;
try {
mBh1750 = new Bh1750(i2cBusName);
} catch (IOException e) {
// couldn't configure the device...
}
// Read the current light level:
try {
float lightLevel = mBh1750.readLightLevel();
} catch (IOException e) {
// error reading light level
}
// Close the ambient light sensor when finished:
try {
mBh1750.close();
} catch (IOException e) {
// error closing sensor
}
If you need to read sensor values continuously, you can register the BH1750 with the system and listen for sensor values using the Sensor APIs:
SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE);
SensorEventListener mListener = ...;
Bh1750SensorDriver mSensorDriver;
mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_LIGHT) {
mSensorManager.registerListener(mListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
});
try {
mSensorDriver = new Bh1750SensorDriver(i2cBusName);
mSensorDriver.registerLightSensor();
} catch (IOException e) {
// Error configuring sensor
}
// Unregister and close the driver when finished:
mSensorManager.unregisterListener(mListener);
mSensorDriver.unregisterLightSensor();
try {
mSensorDriver.close();
} catch (IOException e) {
// error closing sensor
}
Sample ProjectA sample project using the Android Things kit with iMX7D and Rainbow HAT. Basically shows the value of the light sensor on the alphanumeric display, addressable LEDs, and move a servo motor according to the actual value.
https://github.com/alvarowolfx/bh1750-androidthings/tree/master/sample
Comments