如何在 Android Studio 中延迟 getOutputStream().write()?
How to delay getOutputStream().write() in Android Studio?
我正在使用蓝牙插座将信息从我的 phone 发送到 Arduino。问题是 btSocket.getOutputStream().write(bytes);
发送信息的速度太快,Arduino 无法赶上清空和溢出。我的 Arduino 上的代码 运行 减慢了 Arduino 从 phone 接收信息的能力并且接收 Arduino 缓冲区溢出(传入数据被窃听,因为缓冲区清空比填充慢得多).因此,解决方案是降低 phone 发送信息的速度。
这是我用来将信息从 phone 发送到 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());
}
}
}
这就是 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
}
我怎样才能减慢 btSocket.getOutputStream().write(bytes);
的速度,使其发送信息的速度变慢?添加某种类型的延迟,这样 Arduino 就不会溢出。
您将一个字节数组发送到 OutputStream,但您也可以一次发送一个字节 - 只需循环遍历您的字节数组,并在发送每个字节后延迟一点。
我为此使用了 AsyncTask,但由于它已被弃用,您可能希望使用 Threads 或类似的东西来不锁定您的 UI 线程。
一旦你有了 AsyncTask 框架或线程,你的工作代码(在 doInBackground 或你的辅助函数中)应该是这样的:
for (int i=0; i<bytes.length();i++)
{
btSocket.getOutputStream().write(bytes[i]);
Thread.sleep(2); //2 ms delay
}
有一些我没有处理的异常,但如果需要,您可以将其放入 try-catch 块中。
此处提供了 AsyncTask 的示例模板(同样由您选择):。我只是提出来,因为那是我用过的并且对我有用。
我正在使用蓝牙插座将信息从我的 phone 发送到 Arduino。问题是 btSocket.getOutputStream().write(bytes);
发送信息的速度太快,Arduino 无法赶上清空和溢出。我的 Arduino 上的代码 运行 减慢了 Arduino 从 phone 接收信息的能力并且接收 Arduino 缓冲区溢出(传入数据被窃听,因为缓冲区清空比填充慢得多).因此,解决方案是降低 phone 发送信息的速度。
这是我用来将信息从 phone 发送到 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());
}
}
}
这就是 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
}
我怎样才能减慢 btSocket.getOutputStream().write(bytes);
的速度,使其发送信息的速度变慢?添加某种类型的延迟,这样 Arduino 就不会溢出。
您将一个字节数组发送到 OutputStream,但您也可以一次发送一个字节 - 只需循环遍历您的字节数组,并在发送每个字节后延迟一点。
我为此使用了 AsyncTask,但由于它已被弃用,您可能希望使用 Threads 或类似的东西来不锁定您的 UI 线程。
一旦你有了 AsyncTask 框架或线程,你的工作代码(在 doInBackground 或你的辅助函数中)应该是这样的:
for (int i=0; i<bytes.length();i++)
{
btSocket.getOutputStream().write(bytes[i]);
Thread.sleep(2); //2 ms delay
}
有一些我没有处理的异常,但如果需要,您可以将其放入 try-catch 块中。
此处提供了 AsyncTask 的示例模板(同样由您选择):。我只是提出来,因为那是我用过的并且对我有用。