How to connect android device to pc host in accessory mode

34 Views Asked by At

I want connect android device to pc host in accessory mode ,and send some data from pc to android .

I read the developer's doc . It's very confused. which mode I should configure my android app? USB Device ? USB Host? USB Accessory ?
Initially, I thought I should configure my android app with Accessory mode . I add

<uses-feature android:name="android.hardware.usb.accessory" /> to manifest and  activity add 
     
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
        </intent-filter>

        <meta-data
            android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
            android:resource="@xml/accessory_filter" />

but when I runing this code :

val usbManager = context.getSystemService(USB_SERVICE) as UsbManager
var usbA = usbManager.accessoryList

I get usbA =null

so I modify my code :

       <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data               android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
val usbManager = context.getSystemService(USB_SERVICE) as UsbManager
val deviceMap: HashMap<String, UsbDevice> = usbManager.deviceList

but the result is still empty

Does the prerequisite for this code to run normally is that I must have a windows application to call up the windows usb service? At this stage I just want to discover the device.

Any help is greatly appreciated

0

There are 0 best solutions below