I'm currently working on a project where I need to connect a heart rate sensor to an Android device and visualize the data on a Kivy-based app.
Tried to use serial.tools.list_ports.comports
but it returned nothing on the device. They have already stated that the function supports only a limited no. of operating systems so I thought that it wouldn't work on Android.
I have included all the needed requirements in buildozer.spec and whitelisted termios(so I don't have any problems with including the module):
# requirements of the app
requirements = kivy==master,python3crystax,hostpython3crystax,sdl2_image,sdl2_mixer,sdl2_ttf,sdl2_six,pyjnius,pyserial,serial
I tried to edit the AndroidManifest.xml file's intent-filter part to get my app to have USB permission, but it didn't work either.
I've checked on an app that the device does not have a specific name but a changing one that changes by plug the USB in and out like:
/dev/bus/usb/001/002 --> /dev/bus/usb/001/003 --> ...
Also tried to use a generic name /dev/ttyUSB0/
stated in other pages yet to no avail.
The error returned is that the permission is denied to the port.
How can I even list the USB devices connected to the device, ultimately opening a port to communicate with it?
UPDATE #1: I recently tried to use some pyjnius functions. Here's the code that I've written:
from jnius import autoclass
from jnius import cast #cast?
class Serial:
def __init__(self):
#Defining the variables for USB-OTG comm. in Android.
#The strings in autoclasses are classes in Java
#therefore classes that are used in Android programming.
self.arrayList = autoclass('java.util.ArrayList')
self.hashmap = autoclass('java.util.HashMap')
self.context = autoclass('android.content.Context')
self.usbConstants = autoclass('android.hardware.usb.UsbConstants')
self.usbDevice = autoclass('android.hardware.usb.UsbDevice')
self.usbDeviceConnection = autoclass('android.hardware.usb.UsbDeviceConnection')
self.hashMap = autoclass('java.util.HashMap')
self.usbEndpoint = autoclass('android.hardware.usb.UsbEndpoint')
self.usbManager = autoclass('android.hardware.usb.UsbManager')
self.usbRequest = autoclass('android.hardware.usb.UsbRequest')
self.usbInterface = autoclass('android.hardware.usb.UsbInterface')
self.a = 0
#As we're using python-for-android, we'll be needing only
#the default PythonActivity class.
self.PythonActivity = autoclass('org.kivy.android.PythonActivity')
self.activity = self.PythonActivity.mActivity
#Getting access to make the device a host for USB devices.
self.usbMgr = cast(self.usbManager, self.activity.getSystemService(self.context.USB_SERVICE))
self.device = self.usbMgr.getDeviceList().get("deviceName")
def portName(self):
return self.device
where I call self.device
in main.py
. It returns None.
UPDATE #2:
I tried to convert the HashMap I get from self.usbMgr.getDeviceList()
but couldn't. I only get
<java.util.HashMap at 0xdf8a6150 jclass=java/util/HashMap jself=<LocalRef obj=0x10134a at 0xdfa69760>>>
UPDATE #3: I found a method to return the plugged-in USB devices' names.
values = self.usb_mgr.getDeviceList().values() #returns Collection
valuesArray = values.toArray() #returns list in python
deviceName = valuesArray[0].getDeviceName() #[0] for testing
self.device = str(deviceName)
That worked. I currently do not have the USB sensor with me and I'm unsure whether pySerial works properly on Android but that'll get tested on Monday.