【发布时间】:2020-03-23 17:35:53
【问题描述】:
我正在使用蓝牙插座将信息从我的手机发送到 Arduino。问题是btSocket.getOutputStream().write(bytes); 发送信息太快,Arduino 无法赶上清空和溢出。在我的 Arduino 上运行的代码减慢了 Arduino 从手机接收信息的能力,并且接收 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());
}
}
}
这就是 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 就不会溢出。
【问题讨论】:
标签: java android android-studio arduino overflow