如何通过USB将外部物理交换机与Android应用程序连接并计算每次点击

我正在开发一个项目,该项目将开发一个应用程序,该应用程序将通过USB端口与外部物理设备连接,并且每次按下切换按钮时,该应用程序中都会计入。

我是android开发的新手。我无法了解USB设备连接状态的广播接收器。 android 9.0。

我想知道如何将应用程序与USB设备连接,以及如何知道USB设备是否与我的应用程序连接。

在此先感谢您的帮助

这里是活动:

public class USB_Connect extends AppCompatactivity {

        private static final String actION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
        private static final String actION_USB_DetaCHED  = "android.hardware.usb.action.USB_DEVICE_DetaCHED";


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_usb__connect );

           BroadcastReceiver mReceiver = new BroadcastReceiver() {
               @Override
               public void onReceive(Context context,Intent intent) {
                   String action = intent.getaction();
                   if (UsbManager.actION_USB_DEVICE_ATTACHED.equals(action)) {
                       Log.d( "onAttached","Connected " );
                       Toast.makeText( context,"USB Connected",Toast.LENGTH_SHORT ).show();

                   }else if (UsbManager.actION_USB_DEVICE_DetaCHED.equals(action)) {
                       Log.d( "onDetached","Disconnected " );
                   }


               }
           };

            IntentFilter filter = new IntentFilter();
            filter.addaction( actION_USB_ATTACHED );
            filter.addaction( actION_USB_DetaCHED );
            this.registerReceiver( mReceiver,filter );


        }

        @Override
        protected void onResume() {
            super.onResume();

            Intent intent = getIntent();
            if (intent != null) {
                Log.d( "onResume","intent: " + intent.toString() );
                if (intent.getaction().equals( UsbManager.actION_USB_DEVICE_ATTACHED )) {
                    Toast.makeText( this,"device connected",Toast.LENGTH_SHORT ).show();

                    Log.d( "onResume","Connected " );
                }
            }
        }
    }

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aaa.powerconnection">
    <uses-feature android:name="android.hardware.usb.host" />

    <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=".USB_Connect"
            android:launchMode="singletask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DetaCHED" />
            </intent-filter>
            <meta-data  android:name=
                "android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
            <meta-data android:name=
                "android.hardware.usb.action.USB_DEVICE_DetaCHED"
                android:resource="@xml/device_filter" />

        </activity>
        <activity android:name=".Mainactivity">

        </activity>
    </application>

</manifest>
ouyangvs 回答:如何通过USB将外部物理交换机与Android应用程序连接并计算每次点击

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2919031.html

大家都在问