I would like to create an application to send data via USB from an Android application to a PC. My code is as follows:
package com.sample.dummy.app.senddatathoughserialport; import java.io.UnsupportedEncodingException; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbConstants; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbEndpoint; import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbDevice; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView tv = (TextView)findViewById(R.id.multiAutoCompleteTextView1); Button sButton = (Button) findViewById(R.id.button1); final UsbManager manager = (UsbManager) this.getSystemService(Context.USB_SERVICE); Toaster(""+manager);
And my Android Manifest Xml file looks like this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.sample.dummy.app.senddatathoughserialport" android:versionCode="1" android:versionName="1.0" > <uses-feature android:name="android.hardware.usb.host" /> <uses-sdk android:minSdkVersion="12" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sample.dummy.app.senddatathoughserialport.MainActivity" android:label="@string/app_name" > <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" /> <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> </application></manifest>
I have a problem with UsbDevice mDevice; , which always shows null Please give me an assistant to make it successful.
android usb
Dineshgaru
source share