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);
Related
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);
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
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.
*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
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