2016-04-03 203 views
0

我正在做我的项目在Android和Arduino上,我能够通过蓝牙从Android发送消息到Arduino,但我正在努力通过蓝牙从Arduino到Android的消息。请帮助完成project.Thank你提前Android Arduino和蓝牙

接收代码:

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

} 
+0

显示您的代码以及您到目前为止所尝试的内容。 – Mangesh

+0

私有类ReadInput实现Runnable {0} {0} {0} {0}私有布尔bStop = false; 私人主题t; public ReadInput(){ t = new Thread(this,“Input Thread”); t.start(); } public boolean isRunning(){ return t.isAlive(); } @Override public void run(){ InputStream inputStream; –

+0

try {inputIdStream = mBTSocket.getInputStream(); while(!bStop)byte [] buffer = new byte [256]; (inputStream.available()> 0)inputStream.read(buffer); int i = 0;对于(i = 0; i

回答

0

尝试把Arduino的RX的蓝牙模块和蓝牙模块的TX的TX到的Arduino的RX。你使用的是哪个arduino?你使用的是什么蓝牙模块?是HC05H吗?

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

+0

Arduino UNO和蓝牙模块是HC05 –

+0

该消息是从Arduino发送的,但无法在Android应用上显示。这是问题 –

0

只需使用Serial.print("");就可以将Arduino的字符串发送到Android。欲了解更多信息,refer to this link

+0

是先生,我已经尝试过这个例子,并从github下载了代码,但它没有显示任何内容。 –

+0

先生,请您分享接收来自arduino消息的功能 –