Android Base Class¶
The Android class is the core of QSL4A, providing the connection to the Android runtime and RPC mechanism.
Module Import¶
# Full featured (recommended)
import androidhelper
droid = androidhelper.Android()
# Minimal version (quick, single instance)
import android
droid = android.droid
Class: Android¶
Constructor¶
Parameters:
- addr (tuple, optional): (HOST, PORT) address. If None, uses environment variables AP_HOST and AP_PORT
Environment Variables:
- AP_HOST - Server host address
- AP_PORT - Server port
- AP_HANDSHAKE - Authentication token
Core Methods¶
_rpc()¶
Internal RPC method for calling Android functions.
Parameters:
- method (str): Method name to call
- *args: Variable arguments for the method
Returns: Result namedtuple with fields:
- id (int): Request ID
- result: Method return value
- error (str or None): Error message if failed
getattr()¶
Dynamic method dispatcher. All Android methods are accessed via attribute lookup.
Standalone Functions (android.py)¶
When using the minimal android module:
jsla()¶
Send JSON-RPC request and return raw response.
Returns: JSON string response
rsla()¶
Send request and return result only.
Returns: Result value from RPC call
esla()¶
Send request, raise exception on error.
Raises: Exception if error field is not None
nsla()¶
Send request and return Result namedtuple.
Returns: Result namedtuple
Usage Examples¶
Basic Connection¶
import androidhelper
# Connect to Android runtime
droid = androidhelper.Android()
# Check connection by showing toast
droid.makeToast("Connected!")
Error Handling¶
result = droid.someMethod()
if result.error:
print(f"Error: {result.error}")
else:
print(f"Success: {result.result}")