- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
--------------------编辑-------------------- -----
一点点进步,但不是解决方案。如果我在要发送的命令之间插入以下代码,至少允许命令有时间在远程端处理(但这仍然不是正确的方法,正确的方法是等待在发送另一个命令之前响应“>”)...
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/
如何检查一个元素是否立即隐藏。即如何通知元素的可见性。 在我的例子中,该元素是通过 slideUp 函数隐藏的。我应该立即收到有关该元素的可见性的通知。 我想到了使用bind()方法。但它没有类似 o
if (srcbloc == NULL) { fprintf(stderr, "warning!: memrip source is null!\n"); exit(1); } if
当我在数据库的旧 View 中清理一些问题时,我遇到了这个“奇怪”的连接条件: from tblEmails [e] join tblPersonEmails [pe]
如何水平对齐多张图像,一张一张地?它们不必适合宽度屏幕:相反,我希望它们超过后者的宽度,如果这有任何意义的话。 我已经检查了很多类似问题的答案,但找不到任何可以解决我的问题的答案。 HTML:
我知道 Cassandra 中的列有 TTL。但是也可以在一行上设置 TTL 吗?在每列上设置 TTL 并不能解决我的问题,如下面的用例所示: 在某些时候,一个进程想要删除一个带有 TTL 的完整行(
我有一个 NSTextField 和 Label,其值绑定(bind)到 View Controller 中的相同 NSString 这里的问题是标签只有在我按 Tab 时才会更新。 如何使其连续,以
例如。 1."abc"; ===>abc 2."ab c"; ===>ab_c 3."ab c"; ===>ab_c 4."ab c" ===>ab_c 对于多个连续空格也是如此。 我怎样
大家好,我想获取前一天或最后一天的信息,只有当我按下按钮时,它才会显示最后一天(星期六)的所有信息,如果我再次单击按钮,它将显示最后一天的信息(星期五)如果我再次点击它(星期四)谢谢你们帮助我 编辑:
我需要从实时音频流中提取ICY元数据,并正在使用mplayer进行此操作,因为它在播放音频流时会输出元数据。我欢迎其他方式执行此操作,目标是将更新的元数据(歌曲信息)保存到文本文件中,只要歌曲(或数据
语音识别有没有解决方案 只有几个字(2 个就够了,10 个就不错了。100 个就很棒了。不需要更多) 也在移动浏览器上运行(是否可以为此使用 flash(而不是 java)?) 可以安装在您自己的服务
我有一个单词列表, list1 = ['hello', 'how', 'are', 'you?', 'i', 'am', 'fine', 'thanks.', 'great!'] 我想加入, list
我正在开发一个程序,但我不断收到“对‘dosell’的 undefined reference ”,我不太明白发生了什么。这是函数的声明: void dosell(int *cash, int *nu
我无法提出执行我要做的事情所需的查询。 我有三个这样的表: client_files ----------------------- client_id file_id ---------
我一直在寻找一个插件/脚本,当到达底部时,它会从头开始继续滚动网站,就像一个连续的循环。 示例:http://unfold.no/和 http://www.aquiesdonde.com.ar/ 我尝
这个问题在这里已经有了答案: How to prevent scanf causing a buffer overflow in C? (6 个答案) 关闭 6 年前。 我一直在使用一个非常简单的程
给定一个整数数组,找到具有相同数量的 x 和 y 的连续子序列的总数。例如 x=1 和 y=2 的数组 [1,2,1] ans = 2 表示它的两个子数组 [1,2] 和 [2,1]。检查每个连续的子
所以,我有一个所有正自然数的数组。我得到了一个阈值。我必须找出总和小于给定阈值的数字(连续)的最大计数。 For example, IP: arr = {3,1,2,1} Threshold = 5
我制作了像内置相机一样的相机应用。 我想实现像内置相机一样的连续对焦功能。(此功能我不触摸屏幕,但相机会尝试自行对焦。) 因此,将其设置为 surfaceCreated : Camera.Pa
我有这样的数据: f x A 1.1 A 2.2 A 3.3 B 3.5 B 3.7 B 3.9 B 4.1 B 4.5 A 5.1 A 5.2 C 5.4 C 5.5 C 6.1 B 6.2 B
假设我有一个包含一组数据点的表,每个数据点由一个时间戳和一个值组成。如果至少有 N 个连续记录(按时间戳排序)高于给定值 X,我将如何编写返回 true (1) 的查询,否则返回 false (0)?
我是一名优秀的程序员,十分优秀!