我正在开发一款利用Android的 USB主机功能与基于微控制器的设备(PIC24FJ64GB002)进行通信的应用程序。该器件上有Microchip的USB协议栈,并已通过Windows测试确认。我现在正尝试通过Android应用程序与设备建立通信。到目前为止,我已经能够检测到设备(通过清单文件中的intent过滤器),打开设备并声明控制接口(接口0)。我知道我需要对设备进行一些初始化/设置,但我不知道该怎么做。例如,我知道对于Arduino USB-to-Serial来说......
conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
对于基于FTDI的Arduinos而言......
conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);
但我不知道如何在PIC24上设置Microchip USB协议栈。我到目前为止的代码如下所示...谢谢
MainActivity.java
public class MainActivity extends Activity
{
private static final int VID = 0x04D8;
private static final int PID = 0x000A;
public static int temperature;
public static int humidity;
public static int lpg;
public static int alcohol;
ListView listView;
//USB
UsbDevice USBDevice_s;
UsbDeviceConnection USBDeviceConnection_s;
UsbEndpoint USBEndpoint_s;
UsbInterface USBInterface_s;
UsbManager USBManager_s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{
ids[i] = Integer.toString(i+1);
}
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter);
//Begin USB process after layout has been configured...
USBManager_s = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = USBManager_s.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
{
USBDevice_s = deviceIterator.next();
}
Intent Intent_s = getIntent();
String action = Intent_s.getAction();
USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);
//If device is attached
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
{
USBInterface_s = USBDevice_s.getInterface(0);
USBEndpoint_s = USBInterface_s.getEndpoint(0);
USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
USBDeviceConnection_s.claimInterface(USBInterface_s, true);
Toast.makeText(this,"Value of device :" +USBDevice_s.getDeviceName(), Toast.LENGTH_LONG).show();
if (USBDeviceConnection_s != null && USBDeviceConnection_s.claimInterface(USBInterface_s, true))
{
Toast.makeText(this,"Device Open, Interface Claimed", Toast.LENGTH_LONG).show();
}
if (USBDeviceConnection_s != null)
{
//Perform setup/initialization.....
}
}
//If device is detached
else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
{
USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (USBDevice_s != null)
{
USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
USBDeviceConnection_s.releaseInterface(USBInterface_s);
USBDeviceConnection_s.close();
Toast.makeText(this,"Device Removed, Interface Released", Toast.LENGTH_LONG).show();
}
return;
}
//Update Layout
temperature = 1; humidity = 63; lpg = 5000; alcohol = 500;
Model.LoadModel();
ItemAdapter adapter2 = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter2);
adapter.notifyDataSetChanged();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT)
// .show();
//finish();
break;
default:
break;
}
return true;
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byrdonatwigge.sense"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.byrdonatwigge.sense.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>
device_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1240" product-id="10" />
</resources>