react-native-nitro-sensors
v0.1.6
Published
React Native Nitro sensor APIs for Android and iOS
Readme
react-native-nitro-sensors
Instance-based Nitro sensor APIs for Android and iOS.
Hooks
import { useAccelerometer } from 'react-native-nitro-sensors'
function AccelerometerValues() {
const { reading, error, isAvailable, isObserving } = useAccelerometer({
intervalMs: 16,
})
if (!isAvailable) return null
if (error != null) return <Text>{error.message}</Text>
return (
<Text>
{isObserving && reading != null
? `${reading.x}, ${reading.y}, ${reading.z}`
: 'Waiting for accelerometer...'}
</Text>
)
}Available hooks:
useAccelerometer(options)useBarometer(options)useDeviceMotion(options)useGyroscope(options)useMagnetometer(options)useLightSensor(options)usePedometer(options)
Hooks start observing by default and stop when the component unmounts. Pass
isEnabled: false to keep a hook idle until your app is ready to observe.
options.intervalMs is optional and uses the same native sampling behavior as
startObserving.
const pedometer = usePedometer({
isEnabled: hasPhysicalActivityPermission,
})Hook errors are exposed through error as a JavaScript Error. Platform
preconditions still apply; for example, Android pedometer hooks need the same
Physical activity permission as the instance API before observation starts.
Expo Config Plugin
Add the plugin to your Expo config before running prebuild:
{
"expo": {
"plugins": [
[
"react-native-nitro-sensors",
{
"motionPermission": "Allow $(PRODUCT_NAME) to access your device motion."
}
]
]
}
}The plugin adds NSMotionUsageDescription on iOS and
android.permission.ACTIVITY_RECOGNITION on Android. If motionPermission is
omitted, the plugin preserves an existing NSMotionUsageDescription value or
uses a default message.
Android apps still need to request the Physical activity runtime permission before starting pedometer updates on Android 10/API 29 and newer.
Instance API
import { Sensors } from 'react-native-nitro-sensors'
const accelerometer = Sensors.createAccelerometer()
if (accelerometer.isAvailable) {
accelerometer.startObserving(
{ intervalMs: 16 },
(reading) => {
console.log(reading.x, reading.y, reading.z, reading.timestampMs)
},
(error) => {
console.error(error.message)
}
)
}Available sensors:
- Accelerometer: Android and iOS
- Barometer: Android and iOS
- DeviceMotion: Android and iOS
- Gyroscope: Android and iOS
- Magnetometer: Android and iOS
- LightSensor: Android only
- Pedometer: Android and iOS
iOS apps must include NSMotionUsageDescription in their Info.plist before
starting CoreMotion-backed sensors such as Barometer or Pedometer on a real
device.
Android apps that use Pedometer must declare
android.permission.ACTIVITY_RECOGNITION and request the Physical activity
runtime permission before starting pedometer updates on Android 10/API 29 and
newer.
Each sensor object keeps native observation state and exposes isAvailable,
isObserving, startObserving(options, onReading, onError), and
stopObserving(). Starting an unsupported sensor throws instead of failing
silently.
options.intervalMs is optional. Continuous sensors use it as a sampling-period
hint and default to 16 ms; platform-driven sensors may ignore it when the native
API does not support caller-controlled intervals.
