This driver supports Bosch BMP280 and BME280 environmental sensors.
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.
To use the bmx280
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.google.android.things.contrib:driver-bmx280:<version>'
}
import com.google.android.things.contrib.driver.bmx280.Bmx280;
// Access the environmental sensor:
Bmx280 mBmx280;
try {
mBmx280 = new Bmx280(i2cBusName);
// Configure driver settings according to your use case
mBmx280.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
// Ensure the driver is powered and not sleeping before trying to read from it
mBmx280.setMode(Bmx280.MODE_NORMAL);
} catch (IOException e) {
// couldn't configure the device...
}
// Read the current temperature:
try {
float temperature = mBmx280.readTemperature();
} catch (IOException e) {
// error reading temperature
}
// Close the environmental sensor when finished:
try {
// If nothing else needs to read sensor values, consider calling setMode(Bmx280.MODE_SLEEP)
mBmx280.close();
} catch (IOException e) {
// error closing sensor
}
If you need to read sensor values continuously, you can register the Bmx280 with the system and listen for sensor values using the Sensor APIs:
SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE);
SensorEventListener mListener = ...;
Bmx280SensorDriver mSensorDriver;
mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
mSensorManager.registerListener(mListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
});
try {
mSensorDriver = new Bmx280SensorDriver(i2cBusName);
mSensorDriver.registerTemperatureSensor();
} catch (IOException e) {
// Error configuring sensor
}
// Unregister and close the driver when finished:
mSensorManager.unregisterListener(mListener);
mSensorDriver.unregisterTemperatureSensor();
try {
mSensorDriver.close();
} catch (IOException e) {
// error closing sensor
}
Copyright 2016 Google Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.