java - android:getDeviceList() 总是返回 null
问题描述
我正在尝试创建能够检测 USB 设备的 android 应用程序。java和xml代码是这样的。
package com.example.serialtest;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private UsbManager mUsbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(USB_SERVICE);
mTextView = (TextView) findViewById(R.id.textview);
HashMap<String, UsbDevice> deviceList= mUsbManager.getDeviceList();
if(deviceList == null || deviceList.isEmpty()){
mTextView.setText("no device found");
}else{
String string = "";
for(String name : deviceList.keySet()){
string += name + "\n";
}
mTextView.setText(string);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.serialtest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-feature android:name="android.hardware.usb.host"
android:required="true"/>
</manifest>
一个 Android 设备(Xperia8,android 版本是 9)需要我给它连接权限,但是,这个应用程序总是说“找不到设备”。我应该怎么办?请给我一些建议!
解决方案
推荐阅读
- sftp - SFTP 在 linux 命令行上工作,但 filezilla 失败
- graphics - SV_Position 的 w 分量代表什么?SV_Position 是 Pixel Shader (DirectX 11) 的输入
- node.js - writable.write() 尝试在 writable.destroy() 流之后写入但在 writable.end() 之后给出时没有给出错误
- python - Flask 自定义验证错误 - TypeError:不支持的类型“
" - networking - ABAP AL11 目录不可见
- python - 在 CLI 中运行以及作为 os.system() 的参数时,相同的命令给出不同的结果
- pex - Intellitet Pex 参数化模拟
- flutter - 如何在 Flutter 中获取带有月视图的水平日历?
- java - Spring Security 如何在 SHA256 + salt 中加密传入的密码并将结果与数据库中的加密密码进行比较?
- apache-spark - pyspark 或 sparksql 中的日期转换