Magnetometer
Reads the magnetic field around the device, like a compass.
In one line
Can sense the magnetic field around your device, like a compass
Reads the magnetic field around the device, like a compass.
Gives compass heading and can reveal nearby metal and electronics. Indoor magnetic fields form a rough fingerprint of a building or room.
What it can and can't do
What the user does: Nothing in most browsers. Chrome allows it by default unless you changed Motion sensors in site settings
It can
- Find which way your device points, like a compass
- Spot nearby metal and electronics
- Guess which room or building you are in from its magnetic pattern, roughly
It cannot
- Run in Firefox or Safari, which do not support it
- Show a prompt in Chrome; there is none by default
- Measure the field while the page is closed
How it differs between platforms: Chrome, Edge and Samsung Internet support it. Safari and Firefox do not.
Browser support
| Browser | Status | First version | Released | Note |
|---|---|---|---|---|
| Chrome | Supported | 67 | 2018-05-29 | None |
| Edge | Supported | 79 | 2020-01-15 | None |
| Firefox | Not supported | Not available | Not listed | None |
| Safari | Not supported | Not available | Not listed | None |
| Safari on iPhone | Not supported | Not available | Not listed | None |
| Chrome on Android | Supported | 67 | 2018-05-31 | None |
| Samsung Internet | Supported | 9.0 | 2018-09-15 | BCD lists no support; agent research says otherwise |
How a site asks for it
| API | new Magnetometer({frequency}) |
| Permission name | magnetometer |
| Permissions policy | magnetometer |
| Browser compat keys | api.Magnetometer |
| How access is granted | prompt |
| What the user sees | Chromium shows no bubble: access follows the Motion sensors site setting (default Allow) plus the Permissions API name. Safari and Firefox do not ship the API. |
| Needs a secure context (HTTPS) | Yes |
| Needs a user gesture | No |
| How long it lasts | Site setting Allow or Block per origin |
| How to take it back | chrome://settings/content/sensors; Permissions API 'change' event |
| Inside an iframe | Cross-origin iframes blocked unless allow="magnetometer"; page must be focused and visible |
Typical flow
- Check isSecureContext and Permissions Policy
- Construct the sensor with {frequency: 60}
- sensor.start()
- 'reading' events fire; 'error' event on NotAllowedError or NotReadableError
Data you get
What comes back: Sensor readings at the requested frequency
Shape: event
How often: up to 60 Hz (Chrome caps frequency)
| Field | Type | Unit | Range | Precision |
|---|---|---|---|---|
x, y, z | float | microtesla | about -4900 to 4900 hardware; Earth field 25 to 65 | about 0.1 |
frequency | float | Hz | 1 to 10 effective | 1 Hz |
Technical limits
- Chrome limits sampling for privacy
- Not in Firefox or Safari
The story
What it is
The page reads the magnetic field around your device, like a compass.
For you
A compass or a map turns to match where you face. You may not see a prompt. Nearby metal and electronics change what the sensor reads.
For makers
Makers use it for compass features and AR. Indoor magnetic patterns can help place a person in a building, and they are also a rough fingerprint of where a device has been.
Where you see it
| Type | Example | Apps |
|---|---|---|
| maps | A web map or hiking page turns to the direction you face. | Google Maps, AllTrails |
| ar-vr | A web AR scene lines up virtual objects with north. | 8th Wall |
| games | A treasure hunt or location web game points you toward the next clue. | Geocaching.com |
| travel | A museum or airport web app uses magnetic maps to guide people indoors. | None named |
| security | A fraud tool may check whether a phone reads believable magnetic data, since an emulator does not. | None named |
| analytics | A tracker uses the readings to build a rough fingerprint of the device and its surroundings. | None named |
Risks
- Hints at surroundings
- Fingerprint clue
What the platform says
- Google Chrome: Generic Sensor API. Chrome describes a common way for web pages to read motion and environment sensors such as accelerometer and gyroscope.
Good uses and abuse
Legitimate uses
- Compass in web maps
- AR heading
Known abuse
- Indoor localisation by magnetic fingerprint
Scores
| Score | Value | Meaning |
|---|---|---|
| Sensitivity | 3 of 5 | How revealing the data is |
| Utility | 3 of 5 | How useful it is to honest apps |
| Friction | 1 of 5 | How much the user has to do |
| Fingerprint bits | 3 | Rough identifying entropy, when known |
History
- 2018-05, Chrome 67: Magnetometer ships with the Generic Sensor API
Related capabilities
Related
- AbsoluteOrientationSensor and RelativeOrientationSensor. Can find which way your device is facing and tilted in 3D
- DeviceOrientation and DeviceMotion events. Can sense how your phone is tilted and shaken, sometimes only after you allow it
Sources
How well checked: Based on official documentation.