Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Developer
Posts: 49
Registered: ‎09-25-2009
My Device: Not Specified
Accepted Solution

Accelerometer and GPS - can´t get it working

[ Edited ]

Hi,

 

I am writing a small app for testing the accelerometer and GPS. Unfortunately it does not work and I do not get any updates from the sensors.

 

This is my code:

 

package
{ 
	import flash.display.Sprite;
	import flash.events.AccelerometerEvent;
	import flash.events.GeolocationEvent;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.sensors.Accelerometer;
	import flash.sensors.Geolocation;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import qnx.ui.buttons.Button;
	import qnx.ui.buttons.LabelButton;
	
	[SWF(width="1024", height="600", backgroundColor="#cccccc", frameRate="30")] 
	public class AIRHelloWorld extends Sprite 
	{ 
		
		var geoTextField:TextField = new TextField();
		var accTextField:TextField = new TextField();
		
		public function AIRHelloWorld() 
		{ 
			var helloButton:LabelButton = new LabelButton(); 
			
			var geo:Geolocation; 
			var acc:Accelerometer;
			
			geoTextField.x=10;
			geoTextField.y=10;
			geoTextField.width=400;
			
			accTextField.x=10;
			accTextField.y=30;
			accTextField.width=400;
			
			addChild(geoTextField);
			addChild(accTextField);
			
			if (Geolocation.isSupported) 
			{ 
				geo = new Geolocation(); 
				geo.addEventListener(GeolocationEvent.UPDATE, updateHandler); 
				
				geo.setRequestedUpdateInterval(1000);
				geoTextField.text = "Geolocation feature is supported";
			} 
			else 
			{ 
				geoTextField.text = "Geolocation feature not supported"; 
			} 

			if (Accelerometer.isSupported)
			{
				acc = new Accelerometer();
				acc.setRequestedUpdateInterval(100);
				acc.addEventListener(AccelerometerEvent.UPDATE, accupdateHandler);
				accTextField.text = "Accelerometer feature is supported";
			} else
			{
				accTextField.text = "Accelerometer feature is not supported";
			}
			
			stage.nativeWindow.visible = true; 
		} 
	
		private function updateHandler(event:GeolocationEvent):void 
		{ 
			geoTextField.text = "latitude: " + event.latitude.toString() + "\n" 
				+ "longitude: " + event.longitude.toString() + "\n" 
				+ "altitude: " + event.altitude.toString() 
				+ "speed: " + event.speed.toString() 
				+ "heading: " + event.heading.toString() 
				+ "horizontal accuracy: " + event.horizontalAccuracy.toString() 
				+ "vertical accuracy: " + event.verticalAccuracy.toString() 
		}
		
		private function accupdateHandler(event:AccelerometerEvent):void
		{
			accTextField.text = event.accelerationX.toString() +"\n" + event.accelerationY.toString()+ "\n" + event.accelerationZ.toString();

		}
	}
}

The read_geolocation tag is present in the blackberry-tablet.xml file. Any ideas? The textboxes only show that the features are present - no updates at all.

 

Thanks in advance!

 

 

Contributor
Posts: 45
Registered: ‎03-07-2011
My Device: Not Specified

Re: Accelerometer and GPS - can´t get it working

[ Edited ]

No offence but are you running this code on a real device or the simulator? You will not get any updates in the simulator.

New Developer
Posts: 49
Registered: ‎09-25-2009
My Device: Not Specified

Re: Accelerometer and GPS - can´t get it working

No problem - I run it on a real device ...

BlackBerry Development Advisor
Posts: 52
Registered: ‎04-01-2011
My Device: Torch, Playbook
My Carrier: Rogers

Re: Accelerometer and GPS - can´t get it working

Hi maltic,

 

the problem is that you're declaring both geo and acc variables inside the constructor, so when it exits, your objects are gone as well.

 

Move the two declarations to be global to the class (same place you declared the TextFields) and you'll have it working properly.

 

In addition, you should also test geo.muted, since it will tell you whether your app has the rights to read geolocation data or not. 

 

I hope it helps!

 

---
Paulo Garcia
Regular Contributor
Posts: 82
Registered: ‎11-12-2010
My Device: Android Droid Incredible
My Carrier: Verizion

Re: Accelerometer and GPS - can´t get it working

[ Edited ]

Try this, just make it a class and add it to a project. I've tried on an evaluation unit with latest OS 1.0.1.1710 but it says in the status event that the geolocation is muted even though i have given it permission.

 

<code>

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009
 xmlns:s="library://ns.adobe.com/flex/spark" actionBarVisible="false" creationComplete="init()">
 <fx:smileyfrustrated:cript>
 <![CDATA[
 import flash.sensors.Geolocation;
 import qnx.system.QNXSystem;
 import qnx.system.QNXSystemResource;
 private var geo:Geolocation;
 private function init():void{
 //qnx.system.QNXSystem.system.requestResource(QNXSystemResource.GEOLOCATION)
 if(Geolocation.isSupported){
 geo = new Geolocation();
 geo.setRequestedUpdateInterval(100);
 geo.addEventListener(GeolocationEvent.UPDATE,function(event:GeolocationEvent):void{
 myTA.text = "GEO Update Event\n";
 myTA.appendText("latitude: " + event.latitude.toString() + "°\n");
myTA.appendText("longitude: " + event.longitude.toString() + "°\n");
myTA.appendText("horizontal accuracy: " + event.horizontalAccuracy.toString() + " m");
});
geo.addEventListener(StatusEvent.STATUS,function(event:smileyfrustrated:tatusEvent):void{
myTA2.text = event.toString();
});
if(geo.muted)
myTA.text = "geo muted!";
}else{
myTA.text = "geolocation not supported";
}
}
]]>
</fx:smileyfrustrated:cript>
<s:TextArea y="0" id="myTA" height = "200" width = "800"/>
<s:TextArea y="205" id="myTA2" height = "200" width = "800" />
</s:View>

 

</code>

 

BlackBerry Development Advisor
Posts: 52
Registered: ‎04-01-2011
My Device: Torch, Playbook
My Carrier: Rogers

Re: Accelerometer and GPS - can´t get it working

[ Edited ]

L7ColWinters,

 

I cut & pasted your code, without any changes, and it did work. But for that, I had to add the blackberry-tablet.xml file to the src folder in my project.

 

In this file, you must add the permission tag as shown below, so the end user can manually grant access to geolocation services for your application when it is run for the first time.

 

 

<qnx>
  <initialWindow>
    <systemChrome>none</systemChrome>
    <transparent>false</transparent>
  </initialWindow>
  <publisher>Your Name Goes Here</publisher>
  <category>core.util</category>
  <permission>read_geolocation</permission>
</qnx>

 

In the case o Geolocation, if you get the true value from the property muted, and Geolocation is supported, it means your application doesn't have rights to use this resource.

 

Check the attachment image showing the app output.

 

Hope it helps.

 

 

---
Paulo Garcia
Developer
Posts: 425
Registered: ‎03-17-2011
My Device: Developer
My Carrier: Telus

Re: Accelerometer and GPS - can´t get it working

Just a heads-up that the <permission> tag has been changed to <action> for the new sdk.

 

Documentation is here.

 

 

_________________________
In the dark and need a sky map?
Discover What's up at App World.
Follow What's up on Facebook

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Accelerometer and GPS - can´t get it working

The new one is <action>, but note that <permission> continues to work for now. Consider it merely deprecated, not removed (yet).

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Accelerometer and GPS - can´t get it working

Also, by the way, note that "core.util" is currently an unsupported category.  Only "core.games" and "core.media" are supported for now.  (Just in case anyone tries cloning that file into a real project.) :smileyhappy:


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
Posts: 49
Registered: ‎09-25-2009
My Device: Not Specified

Re: Accelerometer and GPS - can´t get it working

[ Edited ]

Thanks for the replies - I will try the example above and follow the suggestions ...