Start GPS sensor automatically

1,392 views
Skip to first unread message

Brian Moreau

unread,
Aug 21, 2014, 7:30:10 AM8/21/14
to mitappinv...@googlegroups.com
Hi, I am wondering if it is possible to switch on the GPS sensor by a program command.
It is suggested setting LocationSensor1.Enabled = True only allows AI to communicate with the GPS sensor but it doesn't turn it on.
If it is off then location is derived from WiFi or the Passive GPRS network.
I would also assume that this would be phone model dependant i.e. not possible on all phones.
I notice on my Samsung Galaxy S3 calling LocationSensor1.Enabled = True flashes up the GPS icon for a second and gives me the lat and long even if GPS is turned off but on my S5 it doesn't.
 

SteveJG

unread,
Aug 21, 2014, 9:02:34 AM8/21/14
to mitappinv...@googlegroups.com
You may want to read the LocationSensor tutorial here:  http://appinventor.mit.edu/explore/ai2/location-sensor.html 

You can turn the gps off by  LocationSensor.Enabled = False        When you place a LocationSensor on your app, MIT says that the sensor is enabled automatically.  I make a habit of turning it on in the Screen1.Initialize as a matter of course.

Yes, phones, especially the newer models do a lot of switching between Passive and WIFI and GPS ...this is supposed to happen whenever the GPS signal is too weak to get a satellite fix.
This can be prevented.    If you ONLY want GPS,   set Provider to    gps      then   immediately set ProviderLocked = true.    Why?

ProviderLocked
The device will not change the service provider. 
It is possible for providers to switch when the current provider is unable to provide adequate location fixes for circumstances. This is a Boolean switch; true/false. True, to prevent providers from changing.

When the GPS icon is flashing, the GPS receiver in the Android device is making a satellite fix; it is trying to 'find' at least three satellites needed to provide a latitude and longitude.  When the icon stops flashing, the GPS has a fix.   Notice that when the device is first switched on that the flashing may take 10 or 15 seconds or more?  This is common.   subsequent fixes may tak 3 to 5 seconds.  No gps in phones can make a fix in less than several seconds.   Also notice, if the phone is indoors, it takes longer to get a fix and in some structures the gps is unable to get a fix at all.  

Be aware, the GPS is probably working even when the icon is NOT flashing.   Developers can have the icon flashing all the time by changing the Interval value....all this achieves is a rapid battery drain.  It makes no real sense to have the GPS make a fix more frequently than one minute in most applications  and as mentioned, if you force a fix fewer than five seconds, you are probably giving your device a heart attack.

The newer phones have the ability to improve upon the GPS fix , most phones, the GPS fix is the most accurate fix.      Usually the network fix triangulates using cell phone towers...youwill rarely get an Accuracy of less than about 50 meters.... GPS can give you several meters precision in devices with the best GPS receivers.

Hope this was helpful.

Regards,
Steve

Taifun

unread,
Aug 21, 2014, 9:42:57 AM8/21/14
to mitappinv...@googlegroups.com
I am wondering if it is possible to switch on the GPS sensor by a program command.
this is not possible
however for Android 2.x devices, I can offer this workaround https://puravidaapps.com/togglegps.php
Taifun

SteveJG

unread,
Aug 21, 2014, 10:51:09 AM8/21/14
to mitappinv...@googlegroups.com

Hmmm.  This works on my Android 4.2.2  :

I get this      by using the following Activity.Starter code:  :


Regards,
Steve


Brian Moreau

unread,
Aug 21, 2014, 3:53:34 PM8/21/14
to mitappinv...@googlegroups.com
Thanks guys, useful information.
I will consider using your solution Taifun.

Taifun

unread,
Aug 21, 2014, 4:23:31 PM8/21/14
to mitappinv...@googlegroups.com
Hmmm.  This works on my Android 4.2.2  :
yes, you can use the activity starter to open the settings, then the user can toggle GPS manually, however you can't programmatically toggle the GPS from within App Inventor
Taifun

SteveJG

unread,
Aug 22, 2014, 11:00:26 AM8/22/14
to mitappinv...@googlegroups.com
For security reasons, the GPS can not be turned on or off programably.   See discussion here http://stackoverflow.com/questions/20048995/android-4-1-2-how-to-turn-off-gps-programmatically 

So, below 4, you can, but it is a recognized security issue.  See the code above to turn the GPS on manually.

Regards,
Steve
Reply all
Reply to author
Forward
0 new messages