Search code examples
javaandroidandroid-sensors

Android Application works fine on emulator but not on a real device


I run the program on an AVD and it works fine when i alter the virtual sensors but it fails to function on a real device.

The real device just shows 0.0 as the pressure.

I have also added the following permissions in the manifest file,

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Here is the main java class

public class MainActivity extends AppCompatActivity implements SensorEventListener{

public Sensor TemperatureSensor;
public SensorManager sensorManager;
public float pressurereading = 0;
public TextView Textfield;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sensorManager = (SensorManager)getSystemService(getApplicationContext().SENSOR_SERVICE);
    TemperatureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
    Textfield = findViewById(R.id.Textfield);
    Textfield.setText(String.valueOf(pressurereading));
}
@Override
public void onResume() {
    super.onResume();
    sensorManager.registerListener(this,TemperatureSensor,SensorManager
            .SENSOR_DELAY_NORMAL);
}
@Override
public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor,int Accuracy) {

}
@Override
public void onSensorChanged(SensorEvent e) {
    pressurereading = e.values[0];
    Textfield.setText(String.valueOf(pressurereading));
}
}

Here is the xml layout

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Device Temperature"
    android:background="@android:drawable/alert_dark_frame"
    android:textColorHint="@android:color/holo_green_light"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="30sp"
    android:textColor="@android:color/holo_green_light"
    android:id="@+id/Textfield"/>
    </RelativeLayout>

Here is the Logcat for a successful build on a real device.

06-20 23:13:42.009 8881-8881/? I/art: Late-enabling -Xcheck:jni
06-20 23:13:42.057 8881-8881/? D/TidaProvider: TidaProvider()
06-20 23:13:42.261 8881-8881/lenovo.learning W/System: ClassLoader referenced unknown path: /data/app/lenovo.learning-1/lib/arm64
06-20 23:13:42.275 8881-8881/lenovo.learning I/InstantRun: starting instant run server: is main process
06-20 23:13:42.345 8881-8881/lenovo.learning W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-20 23:13:42.361 8881-8881/lenovo.learning D/AccessibilityManager: current package=lenovo.learning, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
06-20 23:13:42.443 8881-8881/lenovo.learning E/SensorManager: sensor or listener is null
06-20 23:13:42.488 8881-8910/lenovo.learning I/Adreno: QUALCOMM build                   : 74bd0df, Ida1be052e0
Build Date                       : 11/27/17
OpenGL ES Shader Compiler Version: XE031.14.00.04
Local Branch                     : 
Remote Branch                    : 
Remote Branch                    : 
Reconstruct Branch               : 
06-20 23:13:42.491 8881-8910/lenovo.learning I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
06-20 23:13:42.493 8881-8910/lenovo.learning I/OpenGLRenderer: Initialized EGL, version 1.4
06-20 23:13:42.494 8881-8910/lenovo.learning D/OpenGLRenderer: Swap behavior 1
06-20 23:13:42.525 8881-8881/lenovo.learning W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

Solution

  • This may be caused by real device not having the proper sensor type. According to the documentation of SensorManager, getDefaultSensor(int) will return

    the default sensor matching the requested type if one exists and the application has the necessary permissions, or null otherwise.

    If you end up passing null to sensorManager.registerListener no events would be fired, therefore onSensorChanged will never be called.