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

Re: Newby help request to get me started

Post by simon.schvartzman » Wed Aug 25, 2021 4:54 pm

Hi Paul, the good news: using your source code I was able to compile (point for you)

The bad news: when running on the cell (with a catch error command) it throws the following error:
error2.jpg
Monte (who seems to know everything there is to know about LCB :D ) gave a suggestion as follows:

Code: Select all

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

-- by
foreign handler JavaCheckIfBluetoothIsEnabled(in pAdapter as JObject) returns JBoolean binds to "java:android.bluetooth.BluetoothAdapter>isEnabled()Z"
which also allows to compile but gives exactly the same error (waiting for his feedback about it)
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 5:17 pm

Monte (who seems to know everything there is to know about LCB :D ) gave a suggestion as follows:
Yeah Monte, Ali, Trevor Devore have all been enormously helpful to me over the past few years providing clues to the LCB FFI enigma. Much gratitude to those guys!

That's almost identical to what I did, but I'm wondering why he took the "optional" out, which allows for a null/void ("nothing" in LCB) to be returned.

I'm currently trying to get my Android battery status code working again, which lives here if anyone would like to look at it:
https://github.com/PaulMcClernan/LCAndroidBatteryLib
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 » Wed Aug 25, 2021 5:36 pm

To your remark
I'm wondering why he took the "optional" out, which allows for a null/void ("nothing" in LCB) to be returned.
this was Monte's explanation for his suggestion
The issue being as an instance method you must include the instance object as the first parameter.
hope it helps to answer your doubt
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 11:15 pm

simon.schvartzman wrote:
Wed Aug 25, 2021 5:36 pm
To your remark
I'm wondering why he took the "optional" out, which allows for a null/void ("nothing" in LCB) to be returned.
this was Monte's explanation for his suggestion
The issue being as an instance method you must include the instance object as the first parameter.
hope it helps to answer your doubt
Ah I see the mistake now, my intention was to add an if/then check to ensure that the BT Adapter object was actually retrieved properly before trying to pass it but then the 'optional' is not needed when passing it anyway. For that checking for 'nothing' that 'optional' should be in the first call that returns the object:

Code: Select all

 foreign handler JavaGetDefaultBluetoothAdapter() returns optional JObject binds to "java:android.bluetooth.BluetoothAdapter>getDefaultAdapter()Landroid/bluetooth/BluetoothAdapter;!static" 
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 » Thu Aug 26, 2021 1:13 pm

Ah I see the mistake now, my intention was to add an if/then check to ensure that the BT Adapter object was actually retrieved properly before trying to pass it but then the 'optional' is not needed when passing it anyway. For that checking for 'nothing' that 'optional' should be in the first call that returns the object:

Code: Select all

foreign handler JavaGetDefaultBluetoothAdapter() returns optional JObject binds to "java:android.bluetooth.BluetoothAdapter>getDefaultAdapter()Landroid/bluetooth/BluetoothAdapter;!static" 
Wow, this to me is becoming more and more complex with each post :(
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 » Thu Aug 26, 2021 10:08 pm

simon.schvartzman wrote:
Thu Aug 26, 2021 1:13 pm
Wow, this to me is becoming more and more complex with each post :(
Hey man, don't get cold feet now!

The 'optional' LCB keyword just tells the compiler that the variable can contain something or "nothing", which is another LCB keyword. It might help if you think of 'nothing' as the LCB equivalent to 'empty' in LiveCode Script.
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 » Fri Aug 27, 2021 2:12 pm

Paul, no way I was going to find out this myself...in any case I really appreciate your help and I'm sure is going to be useful in the future (if I ever adventure my self into this universe...)

See below last Monte's input which made it work!
I couldn't figure out the issue myself at first so I tested and had the same exception. After reviewing the docs I realised there were two issues:

1. `getDefaultAdapter` is documented to return `null` if the device does not support bluetooth at all so we need to check for that
2. `isEnabled` is documented to require the `android.permission.BLUETOOTH` permission added to the manifest. We can add some metadata to the LCB file to ensure the standalone builder does this when we include the extension.

So here's my patched file:

Code: Select all

library community.livecode.ss.btstatus

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

metadata android.permissions is "BLUETOOTH"

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

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

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

public handler CheckIfBluetoothIsEnabled() returns Boolean
	unsafe
		variable tBluetoothAdapter as optional JObject
		put JavaGetDefaultBluetoothAdapter() into tBluetoothAdapter
		if tBluetoothAdapter is nothing then
			return false
		end if

		variable tIsEnabled as Boolean
		put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
		return tIsEnabled
	end unsafe
end handler
end library
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 » Sat Aug 28, 2021 2:45 am

simon.schvartzman wrote:
Fri Aug 27, 2021 2:12 pm
Paul, no way I was going to find out this myself...in any case I really appreciate your help and I'm sure is going to be useful in the future (if I ever adventure my self into this universe...)

See below last Monte's input which made it work!
I couldn't figure out the issue myself at first so I tested and had the same exception. After reviewing the docs I realised there were two issues:

1. `getDefaultAdapter` is documented to return `null` if the device does not support bluetooth at all so we need to check for that
2. `isEnabled` is documented to require the `android.permission.BLUETOOTH` permission added to the manifest. We can add some metadata to the LCB file to ensure the standalone builder does this when we include the extension.

So here's my patched file:

Code: Select all

library community.livecode.ss.btstatus

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

metadata android.permissions is "BLUETOOTH"

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

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

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

public handler CheckIfBluetoothIsEnabled() returns Boolean
	unsafe
		variable tBluetoothAdapter as optional JObject
		put JavaGetDefaultBluetoothAdapter() into tBluetoothAdapter
		if tBluetoothAdapter is nothing then
			return false
		end if

		variable tIsEnabled as Boolean
		put JavaCheckIfBluetoothIsEnabled(tBluetoothAdapter) into tIsEnabled
		return tIsEnabled
	end unsafe
end handler
end library
Excelente! I guessed that permissions was a factor! And "returns optional JObject" is in there too! My guessing is getting better!

I hadn't forgotten about this, I've been busy re-setting up several machines I work on to try to do some Android dev again.
I've also been reading up on the fundamental concepts of the Android system, which has some unique characteristics that are a bit foreign to me.
https://developer.android.com/guide/com ... tals?hl=en
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Newby help request to get me started

Post by SparkOut » Sat Aug 28, 2021 8:59 am

I've been reading this thread with interest and congratulations and thanks and kudos are due all round. Well done. I would love to learn LCB properly and how to bind FFIs, apart from having almost non-existent chance, it is hard to find entry points.
PaulDaMacMan wrote:
Sun Aug 22, 2021 6:16 pm
Happy to help, I wish more people would dive in, the water is a little bit cold but you'll get used to it!
More like "your skull will recover from the cracks endured when smashing into the ice!" :lol:

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 29, 2021 3:39 am

SparkOut wrote:
Sat Aug 28, 2021 8:59 am
I would love to learn LCB properly and how to bind FFIs, apart from having almost non-existent chance, it is hard to find entry points.
I agree, info on LiveCode Builder is scattered around in various in places, and some of the older examples are broken, like that Battery code from a now old blog post, due to things like changes in Android. Language guide, dictionary, and built in LCB samples are probably the most important sources.
It was difficult to get started for me. I had to (and still have to) learn a bunch of Objective C / C to use Apple APIs, and Android / Java is different from that, another hurtle. Personally I think it's worth the effort and gets easier the more you do it.
There is a command line tool that comes with LC that can automatically generate binding strings for Java stuff, but I've yet to try to used that.

FYI, I tried to test this in an emulated Android device, but it turns out that Android emulator does not have emulated bluetooth capabilities. I built a test app and installed it on my phone (Android 9) which worked fine.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”