I was using pyserial for testing the Arduino connection on Linux via a USB cable, and it worked fine. However, after converting the project into an APK using Buildozer, I added USB permissions using this code i got deivce permission
from jnius import autoclass, cast
from android.permission import request_permissions, Permission
def find_arduino_device():
"""Find and return the Arduino device."""
try:
print("Initializing USB Manager...")
UsbManager = autoclass('android.hardware.usb.UsbManager')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
usb_manager = cast(UsbManager, activity.getSystemService(activity.USB_SERVICE))
# Request USB permissions
print("Requesting permissions for USB access...")
request_permissions([
"android.permission.USB_PERMISSION"
])
print("Fetching the list of connected USB devices...")
device_list = usb_manager.getDeviceList()
print(f"Device list retrieved: {device_list}")
# Loop through devices to find Arduino
for entry in device_list.entrySet():
device_name = entry.getKey()
device = entry.getValue()
vid = device.getVendorId()
pid = device.getProductId()
print(f"Device found: {device_name}, VID: {vid}, PID: {pid}")
# Replace with the correct Vendor ID and Product ID for your Arduino
if vid == 9025 and pid == 67:
print("Arduino device detected.")
print(f"Checking permissions for {device_name}...")
usb_manager.requestPermission(device, None)
time.sleep(5)
# Check for permission
if usb_manager.hasPermission(device):
print(f"Permission granted for device: {device_name}")
return device
I have successfully detected the device and obtained the necessary permissions. How can I establish a serial connection with a baud rate of 9600 to control the Arduino? I am struggling with this and would appreciate any suggestions.