Creating an extension library with ChatGPT

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

Post Reply
trevix
Posts: 979
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Creating an extension library with ChatGPT

Post by trevix » Fri Jun 14, 2024 8:43 pm

Having the need to list, on a Android device, the names (SSIDs) of available WiFi (all), I decided to give it a try to build it with the help of ChatGPT.
Amazingly, I got back a lot of stuff that seemed like a complete set of instructions for using Livecode builder.
PS: Then I've got stuck on Step 4

This was my question to chatGPT:
Please write the code, to create a Livecode script builder extension for Livecode.
The LCB extension should be used on Android 10 or android 11, in order to return a list of all the available WiFi names (SSID's) on the device which runs the Livecode standalone. The list of wifi names must include both those to which you have already been connected and those that have not. The extension should include all the code to make it working and all its Livecode dictionary entries, which explain how to use it.
An this was the answer:
To create a LiveCode Builder (LCB) extension that returns a list of all available WiFi SSIDs on an Android device, we need to write a combination of LCB and Java code. The LCB code will call the Java methods, which interact with Android APIs to fetch the WiFi SSIDs. Here's a step-by-step guide:

Step 1: Create the Project Structure
Create Project Directory:
Create a directory named WiFiExtension.

Create Directory Structure:
Inside WiFiExtension, create the following subdirectories:

android
resources
Step 2: Write the Java Code
Inside the android directory, create a file named WiFiHelper.java with the following content:

Code: Select all

package com.example.wifiextension;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.ScanResult;
import java.util.List;
import java.util.ArrayList;

public class WiFiHelper {
    private WifiManager wifiManager;

    public WiFiHelper(Context context) {
        this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    public String[] getAvailableSSIDs() {
        List<ScanResult> scanResults = wifiManager.getScanResults();
        List<String> ssidList = new ArrayList<>();

        for (ScanResult scanResult : scanResults) {
            if (scanResult.SSID != null && !scanResult.SSID.isEmpty()) {
                ssidList.add(scanResult.SSID);
            }
        }
        return ssidList.toArray(new String[0]);
    }
}
Step 3: Write the LiveCode Builder (LCB) Code
In the WiFiExtension directory, create a file named WiFiExtension.lcb with the following content:

Code: Select all

module WiFiExtension

use com.livecode.foreign
use com.livecode.system
use com.livecode.mobile

private variable mWiFiHelper as optional javalibrary

metadata title is "WiFiExtension"
metadata author is "Your Name"
metadata version is "1.0"
metadata platform is "android"
metadata description is "A LiveCode extension to fetch available WiFi SSIDs."

public handler Initialize
    if the environment is not "android" then
        log "WiFiExtension can only be used on Android."
        exit to top
    end if

    if mWiFiHelper is empty then
        put foreign library "com.example.wifiextension.WiFiHelper" into mWiFiHelper
        put the result into mWiFiHelper
        if mWiFiHelper is empty then
            log "Failed to load WiFiHelper library."
            exit to top
        end if
    end if
end handler

public handler GetAvailableSSIDs() returns list
    variable context as object
    put mobileGetContext() into context
    
    variable wifiHelper as object
    put mWiFiHelper.create(context) into wifiHelper
    
    variable ssids as list
    put wifiHelper.getAvailableSSIDs() into ssids
    
    return ssids
end handler

end module
Step 4: Compile the Java Code
Set up the Android SDK: Make sure you have the Android SDK installed and configured.

Compile the Java Code:
Navigate to the WiFiExtension/android directory and compile the WiFiHelper.java file to generate the WiFiHelper.class file.

Create a JAR file:

Code: Select all

jar cf WiFiHelper.jar com/example/wifiextension/WiFiHelper.class
Step 5: Package the Extension
Copy the JAR File:
Copy the WiFiHelper.jar file to the WiFiExtension/resources directory.

Create Extension Manifest:
In the WiFiExtension directory, create a file named WiFiExtension.lce with the following content:

Code: Select all

<widget name="WiFiExtension"
        version="1.0"
        license="MIT"
        platforms="android"
        author="Your Name"
        description="A LiveCode extension to fetch available WiFi SSIDs.">

    <libraries>
        <library src="resources/WiFiHelper.jar" platform="android" />
    </libraries>

    <components>
        <component name="WiFiExtension" />
    </components>
</widget>
Step 6: Load and Use the Extension in LiveCode
What follows is how to use the LC Extension manager, with I knew.

Ok, the problem is that I have no idea how and if I can compile Java code (I am on OSX Sonoma, LC10).
I follow all the instructions but when I type on the terminal:

Code: Select all

javac com/example/wifiextension/WiFiHelper.java
even if the file is there I get back what follows, which I don't understand.
Any help would be appreciated.
Thanks
com/example/wifiextension/WiFiHelper.java:3: error: package android.content does not exist
import android.content.Context;
^
com/example/wifiextension/WiFiHelper.java:4: error: package android.net.wifi does not exist
import android.net.wifi.WifiManager;
^
com/example/wifiextension/WiFiHelper.java:5: error: package android.net.wifi does not exist
import android.net.wifi.ScanResult;
^
com/example/wifiextension/WiFiHelper.java:10: error: cannot find symbol
private WifiManager wifiManager;
^
symbol: class WifiManager
location: class WiFiHelper
com/example/wifiextension/WiFiHelper.java:12: error: cannot find symbol
public WiFiHelper(Context context) {
^
symbol: class Context
location: class WiFiHelper
com/example/wifiextension/WiFiHelper.java:13: error: cannot find symbol
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
^
symbol: class WifiManager
location: class WiFiHelper
com/example/wifiextension/WiFiHelper.java:13: error: cannot find symbol
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
^
symbol: variable Context
location: class WiFiHelper
com/example/wifiextension/WiFiHelper.java:17: error: cannot find symbol
List<ScanResult> scanResults = wifiManager.getScanResults();
^
symbol: class ScanResult
location: class WiFiHelper
com/example/wifiextension/WiFiHelper.java:20: error: cannot find symbol
for (ScanResult scanResult : scanResults) {
^
symbol: class ScanResult
location: class WiFiHelper
9 errors
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 979
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Creating an extension library with ChatGPT

Post by trevix » Mon Jun 17, 2024 6:53 pm

TATAAAAA!!!
After numorous web searches, I was able, on my OSX Sonoma, to fix all the various Java paths, JAVA_HOME,ANDROID_SDK_ROOT,.zshrc, etc...
And to compile the java file "WiFiHelper.java" into the needed "WiFiHelper.jar" !!!

What I want to achive is to create a library extension in order to list the available WiFi names, on Android 10 and 11.

It is quite difficult to find informations on LCB: they are old, confused and disseminated on the web.
This is probably why I am so confused on how the extension directory should be arranged.

With what I came up, I tried to use the Extension builder to create the extension, but I get one of these two errors:
7:06 PM: Error: Error: missing manifest /Users/trevix/Documents/AAA_LavoriInCorso/Segnapunto/Segnapunto soft/TEST stacks/WiFiSSIDs/WiFiExtension/manifest.xml
7:06 PM: Error: no name metadata found in /Users/trevix/Documents/AAA_LavoriInCorso/Segnapunto/Segnapunto soft/TEST stacks/WiFiSSIDs/WiFiExtension/WiFiExtension.lcb
The manifest file disappear when I build the extension. As for the "no name metadata", all my metadata are on the .lcb file, so it's a real mystery.

Find what I did, up to now, on the attachment.
Any help would be appreciated.
Trevix
Attachments
WiFiExtension.zip
(13.44 KiB) Downloaded 12 times
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “LiveCode Builder”