React Native wrapper for the Android Activity Recognition API. It attempts to determine the user activity such as driving, walking, running and cycling. Possible detected activities are listed here.
Right now only Android devices are supported, but iOS support could be added using CMMotionActivity. I would love to see a pull request that adds iOS support.
npm i -S react-native-activity-recognition
or with Yarn:
yarn add react-native-activity-recognition
Make alterations to the following files in your project:
...
include ':react-native-activity-recognition'
project(':react-native-activity-recognition').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-activity-recognition/android')
...
...
dependencies {
...
compile project(':react-native-activity-recognition')
...
}
import com.xebia.activityrecognition.RNActivityRecognitionPackage; // <--- add import
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// ...
new RNActivityRecognitionPackage() // <--- add package
);
}
...
<application ...>
...
<service android:name="com.xebia.activityrecognition.DetectionService"/>
...
</application>
...
import ActivityRecognition from 'react-native-activity-recognition'
...
// Start activity detection
const detectionIntervalMillis = 1000
ActivityRecognition.start(detectionIntervalMillis)
// Subscribe to updates
this.unsubscribe = ActivityRecognition.subscribe(detectedActivities => {
const mostProbableActivity = detectedActivities.sorted[0]
})
...
// Stop activity detection and remove the listener
ActivityRecognition.stop()
this.unsubscribe()
detectedActivities
is an object with keys for each detected activity, each of which have an integer percentage (0-100)
indicating the likelihood that the user is performing this activity. For example:
{
ON_FOOT: 8,
IN_VEHICLE: 15,
WALKING: 8,
STILL: 77
}
Additionally, the detectedActivities.sorted
getter is provided which returns an array of activities, ordered by their
confidence value:
[
{ type: 'STILL', confidence: 77 },
{ type: 'IN_VEHICLE', confidence: 15 },
{ type: 'ON_FOOT', confidence: 8 },
{ type: 'WALKING', confidence: 8 },
]
Because the activities are sorted by confidence level, the first value will be the one with the highest probability. Note that ON_FOOT and WALKING are related but won't always have the same value. I have never seen WALKING with a higher confidence than ON_FOOT, but it may happen that WALKING comes before ON_FOOT in the array if they have the same value.
The following activity types are supported:
- IN_VEHICLE
- ON_BICYCLE
- ON_FOOT
- RUNNING
- WALKING
- STILL
- TILTING
- UNKNOWN
Starts listening for activity updates. The detectionIntervalMillis is passed to ActivityRecognitionApi.requestActivityUpdates().
Subscribes a callback function to be invoked on each activity update. Returns a function which can be called in order to unsubscribe. The update callback will be invoked with the detectedActivities object.
Stops listening for activity updates.
The following projects were very helpful in developing this library: