{"id":18784475,"url":"https://github.com/pwittchen/reactivesensors","last_synced_at":"2025-04-03T03:09:52.621Z","repository":{"id":56597327,"uuid":"41884382","full_name":"pwittchen/ReactiveSensors","owner":"pwittchen","description":"Android library monitoring device hardware sensors with RxJava","archived":false,"fork":false,"pushed_at":"2023-03-04T09:14:31.000Z","size":429,"stargazers_count":168,"open_issues_count":1,"forks_count":34,"subscribers_count":13,"default_branch":"RxJava3.x","last_synced_at":"2025-03-24T08:19:39.801Z","etag":null,"topics":["android","hardware","rxandroid","rxjava","rxjava2","rxjava3","sensors"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pwittchen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["pwittchen"],"custom":["https://paypal.me/pwittchen"]}},"created_at":"2015-09-03T21:20:24.000Z","updated_at":"2024-11-07T13:52:26.000Z","dependencies_parsed_at":"2024-01-03T01:29:39.036Z","dependency_job_id":"44b0738c-7b8d-40c6-b3a7-a51d05a27615","html_url":"https://github.com/pwittchen/ReactiveSensors","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FReactiveSensors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FReactiveSensors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FReactiveSensors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FReactiveSensors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pwittchen","download_url":"https://codeload.github.com/pwittchen/ReactiveSensors/tar.gz/refs/heads/RxJava3.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246927835,"owners_count":20856198,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","hardware","rxandroid","rxjava","rxjava2","rxjava3","sensors"],"created_at":"2024-11-07T20:43:08.626Z","updated_at":"2025-04-03T03:09:52.581Z","avatar_url":"https://github.com/pwittchen.png","language":"Java","readme":"# ReactiveSensors\n[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-ReactiveSensors-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/2451) [![Android CI](https://github.com/pwittchen/ReactiveSensors/actions/workflows/android.yml/badge.svg)](https://github.com/pwittchen/ReactiveSensors/actions/workflows/android.yml)\n\nAndroid library monitoring hardware sensors with RxJava.\n\n| Current Branch | Branch  | Artifact Id | Maven Central |\n|:--------------:|:-------:|:-----------:|:--------------:|\n| | [`RxJava1.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava1.x) | `reactivesensors` | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors.svg?style=flat) |\n| | [`RxJava2.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava2.x) | `reactivesensors-rx2` | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx2.svg?style=flat) |\n| :ballot_box_with_check: | [`RxJava3.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava3.x) | `reactivesensors-rx3` | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx3.svg?style=flat) |\n\n\nContents\n--------\n- [Usage](#usage)\n  - [Setting sampling period](#setting-sampling-period)\n- [Example](#example)\n- [Good practices](#good-practices)\n  - [Checking whether sensor exists](#checking-whether-sensor-exists)\n  - [Letting it crash](#letting-it-crash)\n  - [Subscribing and disposing flowables](#subscribing-and-disposing-flowables)\n  - [Filtering stream](#filtering-stream)\n  - [Writing tests](#writing-tests)\n  - [Other practices](#other-practices)\n- [Download](#download)\n- [Tests](#tests)\n- [Code style](#code-style)\n- [Static code analysis](#static-code-analysis)\n- [Releasing](#releasing)\n- [References](#references)\n- [License](#license)\n\nUsage\n-----\n\nCode sample below demonstrates how to observe Gyroscope sensor.\n\nPlease 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.\n\n```java\nnew ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)\n    .subscribeOn(Schedulers.computation())\n    .filter(ReactiveSensorEvent::sensorChanged)\n    .observeOn(AndroidSchedulers.mainThread())\n    .subscribe(new Consumer\u003cReactiveSensorEvent\u003e() {\n      @Override public void call(ReactiveSensorEvent event) {\n\n        float x = event.sensorValues()[0];\n        float y = event.sensorValues()[1];\n        float z = event.sensorValues()[2];\n\n        String message = String.format(\"x = %f, y = %f, z = %f\", x, y, z);\n\n        Log.d(\"gyroscope readings\", message);\n      }\n    });\n}\n```\n\nWe 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.\n\n### Setting sampling period\n\nDefault sampling period for `Flowable` below is set to `SensorManager.SENSOR_DELAY_NORMAL`.\n\n```java\nFlowable\u003cReactiveSensorEvent\u003e observeSensor(int sensorType)\n ```\n\nWe can configure sampling period according to our needs with the following flowable:\n\n```java\nFlowable\u003cReactiveSensorEvent\u003e observeSensor(final int sensorType, final int samplingPeriodInUs)\n ```\n\nWe can use predefined values available in `SensorManager` class from Android SDK:\n- `int SENSOR_DELAY_FASTEST` - get sensor data as fast as possible\n- `int SENSOR_DELAY_GAME` - rate suitable for games\n- `int SENSOR_DELAY_NORMAL` - rate (default) suitable for screen orientation changes\n- `int SENSOR_DELAY_UI` - rate suitable for the user interface\n\nWe can also define our own integer value in microseconds, but it's recommended to use predefined values.\n\nWe can customize RxJava Backpressure Strategy for our flowable with method:\n\n```java\nFlowable\u003cReactiveSensorEvent\u003e observeSensor(final int sensorType, final int samplingPeriodInUs,\n                                            final BackpressureStrategy strategy)\n```\n\nDefault Backpressure Strategy is `BUFFER`.\n\nExample\n-------\n\nExemplary 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.\n\nGood practices\n--------------\n\n### Checking whether sensor exists\n\nWe should check whether device has concrete sensor before we start observing it.\n\nWe can do it in the following way:\n\n```java\nif (reactiveSensors.hasSensor(SENSOR_TYPE)) {\n  // observe sensor\n} else {\n  // show error message\n}\n```\n\n### Letting it crash\n\nWe can let our subscription crash and handle situation when device does not have given sensor e.g. in the `Consumer\u003cThrowable\u003e()` 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.\n\n```java\nnew ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)\n    .subscribeOn(Schedulers.computation())\n    .filter(ReactiveSensorEvent::sensorChanged)\n    .observeOn(AndroidSchedulers.mainThread())\n    .subscribe(new Consumer\u003cReactiveSensorEvent\u003e() {\n      @Override public void accept(ReactiveSensorEvent reactiveSensorEvent) throws Exception {\n        // handle reactiveSensorEvent\n      }\n    }, new Consumer\u003cThrowable\u003e() {\n      @Override public void accept(Throwable throwable) throws Exception {\n        if (throwable instanceof SensorNotFoundException) {\n          textViewForMessage.setText(\"Sorry, your device doesn't have required sensor.\");\n        }\n      }\n    });\n```\n\n### Subscribing and disposing flowables\n\nWhen we are using Disposables in Activity, we should subscribe them in `onResume()` method and dispose them in `onPause()` method.\n\n### Filtering stream\n\nWhen we want to receive **only sensor updates**, we should use `ReactiveSensorEvent::sensorChanged` method in `filter(...)` method from RxJava.\n\nWhen we want to receive **only accuracy updates**, we should use `ReactiveSensorEvent::accuracyChanged` method in `filter(...)` method from RxJava.\n\nIf we don't apply any filter, we will receive both accuracy and sensor readings updates.\n\n### Writing tests\n\n`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.\n\n### Other practices\n\nSee also [Best Practices for Accessing and Using Sensors](http://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-practices).\n\nDownload\n--------\n\nlatest version: ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx3.svg?style=flat)\n\nreplace `x.y.z` with the latest version\n\nYou can depend on the library through Maven:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.pwittchen\u003c/groupId\u003e\n    \u003cartifactId\u003ereactivesensors-rx3\u003c/artifactId\u003e\n    \u003cversion\u003ex.y.z\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor through Gradle:\n\n```groovy\ndependencies {\n  compile 'com.github.pwittchen:reactivesensors-rx3:x.y.z'\n}\n```\n\nTests\n-----\n\nTests are available in `library/src/androidTest/java/` directory and can be executed on emulator or Android device from Android Studio or CLI with the following command:\n\n```\n./gradlew connectedCheck\n```\n\nCode style\n----------\n\nCode style used in the project is called `SquareAndroid` from Java Code Styles repository by Square available at: https://github.com/square/java-code-styles. Currently, library doesn't have checkstyle verification attached. It can be done in the future.\n\nStatic code analysis\n--------------------\n\nStatic code analysis runs Checkstyle, FindBugs, PMD and Lint. It can be executed with command:\n\n ```\n ./gradlew check\n ```\n\nReports from analysis are generated in `library/build/reports/` directory.\n\nReleasing\n----------\n\nSee [RELEASING.md](https://github.com/pwittchen/ReactiveSensors/blob/RxJava3.x/RELEASING.md) file.\n\nReferences\n----------\n- [Sensors Overview](http://developer.android.com/guide/topics/sensors/sensors_overview.html)\n- [SensorManager class from Android SDK](http://developer.android.com/reference/android/hardware/SensorManager.html)\n\nLicense\n-------\n\n    Copyright 2015 Piotr Wittchen\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","funding_links":["https://github.com/sponsors/pwittchen","https://paypal.me/pwittchen"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwittchen%2Freactivesensors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpwittchen%2Freactivesensors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwittchen%2Freactivesensors/lists"}