USB Communication between android and PC

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 14:37:42

问题


Background :I have tried finding help on stackoverflow, android developers, and various other resources on web. This is my last hope. I am new to communication and starting with implementation of USB. Following are my questions:

1) When I connect my phone to windows PC, which one is host? Suppose I want to create an application that can send data, am I making my phone host?

2) For the case (Windows PC and Android phone), the other one would be a peripheral or a device? are they same?

3) From android developers website and a windows forum about USB, I understand that there are certain steps that need to be followed, and it's like - Create an instance of USBManager. - Create get list of devices - Choose a device from which you want to establish connection - Create an interface - get endpoint from that interface. - Create instance of DeviceConnections and call bulkTransfer method and send data.

But when I tried above steps, I get device == null. I don't even know if my understanding for above communication method is correct.

Can someone please help me understand and establish basic communication between a PC and a android phone and send at least "hello world".

Big thank you for even reading such a long question.

Here is code sample of what I have done. here devicelist returns null.

 public class MainActivity extends AppCompatActivity {

    android.widget.Button usbButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CustomUSBManager cmanager = new CustomUSBManager();

//1)        //Create instance of USB Manager using getSystemService
        final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

//2)        //Create a list of devices
        HashMap<String,UsbDevice> deviceList = manager.getDeviceList();

        Log.d("Devicelist = ", String.valueOf(deviceList.get(0)));

//3)        //Get a specific device from the list



//-----------------------------------------------------------------

Here is my manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.neeraj.usbcommunication1">
    <uses-feature android:name="android.hardware.usb.host"></uses-feature>

    <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>

</manifest> 
        final UsbDevice device = deviceList.get(0); //getting first device

回答1:


This will not work the way you want. The UsbManager.getDeviceList() is meant to be used with Android devices with an USB port (for example a tablet). However, you connect an Android device acting as a device to your PC acting as a host.

If you want to communicate between Android USB device and some USB host you need to use the accessory mode (https://developer.android.com/guide/topics/connectivity/usb/). But this mode requires special driver support on the USB host side (which is your PC).

Also note that getDeviceList() makes no sense in Accessory mode. This is because the connected accessory is a USB host, not a USB device.

Read more about this in this post: Android to PC USB Read/Write

Note that my answer is based on this answer: https://stackoverflow.com/a/14099963/5457878



来源:https://stackoverflow.com/questions/51437660/usb-communication-between-android-and-pc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!