8
我想創建一個應用程序通過USB從Android應用程序發送數據到PC。我的代碼如下:通過USB從Android應用程序發送數據到PC
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);
// -- register click event with first button ---
sButton.setOnClickListener(new View.OnClickListener()
{
@SuppressWarnings({ "unused" })
public void onClick(View v)
{
try
{
Intent intent = new Intent("android.hardware.usb.action.USB_DEVICE_ATTACHED");
UsbDevice mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.ACTION_USB_DEVICE_ATTACHED);
Toaster(""+mDevice);
final UsbDeviceConnection connection = manager.openDevice(mDevice);
UsbInterface usbIf = null;
Toaster("Its ok here 1");
int count = mDevice.getInterfaceCount();
Toaster("Its ok here 1");
for(int i=0; i< count; i++)
{
usbIf = mDevice.getInterface(i);
}
UsbEndpoint epIN = null;
UsbEndpoint epOUT = null;
Toaster("It's ok here 2");
for (int i = 0; i < usbIf.getEndpointCount(); i++)
{
if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
epIN = usbIf.getEndpoint(i);
else
epOUT = usbIf.getEndpoint(i);
}
else
{
Log.d("USB","Not Bulk");
Toaster("Thedaa log daggara");
}
}
String get = tv.getText().toString();
try
{
byte[] str = get.getBytes(get);
connection.bulkTransfer(epOUT, str, str.length, 500);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Toaster("Data sending failed!");
}
finally
{
Toaster("Data sent through USB !");
}
}
catch(Exception e)
{
Log.d("Failed", e.toString());
Toaster("Attempt Failed !!");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Toaster(String string)
{
Toast.makeText(this, string,Toast.LENGTH_LONG).show();
}}
而且我Android
Manifest Xml
文件如下:
<?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>
我越來越問題UsbDevice mDevice;
會始終顯示的null
值。請給我助理,使它一個成功的應用。
我會檢查並給你反饋。 – Dineshgaru
另外我不知道你是否可以與你的電腦溝通,因爲ADB自動啓動自己的驅動程序。也許如果你嘗試一臺沒有adb的電腦並且使用自定義驅動程序,那麼可以完成。 – DaviF