Android 2.1 Bluetooth SPP中的Inputstream.read错误

Android 2.1 Bluetooth SPP中的Inputstream.read错误,android,bluetooth,Android,Bluetooth,我尝试连接一个蓝牙适配器,它在发送握手后向我发送一些数据。 我的问题是,在适配器发送之前,代码工作正常,但如果它停止,in.read(xxx)命令将阻塞,而不带任何例外。我必须关闭适配器并等待几秒钟,然后出现异常,代码恢复。 如何解决这个问题?(为了更好地理解,我删除了错误处理) BluetoothAdapter bt_adapter=BluetoothAdapter.getDefaultAdapter(); 蓝牙设备bt_设备=空; //蓝牙不支持 if(bt_适配器==null){ 返回fa

我尝试连接一个蓝牙适配器,它在发送握手后向我发送一些数据。 我的问题是,在适配器发送之前,代码工作正常,但如果它停止,in.read(xxx)命令将阻塞,而不带任何例外。我必须关闭适配器并等待几秒钟,然后出现异常,代码恢复。 如何解决这个问题?(为了更好地理解,我删除了错误处理)

BluetoothAdapter bt_adapter=BluetoothAdapter.getDefaultAdapter();
蓝牙设备bt_设备=空;
//蓝牙不支持
if(bt_适配器==null){
返回false;
}
//启用蓝牙
如果(!bt_adapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
返回false;
}    
//搜索K01蓝色适配器
Set pairedDevices=bt_adapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
String name=device.getName();
如果(名称包含(“K01蓝色”)){
bt_device=BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
打破
}
}
}
//如果未找到适配器,请退出
如果(bt_device==null){return false;}
//创建套接字
试一试{
UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
GlobalVars.bluetooth.bt_socket=GlobalVars.bluetooth.bt_device.createrfcomsockettoservice记录(uuid);
}捕获(例外e){
e、 printStackTrace();
}
//如果未创建套接字,则退出
如果(bt_socket==null){return false;}
/连接到插座,否则退出
试试{bt_socket.connect();
}catch(IOException e1){return false;}
//获取输入流
InputStream in=null;
试试{in=GlobalVars.bluetooth.bt_socket.getInputStream();
}catch(IOException e1){return false;}
//获取输出流
OutputStream out=null;
试试{out=GlobalVars.bluetooth.bt_socket.getOutputStream()
}catch(IOException e1){return false;}
//向K01适配器问好
字节[]hello={0x2F,0x3F,0x21,0x0D,0x0A};
试试{out.write(hello);
}catch(IOException e1){return false;}
//听K01适配器的回答
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
国际nRead;
字节[]数据=新字节[1024];
试一试{
nRead=in.read(数据,0,数据长度);
而(nRead!=-1){
nRead=in.read(数据,0,数据长度);
写入(数据,0,nRead);
}
}catch(IOException e1){e1.printStackTrace();}
//从Bytearray创建字符串
String str1=新字符串(buffer.toByteArray());

这就是我的工作原理:

static ByteArrayOutputStream readbuffer_to()
     {
        ByteArrayOutputStream found = new ByteArrayOutputStream();
        long start = System.currentTimeMillis();
        try {
              if(GlobalVars.bluetooth.in.available() == 0){return found;}
              int nRead = GlobalVars.bluetooth.in.read();
              boolean catched = false;
              while (true) {
                  if(GlobalVars.bluetooth.in.available() > 0)
                  {
                      found.write(nRead);
                      nRead = GlobalVars.bluetooth.in.read(); 
                      start = System.currentTimeMillis();

                  }
                    if(System.currentTimeMillis() - start > 5000) {break;} 

              } 


        } catch (IOException e1) {


        }
         return found;
     }
“阻塞”不是意味着in.read()会阻塞线程,直到数据来自蓝牙吗


因此,如果此侦听器位于您自己的线程中,您不需要任何is.available()?

否,它只是阻塞数据,并且永远不会读取数据,但解决方法适用于1.6,而问题似乎在2.2中没有出现+
static ByteArrayOutputStream readbuffer_to()
     {
        ByteArrayOutputStream found = new ByteArrayOutputStream();
        long start = System.currentTimeMillis();
        try {
              if(GlobalVars.bluetooth.in.available() == 0){return found;}
              int nRead = GlobalVars.bluetooth.in.read();
              boolean catched = false;
              while (true) {
                  if(GlobalVars.bluetooth.in.available() > 0)
                  {
                      found.write(nRead);
                      nRead = GlobalVars.bluetooth.in.read(); 
                      start = System.currentTimeMillis();

                  }
                    if(System.currentTimeMillis() - start > 5000) {break;} 

              } 


        } catch (IOException e1) {


        }
         return found;
     }