Android通过蓝牙发送信息,快速记录

时间:2017-12-01 13:24:16

标签: android bluetooth arduino

我正在尝试编写一个应用程序,通过BT将球的坐标传递给Arduino。坐标每4毫秒发送一次。对于此测试,我发送“123”而不是完整坐标。我现在得到的(在Arduino串行监视器上)是“123123123123123 ......”,只有在我关闭应用程序后它才会刷新。

我想要实现的是每一行中的“123”,在发送消息后立即显示。

Android代码BT:

 private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private OutputStream outStream ;

    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket
        // because mmSocket is final.
        BluetoothSocket tmp = null;
        mmDevice = device;
        try {
            // Get a BluetoothSocket to connect with the given BluetoothDevice.
            // MY_UUID is the app's UUID string, also used in the server code.
            tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Log.e(TAG, "Socket's create() method failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it otherwise slows down the connection.
        mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect to the remote device through the socket. This call blocks
            // until it succeeds or throws an exception.
            mmSocket.connect();
            Log.i(TAG, "run: CONNECTED");
        } catch (IOException connectException) {
            Log.i(TAG, "run:  NOT CONNECTED");
        }
    }

    // Closes the client socket and causes the thread to finish.
    public void cancel() {
        try {
            mmSocket.close();
            if(outStream != null)
                outStream.close();
            finish();
        } catch (IOException e) {
            Log.e(TAG, "Could not close the client socket", e);
        }
    }

    //Sending Message
    public void writeData(String data){
        String info = data;
        try {
            outStream = mmSocket.getOutputStream();
            outStream.write(info.getBytes());
            Log.i(TAG, "writeData: MSG SENT");
        } catch (IOException e) {
            e.printStackTrace();
            Log.i(TAG, "run: CANT SEND MSG");
        }
    }

    public boolean isConnected(){
        return mmSocket.isConnected();
    }
}

在我的主要功能中,我打电话:

if(connectThread.isConnected())
    connectThread.writeData("123");

Arduino代码:

String incomingByte;

void setup() {
  //pinMode(53, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.readString();
    Serial.println(incomingByte);
    delay(10);
    }
  }

2 个答案:

答案 0 :(得分:2)

您需要在坐标后至少放置\n(或者\r\n),否则蓝牙模块会保持缓冲。

答案 1 :(得分:1)

除非您自己制作消息,否则串行通信中不存在消息概念。

Serial.readString()用时间(默认为1秒)界定您的“消息”,并且您将发送相距4 ms的“消息”。这显然会连接你的“信息”。

要实际发送消息,您需要分隔它们。你可以通过发送行来做到这一点。

在Android上,您需要使用换行符结束消息:

outStream.write(info.getBytes());
outStream.write(10); // send a new line character (ASCII code 10)

在Arduino上,你需要阅读,直到你找到一个新的行字符:

incomingByte = Serial.readStringUntil('\n');
Serial.read(); // remove the leftover new line character from the buffer