我目前正在做一个项目,在这个项目中,我需要将心率传感器连接到Android设备上,并在一个基于Kivy的应用程序上可视化数据。
尝试使用serial.tools.list_ports.comports,但它在设备上没有返回任何内容。他们已经表示,该职能只支持有限的“否”。所以我认为它不能在Android上工作。
我已经在buildozer.spec中包含了所有所需的需求,并在白名单中添加了该模块(所以我在包含模块时没有任何问题):
# requirements of the app
requirements = kivy==master,python3crystax,hostpython3crystax,sdl2_image,sdl2_mixer,sdl2_ttf,sdl2_six,pyjnius,pyserial,serial我试图编辑AndroidManifest.xml文件的意图过滤部分,以获得我的应用程序的USB许可,但它也没有工作。
我在一个应用程序上检查过,该设备没有特定的名称,而是通过插入或插入USB进行更改,比如:
/dev/bus/usb/001/002 -> /dev/bus/usb/001/003 ->
还试图使用/dev/ttyUSB0/在其他页面中声明的通用名称,但没有效果。
返回的错误是对端口的权限被拒绝。
我如何列出连接到设备上的USB设备,最终打开一个端口与其通信?
更新#1:最近尝试使用一些pyjnius函数。下面是我写的代码:
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 我在这里叫self.device in main.py。它什么也不回。
更新2:我试图转换从self.usbMgr.getDeviceList()获得的HashMap,但没有。
更新3:我找到了一种返回插入USB设备名称的方法。
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)起作用了。目前我还没有带着USB传感器,我也不确定pySerial在Android上是否正常工作,但这将在周一进行测试。
发布于 2018-08-02 05:00:39
所以,我找到了一个解决办法,这个方法有点难看,但对我来说很管用。
我试着自己写一份文件,但我做不到,所以我搜索了更多的网络,发现:
通过使用这个库,我可以连接到我的设备并进行通信。
所以救恩一直在那里,虽然我看不见。
我给出的密码是:
values = self.usb_mgr.getDeviceList().values() valuesArray = values.toArray() #returns list in python deviceName = valuesArray[0].getDeviceName() self.device = str(deviceName)
工作(以一种丑陋的方式,因为我是一个Java新手)来获取设备的名称。
我意识到,pySerial无法通过自己的端口代码连接到设备,就像在非根设备中,您根本无法访问或更改/dev或其子目录。
https://stackoverflow.com/questions/51551081
复制相似问题