简体   繁体   中英

Making a call when button pressed on Arduino

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);
        }
    }
}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM