?
Solved

How do I use this android app

Posted on 2013-05-22
1
Medium Priority
?
910 Views
Last Modified: 2013-06-04
Ok this is a dumb question so sorry in advance.
I have the android SDK and eclipse.
I found this script to interface arduino and android but dont have the first clue on how to import it into the eclipse or run it on my phone.  
I do have my phone connected to the SDK and have been able to run other android demo apps.
code here: http://pastebin.com/a7wPqA3d
topic here: http://forum.arduino.cc/index.php?topic=157621.msg1248849#msg1248849
	

    package com.example.myApp;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.UUID;
     
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothSocket;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ToggleButton;
     
    public class Main extends Activity implements OnClickListener {
     
            Button Connect;
            ToggleButton OnOff;
            TextView Result;
            private String dataToSend;
           
            private static final String TAG = "Jon";
            private BluetoothAdapter mBluetoothAdapter = null;
            private BluetoothSocket btSocket = null;
            private OutputStream outStream = null;
            private static String address = "XX:XX:XX:XX:XX:XX";
            private static final UUID MY_UUID = UUID
                            .fromString("00001101-0000-1000-8000-00805F9B34FB");
            private InputStream inStream = null;
        Handler handler = new Handler();
        byte delimiter = 10;
        boolean stopWorker = false;
        int readBufferPosition = 0;
        byte[] readBuffer = new byte[1024];
     
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
     
                    Connect = (Button) findViewById(R.id.connect);
                    OnOff = (ToggleButton) findViewById(R.id.tgOnOff);
                    Result = (TextView) findViewById(R.id.msgJonduino);
     
                    Connect.setOnClickListener(this);
                    OnOff.setOnClickListener(this);
     
                    CheckBt();
                    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                    Log.e("Jon", device.toString());
     
            }
     
            @Override
            public void onClick(View control) {
                    switch (control.getId()) {
                    case R.id.connect:
                                    Connect();
                            break;
                    case R.id.tgOnOff:
                            if (OnOff.isChecked()) {
                                    dataToSend = "1";
                                    writeData(dataToSend);
                            } else if (!OnOff.isChecked()) {
                                    dataToSend = "0";
                                    writeData(dataToSend);
                            }
                            break;
                    }
            }
     
            private void CheckBt() {
                    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     
                    if (!mBluetoothAdapter.isEnabled()) {
                            Toast.makeText(getApplicationContext(), "Bluetooth Disabled !",
                                            Toast.LENGTH_SHORT).show();
                    }
     
                    if (mBluetoothAdapter == null) {
                            Toast.makeText(getApplicationContext(),
                                            "Bluetooth null !", Toast.LENGTH_SHORT)
                                            .show();
                    }
            }
           
                    public void Connect() {
                            Log.d(TAG, address);
                            BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                            Log.d(TAG, "Connecting to ... " + device);
                            mBluetoothAdapter.cancelDiscovery();
                            try {
                                    btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                                    btSocket.connect();
                                    Log.d(TAG, "Connection made.");
                            } catch (IOException e) {
                                    try {
                                            btSocket.close();
                                    } catch (IOException e2) {
                                            Log.d(TAG, "Unable to end the connection");
                                    }
                                    Log.d(TAG, "Socket creation failed");
                            }
                           
                            beginListenForData();
                    }
           
            private void writeData(String data) {
                    try {
                            outStream = btSocket.getOutputStream();
                    } catch (IOException e) {
                            Log.d(TAG, "Bug BEFORE Sending stuff", e);
                    }
     
                    String message = data;
                    byte[] msgBuffer = message.getBytes();
     
                    try {
                            outStream.write(msgBuffer);
                    } catch (IOException e) {
                            Log.d(TAG, "Bug while sending stuff", e);
                    }
            }
     
            @Override
            protected void onDestroy() {
                    super.onDestroy();
           
                            try {
                                    btSocket.close();
                            } catch (IOException e) {
                            }
            }
           
            public void beginListenForData()   {
                     try {
                                    inStream = btSocket.getInputStream();
                            } catch (IOException e) {
                            }
                     
                    Thread workerThread = new Thread(new Runnable()
                    {
                        public void run()
                        {                
                           while(!Thread.currentThread().isInterrupted() && !stopWorker)
                           {
                                try
                                {
                                    int bytesAvailable = inStream.available();                        
                                    if(bytesAvailable > 0)
                                    {
                                        byte[] packetBytes = new byte[bytesAvailable];
                                        inStream.read(packetBytes);
                                        for(int i=0;i<bytesAvailable;i++)
                                        {
                                            byte b = packetBytes[i];
                                            if(b == delimiter)
                                            {
                                                byte[] encodedBytes = new byte[readBufferPosition];
                                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                                final String data = new String(encodedBytes, "US-ASCII");
                                                readBufferPosition = 0;
                                                handler.post(new Runnable()
                                                {
                                                    public void run()
                                                    {
                                                           
                                                            if(Result.getText().toString().equals("..")) {
                                                                    Result.setText(data);
                                                            } else {
                                                                    Result.append("\n"+data);
                                                            }
                                                           
                                                            /* You also can use Result.setText(data); it won't display multilines
                                                            */
                                                           
                                                    }
                                                });
                                            }
                                            else
                                            {
                                                readBuffer[readBufferPosition++] = b;
                                            }
                                        }
                                    }
                                }
                                catch (IOException ex)
                                {
                                    stopWorker = true;
                                }
                           }
                        }
                    });
     
                    workerThread.start();
                }
    }

Open in new window

0
Comment
Question by:livegirllove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
PCableGuy earned 2000 total points
ID: 39189726
Hi livegirllove,

Best way to do that is start a new Android Application Project under File->New in Eclipse. Then create the GUI in the res/layout section on Eclipse, name it activity_main.xml so it matches the Class code that you posted. Looks like the GUI uses one Button named Connect, one ToggleButton named OnOff, and a TextView named Result.

Then make a Class named Main under src/your.package.name in Eclipse and delete all the code that Eclipse automatically puts in there except for the package your.package.name line.

Then copy/paste the code that you posted into that Main class that you just cleared. You won't need the package com.example.myApp; line, don't copy/paste that line. Save the Main class.

That's the short version, but it will get you started in the right direction.

Feel free to post some questions back for further discussion.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question