简体   繁体   English

Android Arduino和蓝牙

[英]Android Arduino and Bluetooth

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 我正在Android和Arduino上做我的项目,我能够通过蓝牙将消息从Android发送到Arduino,但正在努力通过蓝牙从Arduino将消息发送到Android。请帮助完成项目。在此先感谢您

Receiving code : 接收代码:

private class ReadInput implements Runnable { 私有类ReadInput实现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;
    }

}

Try putting rx of arduino to tx of bluetooth module and tx of bluetooth module to rx of arduino. 尝试将arduino的rx放入蓝牙模块的tx,将蓝牙模块的tx放入arduino的rx。 Which arduino are you using and what is the bluetooth module you are using? 您正在使用哪个arduino,并且正在使用什么蓝牙模块? Is it HC05H? 是HC05H吗?

Also show your code if possible. 如果可能,还显示您的代码。

Just using Serial.print(""); 只使用Serial.print(""); you can send Strings to and from Arduino to Android. 您可以在Arduino和Android之间发送字符串。 For more information, refer to this link . 有关更多信息, 请参考此链接

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. 我已经使用蓝牙和Arduino进行了类似的项目,并且我有一个具有完整工作代码的github: https : //github.com/Primaelq/Mapping-Robot/blob/master/Companion%20App/Eye-BotCompanionApp/app/ src / main / java / studio / eye / a / eye_botcompanionapp / BluetoothService.java这是BluetoothService类,您应该看看连接的线程方法。 Feel free to use any of the code or ask any questions. 随意使用任何代码或提出任何问题。

Hope that helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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