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

}
7
  • Show your code and what you have tried so far. Commented Apr 3, 2016 at 7: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; Commented Apr 3, 2016 at 8:04
  • try { inputStream = mBTSocket.getInputStream(); while (!bStop) { byte[] buffer = new byte[256]; if (inputStream.available() > 0) { inputStream.read(buffer); int i = 0; /* Commented Apr 3, 2016 at 8: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); Commented Apr 3, 2016 at 8: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 stackoverflow.com/a/4612082/1287554 @Override public void run() scrollView.fullScroll(View.FOCUS_DOWN); } }); } Commented Apr 3, 2016 at 8:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Arduino UNO and Bluetooth Module is HC05
The message is sending from Arduino but am not able to display it on Android app.This is the problem
0

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

2 Comments

yes sir, I have already tried this example and downloaded the code from github but it does not display anything.
sir,can u please the share the function that receive the message from arduino
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.

1 Comment

Ok sir,I will check it once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.