简体   繁体   English

Android蓝牙Arduino

[英]Android Bluetooth Arduino

I'm currently trying to get data from an Arduino to Android using the Bluesmirf module. 我目前正在尝试使用Bluesmirf模块从Arduino获取数据到Android。

Here is my code for the Arduino. 这是我的Arduino代码。

void setup() {

 Serial.begin(115200);

}
void loop() {
   if(Serial.available()){
    char val = Serial.read();
    if(val == '.'){
          Serial.println("t1|x1|x2|x3|x4");
   } 
  }
 }

As you can see, I'm just having it write a long string. 正如你所看到的,我只是在写一个长字符串。 Eventually, the string will contain values. 最终,该字符串将包含值。 If i write a period to the arduino, it will return those values. 如果我给arduino写一个句点,它将返回这些值。 Here is my Bluetooth Code, its very similar to the one in the Bluetooth Chat Sample: 这是我的蓝牙代码,它非常类似于蓝牙聊天示例中的代码:

private class ConnectedThread extends Thread{
    private BluetoothSocket mmSocket;
    private InputStream mmInStream;
    private OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        // TODO Auto-generated constructor stub
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (Exception e) {
            // TODO: handle exception
        }
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run(){
        byte[] buffer = new byte[1024];
        int bytes;


        while(true){
            try {
                // Garbage collector necessary to prevent data loss
                System.gc();
                bytes = mmInStream.read(buffer);
                Log.d("Value of Output", new String(buffer, 0, bytes)) 

            } catch (IOException e) {
                e.printStackTrace();
                connectionLost();
            }

        }

    }

    public void write(byte[] buffer){
        try{
            mmOutStream.write(buffer);
        } catch(IOException e){
            Log.d("Write", "failed");
        }
    }

    public void cancel(){
        if (mmInStream != null) {
            try{mmInStream.close();}catch(Exception e){}
            mmInStream = null;
        }
        if (mmOutStream != null) {
            try{mmOutStream.close();} catch(Exception e){}
            mmOutStream = null;
        }

        if (mmSocket != null) {
            try{mmSocket.close();}catch(Exception e){}
            mmSocket = null;
        }
    }
}

A few things I want to mention. 我要提几件事。 The System.gc() is there because if I don't put it there, I sometimes get faulty data. System.gc()就在那里,因为如果我不把它放在那里,我有时会得到错误的数据。 Sometimes the data is loss, sometimes it's repeated. 有时数据会丢失,有时会重复。

The problem that I'm having is that the output comes back in more than one line. 我遇到的问题是输出返回多行。 So in my log I'll get something like 所以在我的日志中我会得到类似的东西

The value of Output t1|x1|x 输出t1 | x1 | x的值

the value of Output 2|x3|x4 输出2 | x3 | x4的值

Instead of it all in one line. 而不是一切都在一条线上。 When I connect the arduino to the computer via Bluetooth(bluetooth dongle), the data comes back in one line. 当我通过蓝牙(蓝牙加密狗)将arduino连接到计算机时,数据返回一行。 How can I ensure that the data comes back in one line. 如何确保数据返回一行。

I can try concatenate your data in a "buffer" and check this buffer for the new line/carriage return character. 我可以尝试在“缓冲区”中连接您的数据,并检查此缓冲区是否有新的行/回车字符。 Once it is found, you asign the data to your real variable, clear the buffer and starts all over again. 找到后,将数据设置为真实变量,清除缓冲区并重新开始。 That should do the trick. 这应该够了吧。

Hope it helps! 希望能帮助到你! :) :)

Well. 好。 I had the same issue. 我遇到过同样的问题。 To solve it, I used mDinput = new DataInputStream(mmInStream); 为了解决这个问题,我使用了mDinput = new DataInputStream(mmInStream); , then mDinput.readFully(dateBuffer, 0, sizeYouWant); ,然后是mDinput.readFully(dateBuffer, 0, sizeYouWant); . Then, readFully will return you the buffer only when it is full 然后, readFully只有在它满时才会返回缓冲区

Hope this helps 希望这可以帮助

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

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