community-cordova-plugin-magnetometer 1.0.1

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.
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="inheritedJdk" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/community-cordova-plugin-magnetometer.iml" filepath="$PROJECT_DIR$/.idea/community-cordova-plugin-magnetometer.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
package/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2025-01-20
9
+
10
+ ### Added
11
+
12
+ - Initial release
13
+ - `isAvailable()` - Check if magnetometer sensor is available
14
+ - `getReading()` - Get single magnetometer reading with x, y, z values in microteslas
15
+ - `getHeading()` - Get compass heading (magnetic and true north)
16
+ - `watchReadings()` - Continuous magnetometer monitoring with configurable frequency
17
+ - `stopWatch()` - Stop magnetometer monitoring
18
+ - `watchHeading()` - Continuous compass heading monitoring
19
+ - `stopWatchHeading()` - Stop heading monitoring
20
+ - `getMagnetometerInfo()` - Get complete magnetometer information
21
+ - `getAccuracy()` - Get current sensor accuracy level
22
+ - `isCalibrationNeeded()` - Check if calibration is required
23
+ - `getFieldStrength()` - Get total magnetic field magnitude
24
+ - Full TypeScript definitions
25
+ - Android support using SensorManager
26
+ - iOS support using CoreMotion and CoreLocation frameworks
27
+ - Browser support with Generic Sensor API and mock fallback
package/README.md ADDED
@@ -0,0 +1,383 @@
1
+ [![NPM version](https://img.shields.io/npm/v/community-cordova-plugin-magnetometer)](https://www.npmjs.com/package/community-cordova-plugin-magnetometer)
2
+
3
+ # Community Cordova Magnetometer Plugin
4
+
5
+ Cordova plugin to access the device magnetometer (compass) sensor. Provides raw magnetic field data, compass heading, and continuous monitoring capabilities.
6
+
7
+ I dedicate a considerable amount of my free time to developing and maintaining many cordova plugins for the community ([See the list with all my maintained plugins][community_plugins]).
8
+ To help ensure this plugin is kept updated,
9
+ new features are added and bugfixes are implemented quickly,
10
+ please donate a couple of dollars (or a little more if you can stretch) as this will help me to afford to dedicate time to its maintenance.
11
+ Please consider donating if you're using this plugin in an app that makes you money,
12
+ or if you're asking for new features or priority bug fixes. Thank you!
13
+
14
+ [![](https://img.shields.io/static/v1?label=Sponsor%20Me&style=for-the-badge&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/eyalin)
15
+
16
+ ## Supported Platforms
17
+
18
+ - Android
19
+ - iOS
20
+ - Browser (with Generic Sensor API support, falls back to mock data)
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ cordova plugin add community-cordova-plugin-magnetometer
26
+ ```
27
+
28
+ Or install from GitHub:
29
+
30
+ ```bash
31
+ cordova plugin add https://github.com/EYALIN/community-cordova-plugin-magnetometer.git
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ### TypeScript
37
+
38
+ ```typescript
39
+ import MagnetometerManager, {
40
+ IMagnetometerReading,
41
+ IHeadingData,
42
+ IMagnetometerInfo
43
+ } from 'community-cordova-plugin-magnetometer';
44
+
45
+ // Check availability
46
+ const available = await MagnetometerManager.isAvailable();
47
+ console.log('Magnetometer available:', available === 1);
48
+
49
+ // Get single reading
50
+ const reading: IMagnetometerReading = await MagnetometerManager.getReading();
51
+ console.log(`Magnetic field: X=${reading.x}, Y=${reading.y}, Z=${reading.z} μT`);
52
+
53
+ // Get compass heading
54
+ const heading: IHeadingData = await MagnetometerManager.getHeading();
55
+ console.log(`Heading: ${heading.magneticHeading}°`);
56
+
57
+ // Get complete info
58
+ const info: IMagnetometerInfo = await MagnetometerManager.getMagnetometerInfo();
59
+ console.log('Magnetometer info:', info);
60
+ ```
61
+
62
+ ### JavaScript
63
+
64
+ ```javascript
65
+ // Check if magnetometer is available
66
+ MagnetometerPlugin.isAvailable()
67
+ .then(function(available) {
68
+ console.log('Magnetometer available:', available === 1);
69
+ });
70
+
71
+ // Get single magnetometer reading
72
+ MagnetometerPlugin.getReading()
73
+ .then(function(reading) {
74
+ console.log('X:', reading.x, 'μT');
75
+ console.log('Y:', reading.y, 'μT');
76
+ console.log('Z:', reading.z, 'μT');
77
+ console.log('Magnitude:', reading.magnitude, 'μT');
78
+ })
79
+ .catch(function(error) {
80
+ console.error('Error:', error);
81
+ });
82
+
83
+ // Get compass heading
84
+ MagnetometerPlugin.getHeading()
85
+ .then(function(heading) {
86
+ console.log('Magnetic Heading:', heading.magneticHeading, '°');
87
+ console.log('True Heading:', heading.trueHeading, '°');
88
+ });
89
+
90
+ // Watch magnetometer readings continuously
91
+ MagnetometerPlugin.watchReadings(
92
+ function(reading) {
93
+ console.log('Reading:', reading.x, reading.y, reading.z);
94
+ },
95
+ function(error) {
96
+ console.error('Error:', error);
97
+ },
98
+ { frequency: 100 } // Update every 100ms
99
+ );
100
+
101
+ // Stop watching readings
102
+ MagnetometerPlugin.stopWatch();
103
+
104
+ // Watch compass heading continuously
105
+ MagnetometerPlugin.watchHeading(
106
+ function(heading) {
107
+ console.log('Heading:', heading.magneticHeading, '°');
108
+ },
109
+ function(error) {
110
+ console.error('Error:', error);
111
+ },
112
+ { frequency: 100, filter: 1 } // Update every 100ms, minimum 1° change
113
+ );
114
+
115
+ // Stop watching heading
116
+ MagnetometerPlugin.stopWatchHeading();
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ ### Methods
122
+
123
+ #### `isAvailable(): Promise<number>`
124
+
125
+ Check if magnetometer sensor is available on the device.
126
+
127
+ **Returns:** `1` if available, `0` if not.
128
+
129
+ ---
130
+
131
+ #### `getReading(): Promise<IMagnetometerReading>`
132
+
133
+ Get a single magnetometer reading.
134
+
135
+ **Returns:**
136
+ ```typescript
137
+ {
138
+ x: number; // Magnetic field X axis (μT)
139
+ y: number; // Magnetic field Y axis (μT)
140
+ z: number; // Magnetic field Z axis (μT)
141
+ magnitude: number; // Total field strength (μT)
142
+ timestamp: number; // Reading timestamp (ms)
143
+ }
144
+ ```
145
+
146
+ ---
147
+
148
+ #### `getHeading(): Promise<IHeadingData>`
149
+
150
+ Get current compass heading.
151
+
152
+ **Returns:**
153
+ ```typescript
154
+ {
155
+ magneticHeading: number; // Degrees from magnetic north (0-359.99)
156
+ trueHeading: number; // Degrees from true north (0-359.99)
157
+ headingAccuracy: number; // Accuracy in degrees (iOS only, -1 if unavailable)
158
+ timestamp: number; // Reading timestamp (ms)
159
+ }
160
+ ```
161
+
162
+ ---
163
+
164
+ #### `watchReadings(successCallback, errorCallback, options?): void`
165
+
166
+ Start watching magnetometer readings continuously.
167
+
168
+ **Parameters:**
169
+ - `successCallback`: Function called with `IMagnetometerReading` on each update
170
+ - `errorCallback`: Function called on error
171
+ - `options`: Optional settings
172
+ - `frequency`: Update interval in milliseconds (default: 100)
173
+
174
+ ---
175
+
176
+ #### `stopWatch(): Promise<void>`
177
+
178
+ Stop watching magnetometer readings.
179
+
180
+ ---
181
+
182
+ #### `watchHeading(successCallback, errorCallback, options?): void`
183
+
184
+ Start watching compass heading continuously.
185
+
186
+ **Parameters:**
187
+ - `successCallback`: Function called with `IHeadingData` on each update
188
+ - `errorCallback`: Function called on error
189
+ - `options`: Optional settings
190
+ - `frequency`: Update interval in milliseconds (default: 100)
191
+ - `filter`: Minimum heading change in degrees to trigger update (iOS only)
192
+
193
+ ---
194
+
195
+ #### `stopWatchHeading(): Promise<void>`
196
+
197
+ Stop watching compass heading.
198
+
199
+ ---
200
+
201
+ #### `getMagnetometerInfo(): Promise<IMagnetometerInfo>`
202
+
203
+ Get complete magnetometer information.
204
+
205
+ **Returns:**
206
+ ```typescript
207
+ {
208
+ isAvailable: boolean;
209
+ reading: IMagnetometerReading;
210
+ heading: IHeadingData;
211
+ accuracy: number; // 0=unreliable, 1=low, 2=medium, 3=high
212
+ calibrationNeeded: boolean;
213
+ platform: string; // 'android', 'ios', or 'browser'
214
+ }
215
+ ```
216
+
217
+ ---
218
+
219
+ #### `getAccuracy(): Promise<number>`
220
+
221
+ Get current sensor accuracy level.
222
+
223
+ **Returns:** Accuracy level:
224
+ - `0` - Unreliable
225
+ - `1` - Low
226
+ - `2` - Medium
227
+ - `3` - High
228
+
229
+ ---
230
+
231
+ #### `isCalibrationNeeded(): Promise<number>`
232
+
233
+ Check if the magnetometer needs calibration.
234
+
235
+ **Returns:** `1` if calibration needed, `0` if not.
236
+
237
+ ---
238
+
239
+ #### `getFieldStrength(): Promise<number>`
240
+
241
+ Get the total magnetic field strength (magnitude).
242
+
243
+ **Returns:** Field strength in microteslas (μT).
244
+
245
+ ## Interfaces
246
+
247
+ ### IMagnetometerReading
248
+
249
+ ```typescript
250
+ interface IMagnetometerReading {
251
+ x: number; // Magnetic field X axis in microteslas (μT)
252
+ y: number; // Magnetic field Y axis in microteslas (μT)
253
+ z: number; // Magnetic field Z axis in microteslas (μT)
254
+ magnitude: number; // Total magnetic field magnitude in microteslas (μT)
255
+ timestamp: number; // Timestamp of the reading in milliseconds
256
+ }
257
+ ```
258
+
259
+ ### IHeadingData
260
+
261
+ ```typescript
262
+ interface IHeadingData {
263
+ magneticHeading: number; // Heading relative to magnetic north (0-359.99°)
264
+ trueHeading: number; // Heading relative to true north (0-359.99°)
265
+ headingAccuracy: number; // Accuracy in degrees (iOS only)
266
+ timestamp: number; // Timestamp in milliseconds
267
+ }
268
+ ```
269
+
270
+ ### IMagnetometerInfo
271
+
272
+ ```typescript
273
+ interface IMagnetometerInfo {
274
+ isAvailable: boolean;
275
+ reading: IMagnetometerReading;
276
+ heading: IHeadingData;
277
+ accuracy: number;
278
+ calibrationNeeded: boolean;
279
+ platform: string;
280
+ }
281
+ ```
282
+
283
+ ## Platform Notes
284
+
285
+ ### iOS
286
+
287
+ - Uses `CoreMotion` framework for raw magnetometer data
288
+ - Uses `CoreLocation` framework for compass heading
289
+ - `trueHeading` requires location services to be enabled
290
+ - `headingAccuracy` is available and indicates the accuracy in degrees
291
+ - Calibration prompt is shown automatically when needed
292
+
293
+ ### Android
294
+
295
+ - Uses `SensorManager` with `TYPE_MAGNETIC_FIELD` sensor
296
+ - Compass heading calculated using rotation matrix from magnetometer + accelerometer
297
+ - `headingAccuracy` returns `-1` (not available on Android)
298
+ - `trueHeading` equals `magneticHeading` (GPS-based declination not implemented)
299
+
300
+ ### Browser
301
+
302
+ - Uses Generic Sensor API (`Magnetometer`, `AbsoluteOrientationSensor`) when available
303
+ - Falls back to mock data for testing when sensors not available
304
+ - Mock data simulates realistic sensor behavior with slight variations
305
+
306
+ ## Typical Magnetic Field Values
307
+
308
+ - Earth's magnetic field: 25-65 μT (varies by location)
309
+ - Near a smartphone: 100-500 μT
310
+ - Near a strong magnet: 1000+ μT
311
+
312
+ ## Use Cases
313
+
314
+ - Metal detector apps
315
+ - Compass applications
316
+ - Augmented reality navigation
317
+ - Magnetic field measurement tools
318
+ - Device orientation detection
319
+
320
+ ## About the Community Cordova Plugins
321
+
322
+ This plugin is part of the **Community Cordova Plugins** collection, maintained by [EYALIN](https://github.com/EYALIN).
323
+
324
+ I spend most of my free time maintaining and improving these open-source Cordova plugins to help the mobile development community. The goal is to keep essential Cordova plugins alive, updated, and compatible with the latest platform versions.
325
+
326
+ ### Maintained Plugins
327
+
328
+ Here are some of the community plugins I maintain:
329
+
330
+ | Plugin | Description |
331
+ |--------|-------------|
332
+ | [community-cordova-plugin-admob](https://github.com/eyalin/eyalin-admob-plus) | AdMob integration for ads monetization |
333
+ | [community-cordova-plugin-battery-status](https://github.com/EYALIN/community-cordova-plugin-battery-status) | Battery level and charging status |
334
+ | [community-cordova-plugin-cpu](https://github.com/EYALIN/community-cordova-plugin-cpu) | CPU information and usage |
335
+ | [community-cordova-plugin-ram](https://github.com/EYALIN/community-cordova-plugin-ram) | RAM/memory information |
336
+ | [community-cordova-plugin-apps](https://github.com/EYALIN/community-cordova-plugin-apps) | Installed apps information |
337
+ | [community-cordova-plugin-file-opener](https://github.com/EYALIN/community-cordova-plugin-file-opener) | Open files with external apps |
338
+ | [community-cordova-plugin-files-picker](https://github.com/EYALIN/community-cordova-plugin-files-picker) | Native file picker |
339
+ | [community-cordova-plugin-image-picker](https://github.com/EYALIN/community-cordova-plugin-image-picker) | Native image picker |
340
+ | [community-cordova-plugin-firebase-analytics](https://github.com/EYALIN/community-cordova-plugin-firebase-analytics) | Firebase Analytics integration |
341
+ | [community-cordova-plugin-social-sharing](https://github.com/EYALIN/community-cordova-plugin-social-sharing) | Social sharing functionality |
342
+ | [community-cordova-plugin-sqlite](https://github.com/EYALIN/community-cordova-plugin-sqlite) | SQLite database |
343
+ | [community-cordova-plugin-sim](https://github.com/EYALIN/community-cordova-plugin-sim) | SIM card information |
344
+ | [community-cordova-plugin-wifi](https://github.com/EYALIN/community-cordova-plugin-wifi) | WiFi network information |
345
+ | [community-cordova-plugin-nfc](https://github.com/EYALIN/community-cordova-plugin-nfc) | NFC functionality |
346
+ | [community-cordova-plugin-inappbrowser](https://github.com/EYALIN/community-cordova-plugin-inappbrowser) | In-app browser |
347
+ | [community-cordova-plugin-security-check](https://github.com/EYALIN/community-cordova-plugin-security-check) | Device security status checks |
348
+ | [community-cordova-plugin-magnetometer](https://github.com/EYALIN/community-cordova-plugin-magnetometer) | Magnetometer/compass sensor |
349
+ | [community-cordova-plugin-screen-time](https://github.com/EYALIN/community-cordova-plugin-screen-time) | Screen time tracking |
350
+ | [community-cordova-plugin-native-settings](https://github.com/EYALIN/community-cordova-plugin-native-settings) | Open native device settings |
351
+ | [community-cordova-plugin-printer](https://github.com/EYALIN/community-cordova-plugin-printer) | Document printing |
352
+ | ...and many more!
353
+
354
+ ### Why Support?
355
+
356
+ Maintaining these plugins requires significant time and effort:
357
+ - Keeping up with Android and iOS platform changes
358
+ - Testing on multiple devices and OS versions
359
+ - Responding to issues and pull requests
360
+ - Adding new features requested by the community
361
+ - Writing documentation and examples
362
+
363
+ If these plugins help you in your projects, please consider supporting the work:
364
+
365
+ [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/eyalin)
366
+
367
+ [![Sponsor](https://img.shields.io/badge/Sponsor-GitHub-pink.svg)](https://github.com/sponsors/eyalin)
368
+
369
+ Your support helps keep these plugins maintained and free for everyone!
370
+
371
+ ## License
372
+
373
+ MIT
374
+
375
+ ## Author
376
+
377
+ EYALIN
378
+
379
+ ## Contributing
380
+
381
+ Contributions are welcome! Please feel free to submit a Pull Request.
382
+
383
+ [community_plugins]: https://github.com/eyalin?tab=repositories&q=community-cordova-plugin
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "community-cordova-plugin-magnetometer",
3
+ "version": "1.0.1",
4
+ "description": "Cordova plugin to access the device magnetometer (compass) sensor",
5
+ "main": "./types/index.d.ts",
6
+ "types": "./types/index.d.ts",
7
+ "cordova": {
8
+ "id": "community-cordova-plugin-magnetometer",
9
+ "platforms": [
10
+ "android",
11
+ "ios",
12
+ "browser"
13
+ ]
14
+ },
15
+ "readme": "README.md",
16
+ "repository": "EYALIN/community-cordova-plugin-magnetometer",
17
+ "homepage": "https://github.com/EYALIN/community-cordova-plugin-magnetometer",
18
+ "funding": "https://github.com/sponsors/EYALIN",
19
+ "bugs": "https://github.com/EYALIN/community-cordova-plugin-magnetometer/issues",
20
+ "keywords": [
21
+ "ecosystem:cordova",
22
+ "cordova-android",
23
+ "cordova-ios",
24
+ "cordova-browser",
25
+ "magnetometer",
26
+ "compass",
27
+ "sensor",
28
+ "magnetic",
29
+ "heading",
30
+ "orientation"
31
+ ],
32
+ "scripts": {
33
+ "test": "npm run lint",
34
+ "lint": "eslint ."
35
+ },
36
+ "author": "EYALIN",
37
+ "license": "MIT",
38
+ "engines": {
39
+ "cordovaDependencies": {
40
+ "1.0.0": {
41
+ "cordova": ">100"
42
+ }
43
+ }
44
+ },
45
+ "devDependencies": {
46
+ "@cordova/eslint-config": "^3.0.0"
47
+ }
48
+ }
package/plugin.xml ADDED
@@ -0,0 +1,54 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3
+ xmlns:android="http://schemas.android.com/apk/res/android"
4
+ id="community-cordova-plugin-magnetometer"
5
+ version="1.0.1">
6
+
7
+ <name>Magnetometer</name>
8
+ <description>Cordova plugin to access the device magnetometer (compass) sensor</description>
9
+ <license>MIT</license>
10
+ <keywords>cordova,magnetometer,compass,sensor,magnetic,heading</keywords>
11
+
12
+ <js-module src="www/magnetometer.js" name="magnetometer">
13
+ <clobbers target="MagnetometerPlugin" />
14
+ </js-module>
15
+
16
+ <!-- Android -->
17
+ <platform name="android">
18
+ <config-file target="res/xml/config.xml" parent="/*">
19
+ <feature name="Magnetometer">
20
+ <param name="android-package" value="com.community.cordova.magnetometer.Magnetometer" />
21
+ </feature>
22
+ </config-file>
23
+
24
+ <source-file src="src/android/Magnetometer.java" target-dir="src/com/community/cordova/magnetometer" />
25
+ </platform>
26
+
27
+ <!-- iOS -->
28
+ <platform name="ios">
29
+ <config-file target="config.xml" parent="/*">
30
+ <feature name="Magnetometer">
31
+ <param name="ios-package" value="CDVMagnetometer" />
32
+ </feature>
33
+ </config-file>
34
+
35
+ <header-file src="src/ios/CDVMagnetometer.h" />
36
+ <source-file src="src/ios/CDVMagnetometer.m" />
37
+
38
+ <framework src="CoreMotion.framework" />
39
+ <framework src="CoreLocation.framework" />
40
+ </platform>
41
+
42
+ <!-- Browser -->
43
+ <platform name="browser">
44
+ <config-file target="config.xml" parent="/*">
45
+ <feature name="Magnetometer">
46
+ <param name="browser-package" value="Magnetometer" />
47
+ </feature>
48
+ </config-file>
49
+
50
+ <js-module src="src/browser/MagnetometerProxy.js" name="MagnetometerProxy">
51
+ <runs />
52
+ </js-module>
53
+ </platform>
54
+ </plugin>