arduinoでandroid

#概要
arduino unoをandroidにつないで、Lチカしてみた。
androidは、nexus7です。
#写真
![MVC-006S.JPG](https://qiita-image-store.s3.amazonaws.com/0/18104/3614c7aa-84b9-25b8-5f23-f1fc22d4df78.jpeg)

![MVC-007S.JPG](https://qiita-image-store.s3.amazonaws.com/0/18104/233ec5d2-b4d1-dafc-ce31-849e67afbc10.jpeg)

#サンプルコード
```
void setup()
{
	Serial.begin(9600);
	pinMode(13, OUTPUT);
	digitalWrite(13, HIGH);
	Serial.println("start");
}
void loop()
{
	char c;
	if (Serial.available())
	{
		c = Serial.read();
		if (c == '2')
		{
			digitalWrite(13, HIGH);
			Serial.println("on");
		}
		if (c == '1')
		{
			digitalWrite(13, LOW);
			Serial.println("off");
		}
	}
}
```
#サンプルコード
```
package com.ohisamallc.ohiapp133;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.hoho.android.usbserial.driver.UsbSerialProber;

public class Ohiapp133 extends Activity
{
	private UsbManager mUsbManager;
	private static UsbSerialDriver sDriver = null;
	private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
	private SerialInputOutputManager mSerialIoManager;
	private String s = "";
    private final SerialInputOutputManager.Listener mListener = new SerialInputOutputManager.Listener() 
    {
        @Override public void onRunError(Exception e) 
        {
        }
        @Override public void onNewData(final byte[] data) 
        {
            Ohiapp133.this.runOnUiThread(new Runnable() 
            {
                @Override public void run() 
                {
                    Ohiapp133.this.updateReceivedData(data);
                }
            }); 
        }
    };
    private void updateReceivedData(byte[] data) 
    {
   		if (data.length < 1) return;
		s += new String(data, 0, data.length);
		s = "";
	}     
    @Override protected void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ohiapp133);
		mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
		sDriver = UsbSerialProber.findFirstDevice(mUsbManager);
		Button btn1 = (Button) findViewById(R.id.Button_1);
		btn1.setOnClickListener(new View.OnClickListener() 
		{
			@Override public void onClick(View view) 
			{
		    	String data = "1";
		    	mSerialIoManager.writeAsync(data.getBytes());  
			}
		});
		Button btn2 = (Button) findViewById(R.id.Button_2);
		btn2.setOnClickListener(new View.OnClickListener() 
		{
			@Override public void onClick(View view) 
			{
		    	String data = "2";
		    	mSerialIoManager.writeAsync(data.getBytes());  
			}
		});		
	}
    private void stopIoManager() 
    {
        if (mSerialIoManager != null) 
        {
            mSerialIoManager.stop();
            mSerialIoManager = null;
        }
    }
    private void startIoManager() 
    {
        if (sDriver != null) 
        {
        	
            mSerialIoManager = new SerialInputOutputManager(sDriver, mListener);
            mExecutor.submit(mSerialIoManager);
        }
    }
    private void onDeviceStateChange()
    {
        stopIoManager();
        startIoManager();
    } 
    @Override protected void onResume() 
    {
        super.onResume();
        onDeviceStateChange();
        if (sDriver == null)
        {
        }
        else
        {
            try
            {
                sDriver.open();
                sDriver.setParameters(9800, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
            }
            catch (IOException e)
            {
                try
                {
                    sDriver.close();
                }
                catch (IOException e2)
                {                	
                }
                sDriver = null;
                return;
            }
        }
    }
    @Override protected void onPause()
    {
        super.onPause();
        stopIoManager();
        if (sDriver != null)
        {
            try 
            {
                sDriver.close();
            }
            catch (IOException e)
            {
            }
            sDriver = null;
        }
        finish();
    }
}

```