Newby help request to get me started

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

simon.schvartzman
Posts: 641
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Newby help request to get me started

Post by simon.schvartzman » Wed Aug 18, 2021 2:15 pm

Hi all, I'm new to Externals and desperately in need of help.

My first challenge is an external to check if BT is enabled in Android.

Heather (kindly enough) has provided some code but I'm getting an error which I don't know how to fix and even worst I don't know where can I look for help. It seems to be a problem with the parameter definition of one of the calls (as described below) but since I wasn't able to learn how the definition should be I'm in a dead lock.

So, this is the code

Code: Select all

library community.livecode.ss.btstatus

metadata title is "BT Status"
metadata author is "SS"
metadata version is "1.0.0"

use com.livecode.foreign
use com.livecode.java

foreign handler JavaGetDefaultBluetoothAdapter() returns JObject binds to "java:android.bluetooth.BluetoothAdapter>getDefaultAdapter()Landroid/bluetooth/BluetoothAdapter;!static"

foreign handler JavaCheckIfBluetoothIsEnabled() returns JBoolean binds to "java:android.bluetooth.BluetoothAdapter>isEnabled()Z"

public handler CheckIfBluetoothIsEnabled() returns Boolean
	unsafe
		variable tBluetoothAdapter as JObject
		put JavaGetDefaultBluetoothAdapter() into tBluetoothAdapter
	        variable tIsEnabled as Boolean
		put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
                return tIsEnabled
	end unsafe
end handler
end library
and this is the error log
9:59 AM: Compiling module /Users/simonschvartzman/Desktop/SegnaPunto/Externals/BTStatus/BTStatus.lcb
9:59 AM: Error: on line 19 ():
9:59 AM: Error: /Users/simonschvartzman/Desktop/SegnaPunto/Externals/BTStatus/BTStatus.lcb:19:7: error: Too many arguments for specified handler
put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
^

9:59 AM: Error: failed to compile module
9:59 AM: Error: Could not compile module /Users/simonschvartzman/Desktop/SegnaPunto/Externals/BTStatus
if I replace

Code: Select all

put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
by

Code: Select all

put JavaCheckIfBluetoothIsEnabled() into tIsEnabled
then the error goes away but the App crashes when trying to run it :oops:

So my questions are:
  • where can I find the arguments I should be using ?
  • where can I learn about how binds are to be defined, I mean for example why in one of the definition it goes:
>getDefaultAdapter()Landroid/bluetooth/BluetoothAdapter;!static"

and in the other

>isEnabled()Z"

What the !static and Z mean ?

Many thanks in advance for any help
Simon
________________________________________
To ";" or not to ";" that is the question

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Sun Aug 22, 2021 2:30 pm

When looking for info about Android APIs you should probably start with Google's Android Developers site:
https://developer.android.com/reference ... sEnabled()

OK so here is something that may not occur to LC scripters...
From the Android documentation site that function doesn't look like it takes any parameters, however (and someone please correct me if I get this wrong as I still consider myself as a newb with some OOP concepts and I've mostly been tapping into C / ObjectiveC APIs) with FFI you often have to at least pass a reference to the object you're dealing with, it's SELF (and 'self' is what it's often referred to).

Are you running this on an Android Device? If not then that may be why it's crashing the engine. Are BT permissions enabled for your app manifest in the Standalone Settings? Is the LCB extension being included in the APK build from the Standalone settings?

As for where to get info about binding strings, the (sparse) documentation is here:
https://livecode.com/docs/9-6-1/extendi ... reference/
But you've probably already seen that. I've 'grok'd a lot of things by looking at whatever LCB examples are available (which isn't all that much) and then comparing to the official documentation for whatever APIs the LCB FFI code is tapping into.
Last edited by PaulDaMacMan on Sun Aug 22, 2021 11:08 pm, edited 2 times in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Sun Aug 22, 2021 2:47 pm

simon.schvartzman wrote:
Wed Aug 18, 2021 2:15 pm
What the !static and Z mean ?
!static is the calling convention, I haven't had to deal with that with Objective C APIs but it seems to be important on Android and Windows. Sorry, I can't offer more help with that.
That does seem to indicate, according to the LCB language ref, that is a signal to pass a reference to the object, it's 'self':
Instance and nonvirtual calling conventions require instances of the given Java class, so the foreign handler declaration will always require a Java object parameter.
For info about what the Z on the end means (indicates data type) look here ( from a link in the LCB Language Ref):
http://journals.ecs.soton.ac.uk/java/tu ... ethod.html
Java VM Type Signatures

Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ; fully-qualified-class
[ type type[] (Array of types indicated by a leading square bracket ([) followed by the type of the array elements.
( arg-types ) ret-type method type
Last edited by PaulDaMacMan on Sun Aug 22, 2021 5:19 pm, edited 1 time in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

simon.schvartzman
Posts: 641
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Newby help request to get me started

Post by simon.schvartzman » Sun Aug 22, 2021 3:29 pm

Paul many thanks for your willingness to help. Unfortunately my level of knowledge about externals and Objective C is less than nothing so there are things I can't even imagine what it means :) for instance
that is a signal to pass a reference to the object, it's 'self
Now answering your questions (may be some light comes out of this)
Are you running this on an Android Device?
Yes, on a Motorola cell running Android 6

Are BT permissions enabled for your app manifest in the Standalone Settings?
I see no place where I have to enable BT in the Standalone Settings. Am I missing something here?
Screen Shot 2021-08-22 at 11.19.01.png
Is the LCB extension being included in the APK build from the Standalone settings?
I have no idea, how can I check it is ?

Paul, please apologize for so many questions, first time working with this has been a challenge...

Final question (for now :( ) If it were you, what would you change on the source code to give it a different try?

Best!
Simon
________________________________________
To ";" or not to ";" that is the question

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Sun Aug 22, 2021 6:16 pm

simon.schvartzman wrote:
Sun Aug 22, 2021 3:29 pm
Paul many thanks for your willingness to help. Unfortunately my level of knowledge about externals and Objective C is less than nothing
You're in luck because this isn't Objective C, it's JAVA JNI that you have to deal with for Android APIs.
so there are things I can't even imagine what it means :) for instance that is a signal to pass a reference to the object, it's 'self
The 'self' object that you must pass here would be your Android device's default bluetooth adapter that you retrieved as a JObject type in the previous line.
Are you running this on an Android Device?
Yes, on a Motorola cell running Android 6
OK good, some people don't understand that the mobile API that they're trying to tap into probably isn't going to be available on the OS they're coding on (which seems to be changing with macOS 10.15/11 adding iOS 'catalyst' and Windows 11 adding Android runtime)
Are BT permissions enabled for your app manifest in the Standalone Settings?
I see no place where I have to enable BT in the Standalone Settings. Am I missing something here?
Hmm, it's been a while since I've done any programming stuff targeting Android, and haven't (yet) done anything with Bluetooth, maybe you don't need to get permissions to use bluetooth at build-time and the mobile OS will ask you automatically when an app asks to use it.
Is the LCB extension being included in the APK build from the Standalone settings?
I have no idea, how can I check it is ?
"Search for inclusions" in the General settings tab, which tries to automatically include extensions (but doesn't seem to work very well), Change it to "Select Inclusions"!
Screen Shot 2021-08-22 at 1.01.41 PM.png
Then the "inclusions" tab will become enabled and you can manually check what add-ons you want included in your build.
Screen Shot 2021-08-22 at 1.02.07 PM.png
Paul, please apologize for so many questions, first time working with this has been a challenge...
Happy to help, I wish more people would dive in, the water is a little bit cold but you'll get used to it!
Final question (for now :( ) If it were you, what would you change on the source code to give it a different try?
I definitely would put that binding string back to the way Heather had it setup, you need to pass that JObject that represents the default Bluetooth adapter on the device.
Maybe, I'll give this code a test try myself when I get a chance (my Phone is on Android 9 though, not sure if that matters)
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Sun Aug 22, 2021 10:28 pm

@Klaus or some other moderator
Could you please move this post to the LiveCode Builder area?
Thank you.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Newby help request to get me started

Post by FourthWorld » Sun Aug 22, 2021 10:49 pm

PaulDaMacMan wrote:
Sun Aug 22, 2021 10:28 pm
@Klaus or some other moderator
Could you please move this post to the LiveCode Builder area?
Good suggestion - Done.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Sun Aug 22, 2021 11:15 pm

FourthWorld wrote:
Sun Aug 22, 2021 10:49 pm
PaulDaMacMan wrote:
Sun Aug 22, 2021 10:28 pm
@Klaus or some other moderator
Could you please move this post to the LiveCode Builder area?
Good suggestion - Done.
Thanks Richard!
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

simon.schvartzman
Posts: 641
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Newby help request to get me started

Post by simon.schvartzman » Mon Aug 23, 2021 1:21 am

Paul, just to avoid leaving it answered I misunderstood your question about the Library being included on the standalone. Answer is yes it is included.

At this point the fact the you have Android 9 won't make any difference because the code, as suggested by Heather, doesn't even compile on Live Code Builder...

Thanks all for placing the post where it belongs.

Best !
Simon
________________________________________
To ";" or not to ";" that is the question

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Mon Aug 23, 2021 8:17 am

simon.schvartzman wrote:
Mon Aug 23, 2021 1:21 am
Paul, just to avoid leaving it answered I misunderstood your question about the Library being included on the standalone. Answer is yes it is included.

At this point the fact the you have Android 9 won't make any difference because the code, as suggested by Heather, doesn't even compile on Live Code Builder...

Thanks all for placing the post where it belongs.

Best !
That's a good point, the LCB should at least compile if the syntax and parameters were all correct.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Tue Aug 24, 2021 3:54 pm

So I've re-setup Android SDK, Installed JAVA 8 and I'm now able to build Android apk successfully again...
I put this LCB code back to the way Heather wrote it and it compiles into an LCB module without complaint.
I include that with my test apk build and installed it on my Android 9 Samsung phone where it does nothing at all.
So then I merged this code with a old Battery Status lib I had messed around with a few years back, followed the same steps, and tried that on my phone and that does nothing at all either, not even the battery check code that previously worked fine under Android 4 through 6! So I installed my old test apk that I had built back then (for the older Arm7 CPU arch of my old phone) and that still works just fine, even on the newer (ARM 8 64bit arch) phone...
I'm not sure what the problem is... the battery code hasn't changed at all and yet that isn't working either!
From what I've read on several StackOverflow posts you do need "android.permission.BLUETOOTH" in your AndroidManifest, but I don't see anywhere to indicate you need to use BT in the Android Standalone settings.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

simon.schvartzman
Posts: 641
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Newby help request to get me started

Post by simon.schvartzman » Tue Aug 24, 2021 8:43 pm

Paul, this is very interesting, things are really very strange...
I put this LCB code back to the way Heather wrote it and it compiles into an LCB module without complaint.
I wonder why it gives me an error and it doesn't to you...just "for fun" could you please share your basic code (without your Battery Status lib) for me to test it? Which environment are you on? Mine is:

macOS Big Sur Version 11.2.3
Livecode 9.6.3 Indy
Editor: Atom

Thanks for your time
Simon
________________________________________
To ";" or not to ";" that is the question

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Wed Aug 25, 2021 2:45 am

simon.schvartzman wrote:
Tue Aug 24, 2021 8:43 pm
Paul, this is very interesting, things are really very strange...
I put this LCB code back to the way Heather wrote it and it compiles into an LCB module without complaint.
I wonder why it gives me an error and it doesn't to you...just "for fun" could you please share your basic code (without your Battery Status lib) for me to test it? Which environment are you on? Mine is:

macOS Big Sur Version 11.2.3
Livecode 9.6.3 Indy
Editor: Atom

Thanks for your time
I'm still running Mohave 10.14.6 (I still want to use some 32bit things a home)
LC 9.6.3
Atom (with LiveCode LCB colorization)
I was getting some message about not being able to build a class or something but installed an JDK 8 (one of the OpenJDK builds because I didn't feel like making an account with Oracle) which got me past that but it could be part of my problem.

From Googling around for answers it looks like LCB can add to the Android manifest by using metadata, something like:

Code: Select all

metadata android.permissions is "BLUETOOTH"
and/or:

Code: Select all

metadata android.permissions is "BLUETOOTH_ADMIN""
Could be an important puzzle piece!

Also found this tip on where to edit LC's default Android Manifest xml :
viewtopic.php?t=35354
Last edited by PaulDaMacMan on Wed Aug 25, 2021 3:18 pm, edited 4 times in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Wed Aug 25, 2021 2:49 am

Here is the code that compiles in extension builder, but is non-functional at least on my Android set-up:

Code: Select all

library community.livecode.ss.btstatus

metadata title is "BT Status"
metadata author is "SS"
metadata version is "1.0.0"

use com.livecode.foreign
use com.livecode.java

foreign handler JavaGetDefaultBluetoothAdapter() returns JObject binds to "java:android.bluetooth.BluetoothAdapter>getDefaultAdapter()Landroid/bluetooth/BluetoothAdapter;!static"

foreign handler JavaCheckIfBluetoothIsEnabled( in pBluetoothAdapter as optional JObject) returns JBoolean binds to "java:android.bluetooth.BluetoothAdapter>isEnabled()Z"

public handler CheckIfBluetoothIsEnabled() returns Boolean
	unsafe
		variable tBluetoothAdapter as JObject
		put JavaGetDefaultBluetoothAdapter() into tBluetoothAdapter
		--- should add check here to ensure that tBluetoothAdapter is not nothing
	        variable tIsEnabled as Boolean
		put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
                return tIsEnabled
	end unsafe
end handler

end library
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 627
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Newby help request to get me started

Post by PaulDaMacMan » Wed Aug 25, 2021 4:36 pm

I caved and installed Oracle's official JavaJDK 8 (jdk-8u231-macosx-x64, same as @bmilby is using)
Still no joy! Not even just straight rebuilding of LCB stuff that had worked fine 4 years ago, and ran just fine on Android 4 ICS and above (including my current Android 9 Phone). My Battery Code was based on https://livecode.com/livecode-9-0-dp-6- ... e-preview/

I did make a test apk using Android Native Field and Native Button that come with LC 9.6.3 and that works just fine, so I know my build environment is good.

The LC Standalone Builder (most likely due to Playstore current requirements ) forces you to update Android target APIs to at least Android 5 (which adds about 1MB to the apk size). Personally I think LC should allow you to target older versions if you want to (maybe with a warning about PlayStore requirements) for people that still want to make use of old devices, just my opinion.

Obviously there have been a lot of changes to Android OS from 4 to 9, like security/permissions and I'm guessing even to basic things like checking battery status or getting a hold of the Bluetooth adapter. So it seems my Android / LCB experience from four years ago is completely outdated now and my old code needs to be updated. And also there's something incomplete or outdated about your BT status LCB code.
Last edited by PaulDaMacMan on Tue Aug 31, 2021 4:02 am, edited 1 time in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”