我无法读取通过蓝牙从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;
}
});
APP应该读取不同的字节并显示它们。我不明白错误,有人可以向我解释吗?
答案 0 :(得分:0)
如果要深入分析代码,我们可以看到问题是由IOException引起的操作InputStream.read()
引起的,具体来说是以下问题:bt socket closed, read
,因此连接似乎断开了不知何故。根据一些论坛的说法,这是因为该库较旧,并且自Android 4.2起,BT堆栈已更改。
无论如何,存在一种变通方法,可以通过在出现此异常时实例化后备来使其工作:IOException: read failed, socket might closed - Bluetooth on Android 4.3