USB Communication between android and PC
Asked Answered
F

1

7

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
Fuge answered 20/7, 2018 at 7:50 Comment(3)
"But when I tried above steps, I get device == null" We have no way of knowing what you may have done wrong since you haven't shown us your code.Hypersensitize
Are you familiar with ADB, I think you should have a look. Plus your question is too broad for the stack overflow platform.Plasmo
I read about ADB, but even before that I need to understand basics which I am not finding in blogs, websites anywhere. Also, For my usecase, I don't think ADB will be helpful for me. Can you please suggest me a good article that can answer my questions, I have already given up searching?Fuge
V
4

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://mcmap.net/q/1308973/-android-to-pc-usb-read-write

Visualize answered 20/7, 2018 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.