@dreiver1/capacitor-step-counter 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,96 +1,113 @@
|
|
|
1
1
|
# step-counter
|
|
2
2
|
|
|
3
|
-
A Capacitor plugin that provides access to the
|
|
3
|
+
A Capacitor plugin that provides access to the **Android Step Counter sensor**
|
|
4
|
+
(`TYPE_STEP_COUNTER`), allowing apps to read step count data and receive real-time
|
|
5
|
+
step updates. Includes a web fallback for non-Android platforms.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ⚠️ Platform Support
|
|
10
|
+
|
|
11
|
+
| Platform | Supported |
|
|
12
|
+
|---------|-----------|
|
|
13
|
+
| Android | ✅ Yes |
|
|
14
|
+
| Web | ⚠️ Fallback (no real sensor) |
|
|
15
|
+
| iOS | ❌ Not supported |
|
|
16
|
+
|
|
17
|
+
> This plugin relies on Android’s `TYPE_STEP_COUNTER` sensor, which is **not available on iOS**.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 📦 Installation
|
|
6
22
|
|
|
7
23
|
```bash
|
|
8
24
|
npm install step-counter
|
|
9
25
|
npx cap sync
|
|
10
|
-
```
|
|
11
26
|
|
|
12
|
-
## API
|
|
13
27
|
|
|
14
|
-
|
|
28
|
+
🔐 Android Permissions
|
|
15
29
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* [`getCurrentSteps()`](#getcurrentsteps)
|
|
19
|
-
* [`addListener('step', ...)`](#addlistenerstep-)
|
|
20
|
-
* [Interfaces](#interfaces)
|
|
30
|
+
On Android 10 (API 29) and above, this plugin requires the
|
|
31
|
+
ACTIVITY_RECOGNITION permission.
|
|
21
32
|
|
|
22
|
-
|
|
33
|
+
The permission is:
|
|
23
34
|
|
|
24
|
-
|
|
25
|
-
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
35
|
+
Automatically requested when calling start()
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
Mandatory for accessing step counter data
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
start() => Promise<void>
|
|
31
|
-
```
|
|
39
|
+
🧠 How the Step Counter Works
|
|
32
40
|
|
|
33
|
-
|
|
41
|
+
Uses Sensor.TYPE_STEP_COUNTER
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
The step count represents total steps since the device last rebooted
|
|
36
44
|
|
|
45
|
+
The value cannot be reset programmatically
|
|
37
46
|
|
|
38
|
-
|
|
47
|
+
For daily or session-based steps, store a baseline value in your app
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
🚀 API
|
|
50
|
+
start()
|
|
51
|
+
start() => Promise<void>
|
|
43
52
|
|
|
44
|
-
Stops listening to the step counter sensor
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
Starts listening to the Android step counter sensor.
|
|
47
55
|
|
|
56
|
+
Requests ACTIVITY_RECOGNITION permission if required
|
|
48
57
|
|
|
49
|
-
|
|
58
|
+
Throws an error if the sensor is not available on the device
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
```
|
|
60
|
+
stop()
|
|
61
|
+
stop() => Promise<void>
|
|
54
62
|
|
|
55
|
-
Returns the current step count since device boot
|
|
56
63
|
|
|
57
|
-
|
|
64
|
+
Stops listening to the step counter sensor.
|
|
58
65
|
|
|
59
|
-
|
|
66
|
+
getCurrentSteps()
|
|
67
|
+
getCurrentSteps() => Promise<StepCounterData>
|
|
60
68
|
|
|
61
69
|
|
|
62
|
-
|
|
70
|
+
Returns the current number of steps since the device last boot.
|
|
63
71
|
|
|
64
|
-
|
|
65
|
-
addListener(
|
|
66
|
-
|
|
72
|
+
addListener('step', ...)
|
|
73
|
+
addListener(
|
|
74
|
+
eventName: 'step',
|
|
75
|
+
listenerFunc: (data: StepCounterData) => void
|
|
76
|
+
) => Promise<PluginListenerHandle>
|
|
67
77
|
|
|
68
|
-
Emits step updates
|
|
69
78
|
|
|
70
|
-
|
|
71
|
-
| ------------------ | ------------------------------------------------------------------------------ |
|
|
72
|
-
| **`eventName`** | <code>'step'</code> |
|
|
73
|
-
| **`listenerFunc`** | <code>(data: <a href="#stepcounterdata">StepCounterData</a>) => void</code> |
|
|
79
|
+
Emits step updates whenever the system sensor reports a change.
|
|
74
80
|
|
|
75
|
-
|
|
81
|
+
Emits the total accumulated steps since boot
|
|
76
82
|
|
|
77
|
-
|
|
83
|
+
Triggered in real time
|
|
78
84
|
|
|
85
|
+
🧩 Interfaces
|
|
86
|
+
StepCounterData
|
|
87
|
+
Prop Type Description
|
|
88
|
+
steps number Total number of steps since device boot
|
|
89
|
+
PluginListenerHandle
|
|
90
|
+
Prop Type
|
|
91
|
+
remove () => Promise<void>
|
|
92
|
+
📝 Example Usage
|
|
93
|
+
import { StepCounter } from 'step-counter';
|
|
79
94
|
|
|
80
|
-
|
|
95
|
+
await StepCounter.start();
|
|
81
96
|
|
|
97
|
+
const listener = await StepCounter.addListener('step', data => {
|
|
98
|
+
console.log('Steps:', data.steps);
|
|
99
|
+
});
|
|
82
100
|
|
|
83
|
-
|
|
101
|
+
// Stop listening
|
|
102
|
+
await StepCounter.stop();
|
|
103
|
+
await listener.remove();
|
|
84
104
|
|
|
85
|
-
|
|
86
|
-
| ----------- | ------------------- |
|
|
87
|
-
| **`steps`** | <code>number</code> |
|
|
105
|
+
❗ Notes & Limitations
|
|
88
106
|
|
|
107
|
+
Step count resets when the device restarts
|
|
89
108
|
|
|
90
|
-
|
|
109
|
+
Not all Android devices include a step counter sensor
|
|
91
110
|
|
|
92
|
-
|
|
93
|
-
| ------------ | ----------------------------------------- |
|
|
94
|
-
| **`remove`** | <code>() => Promise<void></code> |
|
|
111
|
+
This plugin does not manage background execution
|
|
95
112
|
|
|
96
|
-
|
|
113
|
+
Background behavior depends on OS and app lifecycle
|
package/android/src/main/java/com/davidnascimento/plugins/stepcounter/StepCounterPlugin.java
CHANGED
|
@@ -7,11 +7,11 @@ import android.hardware.SensorEventListener;
|
|
|
7
7
|
import android.hardware.SensorManager;
|
|
8
8
|
import android.os.Build;
|
|
9
9
|
|
|
10
|
-
import androidx.core.app.ActivityCompat;
|
|
11
|
-
|
|
12
10
|
import com.getcapacitor.*;
|
|
13
11
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
14
12
|
import com.getcapacitor.annotation.Permission;
|
|
13
|
+
import com.getcapacitor.annotation.PermissionCallback;
|
|
14
|
+
|
|
15
15
|
|
|
16
16
|
@CapacitorPlugin(
|
|
17
17
|
name = "StepCounter",
|
|
@@ -22,8 +22,7 @@ import com.getcapacitor.annotation.Permission;
|
|
|
22
22
|
)
|
|
23
23
|
}
|
|
24
24
|
)
|
|
25
|
-
public class StepCounterPlugin extends Plugin
|
|
26
|
-
implements SensorEventListener {
|
|
25
|
+
public class StepCounterPlugin extends Plugin implements SensorEventListener {
|
|
27
26
|
|
|
28
27
|
private SensorManager sensorManager;
|
|
29
28
|
private Sensor stepSensor;
|
|
@@ -31,20 +30,45 @@ public class StepCounterPlugin extends Plugin
|
|
|
31
30
|
|
|
32
31
|
@Override
|
|
33
32
|
public void load() {
|
|
34
|
-
sensorManager =
|
|
35
|
-
|
|
36
|
-
stepSensor =
|
|
37
|
-
|
|
33
|
+
sensorManager = (SensorManager)
|
|
34
|
+
getContext().getSystemService(Context.SENSOR_SERVICE);
|
|
35
|
+
stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@PermissionCallback
|
|
39
|
+
private void permissionCallback(PluginCall call) {
|
|
40
|
+
|
|
41
|
+
if (getPermissionState("activityRecognition") == PermissionState.GRANTED) {
|
|
42
|
+
startSensor(call);
|
|
43
|
+
} else {
|
|
44
|
+
call.reject("ACTIVITY_RECOGNITION permission denied");
|
|
45
|
+
}
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
@PluginMethod
|
|
41
49
|
public void start(PluginCall call) {
|
|
50
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
|
|
51
|
+
!getPermissionState("activityRecognition").equals(PermissionState.GRANTED)) {
|
|
52
|
+
requestPermissionForAlias("activityRecognition", call, "permissionCallback");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
42
55
|
|
|
43
|
-
if (
|
|
44
|
-
|
|
56
|
+
if (stepSensor == null) {
|
|
57
|
+
call.reject("Step Counter sensor not available");
|
|
45
58
|
return;
|
|
46
59
|
}
|
|
47
60
|
|
|
61
|
+
sensorManager.registerListener(
|
|
62
|
+
this,
|
|
63
|
+
stepSensor,
|
|
64
|
+
SensorManager.SENSOR_DELAY_NORMAL
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
call.resolve();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private void startSensor(PluginCall call) {
|
|
71
|
+
|
|
48
72
|
if (stepSensor == null) {
|
|
49
73
|
call.reject("Step Counter sensor not available");
|
|
50
74
|
return;
|
|
@@ -59,6 +83,7 @@ public class StepCounterPlugin extends Plugin
|
|
|
59
83
|
call.resolve();
|
|
60
84
|
}
|
|
61
85
|
|
|
86
|
+
|
|
62
87
|
@PluginMethod
|
|
63
88
|
public void stop(PluginCall call) {
|
|
64
89
|
sensorManager.unregisterListener(this);
|
|
@@ -78,6 +103,7 @@ public class StepCounterPlugin extends Plugin
|
|
|
78
103
|
|
|
79
104
|
JSObject data = new JSObject();
|
|
80
105
|
data.put("steps", currentSteps);
|
|
106
|
+
|
|
81
107
|
notifyListeners("step", data);
|
|
82
108
|
}
|
|
83
109
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dreiver1/capacitor-step-counter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "A Capacitor plugin that provides access to the device step counter sensor on Android and iOS, with a web fallback.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|