How to test Proximity sensor on emulator ? - android

This is Code for getting proximity sensor.On sensor change i show a Toast message.
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event) {
Toast.makeText(this, "Sensor Changed", Toast.LENGTH_LONG).show();
if (event.values[0] == 0) {
Toast.makeText(this, "Screen off", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Screen on", Toast.LENGTH_LONG).show();
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
For the testing this app i connect to the emulator via telnet like this telnet localhost 5554. The i change the proximity like this sensor set proximity 5. but it doesn't work. What is the wrong thing i did ?

This is achievable with following steps:
Open AS, run the emulator
Click on Extended controls
Choose Virtual sensors
In upper tab choose Additional sensors
And there you have it, play with proximity as you want

No, You cannot use proximity sensors on the emulator but you can test them on the real device because they contains that kind of sensors you want to test like accelerometer, gyroscope , magnetometer. But yes you can try gps like application in emulator using DDMS. You can use Genymotion with limited functionality on free version but real devices are advantageous.

One solution will be use a third party virtualized android machine like Genymotion .
The application has neat controls to change location data and sensor data , it even has built in java libraries to implement test cases

Related

How to get device orientation whilst locking device in portrait

In my app I have set preferred orientations of portrait
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
])
But I would like to listen for changes in the orientation without changing what is displayed on the screen
I want to listen for orientation changes even though it is locked
I would like some code equivalent to this
MediaQuery.of(context).orientation
However, since I have locked it to portrait it will always say portrait
You can use SensorManager class for this:
SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;
#Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1]<6.5 && event.values[1]>-6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation=1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation=0;
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
There are also other ways you can check in this post: Get phone orientation but fix screen orientation to portrait
I suggest using this package native_device_orientation it provides accurate orientation and doesn't just look at the devices width.
// Gets the devices orientation
NativeDeviceOrientation orientation = await NativeDeviceOrientationCommunicator().orientation(useSensor:true);

Minix X68-i accelerometer

I have a Minix X68-i with Android 5.1 , I developed an application which uses the accelerometer and the gyroscope. This application works well in my mobile phone but in the Minix it doesn't work, I suppose is because the Minix doesn't have this sensors but I don't know what to do. In the Debug mode the Sensors aren't null but the application never enters in the onSensorChanged.
I attach the code where I register the sensor.
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(new SensorEventListener()
{
#Override
public void onSensorChanged(SensorEvent event)
{
setXYZ(event.values[0], event.values[1], event.values[2]);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
//nothing here
}
}, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

How to use proximity sensor to manage screen light :Android

In my project I need to use proximity sensor to manage screen light.
From last 3 days I'm trying to do the same thing. But still I din't got success.
My half code is working fine. Am able to off the screen light using proximity sensor. but screen light not getting on. When I'm covering the sensor with my hand, screen light is getting off. but light not getting on after removing my hand from sensor.
My code is:
#Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
{
switch (lastSensorPosition)
{
//case 1 will turn on screen light
case 1:
lastSensorPosition = 2;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getWindow().setAttributes(lp);
break;
//case 2 will turn off screen light
case 2:
lastSensorPosition = 1;
WindowManager.LayoutParams lp1 = getWindow().getAttributes();
lp1.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
getWindow().setAttributes(lp1);
break;
default:
break;
}
}
}
I'm expecting a great help from you guys...
private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
#Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
#Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
#Override
public final void onSensorChanged(SensorEvent event) {
float distance = event.values[0];
// Do something with this sensor data.
}
#Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}
}
Some proximity sensors return binary values that represent "near" or "far." In this case, the sensor usually reports its maximum range value in the far state and a lesser value in the near state. Typically, the far value is a value > 5 cm, but this can vary from sensor to sensor. You can determine a sensor's maximum range by using the getMaximumRange() method.

Android proximity sensor when uncoverd

I work in project where i use proximity sensor my program should mute the phone when the proximity sensor is covered and restore it to normal when the sensor is uncovered. i managed to code for when the sensor is covered but i don't know the value for far when the sensor is uncovered i need a code to unmute when the sensor is uncovered.
#Override
public void onSensorChanged(SensorEvent event) {
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
if(event.values[0]<5){ audio.setRingerMode(AudioManager.RINGER_MODE_youwant);}else{ audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);}
}
}
Here is the code for mute/unmute
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int current_volume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//If you want to player is mute ,then set_volume variable is zero.Otherwise you may supply some value.
int set_volume=0;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,set_volume, 0);

Problem working with sensor - accelerometer

I have a Galaxy Tab 1000.
I am trying to write a code, that will identify ANY movement.
I have version 2.2 on the phone.
This is the oncreate code.
OnCreate()
mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mySensorManager.registerListener(mySensorEventListener, mySensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL); //
This is the sensor onsensorchanged implementation (very simple I think) but not working
if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER) // Do WORK
The event.sensor.getType() returns 1
If I do: getSensorList(Sensor.TYPE_ALL);
I get sensors compass, compass, BMA150 (accel), temp, magnet, proximity, light and Gyro.
(weird - I thought it's only up in 3.0)
Please advise, how can I get motion detection on the phone.
thanks
I am a little confused of what exactly it is that you want to do, but from what I take you want to get acceleration data in your onSensorChanged. Your on create looks fine, however you can check if they are registering the listener correctly by checking the bool that returns from the registerListener. Heres what i do when listening for an event and retrieving the values returned.
#Override
public void onSensorChanged(SensorEvent event)
{
// If the sensor data is unreliable return
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
//Toast.makeText(main.this, "Sensor Status Unreliable",Toast.LENGTH_SHORT).show();
return;
}
// Gets the value of the sensor that has been changed
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
m_vAccel = event.values.clone();
break;
}
Other wise you can also use the Linear Acceleration sensor which is the same as the accelerometer returns minus
gravity. http://developer.android.com/reference/android/hardware/SensorEvent.html

Categories

Resources