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,150 @@
1
+ /**
2
+ * Magnetometer reading data with raw magnetic field values
3
+ */
4
+ export interface IMagnetometerReading {
5
+ /** Magnetic field strength along X axis in microteslas (μT) */
6
+ x: number;
7
+ /** Magnetic field strength along Y axis in microteslas (μT) */
8
+ y: number;
9
+ /** Magnetic field strength along Z axis in microteslas (μT) */
10
+ z: number;
11
+ /** Total magnetic field magnitude in microteslas (μT) */
12
+ magnitude: number;
13
+ /** Timestamp of the reading in milliseconds */
14
+ timestamp: number;
15
+ }
16
+
17
+ /**
18
+ * Compass heading data
19
+ */
20
+ export interface IHeadingData {
21
+ /** Magnetic heading in degrees (0-359.99), relative to magnetic north */
22
+ magneticHeading: number;
23
+ /** True heading in degrees (0-359.99), relative to geographic north (requires location services) */
24
+ trueHeading: number;
25
+ /** Heading accuracy in degrees (iOS only, -1 if unavailable) */
26
+ headingAccuracy: number;
27
+ /** Timestamp of the reading in milliseconds */
28
+ timestamp: number;
29
+ }
30
+
31
+ /**
32
+ * Complete magnetometer information
33
+ */
34
+ export interface IMagnetometerInfo {
35
+ /** Whether magnetometer sensor is available */
36
+ isAvailable: boolean;
37
+ /** Current magnetometer reading */
38
+ reading: IMagnetometerReading;
39
+ /** Current compass heading */
40
+ heading: IHeadingData;
41
+ /** Sensor accuracy level: 0=unreliable, 1=low, 2=medium, 3=high */
42
+ accuracy: number;
43
+ /** Whether calibration is needed */
44
+ calibrationNeeded: boolean;
45
+ /** Device platform (android/ios/browser) */
46
+ platform: string;
47
+ }
48
+
49
+ /**
50
+ * Watch options for continuous readings
51
+ */
52
+ export interface IWatchOptions {
53
+ /** Update frequency in milliseconds (default: 100) */
54
+ frequency?: number;
55
+ /** Minimum heading change in degrees to trigger update (heading watch only) */
56
+ filter?: number;
57
+ }
58
+
59
+ /**
60
+ * Magnetometer sensor accuracy levels
61
+ */
62
+ export enum MagnetometerAccuracy {
63
+ UNRELIABLE = 0,
64
+ LOW = 1,
65
+ MEDIUM = 2,
66
+ HIGH = 3
67
+ }
68
+
69
+ /**
70
+ * Magnetometer plugin manager
71
+ */
72
+ export default class MagnetometerManager {
73
+ /**
74
+ * Check if magnetometer sensor is available on the device
75
+ * @returns Promise resolving to 1 if available, 0 if not
76
+ */
77
+ isAvailable(): Promise<number>;
78
+
79
+ /**
80
+ * Get current magnetometer reading (single reading)
81
+ * @returns Promise resolving to magnetometer data with x, y, z values
82
+ */
83
+ getReading(): Promise<IMagnetometerReading>;
84
+
85
+ /**
86
+ * Get current compass heading
87
+ * @returns Promise resolving to heading data
88
+ */
89
+ getHeading(): Promise<IHeadingData>;
90
+
91
+ /**
92
+ * Start watching magnetometer readings continuously
93
+ * @param successCallback Called with magnetometer data on each update
94
+ * @param errorCallback Called on error
95
+ * @param options Optional settings including frequency
96
+ */
97
+ watchReadings(
98
+ successCallback: (data: IMagnetometerReading) => void,
99
+ errorCallback: (error: string) => void,
100
+ options?: IWatchOptions
101
+ ): void;
102
+
103
+ /**
104
+ * Stop watching magnetometer readings
105
+ * @returns Promise resolving when stopped
106
+ */
107
+ stopWatch(): Promise<void>;
108
+
109
+ /**
110
+ * Start watching compass heading continuously
111
+ * @param successCallback Called with heading data on each update
112
+ * @param errorCallback Called on error
113
+ * @param options Optional settings including frequency and filter
114
+ */
115
+ watchHeading(
116
+ successCallback: (data: IHeadingData) => void,
117
+ errorCallback: (error: string) => void,
118
+ options?: IWatchOptions
119
+ ): void;
120
+
121
+ /**
122
+ * Stop watching compass heading
123
+ * @returns Promise resolving when stopped
124
+ */
125
+ stopWatchHeading(): Promise<void>;
126
+
127
+ /**
128
+ * Get complete magnetometer information
129
+ * @returns Promise resolving to complete magnetometer info
130
+ */
131
+ getMagnetometerInfo(): Promise<IMagnetometerInfo>;
132
+
133
+ /**
134
+ * Get sensor accuracy level
135
+ * @returns Promise resolving to accuracy: 0=unreliable, 1=low, 2=medium, 3=high
136
+ */
137
+ getAccuracy(): Promise<number>;
138
+
139
+ /**
140
+ * Check if device needs calibration
141
+ * @returns Promise resolving to 1 if calibration needed, 0 if not
142
+ */
143
+ isCalibrationNeeded(): Promise<number>;
144
+
145
+ /**
146
+ * Get magnetic field strength (magnitude)
147
+ * @returns Promise resolving to field strength in microteslas
148
+ */
149
+ getFieldStrength(): Promise<number>;
150
+ }
@@ -0,0 +1,122 @@
1
+ var exec = require('cordova/exec');
2
+
3
+ var PLUGIN_NAME = 'Magnetometer';
4
+
5
+ var MagnetometerPlugin = {
6
+ /**
7
+ * Check if magnetometer sensor is available on the device
8
+ * @returns {Promise<number>} 1 if available, 0 if not
9
+ */
10
+ isAvailable: function() {
11
+ return new Promise(function(resolve, reject) {
12
+ exec(resolve, reject, PLUGIN_NAME, 'isAvailable', []);
13
+ });
14
+ },
15
+
16
+ /**
17
+ * Get current magnetometer reading (single reading)
18
+ * @returns {Promise<object>} Magnetometer data with x, y, z values in microteslas
19
+ */
20
+ getReading: function() {
21
+ return new Promise(function(resolve, reject) {
22
+ exec(resolve, reject, PLUGIN_NAME, 'getReading', []);
23
+ });
24
+ },
25
+
26
+ /**
27
+ * Get current compass heading
28
+ * @returns {Promise<object>} Heading data with magneticHeading, trueHeading, headingAccuracy, timestamp
29
+ */
30
+ getHeading: function() {
31
+ return new Promise(function(resolve, reject) {
32
+ exec(resolve, reject, PLUGIN_NAME, 'getHeading', []);
33
+ });
34
+ },
35
+
36
+ /**
37
+ * Start watching magnetometer readings continuously
38
+ * @param {function} successCallback Called with magnetometer data on each update
39
+ * @param {function} errorCallback Called on error
40
+ * @param {object} options Optional settings { frequency: number (ms) }
41
+ * @returns {string} Watch ID to use for stopping
42
+ */
43
+ watchReadings: function(successCallback, errorCallback, options) {
44
+ var frequency = (options && options.frequency) ? options.frequency : 100;
45
+ exec(successCallback, errorCallback, PLUGIN_NAME, 'watchReadings', [frequency]);
46
+ },
47
+
48
+ /**
49
+ * Stop watching magnetometer readings
50
+ * @returns {Promise<void>}
51
+ */
52
+ stopWatch: function() {
53
+ return new Promise(function(resolve, reject) {
54
+ exec(resolve, reject, PLUGIN_NAME, 'stopWatch', []);
55
+ });
56
+ },
57
+
58
+ /**
59
+ * Start watching compass heading continuously
60
+ * @param {function} successCallback Called with heading data on each update
61
+ * @param {function} errorCallback Called on error
62
+ * @param {object} options Optional settings { frequency: number (ms), filter: number (degrees) }
63
+ * @returns {string} Watch ID to use for stopping
64
+ */
65
+ watchHeading: function(successCallback, errorCallback, options) {
66
+ var frequency = (options && options.frequency) ? options.frequency : 100;
67
+ var filter = (options && options.filter) ? options.filter : 0;
68
+ exec(successCallback, errorCallback, PLUGIN_NAME, 'watchHeading', [frequency, filter]);
69
+ },
70
+
71
+ /**
72
+ * Stop watching compass heading
73
+ * @returns {Promise<void>}
74
+ */
75
+ stopWatchHeading: function() {
76
+ return new Promise(function(resolve, reject) {
77
+ exec(resolve, reject, PLUGIN_NAME, 'stopWatchHeading', []);
78
+ });
79
+ },
80
+
81
+ /**
82
+ * Get complete magnetometer information
83
+ * @returns {Promise<object>} Complete magnetometer info including availability, reading, heading, accuracy
84
+ */
85
+ getMagnetometerInfo: function() {
86
+ return new Promise(function(resolve, reject) {
87
+ exec(resolve, reject, PLUGIN_NAME, 'getMagnetometerInfo', []);
88
+ });
89
+ },
90
+
91
+ /**
92
+ * Get sensor accuracy level
93
+ * @returns {Promise<number>} Accuracy: 0=unreliable, 1=low, 2=medium, 3=high
94
+ */
95
+ getAccuracy: function() {
96
+ return new Promise(function(resolve, reject) {
97
+ exec(resolve, reject, PLUGIN_NAME, 'getAccuracy', []);
98
+ });
99
+ },
100
+
101
+ /**
102
+ * Check if device needs calibration
103
+ * @returns {Promise<number>} 1 if calibration needed, 0 if not
104
+ */
105
+ isCalibrationNeeded: function() {
106
+ return new Promise(function(resolve, reject) {
107
+ exec(resolve, reject, PLUGIN_NAME, 'isCalibrationNeeded', []);
108
+ });
109
+ },
110
+
111
+ /**
112
+ * Get magnetic field strength (magnitude)
113
+ * @returns {Promise<number>} Field strength in microteslas
114
+ */
115
+ getFieldStrength: function() {
116
+ return new Promise(function(resolve, reject) {
117
+ exec(resolve, reject, PLUGIN_NAME, 'getFieldStrength', []);
118
+ });
119
+ }
120
+ };
121
+
122
+ module.exports = MagnetometerPlugin;