Skip to content

Instantly share code, notes, and snippets.

@kshoji
Created December 4, 2012 03:03
Show Gist options
  • Save kshoji/4200175 to your computer and use it in GitHub Desktop.
Save kshoji/4200175 to your computer and use it in GitHub Desktop.
Android Advent Calender 2012
@Override
protected void onDestroy() {
super.onDestroy();
if (deviceDetachedReceiver != null) {
unregisterReceiver(deviceDetachedReceiver);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
deviceDetachedReceiver = new UsbDeviceDetachedReceiver(this);
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(deviceDetachedReceiver, filter);
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.kshoji.driver.hid"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".activity.DriverSampleActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>
</manifest>
private class WaiterThread extends Thread {
private byte[] readBuffer = new byte[64];
public boolean stopFlag;
private Handler receiveHandler;
public WaiterThread(Handler handler) {
stopFlag = false;
this.receiveHandler = handler;
}
@Override
public void run() {
while (true) {
synchronized (this) {
if (stopFlag) {
return;
}
}
if (inputEndpoint == null) {
continue;
}
int length = deviceConnection.bulkTransfer(inputEndpoint, readBuffer, readBuffer.length, 0);
if (length > 0) {
byte[] read = new byte[length];
System.arraycopy(readBuffer, 0, read, 0, length);
Message message = new Message();
message.obj = read;
receiveHandler.sendMessage(message);
}
}
}
}
@Override
public void onDataTransfer(byte[] bytes) {
final int maxPacketSize = outputEndpoint.getMaxPacketSize();
for (int i = 0; i < bytes.length; i += maxPacketSize) {
byte[] writeBuffer = new byte[maxPacketSize];
int length = (bytes.length - i) < writeBuffer.length ? bytes.length - i : writeBuffer.length;
System.arraycopy(bytes, i, writeBuffer, 0, length);
deviceConnection.bulkTransfer(outputEndpoint, writeBuffer, writeBuffer.length, 0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Interface class: USB HID -->
<usb-device class="3" subclass="0" protocol="0" />
</resources>
#include "mbed.h"
#include "USBHID.h"
// device
USBHID hid;
// read
HID_REPORT recv;
BusOut leds(LED1, LED2, LED3, LED4);
// write
HID_REPORT send;
AnalogIn input(p15);
int main(void) {
int lastData = 0;
int currentData;
send.length = 64;
while (1) {
// read
hid.readNB(&recv);
if (recv.length > 0) {
leds = recv.data[0];
}
currentData = input.read_u16();
if (currentData != lastData) {
lastData = currentData;
// write
send.data[0] = (currentData >> 8) & 0xff;
send.data[1] = currentData & 0xff;
hid.send(&send);
}
}
}
public class UsbDeviceDetachedReceiver extends BroadcastReceiver {
private OnDeviceDetachedListener onDeviceDetachedListener;
public UsbDeviceDetachedReceiver(OnDeviceDetachedListener onDeviceDetachedListener) {
this.onDeviceDetachedListener = onDeviceDetachedListener;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
if (onDeviceDetachedListener != null) {
onDeviceDetachedListener.onDeviceDetached(device);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment