繁体   English   中英

Android Arduino和蓝牙

[英]Android Arduino and Bluetooth

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

接收代码:

私有类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;
    }

}

尝试将arduino的rx放入蓝牙模块的tx,将蓝牙模块的tx放入arduino的rx。 您正在使用哪个arduino,并且正在使用什么蓝牙模块? 是HC05H吗?

如果可能,还显示您的代码。

只使用Serial.print(""); 您可以在Arduino和Android之间发送字符串。 有关更多信息, 请参考此链接

我已经使用蓝牙和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类,您应该看看连接的线程方法。 随意使用任何代码或提出任何问题。

希望对您有帮助。

暂无
暂无

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

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