Search code examples
nativescriptnativescript-plugin

Reading device's Proximity sensor with NativeScript


I'm developing a cross-platform mobile app, and I need to read the proximity sensor of the device, that provides information about the distance of a nearby physical object using the proximity sensor of a device.

Has anyone implemented this/wrote a plugin for this purpose in Nativescript?


Solution

  • I found a partial answer to my question, on how to read proximity sensor in Android using NativeScript. I will update my answer once I write the code for iOS as well.

    To get access to a sensor in Android, first we have to import 'application' and 'platform' modules that NS provides:

    import * as application from "tns-core-modules/application";
    import * as platform from 'tns-core-modules/platform';
    
    declare var android: any;
    

    Then, acquire android's Sensor Manager, proximity sensor and create an android event listener and register it to listen to changes in the proximity sensor.

    To register proximity sensor:

    registerProximityListener() {
    
            // Get android context and Sensor Manager object
            const activity: android.app.Activity = application.android.startActivity || application.android.foregroundActivity;
            this.SensorManager = activity.getSystemService(android.content.Context.SENSOR_SERVICE) as android.hardware.SensorManager;
    
            // Creating the listener and setting up what happens on change
            this.proximitySensorListener = new android.hardware.SensorEventListener({
                onAccuracyChanged: (sensor, accuracy) => {
                    console.log('Sensor ' + sensor + ' accuracy has changed to ' + accuracy);
                },
                onSensorChanged: (event) => {
                    console.log('Sensor value changed to: ' + event.values[0]);
                }
            });
    
            // Get the proximity sensor
            this.proximitySensor = this.SensorManager.getDefaultSensor(
                android.hardware.Sensor.TYPE_PROXIMITY
            );
    
            // Register the listener to the sensor
            const success = this.SensorManager.registerListener(
                this.proximitySensorListener,
                this.proximitySensor,
                android.hardware.SensorManager. SENSOR_DELAY_NORMAL
            );
    
            console.log('Registering listener succeeded: ' + success);
        }
    

    To unregister the event listener, use:

    unRegisterProximityListener() {
            console.log('Prox listener: ' + this.proximitySensorListener);
             let res = this.SensorManager.unregisterListener( this.proximitySensorListener);
             this.proximitySensorListener = undefined;
             console.log('unRegistering listener: ' + res);
        };
    

    Of course, we can change android.hardware.Sensor.TYPE_PROXIMITY to any other sensor that Android OS provides us. More about sensors in Android Sensor Overview. I didn't check this with other sensors, so the implementation might be a bit diffrent, but I believe the concept is still the same

    This solution is based on Brad Martin's solution found here.

    To make this answer complete, please post your solution for iOS if you have it.