How to use proximity sensor to manage screen light :Android - 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.

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 test Proximity sensor on emulator ?

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

Android,sensor orientation doesn't change

I have problem with my app and i have no idea where is the cause.
I am developing an application, which connect with PC by Wi-Fi, and shows on phone screen orientation. All about sending works perfectly, but part with orientation sensor not. The strange fact is when i do app only with sensor part it works. Maybe threads something changes.
in on create
txtView = (TextView) findViewById(R.id.txtView);
txtView2 = (TextView) findViewById(R.id.txtView2);
txtView3 = (TextView) findViewById(R.id.txtView3);
SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
in on sensor change
#Override
public void onSensorChanged(SensorEvent event) {
azimuth_angle = event.values[0];
pitch_angle = event.values[1];
roll_angle = event.values[2];
txtView.setText(azimuth_angle+" ");
txtView2.setText(pitch_angle+" ");
txtView3.setText(roll_angle+" ");
Log.e("kacper", azimuth_angle+" "+pitch_angle+" "+roll_angle);
i checked that onSensorChanged is no called. why its not working when in new empty activity works good and refresh very often.
have you register the listener?
mSensormanager.registerListener(this, mOrientation ,
SensorManager.SENSOR_DELAY_FASTEST);

the accelerometer which detect the movement of the device that want to change the ringer mode of the phone to Ringer_Mode_silent

*i have an application trying to use the accelerometer which detect the movement of the device that want to change the ringer mode of the phone to Ringer_Mode_silent * i realized the service which detected incoming call but i can't inderstand how to change the mode of phone in background when i move the device,please can i help me to realized this application.
public class MainActivity extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mAccelerometre;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometre = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
Button b1=(Button)findViewById(R.id.start);
Button b2=(Button)findViewById(R.id.stop);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent=new Intent(Main.this,ServiceReceiver.class);
Intent serv = new Intent(MainActivity.this, ServiceBroadcast.class);
startService(serv);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent=new Intent(Main.this,ServiceReceiver.class);
Intent serv = new Intent(MainActivity.this, ServiceBroadcast.class);
stopService(serv);
}
public void onSensorChanged()(SensorEvent event) {
float azimuth,pitch,roll;
if(mSensorManager==SensorManager.Sensor_Accelerometer)
{
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}
}
Simple movement detection is easy with accelerometer ( pattern detecting is difficult )just take sqrt(x^2+y^2+z^2) -9.81) which give you movement,where x,y,z are accelerometer readings.Compare with some threshold and change your profile mode when it crosses.
Note:
as you mentioned in your code accelerometer dont give azimuthla,roll,pitch.
values[0]: Acceleration minus Gx on the x-axis
values[1]: Acceleration minus Gy on the y-axis
values[2]: Acceleration minus Gz on the z-axis

Categories

Resources