USB communication Arduino Pro Micro / Leonardo and Android

edited October 2015 in Android Mode

Hey,

does someone have experiences with connecting an Arduino Pro Micro or Leonardo to an Android phone over USB ?

Found this https://github.com/mik3y/usb-serial-for-android https://groups.google.com/forum/#!topic/usb-serial-for-android/8-fLuUn2Yns

and this https://github.com/inventit/processing-android-serial.

But I have no idea how I would use the AndroidLib in Processing(AndroidMode) and the second method simply doesnt work for me.

thanks, cheers !

Answers

  • I was trying now to understand the android API myself http://developer.android.com/guide/topics/connectivity/usb/host.html

    But quickly ran into this error: The method SystemService(String) is undefined for the type usbcom

    import android.hardware.usb.*;
    import android.content.Context;
    import android.view.MotionEvent;
    import ketai.ui.*;
    import java.util.Map;
    
    
    KetaiGesture gesture;
    UsbManager manager;
    
    
    void setup()
    {
      orientation(LANDSCAPE);
      gesture = new KetaiGesture(this);
      manager = (UsbManager)SystemService(Context.USB_SERVICE);
      HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    
    }
    

    this seems to be a solution:

    http://stackoverflow.com/questions/18990728/android-getsystemservicecontext-usb-service-error

    but I dont know how to apply it in processing.

  • I got it all working without any external library. if someone is interested I can share the code.

  • Yes sure, it would be great if you share your solution.

  • edited March 2016

    ups, now heres the code. everything should be in - but i didnt check if it compiles.

    I also had to remove the HDI interface from the pro micro. should be easy to find how to do that. cheers !

    import android.hardware.usb.*;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.Window.*;
    import android.view.WindowManager;
    import android.view.*;
    import java.util.*;
    import java.lang.Object;
    import android.app.PendingIntent;
    import android.content.IntentFilter;
    import android.content.BroadcastReceiver;
    import android.util.Log;
    import android.content.res.AssetFileDescriptor;
    import android.content.Context;
    import android.app.Activity;
    
    
    
    UsbManager manager;
    UsbManager mUsbManager;
    PendingIntent mPermissionIntent;
    UsbDevice device;
    
    UsbInterface intf;
    UsbDeviceConnection connection;
    UsbEndpoint endpoint;
    UsbEndpoint endpointin;
    
    
    //USB
    final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    HashMap<String, UsbDevice> deviceList;
    boolean sender = false;
    boolean receiver = false;
    int sendern =100;
    byte[] bytes = new byte[]{5};
    byte[] reply = new byte[]{0}; 
    int TIMEOUT = 1000;
    private boolean forceClaim = true;
    
    setup(){
    
    
      manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
      mUsbManager = (UsbManager)  getActivity().getSystemService(Context.USB_SERVICE);
      deviceList = manager.getDeviceList();
    
      connect();
      connect2();
    
    
    }
    
    
    
     void connect(){
    
         manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
         deviceList = manager.getDeviceList();
         for (String keydevicename : deviceList.keySet()) {
           device = deviceList.get(keydevicename);
         }
         mUsbManager.requestPermission(device, mPermissionIntent);
     }
    
    
    
      void connect2(){
    
        intf = device.getInterface(1);
        endpoint = intf.getEndpoint(0);
        endpointin = intf.getEndpoint(1);
        connection = mUsbManager.openDevice(device); 
        connection.claimInterface(intf, forceClaim);
        connection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0);
        receiver = true;
     }
    
    
    
    
    
     void sending(){  
    
        if(sender == true){
    
        new Thread(new Runnable() {
        public void run() {
         connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
           sender = false;
    
        }
      }).start();
      }} 
    
    
    
     void reading(){  
    
            if(receiver == true){
    
        new Thread(new Runnable() {
        public void run() {
        connection.bulkTransfer(endpointin, reply, reply.length, TIMEOUT);
    
        }
      }).start();
      }} 
    
    
    
    
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                          //call method to set up device communication
                       }
                    } 
                    else {
                        //Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };
    
    
     @Override
    public void onCreate(Bundle savedInstanceState)
    {
    
      super.onCreate(savedInstanceState);
      getActivity().getSystemService(Context.USB_SERVICE);
      final String ACTION_USB_PERMISSION =
        "com.android.example.USB_PERMISSION";
    
      mPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(ACTION_USB_PERMISSION), 0);
      IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
      getActivity().registerReceiver(mUsbReceiver, filter);
    
    
    
    }
    
  • setup(){ ??? And which IDE?

Sign In or Register to comment.