Slow communication between Android tablet and Arduino

Hi!

I have a problem...

I'm currently programming an android app which sends specific CAN-messages into my infotainment-network of my car.
Writing CAN-messages from the Arduino into the infotainment-network already works without any problem. But the communication between my Nexus 7 2013 tablet with Android 5.0.2 is driving me crazy. I've made a simple test: If I send 'ToCAN_UP' via serial from the tablet to the Arduino a LED should light up at the Arduino which basically works already. BUT: It is extremly slow! Sending 'ToCAN_UP' takes 2-3 seconds.

I've tested it with the usb-serial-for-android library and the Physicaloid Library. Both have the same problem BUT: I've tested some apps for serial communication like "Android USB Serial Monitor Lite" (which uses the Physicaloid Library) it's wonderful fast as it should be with the same 'ToCAN_UP' I'm sending with my own app.

My current source code is quiet simple and I can't figure out why it is so slow. The app is not even hanging while the Arduino is receiving the data and the port.write method itself is done immediately (so it sends at background).

Does anybody have an idea? Here's the java code:

package de.shavenne.android.youcanlisten;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;

import java.io.IOException;
import java.util.List;


public class MainActivity extends ActionBarActivity {
    private PendingIntent PendingIntent_UsbPermission;
    private static int RQS_USB_PERMISSION = 0;
    private final String ACTION_USB_PERMISSION = "de.shavenne.android.youcanlisten";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);



        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);

        ImageButton buttonUp = (ImageButton) findViewById(R.id.upButton);
        ImageButton buttonDown = (ImageButton) findViewById(R.id.downButton);

        if (!availableDrivers.isEmpty()) {
            UsbSerialDriver driver = availableDrivers.get(0);
            Intent intent_UsbPermission = new Intent(ACTION_USB_PERMISSION);
            PendingIntent_UsbPermission = PendingIntent.getBroadcast(
                    this,      //context
                    RQS_USB_PERMISSION,  //request code
                    intent_UsbPermission, //intent
                    0);      //flags
            IntentFilter intentFilter_UsbPermission = new IntentFilter(ACTION_USB_PERMISSION);

            UsbDevice device = driver.getDevice();
            TextView textView = (TextView) findViewById(R.id.textView);

            String text = device.getDeviceName();
            manager.requestPermission(device, PendingIntent_UsbPermission);

            if(manager.hasPermission(device)) {
                text += " has permission";
            }
            textView.setText(text);


            final UsbDeviceConnection connection = manager.openDevice(device);
            final List<UsbSerialPort> mEntries = driver.getPorts();
            final UsbSerialPort port = mEntries.get(0);
            try {
                port.open(connection);
                port.setParameters(9600, 8, 1, 0);
                buttonUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            port.write("ToCAN_UP\n".getBytes(), 500);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

                buttonDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            port.write("ToCAN_DOWN\n".getBytes(), 500);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I'm thankful for every tip. I'm really in despair.

Regards
Sven

Hello,

You can connect Android and Arduino with usb and use the easy tool App inventor 2 for android

Did you try this new application :

https://play.google.com/store/apps/details?id=bp.usbbridge.appinvDemo

and full version : https://play.google.com/store/apps/details?id=bp.usbbridge.appinv

Find Videos about Explications : USB OTG Bridge Serial UART for APP Inventor 2 and ARDUBLOCK on youtube

shavenne:
Hi!

I have a problem...

I'm currently programming an android app which sends specific CAN-messages into my infotainment-network of my car.
Writing CAN-messages from the Arduino into the infotainment-network already works without any problem. But the communication between my Nexus 7 2013 tablet with Android 5.0.2 is driving me crazy. I've made a simple test: If I send 'ToCAN_UP' via serial from the tablet to the Arduino a LED should light up at the Arduino which basically works already. BUT: It is extremly slow! Sending 'ToCAN_UP' takes 2-3 seconds.

I've tested it with the usb-serial-for-android library and the Physicaloid Library. Both have the same problem BUT: I've tested some apps for serial communication like "Android USB Serial Monitor Lite" (which uses the Physicaloid Library) it's wonderful fast as it should be with the same 'ToCAN_UP' I'm sending with my own app.

My current source code is quiet simple and I can't figure out why it is so slow. The app is not even hanging while the Arduino is receiving the data and the port.write method itself is done immediately (so it sends at background).

Does anybody have an idea? Here's the java code:

package de.shavenne.android.youcanlisten;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;

import java.io.IOException;
import java.util.List;

public class MainActivity extends ActionBarActivity {
    private PendingIntent PendingIntent_UsbPermission;
    private static int RQS_USB_PERMISSION = 0;
    private final String ACTION_USB_PERMISSION = "de.shavenne.android.youcanlisten";

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

List availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);

ImageButton buttonUp = (ImageButton) findViewById(R.id.upButton);
        ImageButton buttonDown = (ImageButton) findViewById(R.id.downButton);

if (!availableDrivers.isEmpty()) {
            UsbSerialDriver driver = availableDrivers.get(0);
            Intent intent_UsbPermission = new Intent(ACTION_USB_PERMISSION);
            PendingIntent_UsbPermission = PendingIntent.getBroadcast(
                    this,      //context
                    RQS_USB_PERMISSION,  //request code
                    intent_UsbPermission, //intent
                    0);      //flags
            IntentFilter intentFilter_UsbPermission = new IntentFilter(ACTION_USB_PERMISSION);

UsbDevice device = driver.getDevice();
            TextView textView = (TextView) findViewById(R.id.textView);

String text = device.getDeviceName();
            manager.requestPermission(device, PendingIntent_UsbPermission);

if(manager.hasPermission(device)) {
                text += " has permission";
            }
            textView.setText(text);

final UsbDeviceConnection connection = manager.openDevice(device);
            final List mEntries = driver.getPorts();
            final UsbSerialPort port = mEntries.get(0);
            try {
                port.open(connection);
                port.setParameters(9600, 8, 1, 0);
                buttonUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            port.write("ToCAN_UP\n".getBytes(), 500);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

buttonDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            port.write("ToCAN_DOWN\n".getBytes(), 500);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

//noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

return super.onOptionsItemSelected(item);
    }
}




I'm thankful for every tip. I'm really in despair.

Regards
Sven

Do you bought it from google market? It haven't app demo!
Are you have demo, please give me this app for test it.
Thanks