简体   繁体   English

如何在 Android Studio 中延迟 getOutputStream().write()?

[英]How to delay getOutputStream().write() in Android Studio?

I'm using a Bluetooth socket to send information from my phone to an Arduino.我正在使用蓝牙插座将信息从我的手机发送到 Arduino。 The problem is that btSocket.getOutputStream().write(bytes);问题是btSocket.getOutputStream().write(bytes); is sending information too fast and the Arduino can't catch up with the emptying up and overflows.发送信息太快,Arduino 无法赶上清空和溢出。 The code running on my Arduino slows down the Arduino's ability to receive the info from the phone and the receiving Arduino buffer gets overflowed (the incoming data gets bugged because the buffer is emptied much slower than it is filled).在我的 Arduino 上运行的代码减慢了 Arduino 从手机接收信息的能力,并且接收 Arduino 缓冲区溢出(传入数据被窃听,因为缓冲区清空比填充慢得多)。 So a solution would be to slow the rate at which the phone sends the info.因此,解决方案是减慢手机发送信息的速度。

This is the function I use to send info from the phone to the Arduino:这是我用来将信息从手机发送到 Arduino 的函数:

public void send_string_to_lim(String s) {
    if (btSocket != null) {
        try {
            byte[] bytes = s.getBytes();
            btSocket.getOutputStream().write(bytes);
        } catch (IOException e) {
            quick_toast("Error: " + e.toString());
        }
    }
}

And this is how the btSocket is created: (not sure if it's needed for the question)这就是 btSocket 的创建方式:(不确定问题是否需要它)

if (btSocket == null || !isBtConnected) {
    myBluetooth = BluetoothAdapter.getDefaultAdapter(); //get the mobile bluetooth device
    BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address); //connects to the device's address and checks if it's available
    btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID); //create a RFCOMM (SPP) connection
    BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
    btSocket.connect(); //start connection
}

How can I slow the btSocket.getOutputStream().write(bytes);如何减慢btSocket.getOutputStream().write(bytes); so it sends information slower?所以它发送信息更慢? Add some type of delay so the Arduino doesn't overflow.添加某种类型的延迟,以便 Arduino 不会溢出。

You send a byte array to OutputStream but you can also send bytes one at a time - simply loop through your byte array, and after sending each byte, delay for a bit.您向 OutputStream 发送一个字节数组,但您也可以一次发送一个字节 - 只需循环遍历您的字节数组,并在发送每个字节后延迟一点。

I've used an AsyncTask for this but since its deprecated you might want to use Threads or something similar to not lock your UI thread.我为此使用了 AsyncTask,但由于它已弃用,您可能希望使用 Threads 或类似的东西来不锁定您的 UI 线程。

Once you have an AsyncTask framework or Thread in place, your working code (which is in doInBackground or your worker function) should be something like:一旦你有了 AsyncTask 框架或线程,你的工作代码(在 doInBackground 或你的工作函数中)应该是这样的:

for (int i=0; i<bytes.length();i++)
{
    btSocket.getOutputStream().write(bytes[i]);
    Thread.sleep(2); //2 ms delay
}

There's a few exceptions that I haven't handled but you can put it in a try-catch block for that if you want.有一些我没有处理过的例外,但如果你愿意,你可以将它放在 try-catch 块中。

An example template for AsyncTask (which, again, is your choice) is here: https://stackoverflow.com/a/9671602/3550337 . AsyncTask 的示例模板(同样是您的选择)在这里: https : //stackoverflow.com/a/9671602/3550337 I only bring it up because that's what I've used and it worked for me.我只是提出它,因为那是我使用过的并且对我有用。

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

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