0

Hi I'm trying to combine 2 projects 1. that tells me wen button on arduino pressed 2. make a call on android

what im trying to do is when button pressed on arduino make a call.. but with no luck :(

Call

    package net.mitchtech.adb;

    import net.mitchtech.adb.simpledigitalinput.R;
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;

    public class phonecalls extends Activity {

    private void call() {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:048598077"));
            startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
            Log.e("dialing-example", "Call failed", activityException);
        }
    }

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.input);
        }
    }

buttons

package net.mitchtech.adb;

import net.mitchtech.adb.simpledigitalinput.R;
import net.mitchtech.adb.phonecalls;

import org.microbridge.server.AbstractServerListener;
import org.microbridge.server.Server;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

public class ButtonView extends FrameLayout {
    private static final String TAG = ButtonView.class.getSimpleName();

    private final View mButtonView;

    private Server mServer;

    private final int BUTTON1 = 2;
    private final int BUTTON2 = 3;

    public ButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mButtonView = inflater.inflate(R.layout.input, this);
    }

    public void setServer(Server server) {
        mServer = server;

        mServer.addListener(new AbstractServerListener() {

            @Override
            public void onReceive(org.microbridge.server.Client client, byte[] data) {
                if (data.length < 2)
                    return;

                final int pinNumber = data[0];
                final int pinState = data[1];
                Log.i(TAG, "data[0]:" + pinNumber + "  data[1]:" + pinState);

                final TextView positionText = (TextView) findViewById(R.id.activeText);

                class InputAction implements Runnable {

                    public void run() {

                        switch (pinNumber) {
                        case BUTTON1:
                            if (pinState == 1) {
                                call();
                                positionText.setText("Button 1 Active");

                            } else {
                                positionText.setText("");
                            }
                            break;
                        case BUTTON2:
                            if (pinState == 1) {
                                positionText.setText("Button 2 Active");
                            } else {
                                positionText.setText("");
                            }
                            break;

                        default:
                            break;
                        }
                    }
                };

                InputAction action = new InputAction();
                post(action);
            }
        });
    }

    public View getmButtonView() {
        return mButtonView;
    }
}

and another activity

package net.mitchtech.adb;

import java.io.IOException;

import net.mitchtech.adb.simpledigitalinput.R;

import org.microbridge.server.Server;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SimpleDigitalInputActivity extends Activity {

    private final static String TAG = SimpleDigitalInputActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Server server = null;
        try
        {
            server = new Server(4567);
            server.start();

            ButtonView buttonView = (ButtonView) findViewById(R.id.inputView);
            buttonView.setServer(server);
            } catch (IOException e)
        {
            Log.e(TAG, "Unable to start TCP server", e);
            System.exit(-1);
        }
    }
}
Sudar
  • 18,954
  • 30
  • 85
  • 131
igor
  • 716
  • 1
  • 9
  • 27
  • What is happening? Is your application crashing, are you getting compile errors, or is it just not working? Please post your LogCat if it's the first one. – Techwolf Nov 08 '12 at 08:22
  • i get an error on the call(); call(); is undefined for the tipe input action – igor Nov 08 '12 at 08:49
  • it's in the buttons -> public void run() -> after the first "if" – igor Nov 08 '12 at 08:59
  • full project is here [link](http://www.2shared.com/complete/rIpL7UsU/OnPressCall.html) – igor Nov 08 '12 at 09:16
  • that's because you need to a/ put the call in the ButtonView or b/ fetch your activity context to access to call function – njzk2 Nov 08 '12 at 10:49
  • sory i'm new in the android programing can you tell me how i do that ? – igor Nov 08 '12 at 11:14

1 Answers1

0

Your call() function is declared in your activity, but you're trying to access it in your ButtonView class. Try moving it into ButtonView (copy & paste).

Techwolf
  • 1,228
  • 13
  • 32
  • i copy that 'code' private void call() { try { callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:048598077")); startActivity(callIntent); } catch (ActivityNotFoundException activityException) { Log.e("dialing-example", "Call failed", activityException); } ' to the ButtonView and now it's telling me "The method StartActivity is undefinded in the button view" if i create a method what to write in it? or should i do thome thing else – igor Nov 12 '12 at 08:00
  • You can't start an `Activity` from a `View` context because the function `startActivity()` is defined in `Activity`. The fix is easy, and it's explained in detail here: http://stackoverflow.com/questions/3073468/starting-an-activity-from-a-view-in-android – Techwolf Nov 12 '12 at 08:22