# ReactiveSensors
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-ReactiveSensors-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/2451)
Android library monitoring hardware sensors with RxJava.
| Current Branch | Branch | Artifact Id | CI Build Status | Maven Central |
|:--------------:|:-------:|:-----------:|:---------------:|:-------------:|
| | [`RxJava1.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava1.x) | `reactivesensors` | ![Android CI](https://github.com/pwittchen/ReactiveSensors/workflows/Android%20CI/badge.svg?branch=RxJava1.x) | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors.svg?style=flat) |
| | [`RxJava2.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava2.x) | `reactivesensors-rx2` | ![Android CI](https://github.com/pwittchen/ReactiveSensors/workflows/Android%20CI/badge.svg?branch=RxJava2.x) | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx2.svg?style=flat) |
| :ballot_box_with_check: | [`RxJava3.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava3.x) | `reactivesensors-rx3` | ![Android CI](https://github.com/pwittchen/ReactiveSensors/workflows/Android%20CI/badge.svg?branch=RxJava3.x) | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx3.svg?style=flat) |
Contents
--------
- [Usage](#usage)
- [Setting sampling period](#setting-sampling-period)
- [Example](#example)
- [Good practices](#good-practices)
- [Checking whether sensor exists](#checking-whether-sensor-exists)
- [Letting it crash](#letting-it-crash)
- [Subscribing and disposing flowables](#subscribing-and-disposing-flowables)
- [Filtering stream](#filtering-stream)
- [Writing tests](#writing-tests)
- [Other practices](#other-practices)
- [Download](#download)
- [Tests](#tests)
- [Code style](#code-style)
- [Static code analysis](#static-code-analysis)
- [References](#references)
- [License](#license)
Usage
-----
Code sample below demonstrates how to observe Gyroscope sensor.
Please note that we are filtering events occurring when sensor readings change with `ReactiveSensorEvent::sensorChanged` method. There's also event describing change of sensor's accuracy, which can be filtered with `ReactiveSensorEvent::accuracyChanged` method. When we don't apply any filter, we will be notified both about sensor readings and accuracy changes.
```java
new ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)
.subscribeOn(Schedulers.computation())
.filter(ReactiveSensorEvent::sensorChanged)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ReactiveSensorEvent>() {
@Override public void call(ReactiveSensorEvent event) {
float x = event.sensorValues()[0];
float y = event.sensorValues()[1];
float z = event.sensorValues()[2];
String message = String.format("x = %f, y = %f, z = %f", x, y, z);
Log.d("gyroscope readings", message);
}
});
}
```
We can observe any hardware sensor in the same way. You can check [list of all sensors in official Android documentation](http://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-intro). To get list of all sensors available on the current device, you can use `getSensors()` method available in `ReactiveSensors` class.
### Setting sampling period
Default sampling period for `Flowable` below is set to `SensorManager.SENSOR_DELAY_NORMAL`.
```java
Flowable<ReactiveSensorEvent> observeSensor(int sensorType)
```
We can configure sampling period according to our needs with the following flowable:
```java
Flowable<ReactiveSensorEvent> observeSensor(final int sensorType, final int samplingPeriodInUs)
```
We can use predefined values available in `SensorManager` class from Android SDK:
- `int SENSOR_DELAY_FASTEST` - get sensor data as fast as possible
- `int SENSOR_DELAY_GAME` - rate suitable for games
- `int SENSOR_DELAY_NORMAL` - rate (default) suitable for screen orientation changes
- `int SENSOR_DELAY_UI` - rate suitable for the user interface
We can also define our own integer value in microseconds, but it's recommended to use predefined values.
We can customize RxJava Backpressure Strategy for our flowable with method:
```java
Flowable<ReactiveSensorEvent> observeSensor(final int sensorType, final int samplingPeriodInUs,
final BackpressureStrategy strategy)
```
Default Backpressure Strategy is `BUFFER`.
Example
-------
Exemplary application, which gets readings of various sensors is located in `app` directory of this repository. You can easily change `SENSOR_TYPE` variable to read values from a different sensor in a given samples.
Good practices
--------------
### Checking whether sensor exists
We should check whether device has concrete sensor before we start observing it.
We can do it in the following way:
```java
if (reactiveSensors.hasSensor(SENSOR_TYPE)) {
// observe sensor
} else {
// show error message
}
```
### Letting it crash
We can let our subscription crash and handle situation when device does not have given sensor e.g. in the `Consumer<Throwable>()` implementation (if we want to return `Disposable`) or in the `onError(throwable)` method implementation of the `Subscriber`. Other types of errors can be handled there as well.
```java
new ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)
.subscribeOn(Schedulers.computation())
.filter(ReactiveSensorEvent::sensorChanged)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ReactiveSensorEvent>() {
@Override public void accept(ReactiveSensorEvent reactiveSensorEvent) throws Exception {
// handle reactiveSensorEvent
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
if (throwable instanceof SensorNotFoundException) {
textViewForMessage.setText("Sorry, your device doesn't have required sensor.");
}
}
});
```
### Subscribing and disposing flowables
When we are using Disposables in Activity, we should subscribe them in `onResume()` method and dispose them in `onPause()` method.
### Filtering stream
When we want to receive **only sensor updates**, we should use `ReactiveSensorEvent::sensorChanged` method in `filter(...)` method from RxJava.
When we want to receive **only accuracy updates**, we should use `ReactiveSensorEvent::accuracyChanged` method in `filter(...)` method from RxJava.
If we don't apply any filter, we will receive both accuracy and sensor readings updates.
### Writing tests
`ReactiveSensors` class implements `SensorsProxy` interface. It allows you to create stubs or mocks for testing behavior of the sensors in your application without need of mocking `SensorManager` class from Android SDK accessing hardware components. Once you instantiate `SensorsProxy`, then you'll be allowed to mock or stub it pretty easily. Moreover, you can mock `ReactiveSensorEvent`, which wraps code from Android API, expose appropriate methods and does not force you to use native code accessing hardware sensors in tests, so you can foucus just on the application logic.
### Other practices
See also [Best Practices for Accessing and Using Sensors](http://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-practices).
Download
--------
latest version: ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx3.svg?style=flat)
replace `x.y.z` with the latest version
You can depend on the library through Maven:
```xml
<dependency>
<groupId>com.github.pwittchen</groupId>
<artifactId>reactivesensors-rx3</artifactId>
<version>x.y.z</version>
</dependency>
```
or through Gradle:
```groovy
dependencies {
compile 'com.github.pwittchen:reactivesensors-rx3:x.y.z'
}
```
Tests
没有合适的资源?快使用搜索试试~ 我知道了~
使用 RxJava 监控设备硬件传感器的 Android 库_java_代码_相关文件_下载
共61个文件
java:22个
xml:13个
gradle:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 137 浏览量
2022-07-06
02:43:34
上传
评论
收藏 127KB ZIP 举报
温馨提示
使用 RxJava 监控硬件传感器的 Android 库。 用法 下面的代码示例演示了如何观察陀螺仪传感器。 请注意,我们正在过滤传感器读数随ReactiveSensorEvent::sensorChanged方法变化时发生的事件。还有描述传感器精度变化的事件,可以用ReactiveSensorEvent::accuracyChanged方法过滤。当我们不应用任何过滤器时,我们将收到有关传感器读数和精度变化的通知。 例子 获取各种传感器读数的示例应用程序位于app此存储库的目录中。您可以轻松更改SENSOR_TYPE变量以从给定样本中的不同传感器读取值。 良好做法 检查传感器是否存在 在开始观察之前,我们应该检查设备是否有具体的传感器。 我们可以通过以下方式做到这一点: 更多详情、使用方法,请下载后阅读README.md文件
资源推荐
资源详情
资源评论
收起资源包目录
ReactiveSensors-RxJava3.x.zip (61个子文件)
ReactiveSensors-RxJava3.x
gradlew.bat 3KB
gradlew 6KB
config
quality
checkstyle
suppressions.xml 357B
checkstyle.xml 5KB
pmd
pmd-ruleset.xml 1KB
findbugs
findbugs-filter.xml 497B
lint
lint.xml 208B
quality.gradle 1KB
gradle.properties 759B
maven_push.gradle 4KB
.github
FUNDING.yml 60B
workflows
android.yml 426B
library
gradle.properties 82B
proguard-rules.pro 674B
src
androidTest
java
com
github
pwittchen
reactivesensors
library
ReactiveSensorsTest.java 1KB
main
java
com
github
pwittchen
reactivesensors
library
SensorNotFoundException.java 793B
ReactiveSensors.java 7KB
SensorsProxy.java 1KB
ReactiveSensorEvent.java 2KB
SensorEventListenerWrapper.java 2KB
AndroidManifest.xml 165B
.gitignore 7B
build.gradle 1KB
LICENSE 11KB
app
proguard-rules.pro 674B
src
main
res
mipmap-hdpi
ic_launcher.png 3KB
values
strings.xml 143B
styles.xml 184B
dimens.xml 205B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_sensor_sample.xml 745B
activity_main.xml 4KB
values-w820dp
dimens.xml 358B
java
com
github
pwittchen
reactivesensors
app
SensorHelper.java 2KB
SensorActivity.java 1KB
MainActivity.java 2KB
samples
AmbientTemperatureActivity.java 1KB
MagneticFieldActivity.java 1KB
PressureActivity.java 1014B
ProximityActivity.java 1017B
AccelerometerActivity.java 1KB
LightActivity.java 1005B
TemperatureActivity.java 1023B
GravityActivity.java 1011B
RelativeHumidityActivity.java 1KB
OrientationActivity.java 1023B
LinearAccelerationActivity.java 1KB
RotationVectorActivity.java 1KB
GyroscopeActivity.java 1017B
AndroidManifest.xml 1KB
.gitignore 7B
build.gradle 780B
gradle
wrapper
gradle-wrapper.jar 57KB
gradle-wrapper.properties 233B
.gitignore 361B
CHANGELOG.md 3KB
README.md 9KB
settings.gradle 27B
build.gradle 2KB
共 61 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功