Sensor Orientation and accelerometer not working on HTC MAGI

Put your problem here if it does not fit any of the other categories.

Sensor Orientation and accelerometer not working on HTC MAGI

Postby psdeepti » Mon Oct 19, 2009 3:34 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.ShakeAndroid;
  2.  
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.hardware.Sensor;
  7. import android.hardware.SensorEvent;
  8. import android.hardware.SensorEventListener;
  9. import android.hardware.SensorManager;
  10. import android.os.Bundle;
  11. import android.widget.TextView;
  12.  
  13.  
  14. public class ShakeAndroid extends Activity {
  15.      
  16.      private TextView accText;
  17.      private SensorManager myManager;
  18.      private List<Sensor> sensors;
  19.      private Sensor accSensor;
  20.      private float oldX, oldY, oldZ = 0f;
  21.      
  22.     /** Called when the activity is first created. */
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);        
  26.              
  27.         setContentView(R.layout.main);
  28.        
  29.         accText = (TextView)findViewById(R.id.accText);  
  30.        
  31.         // Set Sensor + Manager
  32.         myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  33.         sensors = myManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);  
  34.  
  35.         if(sensors.size() > 0)
  36.         {
  37.           accSensor = sensors.get(0);
  38.         }
  39.     }
  40.  
  41.     private void updateTV(float x, float y, float z)
  42.     {
  43.      float thisX = x - oldX * 10;
  44.      float thisY = y - oldY * 10;
  45.      float thisZ = z - oldZ * 10;
  46.      
  47.    
  48.      accText.setText("x: " + Math.round(thisX) + ";n y:" + Math.round(thisY) + ";n z: " + Math.round(thisZ));
  49.      
  50.      oldX = x;
  51.      oldY = y;
  52.      oldZ = z;
  53.     }
  54.    
  55.     private final SensorEventListener mySensorListener = new SensorEventListener()
  56.     {
  57.      public void onSensorChanged(SensorEvent event)
  58.      {
  59.           updateTV(event.values[0],
  60.                     event.values[1],
  61.                     event.values[2]);
  62.      }
  63.      
  64.      public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  65.     };
  66.    
  67.     @Override
  68.     protected void onResume()
  69.     {
  70.      super.onResume();
  71.      myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);      
  72.     }
  73.    
  74.     @Override
  75.     protected void onStop()
  76.     {      
  77.      myManager.unregisterListener(mySensorListener);
  78.      super.onStop();
  79.     }
  80. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4




Android Manifest .xml


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.example.ShakeAndroid"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".ShakeAndroid"
  8.                  android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="3" />
  17.  
  18. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  19.  
  20. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4




The above code works fine but when I replace the constants with TYPE_ACCELEROMETER or TYPE_ORIENTATION ,code is not working.Screen is completely blank and the values are not displayed.

I am testing this code on HTC Magic.
Does anyone have same problem???

Please let me know if I am missing anything???

Thanks
Deepti


[edit by WarrenFaith]Formatted... hint for the next thread: Use the java/xml button and reduce your textsize and color
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Top

Postby padde » Mon Oct 19, 2009 4:00 pm

So you find out how to make the text big and red but you
missed the code tag?!?!
You got to be kidding.. right?
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby psdeepti » Mon Oct 19, 2009 4:13 pm

Sory I dont understand.Let me know the solution.
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Postby sudheer1437 » Mon Oct 19, 2009 4:39 pm

Hi deepthi..
try this code..
It works perfect for 1.5

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.accel;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.hardware.SensorListener;
  10.  
  11. import android.hardware.SensorManager;
  12.  
  13. import android.os.Bundle;
  14.  
  15. import android.util.Log;
  16.  
  17.  
  18.  
  19. public class AccelExample extends Activity implements SensorListener
  20.  
  21. {
  22.  
  23.         private SensorManager sManager;
  24.  
  25.         private SensorListener mySensorListener;
  26.  
  27.        
  28.  
  29.     @Override
  30.  
  31.     public void onCreate(Bundle savedInstanceState)
  32.  
  33.     {
  34.  
  35.         super.onCreate(savedInstanceState);
  36.  
  37.         setContentView(R.layout.main);
  38.  
  39.         sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  40.  
  41.         sManager.registerListener(this,SensorManager.SENSOR_ACCELEROMETER,0);
  42.  
  43.     }
  44.  
  45.    
  46.  
  47.     private long lastUpdate = 0;
  48.  
  49.         private float datax,datay,dataz;
  50.  
  51.         private float last_x = 0;
  52.  
  53.         private float last_y = 0;
  54.  
  55.         private float last_z = 0;
  56.  
  57.         private float SHAKE_THRESHOLD = 700;
  58.  
  59.  
  60.  
  61.         @Override
  62.  
  63.         public void onAccuracyChanged(int arg0, int arg1)
  64.  
  65.         {
  66.  
  67.                 // TODO Auto-generated method stub
  68.  
  69.                
  70.  
  71.         }
  72.  
  73.  
  74.  
  75.         @Override
  76.  
  77.         public void onSensorChanged(int sensor, float[] values)
  78.  
  79.         {
  80.  
  81.                 try
  82.  
  83.                 {
  84.  
  85.                 if (sensor == SensorManager.SENSOR_ACCELEROMETER)
  86.  
  87.                 {
  88.  
  89.                         long curTime = System.currentTimeMillis();
  90.  
  91.                         // only allow one update every 100ms.
  92.  
  93.  
  94.  
  95.                         if ((curTime - lastUpdate) > 100)
  96.  
  97.                         {
  98.  
  99.                                 long diffTime = (curTime - lastUpdate);
  100.  
  101.                                 lastUpdate = curTime;
  102.  
  103.  
  104.  
  105.                                 datax = values[SensorManager.DATA_X];
  106.  
  107.                                 datay = values[SensorManager.DATA_Y];
  108.  
  109.                                 dataz = values[SensorManager.DATA_Z];
  110.  
  111.  
  112.  
  113.                                 float speed = Math.abs(datax+datay+dataz - last_x - last_y - last_z) / diffTime * 10000;
  114.  
  115.                                 if (speed > SHAKE_THRESHOLD)
  116.  
  117.                                 {
  118.  
  119.                                                 Log.i("Shake","Detected");
  120.  
  121.                                 }
  122.  
  123.                                 last_x = datax;
  124.  
  125.                                 last_y = datay;
  126.  
  127.                                 last_z = dataz;
  128.  
  129.                         }
  130.  
  131.                 }
  132.  
  133.                 }
  134.  
  135.                 catch(Exception e)
  136.  
  137.                 {
  138.  
  139.                        
  140.  
  141.                 }
  142.  
  143.        
  144.  
  145.         }
  146.  
  147.        
  148.  
  149.         @Override
  150.  
  151.         protected void onPause()
  152.  
  153.         {
  154.  
  155.                 super.onPause();
  156.  
  157.  
  158.  
  159.                 sManager.unregisterListener(mySensorListener);
  160.  
  161.                
  162.  
  163.         }
  164.  
  165.  
  166.  
  167.         @Override
  168.  
  169.         protected void onResume()
  170.  
  171.         {
  172.  
  173.                 super.onResume();
  174.  
  175.                 sManager.registerListener(this,SensorManager.SENSOR_ACCELEROMETER,0);
  176.  
  177.                
  178.  
  179.         }
  180.  
  181. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby psdeepti » Mon Oct 19, 2009 4:55 pm

Thanks sudheer but didnt work ,came up with warnings.
"The type SensorListener is deprecated"

Still I installed on the device ,doesnt do anything.Screen is blank.

Can anyone post the latest code which works on HTC Magic for Orientation and Accelerometer sensor types.

Thanks
Deepti
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Postby sudheer1437 » Mon Oct 19, 2009 4:59 pm

Hi deepthi..
I used it on HTC Magic and checked..
it works fine....
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Top

Postby psdeepti » Mon Oct 19, 2009 5:02 pm

Can you tell me if you had to enable anything in settings like GPS ,wifi etc.,???

or did I miss any permissions in manifest other than internet??

Thanks
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Postby sudheer1437 » Mon Oct 19, 2009 5:06 pm

its perfect...
u need not give any permissions for accelerometer...
my code is also not working for u now...????
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby psdeepti » Mon Oct 19, 2009 5:11 pm

It never worked for me.

I want to see atleast once working on the device :-)

Do you know HTC magic supports which types of sensors

like

ORIENTATION
TEMPERATURE
LIGHT etc.,
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Postby sudheer1437 » Mon Oct 19, 2009 5:16 pm

It supports 4 sensors...

3-Axis Accelerometer,
3-Axis Magnetic Field Sensor
Orientation Sensor
Temperature Sensor.
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby sudheer1437 » Mon Oct 19, 2009 5:17 pm

can u give me the log file details....
wad is it saying wen u r working on device...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby sudheer1437 » Mon Oct 19, 2009 5:18 pm

shall i give my apk file.. so that u can check on ur device...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby psdeepti » Tue Oct 20, 2009 9:47 am

Hi Sudheer,

That sounds great.Can you please send me apk file which works on HTC Magic.

I am a new starter and dont know how to create logs on the device.Let me know that also.

Thanks alot.
Deepti
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Postby sudheer1437 » Tue Oct 20, 2009 3:54 pm

send me a private message in this website to me, how shall i send u the file...

and u can observe the Log file in IDE not on the device...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby psdeepti » Wed Oct 21, 2009 11:01 am

Hi Sudheer,

I sent you a private message .Can you send me apk file as an attachment.

Thanks
Deepti
psdeepti
Freshman
Freshman
 
Posts: 9
Joined: Thu Oct 15, 2009 4:32 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 6 guests