0

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?

Lorraine R.
  • 1,545
  • 1
  • 14
  • 39
  • Unfortunately there is no plugin that implements proximity apis yet, you might want to write one. – Manoj Mar 18 '19 at 21:41
  • Thank you. Yes, I was thinking about writing a plugin for this purpose. But I'm new to NativeScript and would to see an example of how to acquire native APIs of iOS and Android using NativeScript. Can anyone help with such an example? – Lorraine R. Mar 19 '19 at 12:40
  • I got to a partial solution for this - Android only (below). I will write a plugin for this when I finish the code for iOS as well. Thanks for the help. – Lorraine R. Mar 21 '19 at 12:46

1 Answers1

0

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.

Lorraine R.
  • 1,545
  • 1
  • 14
  • 39