Last active
January 1, 2016 05:59
-
-
Save yamato8/8102411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.hidtest002; | |
import android.hardware.usb.UsbConstants; | |
import android.hardware.usb.UsbDevice; | |
import android.hardware.usb.UsbDeviceConnection; | |
import android.hardware.usb.UsbEndpoint; | |
import android.hardware.usb.UsbInterface; | |
import android.hardware.usb.UsbManager; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class MainActivity extends Activity implements OnClickListener, OnDeviceEventListener { | |
public static class MyThread2 extends Thread{ | |
private byte[] readBuffer = new byte[64]; | |
public boolean stopFlag; | |
private static MainActivity con; | |
//private Handler receiveHandler; | |
public MyThread2(MainActivity mainActivity) { | |
// TODO 自動生成されたコンストラクター・スタブ | |
Log.v("MyThread2", "コンストラクタ"); | |
MyThread2.con = mainActivity; | |
} | |
private static Handler handler = new Handler() { | |
public void handleMessage(Message msg) { | |
//Log.v("handleMessage", msg.toString()); | |
con.onDataTransfer((byte[]) msg.obj); | |
} | |
}; | |
public void run() { | |
Log.v("MyThread2", "run"); | |
while (true) { | |
synchronized (this) { | |
if (stopFlag) { | |
return; | |
} | |
} | |
if (inputEndpoint2 == null) { | |
continue; | |
} | |
int length = deviceConnection.bulkTransfer(inputEndpoint2, 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; | |
handler.sendMessage(message); | |
} | |
} | |
} | |
} | |
private static final String TAG = "HidTest"; | |
private TextView text; | |
private static UsbDeviceConnection deviceConnection = null; | |
private UsbEndpoint outputEndpoint; | |
private InputDevice inputDevice = null;//// | |
private UsbEndpoint outputEndpoint2; | |
private static UsbEndpoint inputEndpoint2; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
text = (TextView) findViewById(R.id.textView1); | |
// ボタンがクリックされた時の処理 | |
Button btn1 = (Button) findViewById(R.id.button1); | |
btn1.setOnClickListener(this); | |
Button btn2 = (Button) findViewById(R.id.button2); | |
btn2.setOnClickListener(this); | |
// USB ディバイスが接続されていない時は終了 | |
if (!UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) { | |
finish(); | |
} else { | |
// USB ディバイスを接続 | |
Intent intent = getIntent(); | |
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); | |
if (device != null) { | |
Log.v(TAG, "UsbDevice がある"); | |
UsbInterface usbInterface = findUsbInterface(device); | |
if (usbInterface != null) { | |
// アタッチ | |
// Mbed へ送信 | |
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); | |
deviceConnection = usbManager.openDevice(device); | |
deviceConnection.claimInterface(usbInterface, true); | |
// Mbed からの受信 | |
//inputDevice = new InputDevice(deviceConnection, usbInterface, this); | |
//inputDevice.start(); | |
//(new Thread(new myThread())).start(); | |
//MyThread thread1 = new MyThread(deviceConnection, usbInterface, this); | |
//thread1.start(); | |
MyThread2 thread2 = new MyThread2(this); | |
thread2.start(); | |
} | |
} | |
} | |
} | |
/* | |
* UsbInterface の確認 | |
*/ | |
private UsbInterface findUsbInterface(UsbDevice device) { | |
// TODO 自動生成されたメソッド・スタブ | |
text.setText("UsbDevice.getDeviceName():" + device.getDeviceName() + "\n"); | |
int count = device.getInterfaceCount();// 4 | |
text.append("UsbDevice.getInterfaceCount():" + count + "\n"); | |
for (int i = 0; i < count; i++) { | |
UsbInterface usbInterface = device.getInterface(i); | |
UsbEndpoint inputEndpoint = null; | |
UsbEndpoint outputEndpoint = null; | |
text.append("**************************" + "\n"); | |
text.append(" UsbInterface.getEndpointCount():" + usbInterface.getEndpointCount() + "\n"); | |
if (usbInterface.getEndpointCount() >= 1) { | |
for (int endpointIndex = 0; endpointIndex < usbInterface.getEndpointCount(); endpointIndex++) { | |
UsbEndpoint endpoint = usbInterface.getEndpoint(endpointIndex); | |
text.append(endpointIndex + ":"); | |
if ((endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK || endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) { | |
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { | |
outputEndpoint = outputEndpoint == null ? endpoint : outputEndpoint; | |
outputEndpoint2 = endpoint; | |
text.append(" UsbEndpoint:getDirection()" + endpoint.getDirection() + "\n");// USB_DIR_OUT | |
}else if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) { | |
inputEndpoint = endpoint; | |
inputEndpoint2 = endpoint; | |
} | |
} | |
} | |
if (inputEndpoint != null || outputEndpoint != null) { | |
return usbInterface; | |
} | |
} | |
} | |
return null; | |
} | |
/* | |
* ボタンが押された時の処理 | |
*/ | |
@Override | |
public void onClick(View v) { | |
// TODO 自動生成されたメソッド・スタブ | |
byte data1 = 0; | |
byte data2 = 0; | |
byte data3 = 0; | |
byte data4 = 0; | |
switch (v.getId()) { | |
case R.id.button1: | |
Log.i("buttonTest", "点灯ボタンが押された"); | |
data1 = 0; | |
data2 = 0; | |
data3 = 0; | |
data4 = (byte) 8; | |
break; | |
case R.id.button2: | |
Log.i("buttonTest", "消灯ボタンが押された"); | |
data1 = 0; | |
data2 = 0; | |
data3 = 0; | |
data4 = 0; | |
break; | |
} | |
outPutMbed(new byte[] { (byte) (data1 | data2 | data3 | data4) }); | |
} | |
// Mbed に送信 | |
private void outPutMbed(byte[] bytes) { | |
// TODO 自動生成されたメソッド・スタブ | |
final int maxPacketSize = outputEndpoint2.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(outputEndpoint2, writeBuffer, writeBuffer.length, 0); | |
} | |
} | |
/* | |
* メニューの処理 | |
*/ | |
@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.itemFinish: | |
Log.v(TAG, "finish"); | |
finish(); | |
return true; | |
} | |
return false; | |
} | |
//OnDeviceEventListener | |
@Override | |
public void onDataTransfer(byte[] bytes) { | |
// TODO 自動生成されたメソッド・スタブ | |
int value = ((0xff & bytes[0]) << 8) | (0xff & bytes[1]); | |
//seekBar1.setProgress(value); | |
Log.v("001", "::" + value ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<!-- Interface class: USB HID --> | |
<usb-device class="3" subclass="0" protocol="0" /> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment