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
ADB
, I think you should have a look. Plus your question is too broad for the stack overflow platform. – Plasmo