gpt4 book ai didi

java - 无法读取蓝牙接收到的数据

转载 作者:行者123 更新时间:2023-12-02 05:39:31 25 4
gpt4 key购买 nike

我无法读取通过蓝牙从 Arduino Uno 接收到的数据,但我知道 Arduino 正在向我发送一些数据。

我收到错误:

W/System.err:     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:537)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.InputStream.read(InputStream.java:101)
at bikit.chaptal.MainDisp$SendReceive.run(MainDisp.java:250)
at bikit.chaptal.MainDisp$SendReceive.<init>(MainDisp.java:239)
at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:200)
at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:168)
at android.os.AsyncTask$2.call(AsyncTask.java:345)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)
java.io.IOException: bt socket closed, read return: -1
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:537)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.InputStream.read(InputStream.java:101)
at bikit.chaptal.MainDisp$SendReceive.run(MainDisp.java:250)
at bikit.chaptal.MainDisp$SendReceive.<init>(MainDisp.java:239)
at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:200)
at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:168)
at android.os.AsyncTask$2.call(AsyncTask.java:345)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)
java.io.IOException: bt socket closed, read return: -1

这是有问题的代码部分:

public  class SendReceive extends Thread
{
private InputStream inputStream;
private OutputStream outputStream;

public SendReceive ( BluetoothSocket socket )
{
try {
inputStream = mmSocket.getInputStream();
outputStream = mmSocket.getOutputStream();
}catch (IOException e ) {
e.printStackTrace();
}
if(inputStream != null){
run();
}
}

public void run()
{
byte[] buffer = new byte[1024];
int bytes;

while (true) {
try {
bytes = inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVED, bytes, -1,buffer).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write (byte[] bytes)
{
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

处理程序:

Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {

switch (msg.what)
{
case STATE_MESSAGE_RECEIVED:
byte[]readBuff = (byte[]) msg.obj;
if (bytes == 1){
Nbatt = readBuff[0];
bytes++;
}
else if (bytes == 2){
tempfloat = ByteBuffer.wrap(new byte[]{readBuff[1]}).getFloat();
temp = Float.toString(tempfloat);
resetbytes(); //bytes++;
}
else if (bytes == 3){
speed = readBuff[2];
resetbytes();
}
break;
}
return false;
}
});

应用程序应该读取不同的字节并显示它们。我不明白这个错误,有人可以向我解释一下吗?

最佳答案

不深入分析代码,我们可以看出问题是由于IOException异常导致的InputStream.read()操作引起的,具体如下:bt socket closeed ,阅读 所以看起来连接已以某种方式断开。根据一些论坛的说法,这是因为该库太旧了,而且自 Android 4.2 以来,BT 堆栈已经发生了变化。

无论如何,有一个解决方法可以通过在出现此异常时实例化回退来使其工作:IOException: read failed, socket might closed - Bluetooth on Android 4.3

关于java - 无法读取蓝牙接收到的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148562/

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