Multiple sensors on an android application

前端 未结 1 877
失恋的感觉
失恋的感觉 2021-01-15 03:34

I am currently developing an android application that uses multiple sensors, I have used mSensor= mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

相关标签:
1条回答
  • 2021-01-15 03:48

    You will need to check if the sensor values are of that type of sensor with the event.sensor.getType() method. So if you wanted to access both the Magnetometer and Accelerometer:

    sensorManager = (SensorManager)   
    getActivity().getSystemService(Context.SENSOR_SERVICE);
    sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorMagnetic = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                magnetic = event.values;
                tv.setText("X: "+ magnetic.values[0] + ...);
            }
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                gravity = event.values;
                tv2.setText(X: " + gravity.values[0] + ...);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题