Skip to content

Commit 1b592db

Browse files
committed
Replace getMostProbableActivity with sorted getter.
1 parent 1873b9f commit 1b592db

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ import ActivityRecognition from 'react-native-activity-recognition'
8888
...
8989

9090
// Start activity detection
91-
ActivityRecognition.start(1000) // detection interval in ms
91+
const detectionIntervalMillis = 1000
92+
ActivityRecognition.start(detectionIntervalMillis)
9293

9394
// Subscribe to updates
9495
this.unsubscribe = ActivityRecognition.subscribe(detectedActivities => {
95-
const mostProbable = ActivityRecognition.getMostProbableActivity(detectedActivities) // => { type: 'STILL', confidence: 77 }
96+
const mostProbableActivity = detectedActivities.sorted[0]
9697
})
9798

9899
...
@@ -102,7 +103,7 @@ ActivityRecognition.stop()
102103
this.unsubscribe()
103104
```
104105

105-
`detectedActivities` is an object with keys for each detected activity, each of which have an integer value from 0 to 100
106+
`detectedActivities` is an object with keys for each detected activity, each of which have an integer percentage (0-100)
106107
indicating the likelihood that the user is performing this activity. For example:
107108

108109
```js
@@ -114,6 +115,22 @@ indicating the likelihood that the user is performing this activity. For example
114115
}
115116
```
116117

118+
Additionally, the `detectedActivities.sorted` getter is provided which returns an array of activities, ordered by their
119+
confidence value:
120+
121+
```js
122+
[
123+
{ type: 'STILL', confidence: 77 },
124+
{ type: 'IN_VEHICLE', confidence: 15 },
125+
{ type: 'ON_FOOT', confidence: 8 },
126+
{ type: 'WALKING', confidence: 8 },
127+
]
128+
```
129+
130+
Because the activities are sorted by confidence level, the first value will be the one with the highest probability.
131+
Note that ON_FOOT and WALKING are related but won't always have the same value. I have never seen WALKING with a higher
132+
confidence than ON_FOOT, but it may happen that WALKING comes before ON_FOOT in the array if they have the same value.
133+
117134
The following activity types are supported:
118135
119136
- IN_VEHICLE
@@ -132,15 +149,11 @@ Starts listening for activity updates. The detectionIntervalMillis is passed to
132149
133150
### `subscribe(callback: Function): Function`
134151
Subscribes a callback function to be invoked on each activity update. Returns a function which can be called in order to unsubscribe.
135-
The update callback will be invoked with an object representing the detected activities and their confidence percentage.
152+
The update callback will be invoked with the detectedActivities object.
136153
137154
### `stop(): void`
138155
Stops listening for activity updates.
139156
140-
### `getMostProbableActivity(detectedActivities: Object): Object`
141-
Util function to determine the most probable activity `type` and its `confidence` percentage based on the detectedActivities object used in the `subscribe` callback.
142-
For example: `{ type: 'STILL', confidence: 77 }`
143-
144157
## Credits / prior art
145158
146159
The following projects were very helpful in developing this library:

index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
const { DeviceEventEmitter, NativeModules } = require('react-native');
2-
const { ActivityRecognition } = NativeModules;
1+
const { DeviceEventEmitter, NativeModules } = require('react-native')
2+
const { ActivityRecognition } = NativeModules
33

4-
ActivityRecognition.subscribe = function subscribe(callback) {
5-
const subscription = DeviceEventEmitter.addListener('DetectedActivity', activities => callback(activities))
6-
return () => DeviceEventEmitter.removeSubscription(subscription)
7-
}
4+
ActivityRecognition.subscribe = subscribe
85

9-
ActivityRecognition.getMostProbableActivity = function getMostProbableActivity(detectedActivities) {
10-
const mostProbableType = Object.keys(detectedActivities).reduce((acc, type) => {
11-
return detectedActivities[acc] > detectedActivities[type] ? acc : type
6+
function subscribe(callback) {
7+
const subscription = DeviceEventEmitter.addListener('DetectedActivity', detectedActivities => {
8+
Object.defineProperty(detectedActivities, 'sorted', {
9+
get: () => Object.keys(detectedActivities)
10+
.map(type => ({ type: type, confidence: detectedActivities[type] }))
11+
.sort((a, b) => b.confidence - a.confidence),
12+
})
13+
callback(detectedActivities)
1214
})
13-
return {
14-
type: mostProbableType,
15-
confidence: detectedActivities[mostProbableType],
16-
}
15+
return () => DeviceEventEmitter.removeSubscription(subscription)
1716
}
1817

1918
module.exports = ActivityRecognition

0 commit comments

Comments
 (0)