0

I am doing my project on Android and Arduino ,i am able to send the message from Android to Arduino via bluetooth ,but am struggling to get the message from Arduino to Android via bluetooth.Please help to complete the project.Thank you in advance

Receiving code :

private class ReadInput implements Runnable {

    private boolean bStop = false;
    private Thread t;

    public ReadInput() {
        t = new Thread(this, "Input Thread");
        t.start();
    }

    public boolean isRunning() {
        return t.isAlive();
    }

    @Override
    public void run() {
        InputStream inputStream;

        try {
            inputStream = mBTSocket.getInputStream();
            while (!bStop) {
                byte[] buffer = new byte[256];
                if (inputStream.available() > 0) {
                    inputStream.read(buffer);
                    int i = 0;
                    /*
                     * This is needed because new String(buffer) is taking the entire buffer i.e. 256 chars on Android 2.3.4 http://stackoverflow.com/a/8843462/1287554
                     */
                    for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
                    }
                    final String strInput = new String(buffer, 0, i);

                    /*
                     * If checked then receive text, better design would probably be to stop thread if unchecked and free resources, but this is a quick fix
                     */

                    if (chkReceiveText.isChecked()) {
                        mTxtReceive.post(new Runnable() {
                            @Override
                            public void run() {
                                mTxtReceive.append(strInput);
                                //Uncomment below for testing
                                //mTxtReceive.append("\n");
                                //mTxtReceive.append("Chars: " + strInput.length() + " Lines: " + mTxtReceive.getLineCount() + "\n");

                                int txtLength = mTxtReceive.getEditableText().length();
                                if (txtLength > mMaxChars) {
                                    mTxtReceive.getEditableText().delete(0, txtLength - mMaxChars);
                                }

                                if (chkScroll.isChecked()) { // Scroll only if this is checked
                                    scrollView.post(new Runnable() { // Snippet from http://stackoverflow.com/a/4612082/1287554
                                        @Override
                                        public void run() {
                                            scrollView.fullScroll(View.FOCUS_DOWN);
                                        }
                                    });
                                }
                            }
                        });
                    }

                }
                Thread.sleep(500);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void stop() {
        bStop = true;
    }

}
PRAVEEN K
  • 11
  • 3
  • Show your code and what you have tried so far. – Mangesh Apr 03 '16 at 07:00
  • private class ReadInput implements Runnable { private boolean bStop = false; private Thread t; public ReadInput() { t = new Thread(this, "Input Thread"); t.start(); } public boolean isRunning() { return t.isAlive(); } @Override public void run() { InputStream inputStream; – PRAVEEN K Apr 03 '16 at 08:04
  • try { inputStream = mBTSocket.getInputStream(); while (!bStop) { byte[] buffer = new byte[256]; if (inputStream.available() > 0) { inputStream.read(buffer); int i = 0; /* – PRAVEEN K Apr 03 '16 at 08:05
  • for (i = 0; i < buffer.length && buffer[i] != 0; i++) { } final String strInput = new String(buffer, 0, i); /* * If checked then receive text, better design would probably be to stop thread if unchecked and free resources, but this is a quick fix */ if (chkReceiveText.isChecked()) { mTxtReceive.post(new Runnable() { @Override public void run() { mTxtReceive.append(strInput); – PRAVEEN K Apr 03 '16 at 08:06
  • nt txtLength = mTxtReceive.getEditableText().length(); if (txtLength > mMaxChars) { mTxtReceive.getEditableText().delete(0, txtLength - mMaxChars); } if (chkScroll.isChecked()) { // Scroll only if this is checked scrollView.post(new Runnable() { // Snippet from http://stackoverflow.com/a/4612082/1287554 @Override public void run() scrollView.fullScroll(View.FOCUS_DOWN); } }); } – PRAVEEN K Apr 03 '16 at 08:07
  • }); } } Thread.sleep(500); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } – PRAVEEN K Apr 03 '16 at 08:07
  • You should edit the question and add code there. Don't add it in the comments. Please read this http://stackoverflow.com/help/how-to-ask – Mangesh Apr 03 '16 at 09:24

3 Answers3

0

Try putting rx of arduino to tx of bluetooth module and tx of bluetooth module to rx of arduino. Which arduino are you using and what is the bluetooth module you are using? Is it HC05H?

Also show your code if possible.

0

Just using Serial.print(""); you can send Strings to and from Arduino to Android. For more information, refer to this link.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

I've made a similar project using Bluetooth and Arduino and I have a github with a fully working code: https://github.com/Primaelq/Mapping-Robot/blob/master/Companion%20App/Eye-BotCompanionApp/app/src/main/java/studio/eye/a/eye_botcompanionapp/BluetoothService.java This is the BluetoothService class, you should take a look at the connected thread method. Feel free to use any of the code or ask any questions.

Hope that helps you.