Android上蓝牙输入流出错
我正在编写一个需要与Arduino HC-06蓝牙设备交换数据的应用程序。以下是数据交换方法:Android上蓝牙输入流出错,android,Android,我正在编写一个需要与Arduino HC-06蓝牙设备交换数据的应用程序。以下是数据交换方法: void ListenForBluetoothData() { final Handler handler = new Handler(); final byte delimiter = 10; //This is the ASCII code for a newline character stopWorker = false; readBufferPosition
void ListenForBluetoothData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
if(BTinStream!=null)
{
int bytesAvailable = BTinStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
BTinStream.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()
{
myLabel1.setText(data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
}else {
continue;
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void ListenForBluetoothData()
{
最终处理程序=新处理程序();
final byte delimiter=10;//这是换行符的ASCII码
stopWorker=false;
readBufferPosition=0;
readBuffer=新字节[1024];
workerThread=新线程(new Runnable())
{
公开募捐
{
而(!Thread.currentThread().isInterrupted()&&!stopWorker){
试一试{
如果(BTinStream!=null)
{
int bytesavable=BTinStream.available();
如果(字节可用>0){
byte[]packetBytes=新字节[bytesAvailable];
b流内读取(packetBytes);
for(int i=0;i
我收到的主要错误是:
06-19 11:21:23.011 17542-17910/com.example.farok.bluetoothcommunicationarduino E/AndroidRuntime: FATAL EXCEPTION: Thread-41375
Process: com.example.farok.bluetoothcommunicationarduino, PID: 17542
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at com.example.farok.bluetoothcommunicationarduino.MainActivity.ListenForBluetoothData(MainActivity.java:179)
at com.example.farok.bluetoothcommunicationarduino.MainActivity$1.run(MainActivity.java:69)
06-19 11:21:23.011 17542-17910/com.example.farok.bluetoothcommunicationarduino E/AndroidRuntime:致命异常:Thread-41375
进程:com.example.farok.bluetoothcommunicationarduino,PID:17542
java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
android.os.Handler.(Handler.java:200)
位于android.os.Handler.(Handler.java:114)
在com.example.farok.bluetoothcommunicationarduino.MainActivity.ListenForBluetoothData(MainActivity.java:179)上
位于com.example.farok.bluetoothcommunicationarduino.MainActivity$1.run(MainActivity.java:69)
请帮助,我尝试了许多解决方案,但它们都不起作用。您正在从工作线程向UI发送数据,而不是使用
处理程序,编辑并使用runOnUiThread
而不是:
handler.post(new Runnable()
{
public void run()
{
myLabel1.setText(data);
}
});
使用以下命令:
runOnUiThread(new Runnable(){
public void run() {
myLabel1.setText(data);
}
});
您正在从工作线程向UI发送数据,而不是使用Handler
,编辑并使用runOnUiThread
而不是:
handler.post(new Runnable()
{
public void run()
{
myLabel1.setText(data);
}
});
使用以下命令:
runOnUiThread(new Runnable(){
public void run() {
myLabel1.setText(data);
}
});
谢谢你的回答。。错误消失了,但应用程序没有对接收到的数据做出响应..?我真的不理解您当前的问题。你所说的“应用程序未响应接收到的数据”是什么意思?我的意思是,从arduino接收到的蓝牙数据没有显示在标签上。尝试查看它是否为空。做一个Toast
并查看结果。谢谢你的回答。。错误消失了,但应用程序没有对接收到的数据做出响应..?我真的不理解您当前的问题。你所说的“应用程序未响应接收到的数据”是什么意思?我的意思是,从arduino接收到的蓝牙数据没有显示在标签上。尝试查看它是否为空。做一个Toast
并查看结果。