# Proximity Sensor

## Editor’s Draft, 1 January 1970

This version:
  https://w3c.github.io/proximity/
Latest published version:
  https://www.w3.org/TR/proximity/
Previous Versions:
  https://www.w3.org/TR/2016/WD-proximity-20160719/
Version History:
  https://github.com/w3c/proximity/commits/gh-pages/index.bs
Feedback:
  public-device-apis@w3.org with subject line “`[proximity] … message topic …`” (archives)
Editors:
  Anssi Kostiainen (Intel Corporation)
  Rijubrata Bhaumik (Intel Corporation)
Former Editor:
  Dzung D Tran (Intel Corporation)
Bug Reports:
  via the w3c/proximity repository on GitHub
Issue Tracking:
  Issues
Test Suite:
  web-platform-tests on GitHub

Copyright © 1970 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.

## Abstract

This specification defines a concrete sensor interface to monitor the presence of nearby physical objects without physical contact.

## Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

If you wish to make comments regarding this document, please send them to public-device-apis@w3.org (subscribe, archives). When sending e-mail, please put the text “proximity” in the subject, preferably like this: “[proximity] …summary of comment…”. All comments are welcome.

This document was produced by the Devices and Sensors Working Group.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2019 W3C Process Document.

## 1. Introduction

The Proximity Sensor extends the Generic Sensor API [GENERIC-SENSOR] to provide information about proximity level, as detected by the device’s primary proximity sensor. The proximity level is reported as the distance (in centimeter) from the sensor to the closest visible surface. § 8 Limitations of Proximity Sensors describes more about the potential limitations, why the precise distance value reported by different devices can be different, due to differences in detection method, sensor construction, etc. Moreover some proximity sensors might be only able to provide just a boolean to indicate if there is a physical object which is near, more like presence detection, than an absolute value for the distance.

## 2. Examples

    let sensor = new ProximitySensor();
    sensor.start();

    sensor.onreading = () => console.log(sensor.distance);

    sensor.onerror = event => console.log(event.error.name, event.error.message);

## 3. Security and Privacy Considerations

Proximity sensor provides information about closeness of the sensor to nearby objects, thus, may expose privacy sensitive information for user identifying and/or fingerprinting attacks.

To mitigate these, user agents should use one or both of the following mitigation strategies:

- reduce accuracy of a sensor reading

- limit maximum sampling frequency

These mitigation strategies complement the generic mitigations defined in the Generic Sensor API [GENERIC-SENSOR].

## 4. Model

The Proximity Sensor sensor type’s associated `Sensor` subclass is the `ProximitySensor` class.

The Proximity Sensor has a default sensor, which is the device’s main proximity detector.

A latest reading for a `Sensor` of Proximity Sensor sensor type includes three entries whose keys are "distance", "max", "near" and whose values contain distance, max and near values.

The distance is a value that represents the distance between a device and the closest visible surface of the physical object within the sensing range. Its unit is the centimeter (cm).

The max is a value that represents the maximum sensing range of the main proximity detector, in centimeters.

The near is a value that represent the presence of a visible surface in the vicinity of the main proximity detector.

## 5. API

### 5.1. The ProximitySensor Interface

    [SecureContext, Exposed=Window]
    interface ProximitySensor : Sensor {
      constructor(optional SensorOptions sensorOptions = {});
      readonly attribute double? distance;
      readonly attribute double? max;
      readonly attribute boolean? near;
    };

To construct a `ProximitySensor` object the user agent must invoke the construct a proximity sensor object abstract operation.

#### 5.1.1. The distance attribute

The `distance` attribute of the `ProximitySensor` interface returns the result of invoking get value from latest reading with this and "distance" as arguments.

If the physical object is outside the sensing range, the attribute must return null.

#### 5.1.2. The max attribute

The `max` attribute of the `ProximitySensor` interface returns the result of invoking get value from latest reading with this and "max" as arguments.

#### 5.1.3. The near attribute

The `near` attribute of the `ProximitySensor` interface returns the result of invoking get value from latest reading with this and "near" as arguments.

If the physical object is outside the sensing range, the attribute must return null.

Note: If the implementation is unable to provide the near value, it could infer the near value from the value of distance. For example, if distance is not equal to max, it could imply there is a physical object in the sensing range.

## 6. Abstract Operations

### 6.1. Construct a proximity sensor object

input

  options, a `SensorOptions` object.

output

  A `ProximitySensor` object.

1. Let allowed be the result of invoking check sensor policy-controlled features with `ProximitySensor`.

2. If allowed is false, then:

   1. Throw a `SecurityError` `DOMException`.

3. Let proximity_sensor be the new `ProximitySensor` object.

4. Invoke initialize a sensor object with proximity_sensor and options.

5. Return proximity_sensor.

## 7. Automation

This section extends the automation section defined in the Generic Sensor API [GENERIC-SENSOR] to provide mocking information about the proximity level for the purposes of testing a user agent’s implementation of `ProximitySensor` API.

### 7.1. Mock Sensor Type

The `ProximitySensor` class has an associated mock sensor type which is "proximity", its mock sensor reading values dictionary is defined as follows:

    dictionary ProximityReadingValues {
      required double? distance;
      required double? max;
      required boolean? near;
    };

## 8. Limitations of Proximity Sensors

Since most proximity sensors detect electromagnetic radiation (e.g., an infrared light or a magnetic field), certain material properties can interfere with the sensor’s ability to sense the presence of a physical object. Things that can interfere with a sensor include, but are not limited to, the material’s translucency, reflectiveness, color, temperature, chemical composition, and even the angle at which the physical object is reflecting the radiation back at the sensor. As such, proximity sensors should not be relied on as a means to measure distance. The only thing that can be deduced from a proximity sensor is that a physical object is somewhere in the distance between the minimum sensing distance and the maximum sensing distance with some degree of certainty.

## 9. Acknowledgements

Tobie Langel for the work on Generic Sensor API and inputs on this specification. Doug Turner for the initial prototype and Marcos Caceres for the test suite.

## 10. Conformance

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

A conformant user agent must implement all the requirements listed in this specification that are applicable to user agents.

The IDL fragments in this specification must be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [WEBIDL]

## Index

### Terms defined by this specification

- conformant user agent, in §10
- Construct a proximity sensor object, in §6
- constructor(), in §5.1
- constructor(sensorOptions), in §5.1
- distance
  - attribute for ProximitySensor, in §5.1
  - definition of, in §4
  - dict-member for ProximityReadingValues, in §7.1
- max
  - attribute for ProximitySensor, in §5.1
  - definition of, in §4
  - dict-member for ProximityReadingValues, in §7.1
- near
  - attribute for ProximitySensor, in §5.1
  - definition of, in §4
  - dict-member for ProximityReadingValues, in §7.1
- ProximityReadingValues, in §7.1
- Proximity Sensor, in §4
- ProximitySensor, in §5.1
- ProximitySensor(), in §5.1
- ProximitySensor(sensorOptions), in §5.1
- sensing range, in §4

https://w3c.github.io/sensors/#dom-mocksensortype-proximity Referenced in:

- 7.1. Mock Sensor Type

https://w3c.github.io/sensors/#sensor Referenced in:

- 4. Model (2)
- 5.1. The ProximitySensor Interface

https://w3c.github.io/sensors/#dictdef-sensoroptions Referenced in:

- 5.1. The ProximitySensor Interface
- 6.1. Construct a proximity sensor object

https://w3c.github.io/sensors#automation Referenced in:

- 7. Automation

https://w3c.github.io/sensors#check-sensor-policy-controlled-features Referenced in:

- 6.1. Construct a proximity sensor object

https://w3c.github.io/sensors#default-sensor Referenced in:

- 4. Model

https://w3c.github.io/sensors#device-fingerprinting Referenced in:

- 3. Security and Privacy Considerations

https://w3c.github.io/sensors/#get-value-from-latest-reading Referenced in:

- 5.1.1. The distance attribute
- 5.1.2. The max attribute
- 5.1.3. The near attribute

https://w3c.github.io/sensors#initialize-a-sensor-object Referenced in:

- 6.1. Construct a proximity sensor object

https://w3c.github.io/sensors#latest-reading Referenced in:

- 4. Model

https://w3c.github.io/sensors/#limit-max-frequency Referenced in:

- 3. Security and Privacy Considerations

https://w3c.github.io/sensors#mitigation-strategies Referenced in:

- 3. Security and Privacy Considerations (2)

https://w3c.github.io/sensors#mock-sensor-reading-values Referenced in:

- 7.1. Mock Sensor Type

https://w3c.github.io/sensors#mock-sensor-type Referenced in:

- 7.1. Mock Sensor Type

https://w3c.github.io/sensors/#reduce-accuracy Referenced in:

- 3. Security and Privacy Considerations

https://w3c.github.io/sensors#sensor-readings Referenced in:

- 3. Security and Privacy Considerations

https://w3c.github.io/sensors#sensor-type Referenced in:

- 4. Model (2)

https://w3c.github.io/sensors#user-identifying Referenced in:

- 3. Security and Privacy Considerations

https://infra.spec.whatwg.org/#map-entry Referenced in:

- 4. Model

https://infra.spec.whatwg.org/#map-key Referenced in:

- 4. Model

https://infra.spec.whatwg.org/#map-value Referenced in:

- 4. Model

https://heycam.github.io/webidl/#idl-DOMException Referenced in:

- 6.1. Construct a proximity sensor object

https://heycam.github.io/webidl/#Exposed Referenced in:

- 5.1. The ProximitySensor Interface

https://heycam.github.io/webidl/#SecureContext Referenced in:

- 5.1. The ProximitySensor Interface

https://heycam.github.io/webidl/#securityerror Referenced in:

- 6.1. Construct a proximity sensor object

https://heycam.github.io/webidl/#idl-boolean Referenced in:

- 5.1. The ProximitySensor Interface
- 7.1. Mock Sensor Type

https://heycam.github.io/webidl/#idl-double Referenced in:

- 5.1. The ProximitySensor Interface (2)
- 7.1. Mock Sensor Type (2)

https://heycam.github.io/webidl/#dfn-throw Referenced in:

- 6.1. Construct a proximity sensor object

### Terms defined by reference

- [GENERIC-SENSOR] defines the following terms:
  - "proximity"
  - Sensor
  - SensorOptions
  - automation
  - check sensor policy-controlled features
  - default sensor
  - fingerprinting
  - get value from latest reading
  - initialize a sensor object
  - latest reading
  - limit maximum sampling frequency
  - mitigation strategies
  - mock sensor reading values
  - mock sensor type
  - reduce accuracy
  - sensor readings
  - sensor type
  - user identifying
- [INFRA] defines the following terms:
  - entry
  - key
  - value
- [WEBIDL] defines the following terms:
  - DOMException
  - Exposed
  - SecureContext
  - SecurityError
  - boolean
  - double
  - throw

## References

### Normative References

[GENERIC-SENSOR]
  Mikhail Pozdnyakov; Alexander Shalamov; Tobie Langel. Generic Sensor API. URL: https://w3c.github.io/sensors/
[INFRA]
  Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[RFC2119]
  S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[WEBIDL]
  Boris Zbarsky. Web IDL. URL: https://heycam.github.io/webidl/

## IDL Index

    [SecureContext, Exposed=Window]
    interface ProximitySensor : Sensor {
      constructor(optional SensorOptions sensorOptions = {});
      readonly attribute double? distance;
      readonly attribute double? max;
      readonly attribute boolean? near;
    };

    dictionary ProximityReadingValues {
      required double? distance;
      required double? max;
      required boolean? near;
    };

#proximity-sensor Referenced in:

- 4. Model (2)

#distance Referenced in:

- 4. Model
- 5.1.3. The near attribute (2)

#sensing-range Referenced in:

- 4. Model
- 5.1.1. The distance attribute
- 5.1.3. The near attribute (2)

#max Referenced in:

- 4. Model
- 5.1.3. The near attribute

#near Referenced in:

- 4. Model
- 5.1.3. The near attribute (2)

#proximitysensor Referenced in:

- 4. Model
- 5.1. The ProximitySensor Interface
- 5.1.1. The distance attribute
- 5.1.2. The max attribute
- 5.1.3. The near attribute
- 6.1. Construct a proximity sensor object (2) (3)
- 7. Automation
- 7.1. Mock Sensor Type

#dom-proximitysensor-distance Referenced in:

- 5.1.1. The distance attribute

#dom-proximitysensor-max Referenced in:

- 5.1.2. The max attribute

#dom-proximitysensor-near Referenced in:

- 5.1.3. The near attribute

#construct-a-proximity-sensor-object Referenced in:

- 5.1. The ProximitySensor Interface