3

So I have made this piece of code to get values of GPS location, date and time. And then I will try to make them in a JSON and send them through Wi-Fi to a server. Anyway, the problem is it always reads previous location values. It always reads the values from the "Network" part of the JSON provided by the previous values. The current location, printed is only two brackets "{ }" as you can see if you run the code.

The GPS icon on the phone does not activate when I run the script directly from the phone. I guess the code is self explanatory but if there are any questions please ask.

import android,time,datetime
droid = android.Android()

latitudine = "initial value"
longitudine = "initial value"
indexVal = 1
lostSignal = False

while True:
 print "~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~\nNew reading: #" + str(indexVal) +"\n"
 droid.startLocating()
 locatie = droid.readLocation().result
 droid.stopLocating()
 print "Reading result: " + str(locatie)
 if locatie == {}:
  print "Current location information not available"
 locatie=droid.getLastKnownLocation().result
 if locatie != {}:
  if locatie['gps'] == None:
   if locatie['network'] == {}:
    latitudine = str(locatie['passive']['latitude'])
    longitudine = str(locatie['passive']['longitude'])
    print "Reading passive data (from last known location):"
    print longitudine
    print latitudine
    lostSignal = True
   elif locatie['network'] != {}:
    latitudine = str(locatie['network']['latitude'])
    longitudine = str(locatie['network']['longitude'])
    print "Reading data from network:"
    print latitudine
    print longitudine
    lostSignal = True
  elif locatie['gps']!=None:
   if lostSignal == True:
    droid.vibrate(500)
    lostSignal = False
    latitudine=str(locatie['gps']['latitude'])
    longitudine=str(locatie['gps']['longitude'])
    print "Reading data from GPS:"
    print latitudine
    print longitudine
    droid.notify("GPS found","GPS signal found.\nData aquired")

 print "\nFull available information:\n"
 for locInfo in locatie.iteritems():
 print str(locInfo)
 print "\n"

 now = datetime.datetime.now()

 ora = now.hour
 minut = now.minute
 secunda = now.second

 ziua = now.day
 luna = now.month
 an = now.year

 print str(ora)+":"+str(minut)+":"+str(secunda)+" / "+str(ziua)+"-"+str(luna)+"-"+str(an)
 indexVal += 1
 time.sleep(20)
raduq
  • 97
  • 1
  • 11
  • The times listed in the getLastKnownLocation seem very current for me (within one second of the script run time). I also get {} for readLocation, but it doesn't seem like this is really a problem. I only get GPS values in the provider section if I turn on GPS before running the script. It seems like startLocating doesn't actually turn on GPS if it is off. – Joseph Bui Feb 23 '12 at 16:29
  • I turn on GPS before starting the script and it still doesn't work. I have managed to find a solution. The script works IF you also turn on the mobile data connection which I think is pointless, because in theory you should be "talking" only with the GPS satellites. Or maybe I got it wrong and the GPS satellites communicate with me by carrier? – raduq Feb 23 '12 at 19:56

1 Answers1

0

Call eventWaitFor("location") before you call readLocation. You might also want to use eventPoll, eventClearBuffer or eventWait as well.

The GPS sensor is very slow to start up, because it needs to acquire satellites, synchronize the time and then triangulate a position based on the timing of signals from the satellites. Also, because you have called startLocating without specifying an update frequency, you are getting the default frequency of 60,000ms or once per minute.

Joseph Bui
  • 1,701
  • 15
  • 22