简介
QtActivity 作为 Qt 应用程序的入口点,负责启动和配置 Qt 应用程序的信息, 后面我们继承 QtActivity 做自定义控制,了解一下 Activity 生命周期概念, 因为 QtActivity 继承自Android的activity,使用周期函数完成我们的任务。
步骤
1. 获取UsbManager
参考 UsbManager(apiref.com), 通过下面代码获取USB服务
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
2. 获取getDeviceList
UsbManager 对象接口中, 我们使用如下代码获取usb设备列表 HashMap<String, UsbDevice> getDeviceList()
3. UsbDevice
UsbDevice接口如下, 打印设备的PID、VID、SN等基本信息即可
接口参考文档 USBDevice 接口说明文档
<class name="android/hardware/usb/UsbDevice" since="12"><extends name="java/lang/Object"/><implements name="android/os/Parcelable"/><method name="getConfiguration(I)Landroid/hardware/usb/UsbConfiguration;" since="21"/><method name="getConfigurationCount()I" since="21"/><method name="getDeviceClass()I"/><method name="getDeviceId()I"/><method name="getDeviceId(Ljava/lang/String;)I"/><method name="getDeviceName()Ljava/lang/String;"/><method name="getDeviceName(I)Ljava/lang/String;"/><method name="getDeviceProtocol()I"/><method name="getDeviceSubclass()I"/><method name="getInterface(I)Landroid/hardware/usb/UsbInterface;"/><method name="getInterfaceCount()I"/><method name="getManufacturerName()Ljava/lang/String;" since="21"/><method name="getProductId()I"/><method name="getProductName()Ljava/lang/String;" since="21"/><method name="getSerialNumber()Ljava/lang/String;" since="21"/><method name="getVendorId()I"/><method name="getVersion()Ljava/lang/String;" since="23"/><field name="CREATOR"/></class>
4. 代码
package usb;import java.util.HashMap;
import android.os.Bundle;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDevice;public class USBListActivity extends org.qtproject.qt.android.bindings.QtActivity
{private UsbManager m_usbManager;public USBListActivity(){}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);m_usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);getDeviceList();}public void getDeviceList(){HashMap<String, UsbDevice> deviceList = m_usbManager.getDeviceList();System.out.println("------------------------->GetDeviceList<-------------------------");deviceList.forEach((key, value) -> {System.out.println("------------------------->" + key + "<-------------------------");System.out.println(value);System.out.println("<-------------------------" + key + "------------------------->");});System.out.println("<-------------------------GetDeviceList------------------------->");}
}
5. 设置你的代码为启动入口
编辑 AndroidManifest.xml, 将
android:name=“org.qtproject.qt.android.bindings.QtActivity”
改为
android:name=“usb.USBListActivity”
代码目录如下
6. 收集设备连接
我使用OTG线连接了USB扩展线, 在扩展上接了两个同样型号的USB转TTL线, 如下图
7. 运行结果
I System.out: ------------------------->GetDeviceList<-------------------------
I System.out: ------------------------->/dev/bus/usb/001/008<-------------------------
I System.out: UsbDevice[mName=/dev/bus/usb/001/008,mVendorId=6790,mProductId=29987,mClass=255,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=USB2.0-Serial,mVersion=2.54,mSerialNumberReader=android.hardware.usb.IUsbSerialReader S t u b Stub StubProxy@a1e1d4b, mHasAudioPlayback=false, mHasAudioCapture=false, mHasMidi=false, mHasVideoCapture=false, mHasVideoPlayback=false, mConfigurations=[
I System.out: UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=48,mInterfaces=[
I System.out: UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=255,mSubclass=1,mProtocol=2,mEndpoints=[
I System.out: UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=32,mInterval=0]
I System.out: UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=32,mInterval=0]
I System.out: UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=1]]]]
I System.out: <-------------------------/dev/bus/usb/001/008------------------------->
I System.out: ------------------------->/dev/bus/usb/001/007<-------------------------
I System.out: UsbDevice[mName=/dev/bus/usb/001/007,mVendorId=6790,mProductId=29987,mClass=255,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=USB2.0-Serial,mVersion=2.54,mSerialNumberReader=android.hardware.usb.IUsbSerialReader S t u b Stub StubProxy@ec18328, mHasAudioPlayback=false, mHasAudioCapture=false, mHasMidi=false, mHasVideoCapture=false, mHasVideoPlayback=false, mConfigurations=[
I System.out: UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=48,mInterfaces=[
I System.out: UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=255,mSubclass=1,mProtocol=2,mEndpoints=[
I System.out: UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=32,mInterval=0]
I System.out: UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=32,mInterval=0]
I System.out: UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=1]]]]
I System.out: <-------------------------/dev/bus/usb/001/007------------------------->
I System.out: <-------------------------GetDeviceList------------------------->
andoid开发文档
UsbManager(apiref.com)
UsbManager android-doc.com
UsbManager(matools.com)
Activity 生命周期概念
Qt for android 错误处理
Qt for Android 乱码问题
其他
Qt for android 文档(thinkinginqt.com)
Qt for android 官方文档(Qt6.2LTS)
Qt Project to Manifest Configuration