android: getDeviceList () всегда возвращает ноль - PullRequest
0 голосов
/ 13 февраля 2020

Я пытаюсь создать приложение android, которое может обнаруживать USB-устройства. 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) потребовало от меня дать ему разрешение на подключение, однако, это приложение всегда говорит " устройство не найдено ". Что мне делать? Пожалуйста, дайте мне несколько советов!

...