Konubinix' opinionated web of thoughts

Control Gpios From Android

Fleeting

using my python runtime on android

from the official driver

https://ftdichip.com/drivers/d2xx-drivers/

I downloaded the one marked with:

Android (Java D2XX) 2023-12-04 Java Driver 2.13 Rooting of Android device is not required. Refer to technical note TN_147. Refer to AN_357 for FT4222H-specific help.

I used the from the python android runtime to a custom app to put it in my apk without building again

# clk ipfs get [[https://ipfs.konubinix.eu/p/bafybeieh6nqr5cg3swgp73ds7522plzsdue44hnnxrjoh3kjbtkpqtc2rq?filename=d2xxx.zip][d2xxx.zip]]
# unzip d2xxx.zip
# mkdir -p dex_output
# d8 ./Android_Java_D2xx_2.13/d2xx.jar --output dex_output
baksmali d dex_output/classes.dex -o poc/smali/com/ftdi/

When apktool building, I get

  Exception in thread "main" brut.androlib.exceptions.AndrolibException: Could not smali folder: smali
	at brut.androlib.ApkBuilder.buildSourcesSmaliJob(SourceFile:53)
	at brut.androlib.ApkBuilder.lambda$buildSourcesSmali$0(SourceFile:207)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: com.android.tools.smali.util.ExceptionWithContext: Exception occurred while writing code_item for method Landroidx/annotation/InspectableProperty$ValueType;->values()[Landroidx/annotation/InspectableProperty$ValueType;
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeDebugAndCodeItems(SourceFile:1101)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeTo(SourceFile:409)
	at brut.androlib.ApkBuilder.buildSourcesSmaliJob(SourceFile:51)
	... 6 more
Caused by: com.android.tools.smali.util.ExceptionWithContext: Error while writing instruction at code offset 0x2
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeCodeItem(SourceFile:1394)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeDebugAndCodeItems(SourceFile:1097)
	... 8 more
Caused by: com.android.tools.smali.util.ExceptionWithContext: Unsigned short value out of range: 65907
	at com.android.tools.smali.dexlib2.writer.DexDataWriter.writeUshort(SourceFile:67)
	at com.android.tools.smali.dexlib2.writer.InstructionWriter.write(SourceFile:398)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeCodeItem(SourceFile:1354)
	... 9 more
[ Babel evaluation exited with code 1 ]

Trying with putting the jar directly in the sdcard and loading it dynamically (not tested yet)

from jnius import autoclass

# Android context
PythonActivity = autoclass('org.kivy.android.PythonActivity')
context = PythonActivity.mActivity

# Java classes
DexClassLoader = autoclass('dalvik.system.DexClassLoader')
File = autoclass('java.io.File')

# Paths
jar_path = str(context.getFilesDir().getAbsolutePath()) + "/d2xx.jar"
optimized_dir = str(context.getCacheDir().getAbsolutePath())  # or any writable dir

# Create DexClassLoader
loader = DexClassLoader(jar_path, optimized_dir, None, context.getClassLoader())

# Load your class
MyClass = loader.loadClass("com.ftdi.D2xxManager")
instance = MyClass.newInstance()

# Call a method (example: getDeviceCount)
method = MyClass.getMethod("getDeviceCount", [])
count = method.invoke(instance, [])
print("Connected devices:", count)

TODO from usb-serial-for-android

TODO using an esp32 as bridge