gpt4 book ai didi

android - Bluetooth Android SPP,向设备连续发送命令?

转载 作者:行者123 更新时间:2023-11-29 00:39:53 24 4
gpt4 key购买 nike

--------------------编辑-------------------- -----
一点点进步,但不是解决方案。如果我在要发送的命令之间插入以下代码,至少允许命令有时间在远程端处理(但这仍然不是正确的方法,正确的方法是等待在发送另一个命令之前响应“>”)...

android.os.SystemClock.sleep(150);

监听线程在系统时钟休眠期间被阻塞,因此调制解调器的输入直到代码序列发送后才会附加到 TextView ,这是不太理想的。不过, sleep 不是正确的方法,我需要找到更好的方法,这样我才能发送新命令,直到“>”结果从另一端的设备返回。当我完成这段代码时,我将需要一种方法来处理输入,所以如果我考虑的话, sleep 真的不是进步。插休眠眠的例子:

    sendData("enable");
android.os.SystemClock.sleep(150);
sendData("password");
android.os.SystemClock.sleep(150);
sendData("conf t");
android.os.SystemClock.sleep(150);
sendData("interface eth0");
android.os.SystemClock.sleep(150);
//etc...etc...etc...

--------------------下面是原帖-------------------- ------

我正在使用来自 Matt Bell 博客的这段优雅编写的代码,位于此处: http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/

来源位于: http://project-greengiant.googlecode.com/svn/trunk/Blog/Android%20Arduino%20Bluetooth

在不破坏代码的情况下,我正在尝试以一种方式优雅地将命令串行发送到连接的调制解调器,每次都等到收到完整的响应后再发送下一个命令。 (我知道这不是 Android 的做事方式,我可以使用其他语言在心跳中处理这个问题)。

这是我目前正在使用的东西(你会看到我还没有完成很远,事实上这段代码一直运行得非常好,直到我需要等待第一个命令完成才能发送更多命令命令)。我尽可能地省略了与这个问题无关的代码。提前致谢。

    package Android.Arduino.Bluetooth;        

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;


public class BluetoothTest extends Activity
{
TextView myLabel;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
int counter;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myLabel = (TextView)findViewById(R.id.label);

try
{
findBT();
openBT();
sendData("enable");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("password");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("conf t");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
sendData("interface eth0");
//insert some code to wait for response before sending (or handle that in the above line, or otherwise)
//etc...etc...etc...
}
catch (IOException ex) { }

}

}

void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
myLabel.append("Bluetooth Opened" + "\n");
}

void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 62; //This is the ASCII code for a > character indicating all data received

stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];

workerThread = new Thread(new Runnable()
{
public void run()
{

while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;

handler.post(new Runnable()
{
public void run()
{
myLabel.append(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}

}
});

workerThread.start();
}

void sendData(String msg0) throws IOException
{
msg0 += "\r";
mmOutputStream.write(msg0.getBytes());
myLabel.append("Data Sent" + "\n");
}

}

最佳答案

我知道这是一个很老的问题。但是,由于我正在开发类似的应用程序,所以我以不同的方式解决了它,这可能会有所帮助。

您可以通过标志实现发送/响应逻辑,而不是发送之间的延迟。所以一开始连接是initial。您发送 enable 并将您的标志设置为 enableRequested。然后让听众等待响应。一旦你让它继续发送 password,将标志设置为 passwordSent 并再次释放线程。

所以我建议不要在 onCreate 中执行它,而是触发一个线程从 onCreate 连接。这应该能很好地工作。

关于android - Bluetooth Android SPP,向设备连续发送命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10094734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com