在android的蓝牙文件传输中 出现java.io.IOException: Broken pipe,哪位大神可以解答一下是什么原因
错误日志
java.io.IOException: Broken pipe
at android.net.LocalSocketImpl.writeba_native(Native Method)
at android.net.LocalSocketImpl.access$600(LocalSocketImpl.java:33)
at android.net.LocalSocketImpl$SocketOutputStream.write(LocalSocketImpl.java:135)
at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:445)
at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at com.example.bluetooth.Thread.BluetoothCommunSocket.write(BluetoothCommunSocket.java:85)
at com.example.bluetooth.Service.BluetoothClientService$1$1MyRunnable.run(BluetoothClientService.java:108)
at java.lang.Thread.run(Thread.java:831)
部分代码
/**
* 写入流
* @param obj
*/
public void write(Object obj) {
try {
TransmitBean transmit = (TransmitBean) obj;
if(transmit.getFilename()!=null&&!"".equals(transmit.getFilename())){
Log.v("调试" , "type:"+2);
String filename=transmit.getFilename();
byte type = 2; //类型为2,即传文件
//读取文件长度
FileInputStream fins=new FileInputStream(transmit.getFilepath());
long fileDataLen = fins.available(); //文件的总长度
int f_len=filename.getBytes("GBK").length; //文件名长度
byte[] data=new byte[f_len];
data=filename.getBytes("GBK");
long totalLen = 4+1+1+f_len+fileDataLen;//数据的总长度
outStream.writeLong(totalLen); //1.写入数据的总长度
outStream.writeByte(type);//2.写入类型
outStream.writeByte(f_len); //3.写入文件名的长度
outStream.write(data); //4.写入文件名的数据
outStream.flush();
//读取文件并发送
try {
byte[] buffer=new byte[1024*32];
downbl=0;
int size=0;
long sendlen=0;
float tspeed=0;
int i=0;
long time1=Calendar.getInstance().getTimeInMillis();
while((size=fins.read(buffer, 0, 1024*32))!=-1)
{
outStream.write(buffer, 0, size);
outStream.flush();
sendlen+=size;
i++;
if(i%10==0){
long time2=Calendar.getInstance().getTimeInMillis();
tspeed=sendlen/(time2-time1)*1000/1024;
}
Log.v("调试" ,"tspeed:"+tspeed);
downbl = (sendlen * 100) / fileDataLen;
TransmitBean up = new TransmitBean();
up.setUppercent(String.valueOf(downbl));
up.setTspeed(String.valueOf(tspeed));
Message msg = serviceHandler.obtainMessage();
msg.what = BluetoothTools.FILE_SEND_PERCENT;
msg.obj = up;
msg.sendToTarget();
}
fins.close();
Log.v("调试" , "文件发送完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}