• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps HELP ME, Arduino communication with Android

Someone please tell me why I'm getting this syntax error, and why when I add the bracket I get the superMethod error... I've spent way too much time trying to figure this out.

here is the book I took it from

http://www.digital2u.net/media/Android/Beginning.Android.ADK.with.Arduino.pdf

starts on page 55...


package helloworld.adk;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.TextView;
import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;
public class HelloWorldActivity extends Activity {
private static final String TAG = HelloWorldActivity.class.getSimpleName();
private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private boolean mPermissionRequestPending;
private UsbManager mUsbManager;
private UsbAccessory mAccessory;
private ParcelFileDescriptor mFileDescriptor;
private FileInputStream mInputStream;
private FileOutputStream mOutputStream;
private static final byte COMMAND_TEXT = 0xF;
private static final byte TARGET_DEFAULT = 0xF;
private TextView textView;


/** Listing 2-9. HelloWorldActivity.java (Lifecycle Methods) */
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUsbManager = UsbManager.getInstance(this);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver, filter);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
}
/** Called when the activity is resumed from its paused state and
immediately after onCreate(). */
@Override
public void onResume() {
super.onResume();
if (mInputStream != null && mOutputStream != null) {
return;
}
UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
openAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory, mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {
Log.d(TAG, "mAccessory is null");
}
}
/** Called when the activity is paused by the system. */
@Override
public void onPause() {
super.onPause();
closeAccessory();
}
/** Called when the activity is no longer needed prior to being
removed from the activity stack. */
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mUsbReceiver);
}




private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
openAccessory(accessory);
} else {
Log.d(TAG, "permission denied for accessory " + accessory);
}
mPermissionRequestPending = false;
}
} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (accessory != null && accessory.equals(mAccessory)) {
closeAccessory();
}
}
}
};






private void openAccessory(UsbAccessory accessory) {
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
mAccessory = accessory;
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
Thread thread = new Thread(null, commRunnable, TAG);
thread.start();
Log.d(TAG, "accessory opened");
} else {
Log.d(TAG, "accessory open fail");
}
}





private void closeAccessory() {
try {
if (mFileDescriptor != null) {
mFileDescriptor.close();
}
} catch (IOException e) {
} finally {
mFileDescriptor = null;
mAccessory = null;
}
}



Runnable commRunnable = new Runnable() {

@Override
public void run() {
int ret = 0;
byte[] buffer = new byte[255];

while (ret >= 0) {
try {
ret = mInputStream.read(buffer);
} catch (IOException e) {
break;
}

switch (buffer[0]) {
case COMMAND_TEXT:

final StringBuilder textBuilder = new StringBuilder();
int textLength = buffer[2];
int textEndIndex = 3 + textLength;
for (int x = 3; x < textEndIndex; x++) {
textBuilder.append((char) buffer[x]);
}

runOnUiThread(new Runnable() {

@Override
public void run() {
textView.setText(textBuilder.toString());
}
});

sendText(COMMAND_TEXT, TARGET_DEFAULT, "Hello World from Android!");
break;

default:
Log.d(TAG, "unknown msg: " + buffer[0]);
break;
}

}
}
};
public void sendText(byte command, byte target, String text) {
int textLength = text.length();
byte[] buffer = new byte[3 + textLength];
if (textLength <= 252) {
buffer[0] = command;
buffer[1] = target;
buffer[2] = (byte) textLength;
byte[] textInBytes = text.getBytes();
for (int x = 0; x < textLength; x++) {
buffer[3 + x] = textInBytes[x];
}
if (mOutputStream != null) {
try {
mOutputStream.write(buffer);
} catch (IOException e) {
Log.e(TAG, "write failed", e);
}
}
}
}
 
When you guys load the code there will be a couple things missing. But I get a syntax error telling me to insert a bracket, which I don't think i need. Then when I do I get


The method run() of type new Runnable(){} must override a superclass method HelloWorldActivity.java /HelloWorld/src/helloworld/adk line 155 Java Problem
The method run() of type new Runnable(){} must override a superclass method HelloWorldActivity.java /HelloWorld/src/helloworld/adk line 179 Java Problem

I saw that some people are fixing the superclass method error by using java 1.6 compiler, but that seems to make things way worse.

My arduino recognizes the device, which means everything is working fine on it's end... this is just holding everything up.. I counted all of my brackets and traced down each pair.
 
Upvote 0
You do seem to be missing a close bracket. The code you posted contains 43 open brackets and 42 close brackets. The same error seems to exist in the book - I think the close bracket should go at the end.

The override problem should go away with the Java 1.6 compiler or you can just delete the "@Override" statements and stick with Java 1.5.

There are a couple of additional spaces in the code you pasted, but nothing serious. This builds for me:

Code:
package helloworld.adk;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.TextView;
import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;

public class HelloWorldActivity extends Activity {
    private static final String TAG = HelloWorldActivity.class.getSimpleName();
    private PendingIntent mPermissionIntent;
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private boolean mPermissionRequestPending;
    private UsbManager mUsbManager;
    private UsbAccessory mAccessory;
    private ParcelFileDescriptor mFileDescriptor;
    private FileInputStream mInputStream;
    private FileOutputStream mOutputStream;
    private static final byte COMMAND_TEXT = 0xF;
    private static final byte TARGET_DEFAULT = 0xF;
    private TextView textView;

    /** Listing 2-9. HelloWorldActivity.java (Lifecycle Methods) */
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUsbManager = UsbManager.getInstance(this);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        registerReceiver(mUsbReceiver, filter);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
    }
    /** Called when the activity is resumed from its paused state and
immediately after onCreate(). */
    @Override
    public void onResume() {
        super.onResume();
        if (mInputStream != null && mOutputStream != null) {
            return;
        }
        UsbAccessory[] accessories = mUsbManager.getAccessoryList();
        UsbAccessory accessory = (accessories == null ? null : accessories[0]);
        if (accessory != null) {
            if (mUsbManager.hasPermission(accessory)) {
                openAccessory(accessory);
            } else {
                synchronized (mUsbReceiver) {
                    if (!mPermissionRequestPending) {
                        mUsbManager.requestPermission(accessory, mPermissionIntent);
                        mPermissionRequestPending = true;
                    }
                }
            }
        } else {
            Log.d(TAG, "mAccessory is null");
        }
    }
    /** Called when the activity is paused by the system. */
    @Override
    public void onPause() {
        super.onPause();
        closeAccessory();
    }
    /** Called when the activity is no longer needed prior to being
removed from the activity stack. */
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbReceiver);
    }

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbAccessory accessory = UsbManager.getAccessory(intent);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        openAccessory(accessory);
                    } else {
                        Log.d(TAG, "permission denied for accessory " + accessory);
                    }
                    mPermissionRequestPending = false;
                }
            } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                UsbAccessory accessory = UsbManager.getAccessory(intent);
                if (accessory != null && accessory.equals(mAccessory)) {
                    closeAccessory();
                }
            }
        }
    };

    private void openAccessory(UsbAccessory accessory) {
        mFileDescriptor = mUsbManager.openAccessory(accessory);
        if (mFileDescriptor != null) {
            mAccessory = accessory;
            FileDescriptor fd = mFileDescriptor.getFileDescriptor();
            mInputStream = new FileInputStream(fd);
            mOutputStream = new FileOutputStream(fd);
            Thread thread = new Thread(null, commRunnable, TAG);
            thread.start();
            Log.d(TAG, "accessory opened");
        } else {
            Log.d(TAG, "accessory open fail");
        }
    }

    private void closeAccessory() {
        try {
            if (mFileDescriptor != null) {
                mFileDescriptor.close();
            }
        } catch (IOException e) {
        } finally {
            mFileDescriptor = null;
            mAccessory = null;
        }
    }
    Runnable commRunnable = new Runnable() {

        @Override
        public void run() {
            int ret = 0;
            byte[] buffer = new byte[255];

            while (ret >= 0) {
                try {
                    ret = mInputStream.read(buffer);
                } catch (IOException e) {
                    break;
                }

                switch (buffer[0]) {
                case COMMAND_TEXT:

                    final StringBuilder textBuilder = new StringBuilder();
                    int textLength = buffer[2];
                    int textEndIndex = 3 + textLength;
                    for (int x = 3; x < textEndIndex; x++) {
                        textBuilder.append((char) buffer[x]);
                    }

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            textView.setText(textBuilder.toString());
                        }
                    });

                    sendText(COMMAND_TEXT, TARGET_DEFAULT, "Hello World from Android!");
                    break;

                default:
                    Log.d(TAG, "unknown msg: " + buffer[0]);
                    break;
                }

            }
        }
    };
    
    public void sendText(byte command, byte target, String text) {
        int textLength = text.length();
        byte[] buffer = new byte[3 + textLength];
        if (textLength <= 252) {
            buffer[0] = command;
            buffer[1] = target;
            buffer[2] = (byte) textLength;
            byte[] textInBytes = text.getBytes();
            for (int x = 0; x < textLength; x++) {
                buffer[3 + x] = textInBytes[x];
            }
            if (mOutputStream != null) {
                try {
                    mOutputStream.write(buffer);
                } catch (IOException e) {
                    Log.e(TAG, "write failed", e);
                }
            }
        }
    }
}
You also need to modify your manifest and layout files and make sure you build for a "Google API's" target rather than a plain Android target. I have attached my project.
 

Attachments

  • HelloWorld.zip
    39.5 KB · Views: 38
  • Like
Reactions: Captainhooyah
Upvote 0
Right on man, thanks for all the help! I've been reading every night on Java and Android since friday, after all weekend of trying to set this up I gave up and decided to turn my brain off and watch some youtube videos, and I came across the sensordrone, which is basically what I'm trying to make. You should check it out, it's awesome.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones