Page 1 of 3

mobileSensorAvailable ("location")

Posted: Mon Jan 18, 2016 10:15 pm
by A1Qicks
I'm having some trouble getting this to work.

It doesn't seem to register any sort of sensor on my phone, a Nexus 5 running Android 6.0. Given that this is about as baseline Android as you can get, I'm worried by the implications for this function. Does it only work on iOS? Is there something I'm missing?

Re: mobileSensorAvailable ("location")

Posted: Tue Jan 19, 2016 10:39 pm
by A1Qicks
I've fixed this. Had to enable the right settings under Standalone.

Now my app is aware of the sensor. What I can't get it to do is post any sort of data relating to that sensor.

I've tried:

on mouseUp
if mobileSensorAvailable("location") is true then
set the backgroundcolor of me to green
else
set the backgroundcolor of me to red
end if
mobileStartTrackingSensor("location", true)
get mobileSensorReading("location", true)
set the label of me to it
mobileStopTrackingSensor("location", true)
end mouseUp

This turns the button green, showing that the sensor is available, but the label doesn't change. I've tried it without the Tracking commands, too, and that doesn't do anything either.

What gives?

Re: mobileSensorAvailable ("location")

Posted: Wed Jan 20, 2016 3:04 am
by Simon
Hi A1Qicks,
You might be hitting it too qickly :)
Try putting the get stuff in locationChanged handler.

Simon

Re: mobileSensorAvailable ("location")

Posted: Fri Jan 22, 2016 11:32 am
by A1Qicks
I've been fiddling with this for days now trying to get it to do something.

If I copy it exactly as is from the Compass tutorial file, it works as expected.

The moment I start making changes, it stops doing anything.

Having played around with it a bit, here are my current pertinent scripts, all in the Card script:

Code: Select all

global uMyLocation

on openCard
set the label of button "Location" to "Location"
...
mobileStartTrackingSensor "Location", "Loosely"
end openCard

on locationChanged
put the params into uMyLocation
set the label of button "Location" to uMyLocation 
end locationChanged

on closeStack
-- call the disconnection handler (above)
mobileStopTrackingSensor "Location", "Loosely"
chatDisconnect
end closeStack
There are other bits and pieces that use the location once it's found, but nothing that interferes with the tracking, and nothing that should prevent the name of the button from changing.

The button changes to green, but no data is posted to its label. Whyyyyyyy.

Re: mobileSensorAvailable ("location")

Posted: Fri Jan 22, 2016 12:05 pm
by Simon
Hi A1Qicks,
I actually don't know if "the params" will work (never tried it) the dictionary has this example;
on locationChanged pLatitude, pLongitude, pAltitude
put pAltitude into field "altitude"
end locationChanged
Which would then end up for you;

Code: Select all

set the label of button "Location" to pLatitude && pLongitude && pAltitude
Aside from that if you posted just a small stack I could test it here.

Simon

Re: mobileSensorAvailable ("location")

Posted: Mon Jan 25, 2016 11:17 pm
by A1Qicks
I've made some progress since my last post - I can now get it to detect location, post it to a button, all the stuff I was having trouble with before.

Now I'm getting a weird interaction with my messaging app. Here's the section of message received:

Code: Select all

if lOnPC is false then
if mobileSensorAvailable("location") is true then
set the itemDelimiter to space
put line 1 of data into tTheirLocation
put item 2 of tTheirLocation into tTheirLat
put "Location Test: Latitude" && item 2 of tTheirLocation & return after field "displayMessage"
put item 3 of tTheirLocation into tTheirLong
put "Location Test: Longitude" && item 3 of tTheirLocation & return after field "displayMessage"
#Location is getting through fine, where is the issue here?
set the backgroundcolor of button "Location" to green
 
put uMyLocation into tMyLocation
 
put item 1 of tMyLocation into tMyLat
put item 2 of tMyLocation into tMyLong
set the label of button "Location" to tMyLocation
 
if tTheirLat + (400/6911) >= tMyLat >= tTheirLat - (400/6911) then #If we're within 2 miles, show the message
#Longitude varies with distance from the equator – this a calculation to work out degrees vs miles
if tTheirLong + ((400/6911) * cos((tMyLat + tTheirLat)/2)) >= tMyLong >= tTheirLong - ((400/6911) * cos((tMyLat + tTheirLat)/2)) then
put line 2 of tMessage and return after field "displayMessage"
end if
end if
 
 
else ##too many elses?
set the backgroundcolor of button "Location" to red
put line 2 of data & return after field "displayMessage"
else
put "PC -" && line 2 of data & return after field "displayMessage"
end if
end if
And here's the message sending section:

Code: Select all

if mobileSensorAvailable("location") is true then
set the backgroundcolor of button "Location" to green
set the label of button "Location" to uMyLocation
write "LOCA" && uMyLocation & return & field "NameField" & ":" && data to socket lChatSocket
Now, from testing I know that when it sends a message, it is indeed sending the location through. I can get it to write the coordinates it's sending to the display field when it receives them.

But for some reason, instead of posting the message (the bit that should be easy) it just posts the word 'false'. Now, I'd think there were an error in calculation or something, or that line 2 of tMessage just contained the word 'false' - except for the fact that it's also not posting return after the word false. And if the longitude calculation failed, surely it'd just skip the if clause entirely rather than posting 'false'?

I'll be the first to admit the code as it stands is ugly - I posted the entire relevant section to ensure I didn't miss anything stupid I'd done.

Re: mobileSensorAvailable ("location")

Posted: Tue Jan 26, 2016 9:29 pm
by jacque
The format of the handler is wrong (you are right, there are too many "else"s) so it doesn't run at all. There can be only one "else". If you need more, use "else if":

Code: Select all

if lOnPC is false then
  -- stuff
else if someOtherThing then
  -- do other stuff
else if someThirdThing then
  -- do third stuff
else -- nothing else matches
  -- do default stuff
end if
With two undefined "else" statements, none of them run. That may not be the entire problem, but it's the first thing to fix.

Re: mobileSensorAvailable ("location")

Posted: Tue Jan 26, 2016 11:10 pm
by A1Qicks
Tidied that up, though it doesn't matter too much. All the PC variables are there just for testing purposes, training wheels since LiveCode won't run mobile scripts even to skip them by.

Either way, the 'false' problem persists, and I can't understand for the life of me why.

Re: mobileSensorAvailable ("location")

Posted: Wed Jan 27, 2016 9:46 pm
by jacque
Well, the clue would be what's in the variable "tMessage". I don't see where that is populated or retrieved in your script examples. If you set a breakpoint wherever the value is being set, you might be able to see what's in there. I suspect it contains "false".

Re: mobileSensorAvailable ("location")

Posted: Wed Jan 27, 2016 11:21 pm
by A1Qicks
Relevant code parts are as stands:

Code: Select all

on chatReceived s,data
put data into tMessage
set the itemDelimiter to space
put item 1 of data into tMessageType
switch tMessageType

case LOCA
if lMobileLoc is true then
set the itemDelimiter to space
put line 1 of data into tTheirLocation
put item 2 of tTheirLocation into tTheirLat
put "Location Test: Latitude" && item 2 of tTheirLocation & return after field "displayMessage"
put item 3 of tTheirLocation into tTheirLong
put "Location Test: Longitude" && item 3 of tTheirLocation & return after field "displayMessage"
set the backgroundcolor of button "Location" to green
 
put uMyLocation into tMyLocation
 
put item 1 of tMyLocation into tMyLat
put item 2 of tMyLocation into tMyLong
set the label of button "Location" to tMyLocation
 
if tTheirLat + (400/6911) >= tMyLat >= tTheirLat - (400/6911) then #If we're within 2 miles, show the message
   #Longitude varies with distance from the equator – this a calculation to work out degrees vs miles
if tTheirLong + ((400/6911) * cos((tMyLat + tTheirLat)/2)) >= tMyLong >= tTheirLong - ((400/6911) * cos((tMyLat + tTheirLat)/2)) then
put line 2 of tMessage and return after field "displayMessage"
end if
 end if
 
if lMobileLoc is false then
set the backgroundcolor of button "Location" to red
-- display the data that was sent
put line 2 of data & return after field "displayMessage"
-- specify that this message is to be sent again when more data is received
end if
end if
break
I've done some tidying and also some testing.

At current, it's doing something new that's still wrong.

It prints:

Location Test: Latitude [Latitude] (which is correct)
Location Test: Latitude [Longitude] (which is correct)
false[Name]: [Message] (which is correct apart from the random inclusion of the word 'false')

I also set the server up to print exactly what's going through. Server-side, the whole thing is exactly as should be, comes through without the word 'false'. So this has to be client-side, and given that this section of code is the only one that directly deals with messages, it has to be something here, except for the fact that that doesn't make sense.

Also, I can't set breakpoints because it's mobile code, and mobile code just causes an error when run in the IDE. It wasn't doing this when it was set up purely for PC.

Re: mobileSensorAvailable ("location")

Posted: Thu Jan 28, 2016 12:51 am
by jacque
Do this on mobile:

Code: Select all

on chatReceived s,data
  put data into tMessage
  answer tMessage
  ... <rest of script>
That way you can see exactly what is coming into the handler. But you may not need to; I just noticed this line:

Code: Select all

if tTheirLong + ((400/6911) * cos((tMyLat + tTheirLat)/2)) >= tMyLong >= tTheirLong - ((400/6911) * cos((tMyLat + tTheirLat)/2))
I think that's where the problem is. You've got two comparisons in the same statement. Some languages allow that, LC doesn't. You can't do:

if a >= b >= c

Try rewriting that and I bet it works. Sorry it took me so long to figure out the problem, I didn't pay much attention to the calculation before. LC seems to be evaluating the first part, putting "false", and then evaluating the second part.

By the way, you don't necessarily need to set the itemdelimiter to space. The "word" syntax is built in. I.e.: "put word 1 of data into tMessageType" or "put word 2 of tTheirLocation into tTheirLat", etc. A word is defined as any string surrounded by spaces or white space (tab, return.) If you set the item delimiter to space, you'll get returns and tabs in some items, but maybe that's what you want. Not sure.

Re: mobileSensorAvailable ("location")

Posted: Thu Jan 28, 2016 1:47 pm
by A1Qicks
I'll give it a go when I'm back from work, thanks!

Though this does raise a couple of questions for me:

1. How come it didn't raise an error message? If it's actually happy doing the double calculation (albeit wrongly) what is it actually calculating when it tries to run it?

2. Where does the false actually come from? It makes sense for it to fail if LC doesn't do double calculations, but at no point did I tell it to put its error into field "displayMessage" or into tMessage. I would have thought it'd pop up with a full-blown error or a crash rather than just posting the rather anomalous 'error'.

As for word vs. item, I wasn't sure if "." or "," would end a string in word, hence a more conservative use of itemDelimitering. But that's useful to know, so thanks for that as well!

Re: mobileSensorAvailable ("location")

Posted: Thu Jan 28, 2016 8:31 pm
by jacque
A1Qicks wrote:1. How come it didn't raise an error message? If it's actually happy doing the double calculation (albeit wrongly) what is it actually calculating when it tries to run it?
Good question and I can only guess. It probably should throw an error, but the engine is very forgiving about a lot of things, and this one may have just gone under the radar. Based only on the results you got, I assume it's trying to calculate the first comparison (which evaluates to false) and then the next one in sequence. The handler uses the entire calculation, which in this case would equate to two items apparently. The syntax is non-standard, so it's kind of a toss-up how the engine will respond. But it does try very hard to understand what the script is trying to do and keep errors to a minimum.
2. Where does the false actually come from? It makes sense for it to fail if LC doesn't do double calculations, but at no point did I tell it to put its error into field "displayMessage" or into tMessage. I would have thought it'd pop up with a full-blown error or a crash rather than just posting the rather anomalous 'error'.
See above. I think the "false" is coming from the first set of comparisons. The script places the entire message into the field, which will contain two items after the engine finishes with it: the boolean result and the original message string. Again, it's sort of a tossup whether the lack of an error message is a bug in the engine or whether you're just getting unreliable results by using non-standard syntax. I'm thinking the compiler should throw an error though.
As for word vs. item, I wasn't sure if "." or "," would end a string in word, hence a more conservative use of itemDelimitering. But that's useful to know, so thanks for that as well!
Yes, punctuation would be included in each "word", but the same is true if you use items with spaces as the delimiter. Both the "word" keyword and items delimited by spaces produce identical results, except that you'll also get carriage returns and tabs if you use the delimiter. But in LC 7.x and above, the "trueword" keyword was introduced which uses the unicode libraries to find "real" words -- eliminating punctuation and other characters that our human brain knows are not part of a word. If removing punctuation is important, try LC 7 and the "trueword" syntax.

Re: mobileSensorAvailable ("location")

Posted: Thu Jan 28, 2016 9:53 pm
by A1Qicks
I've now got:

Code: Select all

if tTheirLat + (400/6911) >= tMyLat and tMyLat >= tTheirLat - (400/6911) then #If we're within 2 miles, show the message
   #Longitude varies with distance from the equator – this a calculation to work out degrees vs miles
if tTheirLong + ((400/6911) * cos((tMyLat + tTheirLat)/2)) >= tMyLong and tMyLong >= tTheirLong - ((400/6911) * cos((tMyLat + tTheirLat)/2)) then
put line 2 of tMessage and return after field "displayMessage"
end if
 end if
This, however, no longer gives the name and message, and just gives the Location Test messages as before. I'm not over-eager to put in 'if' after 'if' (after 'if' after 'if') if (ha!) I can help it - is there a tidier way to do this?

Re: mobileSensorAvailable ("location")

Posted: Thu Jan 28, 2016 10:16 pm
by A1Qicks
Following your advice, I now have:

Code: Select all

if tTheirLat + (400/6911) >= tMyLat and tMyLat >= tTheirLat - (400/6911) then #If we're within 2 miles, show the message
   #Longitude varies with distance from the equator – this a calculation to work out degrees vs miles
   answer tMessage
if tTheirLong + ((400/6911) * cos((tMyLat + tTheirLat)/2)) >= tMyLong and tMyLong >= tTheirLong - ((400/6911) * cos((tMyLat + tTheirLat)/2)) then
answer tMessage
put line 2 of tMessage and return after field "displayMessage"
end if
 end if
Here's the really crazy bit.

Every answer it gives tells me the correct message to post.

Then it posts 'false' instead.