@danyalwe/capacitor-sensors
v1.0.2
Published
Capacitor plugin for device sensors
Readme
Capacitor Plugin - Sensors
Get access to every sensor present in the device!
Supported Android version: 24+
Supported iOS version: Not supported
Supported Browsers: Chromium-based
Install
npm install @danyalwe/capacitor-sensors
npx cap syncIf you want to use the HEART_BEAT or HEART_RATE sensor, you need to request the BODY_SENSORS permission in your app:
<uses-permission android:name="android.permission.BODY_SENSORS" />If you want to use the STEP_COUNTER or STEP_DETECTOR sensor, you need to request the ACTIVITY_RECOGNITION permission in your app:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />Example
The following example demonstrates how to use the Sensors plugin to access the device's accelerometer:
import { Sensors } from '@danyalwe/capacitor-sensors'
// Initialize the specific sensor and obtain infos about it
const sensor = await Sensors.init({ type: 'ACCELEROMETER' })
// Start the sensor to begin receiving data
await Sensors.start({ type: 'ACCELEROMETER' })
// Add a listener to receive accelerometer data
Sensors.addListener('ACCELEROMETER', (data) => {
console.log('Accelerometer data:', data)
})
// Stop the sensor
await Sensors.stop({ type: 'ACCELEROMETER' })Todos
- [ ] Add support for iOS
Supported methods
| Name | Android | iOS | Web | | :------------------ | :------ | :-- | :-- | | init | ✅ | ❌ | ✅ | | getAvailableSensors | ✅ | ❌ | ✅ | | start | ✅ | ❌ | ✅ | | stop | ✅ | ❌ | ✅ | | addListener | ✅ | ❌ | ✅ | | removeAllListeners | ✅ | ❌ | ✅ | | requestPermissions | ✅ | ❌ | ✅ |
Supported sensors
| Sensors | Android | iOS | Web |
| :-------------------------------- | :------ | :-- | :-- |
| MOTION_DETECTOR | ✅ | ❌ | ✅ |
| LINEAR_ACCELERATION | ✅ | ❌ | ✅ |
| MAGNETOMETER | ✅ | ❌ | ✅ |
| GRAVITY | ✅ | ❌ | ✅ |
| GYROSCOPE | ✅ | ❌ | ✅ |
| AMBIENT_LIGHT | ✅ | ❌ | ✅ |
| ACCELEROMETER | ✅ | ❌ | ✅ |
| ABSOLUTE_ORIENTATION | ✅ | ❌ | ✅ |
| RELATIVE_ORIENTATION | ✅ | ❌ | ✅ |
| TEMPERATURE | ✅ | ❌ | ❌ |
| GAME_ROTATION_VECTOR | ✅ | ❌ | ❌ |
| GEOMAGNETIC_ROTATION_VECTOR | ✅ | ❌ | ❌ |
| HEART_BEAT | ✅ | ❌ | ❌ |
| HEART_RATE | ✅ | ❌ | ❌ |
| POSE_6DOF | ✅ | ❌ | ❌ |
| PRESSURE | ✅ | ❌ | ❌ |
| PROXIMITY | ✅ | ❌ | ❌ |
| RELATIVE_HUMIDITY | ✅ | ❌ | ❌ |
| ROTATION_VECTOR | ✅ | ❌ | ❌ |
| SIGNIFICANT_MOTION | ✅ | ❌ | ❌ |
| STATIONARY_DETECTOR | ✅ | ❌ | ❌ |
| STEP_COUNTER | ✅ | ❌ | ❌ |
| STEP_DETECTOR | ✅ | ❌ | ❌ |
API
The Sensors Plugin interface.
Interfaces
SensorOptions
Represents the options for a sensor.
| Prop | Type | Description |
| ----------- | --------------------------------------------------- | ---------------------------------- |
| type | SensorType | The type of sensor to use. |
| delay | SensorDelay | The delay between sensor readings. |
SensorData
Represents the data returned by a sensor, including any additional information about the sensor.
| Prop | Type |
| ----------- | --------------------------------------------------- |
| infos | SensorInfos |
SensorInfos
Interface representing sensor information.
| Prop | Type | Description |
| ---------------- | ------------------- | ---------------------------------------------------------- |
| name | string | The name of the sensor. |
| vendor | string | The vendor of the sensor. |
| version | number | The version of the sensor. |
| type | number | The type of the sensor. |
| maxRange | number | The maximum range of the sensor in sensor units. |
| resolution | number | The resolution of the sensor in sensor units. |
| power | number | The power consumption of the sensor in milliamperes. |
| minDelay | number | The minimum delay between sensor readings in microseconds. |
| maxDelay | number | The maximum delay between sensor readings in microseconds. |
PermissionStatus
| Prop | Type |
| -------------- | --------------------------------------------------------------------------------------------------------------- |
| onchange | (this: PermissionStatus, ev: Event) => any |
| state | PermissionState |
| Method | Signature | Description | | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | addEventListener | <K extends "change">(type: K, listener: (this: PermissionStatus, ev: PermissionStatusEventMap[K]) => any, options?: boolean | AddEventListenerOptions) => void | Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched. The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture. When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET. When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners. When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed. The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. | | addEventListener | (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void | Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched. The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture. When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET. When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners. When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed. The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. | | removeEventListener | <K extends "change">(type: K, listener: (this: PermissionStatus, ev: PermissionStatusEventMap[K]) => any, options?: boolean | EventListenerOptions) => void | Removes the event listener in target's event listener list with the same type, callback, and options. | | removeEventListener | (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => void | Removes the event listener in target's event listener list with the same type, callback, and options. |
PermissionStatusEventMap
| Prop | Type |
| -------------- | --------------------------------------- |
| "change" | Event |
Event
An event which takes place in the DOM.
| Prop | Type | Description |
| ---------------------- | --------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| bubbles | boolean | Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. |
| cancelBubble | boolean | |
| cancelable | boolean | Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. |
| composed | boolean | Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. |
| currentTarget | EventTarget | Returns the object whose event listener's callback is currently being invoked. |
| defaultPrevented | boolean | Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. |
| eventPhase | number | Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. |
| isTrusted | boolean | Returns true if event was dispatched by the user agent, and false otherwise. |
| returnValue | boolean | |
| srcElement | EventTarget | |
| target | EventTarget | Returns the object to which event is dispatched (its target). |
| timeStamp | number | Returns the event's timestamp as the number of milliseconds measured relative to the time origin. |
| type | string | Returns the type of event, e.g. "click", "hashchange", or "submit". |
| AT_TARGET | number | |
| BUBBLING_PHASE | number | |
| CAPTURING_PHASE | number | |
| NONE | number | |
| Method | Signature | Description | | ---------------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | composedPath | () => EventTarget[] | Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget. | | initEvent | (type: string, bubbles?: boolean, cancelable?: boolean) => void | | | preventDefault | () => void | If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled. | | stopImmediatePropagation | () => void | Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects. | | stopPropagation | () => void | When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object. |
EventTarget
EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.
| Method | Signature | Description | | ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | addEventListener | (type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions) => void | Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched. The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture. When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET. When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners. When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed. The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. | | dispatchEvent | (event: Event) => boolean | Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise. | | removeEventListener | (type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean) => void | Removes the event listener in target's event listener list with the same type, callback, and options. |
EventListener
EventListenerObject
| Method | Signature | | --------------- | -------------------------------------------- | | handleEvent | (evt: Event) => void |
AddEventListenerOptions
| Prop | Type |
| ------------- | -------------------- |
| once | boolean |
| passive | boolean |
EventListenerOptions
| Prop | Type |
| ------------- | -------------------- |
| capture | boolean |
SensorResult
Represents the result of a sensor reading.
| Prop | Type | Description |
| --------------- | --------------------- | -------------------------------------------------- |
| accuracy | number | The accuracy of the sensor reading, if available. |
| timestamp | number | The timestamp of the sensor reading, if available. |
| values | number[] | The values obtained from the sensor reading. |
PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove | () => Promise<void> |
Type Aliases
SensorType
(typeof SensorTypes)[number]
SensorDelay
(typeof SensorDelays)[number]
EventListenerOrEventListenerObject
EventListener | EventListenerObject
PermissionState
"denied" | "granted" | "prompt"
