So, today I thought to share it with you guys. It isn't much difficult but will need a little patient. When I started working on it I couldn't find any data on it online and I have to work a lot to make it work. So that's why I am sharing it so that others can get it work easily. When I was starting this project then I have a bit confusion that whether to use the USB Host shield of Arduino or to use the usb cable through which we connect Arduino with computer. After a little research I got the idea that simple USB will work fine so I go with it. I have explained this tutorial in detail. First I have done the Arduino side because that one is quite easy and after that I touched the Android side that's a bit complex and you must have the prior knowledge of it if you wanna make it work. So, let's start this project. :)
#include <SoftwareSerial.h> SoftwareSerial mySerial(2,3); void setup() { Serial.begin(9600); Serial.println("www.TheEngineeringProjects.com"); mySerial.begin(9600); mySerial.println("www.TheEngineeringProjects.com"); } void loop() { if (mySerial.available()) Serial.println(mySerial.read() - 48); if (Serial.available()) mySerial.println(Serial.read() - 48); }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.androidusbhostarduino.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:autoLink="web" android:text="http://www.TheEngineeringProjects.com/" android:textStyle="bold" /> <ToggleButton android:id="@+id/arduinoled" android:layout_width="match_parent" android:layout_height="wrap_content" android:textOn="ON" android:textOff="OFF" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.theengineeringprojects.dani" > <uses-feature android:name="android.hardware.usb.host" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <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" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> </activity> </application> </manifest>
package com.theengineeringprojects.dani; import java.nio.ByteBuffer; import android.support.v7.app.ActionBarActivity; import android.content.Context; import android.content.Intent; 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.hardware.usb.UsbRequest; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.SeekBar; import android.widget.ToggleButton; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends ActionBarActivity implements Runnable{ private static final char CMD_LED_OFF = '1'; private static final char CMD_LED_ON = '2'; SeekBar bar; ToggleButton buttonLed; private UsbManager usbManager; private UsbDevice deviceFound; private UsbDeviceConnection usbDeviceConnection; private UsbInterface usbInterfaceFound = null; private UsbEndpoint endpointOut = null; private UsbEndpoint endpointIn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonLed = (ToggleButton)findViewById(R.id.arduinoled); buttonLed.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ sendCommand(CMD_LED_ON); }else{ sendCommand(CMD_LED_OFF); } }}); usbManager = (UsbManager)getSystemService(Context.USB_SERVICE); } @Override public void onResume() { super.onResume(); Intent intent = getIntent(); String action = intent.getAction(); UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { setDevice(device); } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { if (deviceFound != null && deviceFound.equals(device)) { setDevice(null); } } } private void setDevice(UsbDevice device) { usbInterfaceFound = null; endpointOut = null; endpointIn = null; for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface usbif = device.getInterface(i); UsbEndpoint tOut = null; UsbEndpoint tIn = null; int tEndpointCnt = usbif.getEndpointCount(); if (tEndpointCnt >= 2) { for (int j = 0; j < tEndpointCnt; j++) { if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) { tOut = usbif.getEndpoint(j); } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) { tIn = usbif.getEndpoint(j); } } } if (tOut != null && tIn != null) { // This interface have both USB_DIR_OUT // and USB_DIR_IN of USB_ENDPOINT_XFER_BULK usbInterfaceFound = usbif; endpointOut = tOut; endpointIn = tIn; } } } if (usbInterfaceFound == null) { return; } deviceFound = device; if (device != null) { UsbDeviceConnection connection = usbManager.openDevice(device); if (connection != null && connection.claimInterface(usbInterfaceFound, true)) { usbDeviceConnection = connection; Thread thread = new Thread(this); thread.start(); } else { usbDeviceConnection = null; } } } private void sendCommand(int control) { synchronized (this) { if (usbDeviceConnection != null) { byte[] message = new byte[1]; message[0] = (byte)control; usbDeviceConnection.bulkTransfer(endpointOut, message, message.length, 0); } } } @Override public void run() { ByteBuffer buffer = ByteBuffer.allocate(1); UsbRequest request = new UsbRequest(); request.initialize(usbDeviceConnection, endpointIn); while (true) { request.queue(buffer, 1); if (usbDeviceConnection.requestWait() == request) { byte rxCmd = buffer.get(0); if(rxCmd!=0){ bar.setProgress((int)rxCmd); } try { Thread.sleep(100); } catch (InterruptedException e) { } } else { break; } } } }
The Arduino part is easy but the Android part is bit difficult so if you need help then ask in comments and I will help you out. That's all for today, will meet you guys in coming tutorials. Take care !!! :)