Problem with using Accelerometer on Android target

EDIT2: Ok I've tried on of the free game on the market that utilize accelerometer, and it works, going to try one more time coding some test :-/ EDIT: Ok this is my phone spec: http://www.gsmarena.com/samsung_galaxy_mini_s5570-3725.php And I'm sorry a…

Viewing 1 to 20 (20 Total)
Problem with using Accelerometer on Android target

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

EDIT2: Ok I've tried on of the free game on the market that utilize accelerometer, and it works, going to try one more time coding some test awkward

EDIT: Ok this is my phone spec:http://www.gsmarena.com/samsung_galaxy_mini_s5570-3725.php...
And I'm sorry apparently I was wrong about Ponon! DLX using accelerometer, apparently they used finger slide control (>.<). Anyway what I want is actually controlling an object by tilting the phone, CMIIW this is achieved using the phone's accelerometer right ?

Hi josh, so everything is setup nicely now, and been dabbling with some of the examples, trying out stuff. Just now I stumble upon an issue with NME acceleromater API. I modify the AddingAnimation example, adding textfield to track my phone's accelerometer state. But NME says my phone doesn't have an accelerometer. I already check on some website, my phone is a Galaxy Mini, which is my test device for low end android phone, have an accelerometer, and I'm pretty sure I've used it with Ponon! DLX. So here is my modification to the example


.
.
.
text = new TextField();
text.selectable = false;
text.multiline = true;
text.defaultTextFormat = new TextFormat("_sans", 10, 0x000000);
text.autoSize = TextFieldAutoSize.LEFT;
text.width = stage.stageWidth;
text.height = stage.stageHeight;
addChild(text);

if (Accelerometer.isSupported) {
accl = new Accelerometer();
accl.addEventListener(AccelerometerEvent.UPDATE, onAcclUpdate);
} else {
text.text = "Accelerometer NOT supported sad";
}
}

private function onAcclUpdate(e:AccelerometerEvent):Void {
text.text
= "acceleration X: " + e.accelerationX + "\n"
+ "acceleration Y: " + e.accelerationY + "\n"
+ "acceleration Z: " + e.accelerationZ;
}
.
.
.

Tags:
Posted on November 17, 2011 at 8:36 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Ok so I still can't akses the accelerometer in my phone TT . TT . Anyone have example on how to use it that I could try ?

Accelerometer.isSupported

always return false on me, even though my phone support it awkward

Posted on November 18, 2011 at 11:13 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

Do you get anything if you access the accelerometer this way?

var data = nme.ui.Accelerometer.get ();
trace (data.x);
trace (data.y);
trace (data.z);


This may be a problem with the "isSupported" property, but not the actual call to get the data

Posted on November 18, 2011 at 11:20 AM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

No luck josh, bad news even, since using your method, I crashed my app. Screen goes black for a while than the forced back to the app drawer. The log is pretty saturated with other messages, but this catches my eyes

E/InputDispatcher( 162): channel '40932408 org.haxenme.tutorial.addinganimation/org.haxenme.tutorial.addinganimation.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8

E/InputDispatcher( 162): channel '40932408 org.haxenme.tutorial.addinganimation/org.haxenme.tutorial.addinganimation.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!


don't know if this would be any help.

EDIT: My code, I comment out all nme.sensors.Accelerometer and import nme.ui.Accelerometer instead


.
.
.

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(e:Event):Void {
var data = Accelerometer.get();
//var data = { x:0, y:0, z:0 };
text.text
= "acceleration x: " + data.x + "\n"
+ "acceleration y: " + data.y + "\n"
+ "acceleration z: " + data.z;
}
.
.
.

Posted on November 18, 2011 at 9:23 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Uhmm any news about this, does anybody else succeeded in utilizing Accelerometer using the svn trunk ? Is it my device ? Or my code is wrong ? This is driving me crazy grin

Posted on November 19, 2011 at 8:25 PM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

I have a feeling this has something to do with your device. Perhaps the accelerometer is implemented differently, or there's something unique about how NME is asking for the accelerometer data, that your phone doesn't like?

I've had success with the Android device I have. When I call Accelerometer.get () it gives me the accelerometer data... it doesn't crash. I wish this was easier to debug

If you look at the generated Java classes for your project, you may see the code that accesses the accelerometer. Which device are you using? Do you have anything else to test with?

Posted on November 19, 2011 at 9:11 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Well as a I said earlier, I'm testing it using my low end phone, a Samsung Galaxy Mini. As you can see in link I gave before, this phone have accelerometer and I also already tried playing a game (Abduction from market, free) which uses Accelerometer and it works. I don't think there is anything "weird", since this is a Samsung phone (even if it is targeted towards low end market, which is huge here). Well I'll try again with my Galaxy Tab and SGSII and I'll try checking the output java code when I get back home. Can you give me a minimal code that you use to test Accelerometer on your device (with the nmml, who knows maybe I miss something in the nmml awkward), that would make it easier to narrow down my problem smiling.

Cheers.

Posted on November 20, 2011 at 12:58 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

The simplest use-case is a blank project that only calls Accelerometer.get (), and checks if the return value is null.

If this doesn't work on more of your devices, there definitely is a regression. Otherwise we'll try and figure out what the Mini doesn't like

Posted on November 20, 2011 at 10:13 AM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Ok I'm back, I tested on both my devices, minus the SGSII, here are the specs

- Samsung Galaxy Mini, Android 2.3.4
- Samsung Galaxy Tab 7", Android 2.3.3

Here is the code that I used to test it with:http://www.mediafire.com/?fcxmffzufoz1lzm...
Here is the version information for haxe, hxcpp, nme and other related stuff that I used

- haxe 2.08 vanilla from the website, neko 1.8.2 vanilla from the website, I manual install both, since I installed it in my home dir
- hxcpp, svn using revision 404
- nekonme, svn using revision 1080
- gm2d, svn using revision 125
- sdl-static, svn using revision 52
- android-ndk-r6b
- android-sdk-linux r15, with only platform 7
- OS: Ubuntu 11.10
- JDK: OpenJDK 7 (I also tried on Windows using JDK 1.6.xx, same result)

About the crash, please ignore that, it's me being stupid not checking for null returned by Accelerator.get() >.<

Posted on November 20, 2011 at 1:15 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Just want to update about this, after digging around I came across this

#include <Input.h>


namespace nme
{

#if !defined(IPHONE) && !defined(WEBOS)
bool GetAcceleration(double &outX, double &outY, double &outZ)
{
return false;
}
#endif


}



So apparently I complained about a feature that is not yet implemented for Android, sorry guys.

Posted on November 23, 2011 at 8:47 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Hehe hi guys, so this have been nagging me for the last couple of days (I just can't let it go, even though my co worker said we don't need accelerometer on android), I decided to just jump in and implement it myself. This is my first time doing JNI and I'm still grasping NME internals so if you guys could please review my patch, it would be great smiling

http://www.mediafire.com/?u82r46jkjfhx6t8... (the diff file)

EDIT: I don't know about the behaviour of GetAcceleration on iOS and WebOS, so currently Android return vanilla value (0, 0, 9.8, on a flat surface).

Index: nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/NME.java
===================================================================
--- nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/NME.java (revision 1097)
+++ nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/NME.java (working copy)
@@ -12,6 +12,7 @@
public static final int DEACTIVATE = 2;
public static final int DESTROY = 3;

+ public static native int onAccelerate(float x, float y, float z);
public static native int onTouch(int type, float x, float y, int id);
public static native int onResize(int width, int height);
public static native int onTrackball(float x,float y);
Index: nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/GameActivity.java
===================================================================
--- nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/GameActivity.java (revision 1097)
+++ nekonme-svn/tools/command-line/android/template/src/org/haxe/nme/GameActivity.java (working copy)
@@ -3,6 +3,10 @@

import android.app.Activity;
import android.content.Intent;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
@@ -25,7 +29,7 @@
import java.util.HashMap;
import java.lang.reflect.Constructor;

-public class GameActivity extends Activity {
+public class GameActivity extends Activity implements SensorEventListener {

MainView mView;
static AssetManager mAssets;
@@ -38,6 +42,7 @@
public android.os.Handler mHandler;
static HashMap<String,Class> mLoadedClasses = new HashMap<String,Class>();
static DisplayMetrics metrics;
+ static SensorManager sensorManager;

protected void onCreate(Bundle state) {
super.onCreate(state);
@@ -68,7 +73,13 @@

setContentView(mView);

-
+ sensorManager = (SensorManager)
+ activity.getSystemService(Context.SENSOR_SERVICE);
+ if (sensorManager != null) {
+ sensorManager.registerListener(this,
+ sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
+ SensorManager.SENSOR_DELAY_GAME);
+ }
}

public static GameActivity getInstance() { return activity; }
@@ -337,6 +348,9 @@
mView.onPause();
if (mMediaPlayer!=null)
mMediaPlayer.pause();
+
+ if (sensorManager != null)
+ sensorManager.unregisterListener(this);
}

@Override protected void onResume() {
@@ -347,6 +361,12 @@
if (mMediaPlayer!=null)
mMediaPlayer.start();
mView.sendActivity(NME.ACTIVATE);
+
+ if (sensorManager != null) {
+ sensorManager.registerListener(this,
+ sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
+ SensorManager.SENSOR_DELAY_GAME);
+ }
}

@Override protected void onDestroy() {
@@ -355,5 +375,16 @@
activity=null;
super.onDestroy();
}
+
+ @Override public void onSensorChanged(SensorEvent event) {
+ if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
+ float[] values = event.values;
+ NME.onAccelerate(values[0], values[1], values[2]);
+ }
+ }
+
+ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
+
+ }
}

Index: nekonme-svn/project/android/AndroidFrame.cpp
===================================================================
--- nekonme-svn/project/android/AndroidFrame.cpp (revision 1097)
+++ nekonme-svn/project/android/AndroidFrame.cpp (working copy)
@@ -166,6 +166,14 @@
HandleEvent(mouse);
}
}
+
+ void OnAccelerate(double inX, double inY, double inZ)
+ {
+ mAccX = inX;
+ mAccY = inY;
+ mAccZ = inZ;
+ //__android_log_print(ANDROID_LOG_INFO, "NME", "Accelerometer %f %f %f", inX, inY, inZ);
+ }

void EnablePopupKeyboard(bool inEnable)
{
@@ -188,7 +196,11 @@

double mDX;
double mDY;
-
+
+ double mAccX;
+ double mAccY;
+ double mAccZ;
+
double mDownX;
double mDownY;

@@ -292,8 +304,16 @@
env->CallStaticVoidMethod(cls, mid);
}

+bool GetAcceleration(double& outX, double& outY, double& outZ) {
+ if (sStage) {
+ outX = sStage->mAccX;
+ outY = sStage->mAccY;
+ outZ = sStage->mAccZ;
+ return true;
+ }
+ return false;
+}

-
} // end namespace nme


@@ -336,6 +356,17 @@
return nme::GetResult();
}

+JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onAccelerate(JNIEnv * env, jobject obj, jfloat x, jfloat y, jfloat z)
+{
+
+ int top = 0;
+ gc_set_top_of_stack(&top,true);
+ if (nme::sStage)
+ nme::sStage->OnAccelerate(x,y,z);
+ gc_set_top_of_stack(0,true);
+ return nme::GetResult();
+}
+
JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onTouch(JNIEnv * env, jobject obj, jint type, jfloat x, jfloat y, jint id)
{

Index: nekonme-svn/project/common/Input.cpp
===================================================================
--- nekonme-svn/project/common/Input.cpp (revision 1097)
+++ nekonme-svn/project/common/Input.cpp (working copy)
@@ -4,7 +4,7 @@
namespace nme
{

-#if !defined(IPHONE) && !defined(WEBOS)
+#if !defined(IPHONE) && !defined(WEBOS) && !defined(ANDROID)
bool GetAcceleration(double &outX, double &outY, double &outZ)
{
return false;

Posted on November 24, 2011 at 7:52 PM

mlapasa

mlapasa
Total Posts: 38
Joined: November 20, 2011

Re: Problem with using Accelerometer on Android target

exciting stuff aaulia, I followed your code in your first post and I expected it to work on my Lenovo Thinkpad Tablet running Honeycomb. Didn't work. Hopefully this finds its way into the release.

Posted on November 27, 2011 at 12:13 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

Thanks aaulia!

I had no idea that the accelerometer was missing on Android. I was sure I had seen one of my games running on Android, with the Accelerometer, but I must be imagining things.

All I had to change was dividing the sensor values by gravity, and negating the x and y values, to get it to match the other devices. It should now report values based on gravity, so 0 is neutral, and 1 is (generally) the maximum value, since we are on earth.

I just committed it to SVN, so feel free to update, rebuild the NME binary for Android, and give it a try smiling

Posted on November 27, 2011 at 4:45 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

Thanks Josh ! I only got it compile and briefly tested it this morning, and it looks Ok, I will check again when I got back from work. Also while we're on subject of Accelerometer, I think there is still a slight problem with

nme.sensors.Accelerometer

According to thishttp://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/...

The setRequestedUpdateInterval method is used to set the desired time interval for updates. The time interval is measured in milliseconds. The update interval is only used as a hint to conserve the battery power. The actual time between acceleration updates may be greater or lesser than this value. Any change in the update interval affects all registered listeners. You can use the Accelerometer class without calling the setRequestedUpdateInterval() method. In this case, the application receives updates based on the device's default interval.


Last time I checked it, you can't receive "AccelerometerEvent.UPDATE" event, without first invoking the "setRequestUpdateInterval()". Also "Accelerometer.isSupported" is *ONLY* valid if you instantiate a new Accelerometer using "new", I believe this is not the right behavior.

So while the "nme.ui.Accelerometer" does work, I think for the sake of completeness and cross platform (mainly between other platform with flash) I think we should fixed it. I'll try to make a patch soon, I hope you would review it smiling

Posted on November 27, 2011 at 10:50 PM

aaulia

aaulia
Total Posts: 48
Joined: November 07, 2011

Re: Problem with using Accelerometer on Android target

As promised here it is

Index: nekonme-svn/nme/sensors/Accelerometer.hx
===================================================================
--- nekonme-svn/nme/sensors/Accelerometer.hx (revision 1104)
+++ nekonme-svn/nme/sensors/Accelerometer.hx (working copy)
@@ -11,25 +11,20 @@
class Accelerometer extends EventDispatcher
{

- public static var isSupported(default, null):Bool;
+ public static var isSupported(checkSupport, null):Bool;

public var muted(default, null):Bool;

private var timer:Timer;

+ private static function checkSupport():Bool {
+ return nme_input_get_acceleration() != null;
+ }

public function new()
{
super();
-
- if (nme_input_get_acceleration() == null)
- {
- isSupported = false;
- }
- else
- {
- isSupported = true;
- }
+ setRequestedUpdateInterval(40);
}


@@ -72,4 +67,4 @@

#else
typedef Accelerometer = flash.sensors.Accelerometer;
-#end
\ No newline at end of file
+#end

Posted on November 28, 2011 at 10:30 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

Thanks! I just updated SVN with improvements to nme.sensors.Accelerometer.

Inline with the above patch, you can check if it is supported without instantiating the Accelerometer class, it will apply a default interval if you don't set one, and there are some other changes/improvements to mirror what is outlined in Adobe's API documentation, like handling an interval that is less than zero, or that equals zero (throw an argument error, or use the default interval)

This does not actually control how often the device reports its accelerometer values, but just how often you receive events. I think this is fine for games, but maybe this can be improved as we keep improving NME for application development.

Thanks again

Posted on November 28, 2011 at 11:25 AM

AdamHarte

AdamHarte
Total Posts: 17
Joined: November 03, 2011

Re: Problem with using Accelerometer on Android target

I seem to be having the same issues with my Motorola Milestone (Droid in US). Have these updates made it into the main release? I am using NME 3.1 RC1. Should this be working?

I am getting no data with either nme.sensors.Accelerometer or nme.ui.Accelerometer.

For nme.sensors.Accelerometer, Accelerometer.isSupported always returns false. And for nme.ui.Accelerometer, Accelerometer.get() always returns null.

I know that my accelerometer actually works, but just can't figure this out.

Posted on January 16, 2012 at 3:23 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

For a long time, Android didn't have accelerometer support, and I didn't realize it. You probably have a version of NME that was before it was added.

Would you like to try upgrading to 3.1.1, then trying again? It's available on haxelib, so you just need to run "haxelib upgrade nme"

Posted on January 16, 2012 at 12:36 PM

AdamHarte

AdamHarte
Total Posts: 17
Joined: November 03, 2011

Re: Problem with using Accelerometer on Android target

Running "haxelib upgrade nme" says "All projects are up-to-date". Also it seems to check all the libs, not just NME with that command.

Running "haxelib info nme" tells me I am using version 3.1.1 so seems to be current. However, I am still having issues, and have noticed a few strange things.

My class from nme.ui package looks basically the same as the one in SVN here: nme/ui/Accelerometer.hx, except the comment has changed positions.

The class from nme.sensors is quite different from SVN. My version has this line:
public static var isSupported (default, null):Bool;
Where as the version from SVN matches more along the lines of aaulia's diff. So seems like I don't have the latest version. Should I just use the SVN version? How do I go about this?

Thanks.

Posted on January 17, 2012 at 12:53 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: Problem with using Accelerometer on Android target

I do believe that the accelerometer fix made it into 3.1.1, but if you're in doubt, you could try and remove 3.1.1, then reinstall, or you could give SVN a try.

If you are on SVN and there are any issues, we'll be able to push changes out and test them more easily

Posted on January 17, 2012 at 1:19 AM