C Smith
C Smith

Reputation: 808

onSensorChanged() not calling for Gyroscope Sensor

I'll preface this with saying I am completely new to Android development. Started this week kind of thing. So far, I only know how to do forms with textboxes/buttons/that kind of thing. Graphical programming in Android is voodoo to me, still, so I'm trying to learn via tutorials and sample code. (which I tried asking for before posting this question but got utterly destroyed on downvotes, so I'll try this approach instead).

I'm trying to learn how to make a bubble level. I ran across the following project, complete with source code: http://www.intuitor.com/student/Android%20Phone%20Site/Jean1Android.php. Not really a tutorial, but it's simple enough to be a great place for me to start.

I attempted to run it first, to make sure that it would work, with minimal modifications. I did have to make a change to SensorProgram.java to remove a few TextViews that didn't seem to exist for some reason, and also because the project implemented a 3rd party sensor manager (for controlling the sensor via mouse, it seems) in SensorProgram.java, and I intended to use my actual phone. I replaced his sensor declaration with example declarations from http://developer.android.com/guide/topics/sensors/sensors_motion.html. I assumed everything else would be the same.

My problem is that, while the board draws ok, the bubble is nowhere to be found. I can rotate, flip, and flop my phone all over and there is no bubble to be seen anywhere. For the life of me, I can't figure out what my issue is. 0_o I assume that, besides the declaration, everything else would be the same between the simulated sensor and the real one... right? Or am I missing some kind implementation for the sensor?

Can anyone see what the issue is that might be causing the bubble to not be there? Am I missing something to get the sensor to work? Or is it so completely off that I would do better to scrap this and try a different example?

EDIT: After some looking, it appears that the onsensorChanged() event om SensorProgram.java is not firing at all. Without that, there is no Y value for the bubble. I'm trying to figure out why, now... I edited the title of the question to match actual issue

Below are the sources for my files. Besides SensorProgram.java, it should all match pretty well to the kid's source code from the above link

SensorProgram.java

package com.blah.level;


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

public class SensorProgram extends Activity implements SensorEventListener 
{
        private double highAccel, totalAccel;
        private SensorManager mSensorManager;
        private Sensor mSensor;
        private static Context CONTEXT;
        private Board b;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b=new Board(this);
        setContentView(b);
        CONTEXT = this;
        highAccel = 0;
        totalAccel = 0;
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public static Context getContext()
    {
        return CONTEXT;
    }

    @Override  
    public void onAccuracyChanged(Sensor arg0, int arg1)  
    {  
        //Do nothing.  
    }   

    @Override  
        public void onSensorChanged(SensorEvent event) {
                b.getBubble().setY(215+(int)(event.values[1]*1.194));
                Log.d("SensorChangeEvent", "SENSORCHANGED: " + Float.toString(event.values[1]));

                b.invalidate();
        }

}

Bubble.java

package com.blah.level;


import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

public class Bubble {
        private int x, y, radius;

        public Bubble(int xP, int yP, int r)
        {
                x = xP;
                y = yP;
                radius = r;
        }

        public void draw(Canvas c, Paint g)
        {
                g.setColor(Color.GREEN);
                c.drawCircle(x, y, radius, g);
        }

        public void setY(int newY)
        {
                y = newY;
                Log.d("BubbleY", "BubbleY: " + Integer.toString(y));
        }

        public void setX(int newX)
        {
                x = newX;
                Log.d("BubbleX", "BubbleX: " + Integer.toString(x));
        }

        public void setR(int newR)
        {
                radius = newR;
        }
}

Board.java

package com.blah.level;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

public class Board extends View
{
        int _height, _width;
        int cX, cY1, cY2, r;

        Bubble bubble;

        Bitmap _bitmap;
        Canvas _canvas;
        Paint g;

        public Board(Context context)
        {
                super(context);
                g = new Paint();
                g.setColor(Color.WHITE);
                g.setStyle(Paint.Style.FILL);

                bubble = new Bubble(10,50000,0);
        }
        /**
     * Draws the level to the screen. Then draws the bubble on top.
     */
        private void drawBoard()
        {
                g.setColor(Color.BLACK);
                _canvas.drawRect(0,0,500,500,g);
                g.setColor(Color.WHITE);
                _canvas.drawRect(cX-r, cY1, cX+r, cY2, g);//left, top, right, bottom, paint
                _canvas.drawCircle(cX, cY1, r, g); //centerX, centerY, radius, paint
                _canvas.drawCircle(cX, cY2, r, g);
                bubble.draw(_canvas, g);
        }

        public Bubble getBubble()
        {
                return bubble;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
                _height = View.MeasureSpec.getSize(heightMeasureSpec);
                _width = View.MeasureSpec.getSize(widthMeasureSpec);

                setMeasuredDimension(_width, _height);

                _bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
                _canvas = new Canvas(_bitmap);

                cX = _width/2;
                cY1 = _height/4;
                cY2 = (_height*3)/4;
                r = _width/10;
                bubble.setR(r);
                bubble.setX(cX);
                bubble.setY(cY1);
                drawBoard();
        }
        @Override
        protected void onDraw(Canvas canvas) {
                drawBoard();
                canvas.drawBitmap(_bitmap, 0, 0, g);
                //Log.d("onDrawMessage", "IT DREW");
            invalidate();
        }
}

Excerpt from my manifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blah.level"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="SensorProgram">
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Upvotes: 0

Views: 4025

Answers (1)

jtt
jtt

Reputation: 13541

The reason it is not firing is because you did not register for callbacks for your given activity when sensor changes occur. Refer here http://developer.android.com/reference/android/hardware/SensorManager.html .

Look at the onResume() portion of the sample, notice how its registering this (Activity) to be notified upon changes, through the callback provided by the interface SensorEventListener

Upvotes: 1

Related Questions